diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java index e27b3e06b..e8199832d 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java @@ -18,6 +18,8 @@ package com.alibaba.cloud.nacos; import java.util.Objects; +import javax.annotation.PreDestroy; + import com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureException; import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; @@ -73,6 +75,14 @@ public class NacosConfigManager { return service; } + @PreDestroy + public void destroy() throws NacosException { + if (service != null) { + service.shutDown(); + service = null; + } + } + public NacosConfigProperties getNacosConfigProperties() { return nacosConfigProperties; } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java index feea3582f..82ad817ce 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java @@ -82,7 +82,7 @@ public final class NacosDataParserHandler { } NacosByteArrayResource nacosByteArrayResource = new NacosByteArrayResource( configValue.getBytes(), configName); - nacosByteArrayResource.setFilename(getFileName(configName , extension)); + nacosByteArrayResource.setFilename(getFileName(configName, extension)); List> propertySourceList = propertySourceLoader .load(configName, nacosByteArrayResource); if (CollectionUtils.isEmpty(propertySourceList)) { @@ -135,8 +135,8 @@ public final class NacosDataParserHandler { return DEFAULT_EXTENSION; } - private String getFileName(String name,String extension){ - if(StringUtils.isEmpty(extension)){ + private String getFileName(String name, String extension) { + if (StringUtils.isEmpty(extension)) { return name; } if (StringUtils.isEmpty(name)) { @@ -145,7 +145,7 @@ public final class NacosDataParserHandler { int idx = name.lastIndexOf(DOT); if (idx > 0 && idx < name.length() - 1) { String ext = name.substring(idx + 1); - if(extension.equalsIgnoreCase(ext)){ + if (extension.equalsIgnoreCase(ext)) { return name; } } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosServiceRegistry.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosServiceRegistry.java index 97ab73101..9a79a568f 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosServiceRegistry.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosServiceRegistry.java @@ -40,11 +40,11 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; * @author eshun */ public class NacosServiceRegistry implements ServiceRegistry { - + private static final String STATUS_UP = "UP"; - + private static final String STATUS_DOWN = "DOWN"; - + private static final Logger log = LoggerFactory.getLogger(NacosServiceRegistry.class); private final NacosDiscoveryProperties nacosDiscoveryProperties; @@ -123,7 +123,8 @@ public class NacosServiceRegistry implements ServiceRegistry { @Override public void setStatus(Registration registration, String status) { - if (!STATUS_UP.equalsIgnoreCase(status) && !STATUS_DOWN.equalsIgnoreCase(status)) { + if (!STATUS_UP.equalsIgnoreCase(status) + && !STATUS_DOWN.equalsIgnoreCase(status)) { log.warn("can't support status {},please choose UP or DOWN", status); return; } @@ -141,8 +142,8 @@ public class NacosServiceRegistry implements ServiceRegistry { try { Properties nacosProperties = nacosDiscoveryProperties.getNacosProperties(); - nacosServiceManager.getNamingMaintainService(nacosProperties) - .updateInstance(serviceId, nacosDiscoveryProperties.getGroup(), instance); + nacosServiceManager.getNamingMaintainService(nacosProperties).updateInstance( + serviceId, nacosDiscoveryProperties.getGroup(), instance); } catch (Exception e) { throw new RuntimeException("update nacos instance status fail", e); diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboServiceRegistrationAutoConfiguration.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboServiceRegistrationAutoConfiguration.java index 6fca3f2ad..b0897e6ca 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboServiceRegistrationAutoConfiguration.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboServiceRegistrationAutoConfiguration.java @@ -17,18 +17,25 @@ package com.alibaba.cloud.dubbo.autoconfigure; import java.util.Collection; +import java.util.HashSet; import java.util.List; 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.bootstrap.DubboBootstrapStartCommandLineRunner; +import com.alibaba.cloud.dubbo.bootstrap.DubboBootstrapWrapper; +import com.alibaba.cloud.dubbo.bootstrap.event.DubboBootstrapStartedEvent; import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository; 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.ecwid.consul.v1.agent.model.NewService; import com.netflix.appinfo.InstanceInfo; import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; import org.apache.dubbo.config.spring.ServiceBean; -import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +49,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cloud.client.ServiceInstance; 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.netflix.eureka.EurekaInstanceConfigBean; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry; @@ -63,9 +72,11 @@ import static org.springframework.util.ObjectUtils.isEmpty; * Dubbo Service Registration Auto-{@link Configuration}. * * @author Mercy + * @author theonefx */ @Configuration(proxyBeanMethods = false) -@Import({ DubboServiceRegistrationEventPublishingAspect.class }) +@Import({ DubboServiceRegistrationEventPublishingAspect.class, + DubboBootstrapStartCommandLineRunner.class }) @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) @AutoConfigureAfter(name = { EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME, @@ -106,10 +117,42 @@ public class DubboServiceRegistrationAutoConfiguration { return new RegistryConfig(ADDRESS, PROTOCOL); } + private Map, Set> registrations = new ConcurrentHashMap<>(); + + @EventListener(DubboBootstrapStartedEvent.class) + public void onDubboBootstrapStarted(DubboBootstrapStartedEvent event) { + if (!event.getSource().isReady()) { + return; + } + registrations.forEach( + (registry, registrations) -> registrations.forEach(registration -> { + attachDubboMetadataServiceMetadata(registration); + registry.register(registration); + })); + } + @EventListener(ServiceInstancePreRegisteredEvent.class) public void onServiceInstancePreRegistered(ServiceInstancePreRegisteredEvent event) { Registration registration = event.getSource(); - attachDubboMetadataServiceMetadata(registration); + if (!DubboBootstrap.getInstance().isReady() + || !DubboBootstrap.getInstance().isStarted()) { + ServiceRegistry registry = event.getRegistry(); + synchronized (registry) { + registrations.putIfAbsent(registry, new HashSet<>()); + registrations.get(registry).add(registration); + } + } + else { + attachDubboMetadataServiceMetadata(registration); + } + + } + + @EventListener(ServiceInstancePreDeregisteredEvent.class) + public void onServiceInstancePreDeregistered( + ServiceInstancePreDeregisteredEvent event) { + ServiceRegistry registry = event.getRegistry(); + registrations.remove(registry); } private void attachDubboMetadataServiceMetadata(Registration registration) { @@ -132,21 +175,67 @@ public class DubboServiceRegistrationAutoConfiguration { @Configuration(proxyBeanMethods = false) @ConditionalOnBean(name = EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME) - @Aspect class EurekaConfiguration implements SmartInitializingSingleton { @Autowired private ObjectProvider> serviceBeans; + @EventListener(DubboBootstrapStartedEvent.class) + public void onDubboBootstrapStarted(DubboBootstrapStartedEvent event) { + DubboBootstrapWrapper wrapper = event.getSource(); + if (!wrapper.isReady()) { + return; + } + registrations.forEach( + (registry, registrations) -> registrations.removeIf(registration -> { + if (!(registration instanceof EurekaRegistration)) { + return false; + } + EurekaRegistration eurekaRegistration = (EurekaRegistration) registration; + InstanceInfo instanceInfo = eurekaRegistration + .getApplicationInfoManager().getInfo(); + + EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) eurekaRegistration + .getInstanceConfig(); + config.setInitialStatus(InstanceInfo.InstanceStatus.UP); + + attachDubboMetadataServiceMetadata(instanceInfo.getMetadata()); + eurekaRegistration.getApplicationInfoManager() + .registerAppMetadata(instanceInfo.getMetadata()); + eurekaRegistration.getApplicationInfoManager() + .setInstanceStatus(InstanceInfo.InstanceStatus.UP); + return true; + })); + } + @EventListener(ServiceInstancePreRegisteredEvent.class) public void onServiceInstancePreRegistered( ServiceInstancePreRegisteredEvent event) { Registration registration = event.getSource(); - EurekaRegistration eurekaRegistration = EurekaRegistration.class - .cast(registration); - InstanceInfo instanceInfo = eurekaRegistration.getApplicationInfoManager() - .getInfo(); - attachDubboMetadataServiceMetadata(instanceInfo.getMetadata()); + if (!(registration instanceof EurekaRegistration)) { + return; + } + + if (DubboBootstrap.getInstance().isReady() + && DubboBootstrap.getInstance().isStarted()) { + EurekaRegistration eurekaRegistration = (EurekaRegistration) registration; + InstanceInfo instanceInfo = eurekaRegistration.getApplicationInfoManager() + .getInfo(); + + EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) eurekaRegistration + .getInstanceConfig(); + config.setInitialStatus(InstanceInfo.InstanceStatus.UP); + + attachDubboMetadataServiceMetadata(instanceInfo.getMetadata()); + eurekaRegistration.getApplicationInfoManager() + .registerAppMetadata(instanceInfo.getMetadata()); + } + else { + EurekaRegistration eurekaRegistration = (EurekaRegistration) registration; + EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) eurekaRegistration + .getInstanceConfig(); + config.setInitialStatus(InstanceInfo.InstanceStatus.STARTING); + } } /** @@ -171,21 +260,28 @@ public class DubboServiceRegistrationAutoConfiguration { @AutoConfigureOrder class ConsulConfiguration { - /** - * Handle the pre-registered event of {@link ServiceInstance} for Consul. - * @param event {@link ServiceInstancePreRegisteredEvent} - */ - @EventListener(ServiceInstancePreRegisteredEvent.class) - public void onServiceInstancePreRegistered( - ServiceInstancePreRegisteredEvent event) { - Registration registration = event.getSource(); - Class registrationClass = AopUtils.getTargetClass(registration); - String registrationClassName = registrationClass.getName(); - if (CONSUL_AUTO_SERVICE_AUTO_REGISTRATION_CLASS_NAME - .equalsIgnoreCase(registrationClassName)) { - ConsulRegistration consulRegistration = (ConsulRegistration) registration; - attachURLsIntoMetadata(consulRegistration); + @EventListener(DubboBootstrapStartedEvent.class) + public void attachURLsIntoMetadataBeforeReRegist( + DubboBootstrapStartedEvent event) { + if (!event.getSource().isReady()) { + return; } + registrations.entrySet().removeIf(entry -> { + Set registrations = entry.getValue(); + registrations.removeIf(registration -> { + Class registrationClass = AopUtils.getTargetClass(registration); + String registrationClassName = registrationClass.getName(); + return !CONSUL_AUTO_SERVICE_AUTO_REGISTRATION_CLASS_NAME + .equalsIgnoreCase(registrationClassName); + }); + return registrations.isEmpty(); + }); + + registrations.forEach( + (registry, registrations) -> registrations.forEach(registration -> { + ConsulRegistration consulRegistration = (ConsulRegistration) registration; + attachURLsIntoMetadata(consulRegistration); + })); } private void attachURLsIntoMetadata(ConsulRegistration consulRegistration) { diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/bootstrap/DubboBootstrapStartCommandLineRunner.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/bootstrap/DubboBootstrapStartCommandLineRunner.java new file mode 100644 index 000000000..9f47bb2e8 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/bootstrap/DubboBootstrapStartCommandLineRunner.java @@ -0,0 +1,50 @@ +/* + * 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.bootstrap; + +import com.alibaba.cloud.dubbo.bootstrap.event.DubboBootstrapStartedEvent; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.stereotype.Component; + +/** + * publish Dubbo microsystem startup finish event. + * + * @author theonefx + */ + +@Component +public class DubboBootstrapStartCommandLineRunner + implements CommandLineRunner, ApplicationEventPublisherAware { + + private ApplicationEventPublisher applicationEventPublisher; + + @Override + public void setApplicationEventPublisher( + ApplicationEventPublisher applicationEventPublisher) { + this.applicationEventPublisher = applicationEventPublisher; + } + + @Override + public void run(String... args) { + applicationEventPublisher.publishEvent( + new DubboBootstrapStartedEvent(DubboBootstrapWrapper.getInstance())); + } + +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/bootstrap/DubboBootstrapWrapper.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/bootstrap/DubboBootstrapWrapper.java new file mode 100644 index 000000000..60fcaf6a5 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/bootstrap/DubboBootstrapWrapper.java @@ -0,0 +1,47 @@ +/* + * 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.bootstrap; + +import org.apache.dubbo.config.bootstrap.DubboBootstrap; + +/** + * Wrapper DubboBootstrap operation. + * + * @author theonefx + */ +public final class DubboBootstrapWrapper { + + private DubboBootstrapWrapper() { + } + + private static final DubboBootstrapWrapper INSTANCE = new DubboBootstrapWrapper(); + + public static DubboBootstrapWrapper getInstance() { + return INSTANCE; + } + + public boolean isReady() { + return DubboBootstrap.getInstance().isStarted() + && DubboBootstrap.getInstance().isReady() + && DubboBootstrap.getInstance().isInitialized(); + } + + public DubboBootstrap getDubboBootstrap() { + return DubboBootstrap.getInstance(); + } + +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/bootstrap/event/DubboBootstrapStartedEvent.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/bootstrap/event/DubboBootstrapStartedEvent.java new file mode 100644 index 000000000..3f14525c8 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/bootstrap/event/DubboBootstrapStartedEvent.java @@ -0,0 +1,44 @@ +/* + * 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.bootstrap.event; + +import com.alibaba.cloud.dubbo.bootstrap.DubboBootstrapWrapper; + +import org.springframework.context.ApplicationEvent; + +/** + * Dubbo microsytem start finish event, every thing is ready. + * + * @author theonefx + */ +public class DubboBootstrapStartedEvent extends ApplicationEvent { + + /** + * Create a new {@code DubboBootstrapStartedEvent}. + * @param source the object on which the event initially occurred or with which the + * event is associated (never {@code null}) + */ + public DubboBootstrapStartedEvent(DubboBootstrapWrapper source) { + super(source); + } + + @Override + public DubboBootstrapWrapper getSource() { + return (DubboBootstrapWrapper) super.getSource(); + } + +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/DubboCloudRegistry.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/DubboCloudRegistry.java index 7639e21aa..be24b11fb 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/DubboCloudRegistry.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/DubboCloudRegistry.java @@ -290,7 +290,7 @@ public class DubboCloudRegistry extends FailbackRegistry { .map(templateURL -> templateURL.removeParameter(PID_KEY)) .map(templateURL -> { String protocol = templateURL.getProtocol(); - int port = repository.getDubboProtocolPort(serviceInstance, + Integer port = repository.getDubboProtocolPort(serviceInstance, protocol); if (Objects.equals(templateURL.getHost(), host) && Objects.equals(templateURL.getPort(), port)) { // use @@ -300,15 +300,27 @@ public class DubboCloudRegistry extends FailbackRegistry { return templateURL; } - URLBuilder clonedURLBuilder = from(templateURL) // remove the - // parameters from - // the template - // URL - .setHost(host) // reset the host - .setPort(port); // reset the port + if (port == null) { + if (logger.isWarnEnabled()) { + logger.warn( + "The protocol[{}] port of Dubbo service instance[host : {}] " + + "can't be resolved", + protocol, host); + } + return null; + } + else { + URLBuilder clonedURLBuilder = from(templateURL) // remove the + // parameters from + // the template + // URL + .setHost(host) // reset the host + .setPort(port); // reset the port + + return clonedURLBuilder.build(); + } - return clonedURLBuilder.build(); - }).forEach(clonedExportedURLs::add); + }).filter(Objects::nonNull).forEach(clonedExportedURLs::add); }); return clonedExportedURLs; } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/DubboServiceRegistrationEventPublishingAspect.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/DubboServiceRegistrationEventPublishingAspect.java index 43e679f53..8cd5c6b7d 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/DubboServiceRegistrationEventPublishingAspect.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/DubboServiceRegistrationEventPublishingAspect.java @@ -16,6 +16,7 @@ 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.ServiceInstanceRegisteredEvent; import org.aspectj.lang.annotation.After; @@ -33,6 +34,7 @@ import org.springframework.context.ApplicationEventPublisherAware; * @author Mercy * @see ServiceInstancePreRegisteredEvent * @see ServiceInstanceRegisteredEvent + * @see ServiceInstancePreDeregisteredEvent */ @Aspect public class DubboServiceRegistrationEventPublishingAspect @@ -41,18 +43,29 @@ public class DubboServiceRegistrationEventPublishingAspect /** * 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; - @Before(REGISTER_POINTCUT_EXPRESSION) - public void beforeRegister(Registration registration) { - applicationEventPublisher - .publishEvent(new ServiceInstancePreRegisteredEvent(registration)); + @Before(value = REGISTER_POINTCUT_EXPRESSION, argNames = "registry, registration") + public void beforeRegister(ServiceRegistry registry, Registration registration) { + applicationEventPublisher.publishEvent( + new ServiceInstancePreRegisteredEvent(registry, registration)); + } + + @Before(value = DEREGISTER_POINTCUT_EXPRESSION, argNames = "registry, registration") + public void beforeDeregister(ServiceRegistry registry, Registration registration) { + applicationEventPublisher.publishEvent( + new ServiceInstancePreDeregisteredEvent(registry, registration)); } - @After(REGISTER_POINTCUT_EXPRESSION) - public void afterRegister(Registration registration) { + @After(value = REGISTER_POINTCUT_EXPRESSION, argNames = "registry, registration") + public void afterRegister(ServiceRegistry registry, Registration registration) { applicationEventPublisher .publishEvent(new ServiceInstanceRegisteredEvent(registration)); } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/event/ServiceInstancePreDeregisteredEvent.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/event/ServiceInstancePreDeregisteredEvent.java new file mode 100644 index 000000000..5e60be5b9 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/event/ServiceInstancePreDeregisteredEvent.java @@ -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 Mercy + */ +public class ServiceInstancePreDeregisteredEvent extends ApplicationEvent { + + private final ServiceRegistry registry; + + public ServiceInstancePreDeregisteredEvent(ServiceRegistry registry, + Registration source) { + super(source); + this.registry = registry; + } + + @Override + public Registration getSource() { + return (Registration) super.getSource(); + } + + public ServiceRegistry getRegistry() { + return registry; + } + +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/event/ServiceInstancePreRegisteredEvent.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/event/ServiceInstancePreRegisteredEvent.java index 4733fef69..e1b905c04 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/event/ServiceInstancePreRegisteredEvent.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/event/ServiceInstancePreRegisteredEvent.java @@ -29,8 +29,12 @@ import org.springframework.context.ApplicationEvent; */ public class ServiceInstancePreRegisteredEvent extends ApplicationEvent { - public ServiceInstancePreRegisteredEvent(Registration source) { + private final ServiceRegistry registry; + + public ServiceInstancePreRegisteredEvent(ServiceRegistry registry, + Registration source) { super(source); + this.registry = registry; } @Override @@ -38,4 +42,8 @@ public class ServiceInstancePreRegisteredEvent extends ApplicationEvent { return (Registration) super.getSource(); } + public ServiceRegistry getRegistry() { + return registry; + } + }