1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00
This commit is contained in:
mercyblitz 2019-04-17 11:32:23 +08:00
parent 7af5e12003
commit aef27e885e
5 changed files with 37 additions and 22 deletions

View File

@ -322,8 +322,8 @@ public class DubboServiceMetadataRepository {
return hasText(protocolPort) ? Integer.valueOf(protocolPort) : null;
}
public List<URL> getExportedURLs(String serviceInstance, String group, String version) {
String serviceKey = URL.buildKey(serviceInstance, group, version);
public List<URL> getExportedURLs(String serviceInterface, String group, String version) {
String serviceKey = URL.buildKey(serviceInterface, group, version);
return allExportedURLs.getOrDefault(serviceKey, Collections.emptyList());
}

View File

@ -172,15 +172,11 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
.map(dubboMetadataConfigServiceProxy::getProxy)
.filter(Objects::nonNull)
.forEach(dubboMetadataService -> {
String serviceInterface = url.getServiceInterface();
String group = url.getParameter(GROUP_KEY);
String version = url.getParameter(VERSION_KEY);
String exportedURLsJSON = dubboMetadataService.getExportedURLs(serviceInterface, group, version);
List<URL> exportedURLs = jsonUtils.toURLs(exportedURLsJSON);
List<URL> exportedURLs = getExportedURLs(dubboMetadataService, url);
List<URL> allSubscribedURLs = new LinkedList<>();
for (URL exportedURL : exportedURLs) {
String serviceName = exportedURL.getParameter(APPLICATION_KEY);
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceName);
List<ServiceInstance> serviceInstances = getServiceInstances(serviceName);
String protocol = exportedURL.getProtocol();
List<URL> subscribedURLs = new LinkedList<>();
serviceInstances.forEach(serviceInstance -> {
@ -209,6 +205,21 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
});
}
private List<URL> getExportedURLs(DubboMetadataService dubboMetadataService, URL url) {
String serviceInterface = url.getServiceInterface();
String group = url.getParameter(GROUP_KEY);
String version = url.getParameter(VERSION_KEY);
// The subscribed protocol may be null
String subscribedProtocol = url.getParameter(PROTOCOL_KEY);
String exportedURLsJSON = dubboMetadataService.getExportedURLs(serviceInterface, group, version);
return jsonUtils
.toURLs(exportedURLsJSON)
.stream()
.filter(exportedURL ->
subscribedProtocol == null || subscribedProtocol.equalsIgnoreCase(exportedURL.getProtocol())
).collect(Collectors.toList());
}
private void subscribeDubboMetadataServiceURLs(URL url, NotifyListener listener) {
String serviceInterface = url.getServiceInterface();

View File

@ -64,13 +64,13 @@ public interface DubboMetadataService {
Map<String, String> getAllExportedURLs();
/**
* Get the json content of an exported List<URL> of {@link URL URLs} by the serviceInstance , group and version
* Get the json content of an exported List<URL> of {@link URL URLs} by the serviceInterface , group and version
*
* @param serviceInstance The class name of service interface
* @param serviceInterface The class name of service interface
* @param group {@link Service#group() the service group} (optional)
* @param version {@link Service#version() the service version} (optional)
* @return non-null read-only {@link List}
* @see URL
*/
String getExportedURLs(String serviceInstance, String group, String version);
String getExportedURLs(String serviceInterface, String group, String version);
}

View File

@ -18,6 +18,9 @@ package org.springframework.cloud.alibaba.dubbo.service;
import org.apache.dubbo.rpc.service.GenericService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.stream.Stream;
@ -29,14 +32,7 @@ import java.util.stream.Stream;
*/
class DubboMetadataServiceInvocationHandler implements InvocationHandler {
/**
* The method name of {@link DubboMetadataService#getServiceRestMetadata()}
*/
private static final String METHOD_NAME = "getServiceRestMetadata";
private static final String[] PARAMETER_TYPES = new String[0];
private static final String[] PARAMETER_VALUES = new String[0];
private final Logger logger = LoggerFactory.getLogger(getClass());
private final GenericService genericService;
@ -46,7 +42,15 @@ class DubboMetadataServiceInvocationHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return genericService.$invoke(method.getName(), getParameterTypes(method), args);
Object returnValue = null;
try {
returnValue = genericService.$invoke(method.getName(), getParameterTypes(method), args);
} catch (Throwable e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
}
return returnValue;
}
private String[] getParameterTypes(Method method) {

View File

@ -84,8 +84,8 @@ public class IntrospectiveDubboMetadataService implements DubboMetadataService {
}
@Override
public String getExportedURLs(String serviceInstance, String group, String version) {
List<URL> urls = dubboServiceMetadataRepository.getExportedURLs(serviceInstance, group, version);
public String getExportedURLs(String serviceInterface, String group, String version) {
List<URL> urls = dubboServiceMetadataRepository.getExportedURLs(serviceInterface, group, version);
return jsonUtils.toJSON(urls);
}
}