mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
resolve @order not effect when handle ServiceInstanceChangedEvent
This commit is contained in:
parent
e4a8a14b1b
commit
3d5eaefe1c
@ -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<ServiceInstancesChangedEvent>() {
|
||||
|
||||
private final URL url2subscribe = url;
|
||||
new ServiceInstanceChangeListener() {
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.LOWEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Order
|
||||
public void onApplicationEvent(ServiceInstancesChangedEvent event) {
|
||||
|
||||
Set<String> 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<ServiceInstance> getServiceInstances(Iterable<String> serviceNames) {
|
||||
return stream(serviceNames.spliterator(), false).map(this::getServiceInstances)
|
||||
.flatMap(Collection::stream).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<ServiceInstance> 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<ServiceInstancesChangedEvent>() {
|
||||
|
||||
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<ServiceInstance> 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<ServiceInstance> serviceInstances) {
|
||||
|
||||
String serviceInterface = subscribedURL.getServiceInterface();
|
||||
String version = subscribedURL.getParameter(VERSION_KEY);
|
||||
String protocol = subscribedURL.getParameter(PROTOCOL_KEY);
|
||||
|
||||
List<ServiceInstance> serviceInstances = getServiceInstances(serviceName);
|
||||
|
||||
List<URL> urls = dubboMetadataUtils.getDubboMetadataServiceURLs(serviceInstances,
|
||||
serviceInterface, version, protocol);
|
||||
|
||||
notifyAllSubscribedURLs(subscribedURL, urls, listener);
|
||||
}
|
||||
|
||||
// private void subscribeDubboMetadataServiceURLs(URL subscribedURL,
|
||||
// NotifyListener listener, Set<String> serviceNames) {
|
||||
//
|
||||
// String serviceInterface = subscribedURL.getServiceInterface();
|
||||
// String version = subscribedURL.getParameter(VERSION_KEY);
|
||||
// String protocol = subscribedURL.getParameter(PROTOCOL_KEY);
|
||||
//
|
||||
// List<ServiceInstance> serviceInstances = getServiceInstances(serviceNames);
|
||||
//
|
||||
// List<URL> urls = dubboMetadataUtils.getDubboMetadataServiceURLs(serviceInstances,
|
||||
// serviceInterface, version, protocol);
|
||||
//
|
||||
// notifyAllSubscribedURLs(subscribedURL, urls, listener);
|
||||
// }
|
||||
private void subscribeDubboMetadataServiceURLs(URL subscribedURL,
|
||||
NotifyListener listener, String serviceName) {
|
||||
List<ServiceInstance> serviceInstances = getServiceInstances(serviceName);
|
||||
subscribeDubboMetadataServiceURLs(subscribedURL, listener, serviceName,
|
||||
serviceInstances);
|
||||
}
|
||||
|
||||
private boolean containsProviderCategory(URL subscribedURL) {
|
||||
String category = subscribedURL.getParameter(CATEGORY_KEY);
|
||||
|
@ -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 <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
|
||||
* @see ServiceInstancesChangedEvent
|
||||
* @see Ordered
|
||||
* @see ApplicationListener
|
||||
*/
|
||||
public interface ServiceInstanceChangeListener
|
||||
extends ApplicationListener<ServiceInstancesChangedEvent>, Ordered {
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user