mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
#1805 use twice registry of dubbo
This commit is contained in:
parent
344fc8be51
commit
fd8a6a6f6c
@ -17,18 +17,21 @@
|
|||||||
package com.alibaba.cloud.dubbo.autoconfigure;
|
package com.alibaba.cloud.dubbo.autoconfigure;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import com.alibaba.cloud.dubbo.autoconfigure.condition.MissingSpringCloudRegistryConfigPropertyCondition;
|
import com.alibaba.cloud.dubbo.autoconfigure.condition.MissingSpringCloudRegistryConfigPropertyCondition;
|
||||||
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
||||||
import com.alibaba.cloud.dubbo.registry.DubboServiceRegistrationEventPublishingAspect;
|
import com.alibaba.cloud.dubbo.registry.DubboServiceRegistrationEventPublishingAspect;
|
||||||
|
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreDeregisteredEvent;
|
||||||
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
|
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
|
||||||
import com.ecwid.consul.v1.agent.model.NewService;
|
import com.ecwid.consul.v1.agent.model.NewService;
|
||||||
import com.netflix.appinfo.InstanceInfo;
|
import com.netflix.appinfo.InstanceInfo;
|
||||||
import org.apache.dubbo.config.RegistryConfig;
|
import org.apache.dubbo.config.RegistryConfig;
|
||||||
import org.apache.dubbo.config.spring.ServiceBean;
|
import org.apache.dubbo.config.spring.ServiceBean;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -42,6 +45,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||||
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
|
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
|
||||||
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration;
|
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration;
|
||||||
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
|
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
|
||||||
@ -51,7 +55,10 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.Conditional;
|
import org.springframework.context.annotation.Conditional;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.context.event.ApplicationContextEvent;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
|
||||||
import static com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.CONSUL_AUTO_SERVICE_AUTO_CONFIGURATION_CLASS_NAME;
|
import static com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.CONSUL_AUTO_SERVICE_AUTO_CONFIGURATION_CLASS_NAME;
|
||||||
import static com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME;
|
import static com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME;
|
||||||
@ -106,10 +113,33 @@ public class DubboServiceRegistrationAutoConfiguration {
|
|||||||
return new RegistryConfig(ADDRESS, PROTOCOL);
|
return new RegistryConfig(ADDRESS, PROTOCOL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Map<ServiceRegistry<Registration>, Set<Registration>> registrations = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Order
|
||||||
|
@EventListener(ApplicationContextEvent.class)
|
||||||
|
public void attachDubboMetadataAndRegistAgain(ApplicationContextEvent event) {
|
||||||
|
registrations.forEach(
|
||||||
|
(registry, registrations) -> registrations.forEach(registration -> {
|
||||||
|
attachDubboMetadataServiceMetadata(registration);
|
||||||
|
registry.register(registration);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
@EventListener(ServiceInstancePreRegisteredEvent.class)
|
@EventListener(ServiceInstancePreRegisteredEvent.class)
|
||||||
public void onServiceInstancePreRegistered(ServiceInstancePreRegisteredEvent event) {
|
public void onServiceInstancePreRegistered(ServiceInstancePreRegisteredEvent event) {
|
||||||
Registration registration = event.getSource();
|
Registration registration = event.getSource();
|
||||||
attachDubboMetadataServiceMetadata(registration);
|
ServiceRegistry<Registration> registry = event.getRegistry();
|
||||||
|
synchronized (registry) {
|
||||||
|
registrations.putIfAbsent(registry, new HashSet<>());
|
||||||
|
registrations.get(registry).add(registration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventListener(ServiceInstancePreDeregisteredEvent.class)
|
||||||
|
public void onServiceInstancePreDeregistered(
|
||||||
|
ServiceInstancePreDeregisteredEvent event) {
|
||||||
|
ServiceRegistry<Registration> registry = event.getRegistry();
|
||||||
|
registrations.remove(registry);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void attachDubboMetadataServiceMetadata(Registration registration) {
|
private void attachDubboMetadataServiceMetadata(Registration registration) {
|
||||||
@ -132,21 +162,19 @@ public class DubboServiceRegistrationAutoConfiguration {
|
|||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnBean(name = EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME)
|
@ConditionalOnBean(name = EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME)
|
||||||
@Aspect
|
|
||||||
class EurekaConfiguration implements SmartInitializingSingleton {
|
class EurekaConfiguration implements SmartInitializingSingleton {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ObjectProvider<Collection<ServiceBean>> serviceBeans;
|
private ObjectProvider<Collection<ServiceBean>> serviceBeans;
|
||||||
|
|
||||||
@EventListener(ServiceInstancePreRegisteredEvent.class)
|
@Order(Ordered.LOWEST_PRECEDENCE - 1)
|
||||||
public void onServiceInstancePreRegistered(
|
@EventListener(ApplicationContextEvent.class)
|
||||||
ServiceInstancePreRegisteredEvent event) {
|
public void onServiceInstancePreRegistered(ApplicationContextEvent event) {
|
||||||
Registration registration = event.getSource();
|
registrations.forEach((registry, registrations)-> registrations.forEach(registration -> {
|
||||||
EurekaRegistration eurekaRegistration = EurekaRegistration.class
|
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
|
||||||
.cast(registration);
|
InstanceInfo instanceInfo = eurekaRegistration.getApplicationInfoManager().getInfo();
|
||||||
InstanceInfo instanceInfo = eurekaRegistration.getApplicationInfoManager()
|
attachDubboMetadataServiceMetadata(instanceInfo.getMetadata());
|
||||||
.getInfo();
|
}));
|
||||||
attachDubboMetadataServiceMetadata(instanceInfo.getMetadata());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -171,21 +199,25 @@ public class DubboServiceRegistrationAutoConfiguration {
|
|||||||
@AutoConfigureOrder
|
@AutoConfigureOrder
|
||||||
class ConsulConfiguration {
|
class ConsulConfiguration {
|
||||||
|
|
||||||
/**
|
@Order(Ordered.LOWEST_PRECEDENCE - 1)
|
||||||
* Handle the pre-registered event of {@link ServiceInstance} for Consul.
|
@EventListener(ApplicationContextEvent.class)
|
||||||
* @param event {@link ServiceInstancePreRegisteredEvent}
|
public void attachURLsIntoMetadataBeforeReRegist(ApplicationContextEvent event) {
|
||||||
*/
|
registrations.entrySet().removeIf(entry -> {
|
||||||
@EventListener(ServiceInstancePreRegisteredEvent.class)
|
Set<Registration> registrations = entry.getValue();
|
||||||
public void onServiceInstancePreRegistered(
|
registrations.removeIf(registration -> {
|
||||||
ServiceInstancePreRegisteredEvent event) {
|
Class<?> registrationClass = AopUtils.getTargetClass(registration);
|
||||||
Registration registration = event.getSource();
|
String registrationClassName = registrationClass.getName();
|
||||||
Class<?> registrationClass = AopUtils.getTargetClass(registration);
|
return !CONSUL_AUTO_SERVICE_AUTO_REGISTRATION_CLASS_NAME
|
||||||
String registrationClassName = registrationClass.getName();
|
.equalsIgnoreCase(registrationClassName);
|
||||||
if (CONSUL_AUTO_SERVICE_AUTO_REGISTRATION_CLASS_NAME
|
});
|
||||||
.equalsIgnoreCase(registrationClassName)) {
|
return registrations.isEmpty();
|
||||||
ConsulRegistration consulRegistration = (ConsulRegistration) registration;
|
});
|
||||||
attachURLsIntoMetadata(consulRegistration);
|
|
||||||
}
|
registrations.forEach(
|
||||||
|
(registry, registrations) -> registrations.forEach(registration -> {
|
||||||
|
ConsulRegistration consulRegistration = (ConsulRegistration) registration;
|
||||||
|
attachURLsIntoMetadata(consulRegistration);
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void attachURLsIntoMetadata(ConsulRegistration consulRegistration) {
|
private void attachURLsIntoMetadata(ConsulRegistration consulRegistration) {
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.dubbo.registry;
|
package com.alibaba.cloud.dubbo.registry;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreDeregisteredEvent;
|
||||||
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
|
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
|
||||||
import com.alibaba.cloud.dubbo.registry.event.ServiceInstanceRegisteredEvent;
|
import com.alibaba.cloud.dubbo.registry.event.ServiceInstanceRegisteredEvent;
|
||||||
import org.aspectj.lang.annotation.After;
|
import org.aspectj.lang.annotation.After;
|
||||||
@ -33,6 +34,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
|
|||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
* @see ServiceInstancePreRegisteredEvent
|
* @see ServiceInstancePreRegisteredEvent
|
||||||
* @see ServiceInstanceRegisteredEvent
|
* @see ServiceInstanceRegisteredEvent
|
||||||
|
* @see ServiceInstancePreDeregisteredEvent
|
||||||
*/
|
*/
|
||||||
@Aspect
|
@Aspect
|
||||||
public class DubboServiceRegistrationEventPublishingAspect
|
public class DubboServiceRegistrationEventPublishingAspect
|
||||||
@ -41,18 +43,29 @@ public class DubboServiceRegistrationEventPublishingAspect
|
|||||||
/**
|
/**
|
||||||
* The pointcut expression for {@link ServiceRegistry#register(Registration)}.
|
* The pointcut expression for {@link ServiceRegistry#register(Registration)}.
|
||||||
*/
|
*/
|
||||||
public static final String REGISTER_POINTCUT_EXPRESSION = "execution(* org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(*)) && args(registration)";
|
public static final String REGISTER_POINTCUT_EXPRESSION = "execution(* org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(*)) && target(registry) && args(registration)";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The pointcut expression for {@link ServiceRegistry#deregister(Registration)}.
|
||||||
|
*/
|
||||||
|
public static final String DEREGISTER_POINTCUT_EXPRESSION = "execution(* org.springframework.cloud.client.serviceregistry.ServiceRegistry.deregister(*)) && target(registry) && args(registration)";
|
||||||
|
|
||||||
private ApplicationEventPublisher applicationEventPublisher;
|
private ApplicationEventPublisher applicationEventPublisher;
|
||||||
|
|
||||||
@Before(REGISTER_POINTCUT_EXPRESSION)
|
@Before(value = REGISTER_POINTCUT_EXPRESSION, argNames = "registry, registration")
|
||||||
public void beforeRegister(Registration registration) {
|
public void beforeRegister(ServiceRegistry registry, Registration registration) {
|
||||||
applicationEventPublisher
|
applicationEventPublisher.publishEvent(
|
||||||
.publishEvent(new ServiceInstancePreRegisteredEvent(registration));
|
new ServiceInstancePreRegisteredEvent(registry, registration));
|
||||||
}
|
}
|
||||||
|
|
||||||
@After(REGISTER_POINTCUT_EXPRESSION)
|
@Before(value = DEREGISTER_POINTCUT_EXPRESSION, argNames = "registry, registration")
|
||||||
public void afterRegister(Registration registration) {
|
public void beforeDeregister(ServiceRegistry registry, Registration registration) {
|
||||||
|
applicationEventPublisher.publishEvent(
|
||||||
|
new ServiceInstancePreDeregisteredEvent(registry, registration));
|
||||||
|
}
|
||||||
|
|
||||||
|
@After(value = REGISTER_POINTCUT_EXPRESSION, argNames = "registry, registration")
|
||||||
|
public void afterRegister(ServiceRegistry registry, Registration registration) {
|
||||||
applicationEventPublisher
|
applicationEventPublisher
|
||||||
.publishEvent(new ServiceInstanceRegisteredEvent(registration));
|
.publishEvent(new ServiceInstanceRegisteredEvent(registration));
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.alibaba.cloud.dubbo.registry.event;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||||
|
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||||
|
import org.springframework.context.ApplicationEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The
|
||||||
|
* before-{@link org.springframework.cloud.client.serviceregistry.ServiceRegistry#register(org.springframework.cloud.client.serviceregistry.Registration)
|
||||||
|
* register} event for {@link org.springframework.cloud.client.ServiceInstance}.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
|
*/
|
||||||
|
public class ServiceInstancePreDeregisteredEvent extends ApplicationEvent {
|
||||||
|
|
||||||
|
private final ServiceRegistry<Registration> registry;
|
||||||
|
|
||||||
|
public ServiceInstancePreDeregisteredEvent(ServiceRegistry<Registration> registry,
|
||||||
|
Registration source) {
|
||||||
|
super(source);
|
||||||
|
this.registry = registry;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Registration getSource() {
|
||||||
|
return (Registration) super.getSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceRegistry<Registration> getRegistry() {
|
||||||
|
return registry;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -29,8 +29,12 @@ import org.springframework.context.ApplicationEvent;
|
|||||||
*/
|
*/
|
||||||
public class ServiceInstancePreRegisteredEvent extends ApplicationEvent {
|
public class ServiceInstancePreRegisteredEvent extends ApplicationEvent {
|
||||||
|
|
||||||
public ServiceInstancePreRegisteredEvent(Registration source) {
|
private final ServiceRegistry<Registration> registry;
|
||||||
|
|
||||||
|
public ServiceInstancePreRegisteredEvent(ServiceRegistry<Registration> registry,
|
||||||
|
Registration source) {
|
||||||
super(source);
|
super(source);
|
||||||
|
this.registry = registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -38,4 +42,8 @@ public class ServiceInstancePreRegisteredEvent extends ApplicationEvent {
|
|||||||
return (Registration) super.getSource();
|
return (Registration) super.getSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ServiceRegistry<Registration> getRegistry() {
|
||||||
|
return registry;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user