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.ApplicationListener;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
import static java.util.Collections.emptyList;
|
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.URLBuilder.from;
|
||||||
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
|
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
|
||||||
import static org.apache.dubbo.common.constants.CommonConstants.PID_KEY;
|
import static org.apache.dubbo.common.constants.CommonConstants.PID_KEY;
|
||||||
@ -188,18 +186,24 @@ public class DubboCloudRegistry extends FailbackRegistry {
|
|||||||
// Async subscription
|
// Async subscription
|
||||||
registerServiceInstancesChangedListener(url,
|
registerServiceInstancesChangedListener(url,
|
||||||
|
|
||||||
new ApplicationListener<ServiceInstancesChangedEvent>() {
|
new ServiceInstanceChangeListener() {
|
||||||
|
|
||||||
private final URL url2subscribe = url;
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.LOWEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Order
|
|
||||||
public void onApplicationEvent(ServiceInstancesChangedEvent event) {
|
public void onApplicationEvent(ServiceInstancesChangedEvent event) {
|
||||||
|
|
||||||
Set<String> serviceNames = getServices(url);
|
Set<String> serviceNames = getServices(url);
|
||||||
|
|
||||||
String serviceName = event.getServiceName();
|
String serviceName = event.getServiceName();
|
||||||
|
|
||||||
if (serviceNames.contains(serviceName)) {
|
if (serviceNames.contains(serviceName)) {
|
||||||
|
logger.debug(
|
||||||
|
"handle serviceInstanceChange of general service, serviceName = {}, subscribeUrl={}",
|
||||||
|
event.getServiceName(), url.getServiceKey());
|
||||||
subscribeURLs(url, serviceNames, listener);
|
subscribeURLs(url, serviceNames, listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -419,11 +423,6 @@ public class DubboCloudRegistry extends FailbackRegistry {
|
|||||||
listener.notify(subscribedURLs);
|
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) {
|
private List<ServiceInstance> getServiceInstances(String serviceName) {
|
||||||
return hasText(serviceName) ? doGetServiceInstances(serviceName) : emptyList();
|
return hasText(serviceName) ? doGetServiceInstances(serviceName) : emptyList();
|
||||||
}
|
}
|
||||||
@ -472,26 +471,37 @@ public class DubboCloudRegistry extends FailbackRegistry {
|
|||||||
NotifyListener listener) {
|
NotifyListener listener) {
|
||||||
|
|
||||||
// Sync subscription
|
// Sync subscription
|
||||||
|
if (containsProviderCategory(subscribedURL)) {
|
||||||
|
|
||||||
subscribeDubboMetadataServiceURLs(subscribedURL, listener,
|
subscribeDubboMetadataServiceURLs(subscribedURL, listener,
|
||||||
getServiceName(subscribedURL));
|
getServiceName(subscribedURL));
|
||||||
|
|
||||||
// Sync subscription
|
|
||||||
if (containsProviderCategory(subscribedURL)) {
|
|
||||||
registerServiceInstancesChangedListener(subscribedURL,
|
registerServiceInstancesChangedListener(subscribedURL,
|
||||||
new ApplicationListener<ServiceInstancesChangedEvent>() {
|
new ServiceInstanceChangeListener() {
|
||||||
|
|
||||||
private final URL url2subscribe = subscribedURL;
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.LOWEST_PRECEDENCE - 1;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Order(Ordered.LOWEST_PRECEDENCE - 1)
|
|
||||||
public void onApplicationEvent(
|
public void onApplicationEvent(
|
||||||
ServiceInstancesChangedEvent event) {
|
ServiceInstancesChangedEvent event) {
|
||||||
String sourceServiceName = event.getServiceName();
|
String sourceServiceName = event.getServiceName();
|
||||||
|
List<ServiceInstance> serviceInstances = event
|
||||||
|
.getServiceInstances();
|
||||||
String serviceName = getServiceName(subscribedURL);
|
String serviceName = getServiceName(subscribedURL);
|
||||||
|
|
||||||
if (Objects.equals(sourceServiceName, serviceName)) {
|
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,
|
subscribeDubboMetadataServiceURLs(subscribedURL, listener,
|
||||||
sourceServiceName);
|
sourceServiceName, serviceInstances);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -509,34 +519,25 @@ public class DubboCloudRegistry extends FailbackRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void subscribeDubboMetadataServiceURLs(URL subscribedURL,
|
private void subscribeDubboMetadataServiceURLs(URL subscribedURL,
|
||||||
NotifyListener listener, String serviceName) {
|
NotifyListener listener, String serviceName,
|
||||||
|
List<ServiceInstance> serviceInstances) {
|
||||||
|
|
||||||
String serviceInterface = subscribedURL.getServiceInterface();
|
String serviceInterface = subscribedURL.getServiceInterface();
|
||||||
String version = subscribedURL.getParameter(VERSION_KEY);
|
String version = subscribedURL.getParameter(VERSION_KEY);
|
||||||
String protocol = subscribedURL.getParameter(PROTOCOL_KEY);
|
String protocol = subscribedURL.getParameter(PROTOCOL_KEY);
|
||||||
|
|
||||||
List<ServiceInstance> serviceInstances = getServiceInstances(serviceName);
|
|
||||||
|
|
||||||
List<URL> urls = dubboMetadataUtils.getDubboMetadataServiceURLs(serviceInstances,
|
List<URL> urls = dubboMetadataUtils.getDubboMetadataServiceURLs(serviceInstances,
|
||||||
serviceInterface, version, protocol);
|
serviceInterface, version, protocol);
|
||||||
|
|
||||||
notifyAllSubscribedURLs(subscribedURL, urls, listener);
|
notifyAllSubscribedURLs(subscribedURL, urls, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
// private void subscribeDubboMetadataServiceURLs(URL subscribedURL,
|
private void subscribeDubboMetadataServiceURLs(URL subscribedURL,
|
||||||
// NotifyListener listener, Set<String> serviceNames) {
|
NotifyListener listener, String serviceName) {
|
||||||
//
|
List<ServiceInstance> serviceInstances = getServiceInstances(serviceName);
|
||||||
// String serviceInterface = subscribedURL.getServiceInterface();
|
subscribeDubboMetadataServiceURLs(subscribedURL, listener, serviceName,
|
||||||
// String version = subscribedURL.getParameter(VERSION_KEY);
|
serviceInstances);
|
||||||
// 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 boolean containsProviderCategory(URL subscribedURL) {
|
private boolean containsProviderCategory(URL subscribedURL) {
|
||||||
String category = subscribedURL.getParameter(CATEGORY_KEY);
|
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