mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
do some enhancements about dubbo spring cloud
This commit is contained in:
parent
a939bb3047
commit
82ee081ae6
@ -19,6 +19,7 @@ import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.alibaba.cloud.dubbo.env.DubboCloudProperties;
|
||||
import com.alibaba.cloud.dubbo.metadata.DubboProtocolConfigSupplier;
|
||||
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
||||
import com.alibaba.cloud.dubbo.metadata.repository.MetadataServiceInstanceSelector;
|
||||
@ -65,6 +66,9 @@ public class DubboMetadataAutoConfiguration {
|
||||
@Autowired
|
||||
private DubboMetadataServiceExporter dubboMetadataConfigServiceExporter;
|
||||
|
||||
@Autowired
|
||||
private DubboCloudProperties dubboCloudProperties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MetadataResolver metadataJsonResolver(ObjectProvider<Contract> contract) {
|
||||
@ -89,7 +93,7 @@ public class DubboMetadataAutoConfiguration {
|
||||
@ConditionalOnMissingBean
|
||||
public DubboMetadataServiceProxy dubboMetadataConfigServiceProxy(
|
||||
DubboGenericServiceFactory factory) {
|
||||
return new DubboMetadataServiceProxy(factory);
|
||||
return new DubboMetadataServiceProxy(factory, dubboCloudProperties);
|
||||
}
|
||||
|
||||
// Event-Handling
|
||||
|
@ -46,6 +46,18 @@ public class DubboCloudProperties {
|
||||
*/
|
||||
private String subscribedServices = ALL_DUBBO_SERVICES;
|
||||
|
||||
/**
|
||||
* The Service Instance changed do not means Dubbo service ready, need retry
|
||||
* mechanism. Retry count of Generic Invoke.
|
||||
*/
|
||||
private int retryCount = 5;
|
||||
|
||||
/**
|
||||
* The Service Instance changed do not means Dubbo service ready, need retry
|
||||
* mechanism. Retry interval of Generic Invoke, milliseconds unit.
|
||||
*/
|
||||
private int interval = 10000;
|
||||
|
||||
public String getSubscribedServices() {
|
||||
return subscribedServices;
|
||||
}
|
||||
@ -54,6 +66,22 @@ public class DubboCloudProperties {
|
||||
this.subscribedServices = subscribedServices;
|
||||
}
|
||||
|
||||
public int getRetryCount() {
|
||||
return retryCount;
|
||||
}
|
||||
|
||||
public void setRetryCount(int retryCount) {
|
||||
this.retryCount = retryCount;
|
||||
}
|
||||
|
||||
public int getInterval() {
|
||||
return interval;
|
||||
}
|
||||
|
||||
public void setInterval(int interval) {
|
||||
this.interval = interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subscribed services as a {@link Set} with configuration order.
|
||||
*
|
||||
|
@ -19,6 +19,8 @@ import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.alibaba.cloud.dubbo.env.DubboCloudProperties;
|
||||
|
||||
import org.apache.dubbo.rpc.service.GenericService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -34,8 +36,12 @@ class DubboMetadataServiceInvocationHandler implements InvocationHandler {
|
||||
|
||||
private final GenericService genericService;
|
||||
|
||||
private final DubboCloudProperties dubboCloudProperties;
|
||||
|
||||
DubboMetadataServiceInvocationHandler(String serviceName, String version,
|
||||
DubboGenericServiceFactory dubboGenericServiceFactory) {
|
||||
DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||
DubboCloudProperties dubboCloudProperties) {
|
||||
this.dubboCloudProperties = dubboCloudProperties;
|
||||
this.genericService = dubboGenericServiceFactory.create(serviceName,
|
||||
DubboMetadataService.class, version);
|
||||
}
|
||||
@ -43,13 +49,16 @@ class DubboMetadataServiceInvocationHandler implements InvocationHandler {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
Object returnValue = null;
|
||||
try {
|
||||
returnValue = genericService.$invoke(method.getName(),
|
||||
getParameterTypes(method), args);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
if (logger.isErrorEnabled()) {
|
||||
logger.error(e.getMessage(), e);
|
||||
for (int i = 1; i <= dubboCloudProperties.getRetryCount(); i++) {
|
||||
try {
|
||||
returnValue = genericService.$invoke(method.getName(),
|
||||
getParameterTypes(method), args);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
if (logger.isErrorEnabled()) {
|
||||
logger.error("[failed " + i + " times] " + e.getMessage(), e);
|
||||
}
|
||||
Thread.sleep(dubboCloudProperties.getInterval() * i);
|
||||
}
|
||||
}
|
||||
return returnValue;
|
||||
|
@ -18,6 +18,8 @@ package com.alibaba.cloud.dubbo.service;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.alibaba.cloud.dubbo.env.DubboCloudProperties;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
|
||||
@ -32,13 +34,17 @@ public class DubboMetadataServiceProxy implements BeanClassLoaderAware, Disposab
|
||||
|
||||
private final DubboGenericServiceFactory dubboGenericServiceFactory;
|
||||
|
||||
private final DubboCloudProperties dubboCloudProperties;
|
||||
|
||||
private final Map<String, DubboMetadataService> dubboMetadataServiceCache = new ConcurrentHashMap<>();
|
||||
|
||||
private ClassLoader classLoader;
|
||||
|
||||
public DubboMetadataServiceProxy(
|
||||
DubboGenericServiceFactory dubboGenericServiceFactory) {
|
||||
DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||
DubboCloudProperties dubboCloudProperties) {
|
||||
this.dubboGenericServiceFactory = dubboGenericServiceFactory;
|
||||
this.dubboCloudProperties = dubboCloudProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,7 +97,7 @@ public class DubboMetadataServiceProxy implements BeanClassLoaderAware, Disposab
|
||||
return (DubboMetadataService) newProxyInstance(classLoader,
|
||||
new Class[] { DubboMetadataService.class },
|
||||
new DubboMetadataServiceInvocationHandler(serviceName, version,
|
||||
dubboGenericServiceFactory));
|
||||
dubboGenericServiceFactory, dubboCloudProperties));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user