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 04d9c99e..81464733 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 @@ -45,12 +45,10 @@ import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; import org.springframework.util.CollectionUtils; import static java.lang.String.format; import static java.util.Collections.emptyList; -import static java.util.stream.StreamSupport.stream; import static org.apache.dubbo.common.URLBuilder.from; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PID_KEY; @@ -188,18 +186,24 @@ public class DubboCloudRegistry extends FailbackRegistry { // Async subscription registerServiceInstancesChangedListener(url, - new ApplicationListener() { - - private final URL url2subscribe = url; + new ServiceInstanceChangeListener() { + + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE; + } @Override - @Order public void onApplicationEvent(ServiceInstancesChangedEvent event) { + Set serviceNames = getServices(url); String serviceName = event.getServiceName(); if (serviceNames.contains(serviceName)) { + logger.debug( + "handle serviceInstanceChange of general service, serviceName = {}, subscribeUrl={}", + event.getServiceName(), url.getServiceKey()); subscribeURLs(url, serviceNames, listener); } } @@ -419,11 +423,6 @@ public class DubboCloudRegistry extends FailbackRegistry { listener.notify(subscribedURLs); } - private List getServiceInstances(Iterable serviceNames) { - return stream(serviceNames.spliterator(), false).map(this::getServiceInstances) - .flatMap(Collection::stream).collect(Collectors.toList()); - } - private List getServiceInstances(String serviceName) { return hasText(serviceName) ? doGetServiceInstances(serviceName) : emptyList(); } @@ -471,27 +470,38 @@ public class DubboCloudRegistry extends FailbackRegistry { private void subscribeDubboMetadataServiceURLs(URL subscribedURL, NotifyListener listener) { - // Sync subscription - subscribeDubboMetadataServiceURLs(subscribedURL, listener, - getServiceName(subscribedURL)); - // Sync subscription if (containsProviderCategory(subscribedURL)) { - registerServiceInstancesChangedListener(subscribedURL, - new ApplicationListener() { - private final URL url2subscribe = subscribedURL; + subscribeDubboMetadataServiceURLs(subscribedURL, listener, + getServiceName(subscribedURL)); + + registerServiceInstancesChangedListener(subscribedURL, + new ServiceInstanceChangeListener() { + + @Override + public int getOrder() { + return Ordered.LOWEST_PRECEDENCE - 1; + } @Override - @Order(Ordered.LOWEST_PRECEDENCE - 1) public void onApplicationEvent( ServiceInstancesChangedEvent event) { String sourceServiceName = event.getServiceName(); + List serviceInstances = event + .getServiceInstances(); String serviceName = getServiceName(subscribedURL); if (Objects.equals(sourceServiceName, serviceName)) { + logger.debug( + "handle serviceInstanceChange of metadata service, serviceName = {}, subscribeUrl={}", + event.getServiceName(), + subscribedURL.getServiceKey()); + + // only update serviceInstances of the specified + // serviceName subscribeDubboMetadataServiceURLs(subscribedURL, listener, - sourceServiceName); + sourceServiceName, serviceInstances); } } @@ -509,34 +519,25 @@ public class DubboCloudRegistry extends FailbackRegistry { } private void subscribeDubboMetadataServiceURLs(URL subscribedURL, - NotifyListener listener, String serviceName) { + NotifyListener listener, String serviceName, + List serviceInstances) { String serviceInterface = subscribedURL.getServiceInterface(); String version = subscribedURL.getParameter(VERSION_KEY); String protocol = subscribedURL.getParameter(PROTOCOL_KEY); - List serviceInstances = getServiceInstances(serviceName); - List urls = dubboMetadataUtils.getDubboMetadataServiceURLs(serviceInstances, serviceInterface, version, protocol); notifyAllSubscribedURLs(subscribedURL, urls, listener); } - // private void subscribeDubboMetadataServiceURLs(URL subscribedURL, - // NotifyListener listener, Set serviceNames) { - // - // String serviceInterface = subscribedURL.getServiceInterface(); - // String version = subscribedURL.getParameter(VERSION_KEY); - // String protocol = subscribedURL.getParameter(PROTOCOL_KEY); - // - // List serviceInstances = getServiceInstances(serviceNames); - // - // List urls = dubboMetadataUtils.getDubboMetadataServiceURLs(serviceInstances, - // serviceInterface, version, protocol); - // - // notifyAllSubscribedURLs(subscribedURL, urls, listener); - // } + private void subscribeDubboMetadataServiceURLs(URL subscribedURL, + NotifyListener listener, String serviceName) { + List serviceInstances = getServiceInstances(serviceName); + subscribeDubboMetadataServiceURLs(subscribedURL, listener, serviceName, + serviceInstances); + } private boolean containsProviderCategory(URL subscribedURL) { String category = subscribedURL.getParameter(CATEGORY_KEY); diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/ServiceInstanceChangeListener.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/ServiceInstanceChangeListener.java new file mode 100644 index 00000000..12321483 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/ServiceInstanceChangeListener.java @@ -0,0 +1,35 @@ +/* + * 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; + +import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent; + +import org.springframework.context.ApplicationListener; +import org.springframework.core.Ordered; + +/** + * The interface of ServiceInstanceChange event Listener. + * + * @author theonefx + * @see ServiceInstancesChangedEvent + * @see Ordered + * @see ApplicationListener + */ +public interface ServiceInstanceChangeListener + extends ApplicationListener, Ordered { + +}