mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish alibaba/spring-cloud-alibaba#1758 : [CodeBase] Sync the source code from greenwich to finichley
This commit is contained in:
parent
8614a9c3be
commit
6b8399fa47
@ -31,8 +31,7 @@ import org.springframework.context.annotation.PropertySource;
|
|||||||
*
|
*
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
*/
|
*/
|
||||||
@ConditionalOnClass(
|
@ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.annotation.Endpoint")
|
||||||
name = "org.springframework.boot.actuate.endpoint.annotation.Endpoint")
|
|
||||||
@PropertySource("classpath:/META-INF/dubbo/default/actuator-endpoints.properties")
|
@PropertySource("classpath:/META-INF/dubbo/default/actuator-endpoints.properties")
|
||||||
@ManagementContextConfiguration
|
@ManagementContextConfiguration
|
||||||
public class DubboMetadataEndpointAutoConfiguration {
|
public class DubboMetadataEndpointAutoConfiguration {
|
||||||
|
@ -37,12 +37,16 @@ import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_RETRIES;
|
|||||||
* Spring Cloud Service-to-Service call is transported by Dubbo under the hood, there are
|
* Spring Cloud Service-to-Service call is transported by Dubbo under the hood, there are
|
||||||
* two main scenarios:
|
* two main scenarios:
|
||||||
* <ol>
|
* <ol>
|
||||||
* <li>{@link FeignClient @FeignClient} annotated classes: If
|
* <li>{@link FeignClient @FeignClient} annotated classes:
|
||||||
* {@link DubboTransported @DubboTransported} annotated classes, the invocation of all
|
* <ul>
|
||||||
|
* If {@link DubboTransported @DubboTransported} annotated classes, the invocation of all
|
||||||
* methods of {@link FeignClient @FeignClient} annotated classes.
|
* methods of {@link FeignClient @FeignClient} annotated classes.
|
||||||
*
|
* </ul>
|
||||||
|
* <ul>
|
||||||
* If {@link DubboTransported @DubboTransported} annotated methods of
|
* If {@link DubboTransported @DubboTransported} annotated methods of
|
||||||
* {@link FeignClient @FeignClient} annotated classes.</li>
|
* {@link FeignClient @FeignClient} annotated classes.
|
||||||
|
* </ul>
|
||||||
|
* </li>
|
||||||
* <li>{@link LoadBalanced @LoadBalanced} {@link RestTemplate} annotated field, method and
|
* <li>{@link LoadBalanced @LoadBalanced} {@link RestTemplate} annotated field, method and
|
||||||
* parameters</li>
|
* parameters</li>
|
||||||
* </ol>
|
* </ol>
|
||||||
|
@ -65,7 +65,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
@AutoConfigureAfter(name = {
|
@AutoConfigureAfter(name = {
|
||||||
"org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration" })
|
"org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration" })
|
||||||
public class DubboLoadBalancedRestTemplateAutoConfiguration implements
|
public class DubboLoadBalancedRestTemplateAutoConfiguration implements
|
||||||
BeanClassLoaderAware, ApplicationContextAware, SmartInitializingSingleton {
|
BeanClassLoaderAware, ApplicationContextAware, SmartInitializingSingleton {
|
||||||
|
|
||||||
private static final Class<DubboTransported> DUBBO_TRANSPORTED_CLASS = DubboTransported.class;
|
private static final Class<DubboTransported> DUBBO_TRANSPORTED_CLASS = DubboTransported.class;
|
||||||
|
|
||||||
@ -174,7 +174,7 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration implements
|
|||||||
* {@link DubboTransported @DubboTransported}
|
* {@link DubboTransported @DubboTransported}
|
||||||
*/
|
*/
|
||||||
private void adaptRestTemplate(RestTemplate restTemplate,
|
private void adaptRestTemplate(RestTemplate restTemplate,
|
||||||
Map<String, Object> dubboTranslatedAttributes) {
|
Map<String, Object> dubboTranslatedAttributes) {
|
||||||
|
|
||||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(
|
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(
|
||||||
restTemplate.getInterceptors());
|
restTemplate.getInterceptors());
|
||||||
|
@ -17,18 +17,18 @@
|
|||||||
package com.alibaba.cloud.dubbo.autoconfigure;
|
package com.alibaba.cloud.dubbo.autoconfigure;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import com.alibaba.cloud.dubbo.metadata.DubboProtocolConfigSupplier;
|
import com.alibaba.cloud.dubbo.metadata.DubboProtocolConfigSupplier;
|
||||||
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
||||||
import com.alibaba.cloud.dubbo.metadata.repository.MetadataServiceInstanceSelector;
|
import com.alibaba.cloud.dubbo.metadata.repository.RandomServiceInstanceSelector;
|
||||||
|
import com.alibaba.cloud.dubbo.metadata.repository.ServiceInstanceSelector;
|
||||||
import com.alibaba.cloud.dubbo.metadata.resolver.DubboServiceBeanMetadataResolver;
|
import com.alibaba.cloud.dubbo.metadata.resolver.DubboServiceBeanMetadataResolver;
|
||||||
import com.alibaba.cloud.dubbo.metadata.resolver.MetadataResolver;
|
import com.alibaba.cloud.dubbo.metadata.resolver.MetadataResolver;
|
||||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
|
|
||||||
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceExporter;
|
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceExporter;
|
||||||
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
|
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
|
||||||
import com.alibaba.cloud.dubbo.service.IntrospectiveDubboMetadataService;
|
import com.alibaba.cloud.dubbo.service.IntrospectiveDubboMetadataService;
|
||||||
|
import com.alibaba.cloud.dubbo.util.DubboMetadataUtils;
|
||||||
import com.alibaba.cloud.dubbo.util.JSONUtils;
|
import com.alibaba.cloud.dubbo.util.JSONUtils;
|
||||||
import feign.Contract;
|
import feign.Contract;
|
||||||
import org.apache.dubbo.config.ProtocolConfig;
|
import org.apache.dubbo.config.ProtocolConfig;
|
||||||
@ -44,7 +44,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.context.event.ContextClosedEvent;
|
import org.springframework.context.event.ContextClosedEvent;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring Boot Auto-Configuration class for Dubbo Metadata.
|
* Spring Boot Auto-Configuration class for Dubbo Metadata.
|
||||||
@ -53,7 +52,8 @@ import org.springframework.util.CollectionUtils;
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import({ DubboServiceMetadataRepository.class, IntrospectiveDubboMetadataService.class,
|
@Import({ DubboServiceMetadataRepository.class, IntrospectiveDubboMetadataService.class,
|
||||||
DubboMetadataServiceExporter.class, JSONUtils.class })
|
DubboMetadataServiceExporter.class, JSONUtils.class,
|
||||||
|
DubboMetadataServiceProxy.class, DubboMetadataUtils.class })
|
||||||
public class DubboMetadataAutoConfiguration {
|
public class DubboMetadataAutoConfiguration {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -73,9 +73,8 @@ public class DubboMetadataAutoConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public MetadataServiceInstanceSelector metadataServiceInstanceSelector() {
|
public ServiceInstanceSelector metadataServiceInstanceSelector() {
|
||||||
return serviceInstances -> CollectionUtils.isEmpty(serviceInstances)
|
return new RandomServiceInstanceSelector();
|
||||||
? Optional.empty() : serviceInstances.stream().findAny();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -84,15 +83,7 @@ public class DubboMetadataAutoConfiguration {
|
|||||||
return new DubboProtocolConfigSupplier(protocols);
|
return new DubboProtocolConfigSupplier(protocols);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
public DubboMetadataServiceProxy dubboMetadataConfigServiceProxy(
|
|
||||||
DubboGenericServiceFactory factory) {
|
|
||||||
return new DubboMetadataServiceProxy(factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Event-Handling
|
// Event-Handling
|
||||||
|
|
||||||
@EventListener(ServiceBeanExportedEvent.class)
|
@EventListener(ServiceBeanExportedEvent.class)
|
||||||
public void onServiceBeanExported(ServiceBeanExportedEvent event) {
|
public void onServiceBeanExported(ServiceBeanExportedEvent event) {
|
||||||
ServiceBean serviceBean = event.getServiceBean();
|
ServiceBean serviceBean = event.getServiceBean();
|
||||||
|
@ -35,8 +35,8 @@ import static com.alibaba.cloud.dubbo.autoconfigure.DubboOpenFeignAutoConfigurat
|
|||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
*/
|
*/
|
||||||
@ConditionalOnClass(name = { "feign.Feign", TARGETER_CLASS_NAME })
|
@ConditionalOnClass(name = { "feign.Feign", TARGETER_CLASS_NAME })
|
||||||
@AutoConfigureAfter(
|
@AutoConfigureAfter(name = {
|
||||||
name = { "org.springframework.cloud.openfeign.FeignAutoConfiguration" })
|
"org.springframework.cloud.openfeign.FeignAutoConfiguration" })
|
||||||
@Configuration
|
@Configuration
|
||||||
public class DubboOpenFeignAutoConfiguration {
|
public class DubboOpenFeignAutoConfiguration {
|
||||||
|
|
||||||
@ -47,9 +47,9 @@ public class DubboOpenFeignAutoConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TargeterBeanPostProcessor targeterBeanPostProcessor(Environment environment,
|
public TargeterBeanPostProcessor targeterBeanPostProcessor(Environment environment,
|
||||||
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
||||||
DubboGenericServiceFactory dubboGenericServiceFactory,
|
DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||||
DubboGenericServiceExecutionContextFactory contextFactory) {
|
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||||
return new TargeterBeanPostProcessor(environment, dubboServiceMetadataRepository,
|
return new TargeterBeanPostProcessor(environment, dubboServiceMetadataRepository,
|
||||||
dubboGenericServiceFactory, contextFactory);
|
dubboGenericServiceFactory, contextFactory);
|
||||||
}
|
}
|
||||||
|
@ -24,15 +24,11 @@ import com.alibaba.cloud.dubbo.service.parameter.RequestBodyServiceParameterReso
|
|||||||
import com.alibaba.cloud.dubbo.service.parameter.RequestHeaderServiceParameterResolver;
|
import com.alibaba.cloud.dubbo.service.parameter.RequestHeaderServiceParameterResolver;
|
||||||
import com.alibaba.cloud.dubbo.service.parameter.RequestParamServiceParameterResolver;
|
import com.alibaba.cloud.dubbo.service.parameter.RequestParamServiceParameterResolver;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.context.annotation.Primary;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.core.env.PropertyResolver;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring Boot Auto-Configuration class for Dubbo Service.
|
* Spring Boot Auto-Configuration class for Dubbo Service.
|
||||||
@ -49,17 +45,6 @@ public class DubboServiceAutoConfiguration {
|
|||||||
return new DubboGenericServiceFactory();
|
return new DubboGenericServiceFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Build a primary {@link PropertyResolver} bean to {@link Autowired @Autowired}.
|
|
||||||
* @param environment {@link Environment}
|
|
||||||
* @return alias bean for {@link Environment}
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
public PropertyResolver primaryPropertyResolver(Environment environment) {
|
|
||||||
return environment;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import({ DubboGenericServiceExecutionContextFactory.class,
|
@Import({ DubboGenericServiceExecutionContextFactory.class,
|
||||||
RequestParamServiceParameterResolver.class,
|
RequestParamServiceParameterResolver.class,
|
||||||
|
@ -33,6 +33,7 @@ import com.alibaba.cloud.dubbo.registry.AbstractSpringCloudRegistry;
|
|||||||
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent;
|
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent;
|
||||||
import com.alibaba.cloud.dubbo.registry.event.SubscribedServicesChangedEvent;
|
import com.alibaba.cloud.dubbo.registry.event.SubscribedServicesChangedEvent;
|
||||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
|
import com.alibaba.cloud.nacos.NacosServiceManager;
|
||||||
import com.alibaba.cloud.nacos.discovery.NacosWatch;
|
import com.alibaba.cloud.nacos.discovery.NacosWatch;
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
import com.alibaba.nacos.api.naming.NamingService;
|
import com.alibaba.nacos.api.naming.NamingService;
|
||||||
@ -56,6 +57,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.springframework.beans.factory.ObjectProvider;
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
@ -97,12 +99,11 @@ import static org.springframework.util.StringUtils.hasText;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(name = "org.springframework.cloud.client.discovery.DiscoveryClient")
|
@ConditionalOnClass(name = "org.springframework.cloud.client.discovery.DiscoveryClient")
|
||||||
@ConditionalOnProperty(name = "spring.cloud.discovery.enabled", matchIfMissing = true)
|
@ConditionalOnProperty(name = "spring.cloud.discovery.enabled", matchIfMissing = true)
|
||||||
@AutoConfigureAfter(
|
@AutoConfigureAfter(name = { EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME,
|
||||||
name = { EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME,
|
ZOOKEEPER_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME,
|
||||||
ZOOKEEPER_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME,
|
CONSUL_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME,
|
||||||
CONSUL_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME,
|
NACOS_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME }, value = {
|
||||||
NACOS_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME },
|
DubboServiceRegistrationAutoConfiguration.class })
|
||||||
value = { DubboServiceRegistrationAutoConfiguration.class })
|
|
||||||
public class DubboServiceDiscoveryAutoConfiguration {
|
public class DubboServiceDiscoveryAutoConfiguration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -135,6 +136,9 @@ public class DubboServiceDiscoveryAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
private final ObjectProvider<Predicate<HeartbeatEvent>> heartbeatEventChangedPredicate;
|
private final ObjectProvider<Predicate<HeartbeatEvent>> heartbeatEventChangedPredicate;
|
||||||
|
|
||||||
|
@Value("${spring.application.name:${dubbo.application.name:application}}")
|
||||||
|
private String currentApplicationName;
|
||||||
|
|
||||||
public DubboServiceDiscoveryAutoConfiguration(
|
public DubboServiceDiscoveryAutoConfiguration(
|
||||||
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
||||||
ApplicationEventPublisher applicationEventPublisher,
|
ApplicationEventPublisher applicationEventPublisher,
|
||||||
@ -154,10 +158,12 @@ public class DubboServiceDiscoveryAutoConfiguration {
|
|||||||
* NotifyListener)
|
* NotifyListener)
|
||||||
*/
|
*/
|
||||||
private void dispatchServiceInstancesChangedEvent(String serviceName,
|
private void dispatchServiceInstancesChangedEvent(String serviceName,
|
||||||
Collection<ServiceInstance> serviceInstances) {
|
List<ServiceInstance> serviceInstances) {
|
||||||
if (!hasText(serviceName) || serviceInstances == null) {
|
if (!hasText(serviceName) || Objects.equals(currentApplicationName, serviceName)
|
||||||
|
|| serviceInstances == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceInstancesChangedEvent event = new ServiceInstancesChangedEvent(serviceName,
|
ServiceInstancesChangedEvent event = new ServiceInstancesChangedEvent(serviceName,
|
||||||
serviceInstances);
|
serviceInstances);
|
||||||
if (logger.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
@ -344,7 +350,7 @@ public class DubboServiceDiscoveryAutoConfiguration {
|
|||||||
private final ThreadLocal<String> processedServiceNameThreadLocal;
|
private final ThreadLocal<String> processedServiceNameThreadLocal;
|
||||||
|
|
||||||
ZookeeperConfiguration(ZookeeperDiscoveryProperties zookeeperDiscoveryProperties,
|
ZookeeperConfiguration(ZookeeperDiscoveryProperties zookeeperDiscoveryProperties,
|
||||||
ZookeeperServiceWatch zookeeperServiceWatch) {
|
ZookeeperServiceWatch zookeeperServiceWatch) {
|
||||||
this.zookeeperServiceWatch = zookeeperServiceWatch;
|
this.zookeeperServiceWatch = zookeeperServiceWatch;
|
||||||
this.rootPath = zookeeperDiscoveryProperties.getRoot();
|
this.rootPath = zookeeperDiscoveryProperties.getRoot();
|
||||||
this.pathMatcher = new AntPathMatcher(NODE_PATH_SEPARATOR);
|
this.pathMatcher = new AntPathMatcher(NODE_PATH_SEPARATOR);
|
||||||
@ -512,8 +518,10 @@ public class DubboServiceDiscoveryAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
private final Set<String> listeningServices;
|
private final Set<String> listeningServices;
|
||||||
|
|
||||||
NacosConfiguration(NacosDiscoveryProperties nacosDiscoveryProperties) {
|
NacosConfiguration(NacosServiceManager nacosServiceManager,
|
||||||
this.namingService = nacosDiscoveryProperties.namingServiceInstance();
|
NacosDiscoveryProperties nacosDiscoveryProperties) {
|
||||||
|
this.namingService = nacosServiceManager
|
||||||
|
.getNamingService(nacosDiscoveryProperties.getNacosProperties());
|
||||||
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
|
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
|
||||||
this.listeningServices = new ConcurrentSkipListSet<>();
|
this.listeningServices = new ConcurrentSkipListSet<>();
|
||||||
}
|
}
|
||||||
|
@ -66,12 +66,11 @@ import static org.springframework.util.ObjectUtils.isEmpty;
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import({ DubboServiceRegistrationEventPublishingAspect.class })
|
@Import({ DubboServiceRegistrationEventPublishingAspect.class })
|
||||||
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
|
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
|
||||||
matchIfMissing = true)
|
|
||||||
@AutoConfigureAfter(name = { EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME,
|
@AutoConfigureAfter(name = { EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME,
|
||||||
CONSUL_AUTO_SERVICE_AUTO_CONFIGURATION_CLASS_NAME,
|
CONSUL_AUTO_SERVICE_AUTO_CONFIGURATION_CLASS_NAME,
|
||||||
"org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration" },
|
"org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration" }, value = {
|
||||||
value = { DubboMetadataAutoConfiguration.class })
|
DubboMetadataAutoConfiguration.class })
|
||||||
public class DubboServiceRegistrationAutoConfiguration {
|
public class DubboServiceRegistrationAutoConfiguration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,8 +53,7 @@ import static com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationAuto
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnNotWebApplication
|
@ConditionalOnNotWebApplication
|
||||||
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
|
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
|
||||||
matchIfMissing = true)
|
|
||||||
@AutoConfigureAfter(DubboServiceRegistrationAutoConfiguration.class)
|
@AutoConfigureAfter(DubboServiceRegistrationAutoConfiguration.class)
|
||||||
@Aspect
|
@Aspect
|
||||||
public class DubboServiceRegistrationNonWebApplicationAutoConfiguration {
|
public class DubboServiceRegistrationNonWebApplicationAutoConfiguration {
|
||||||
@ -76,12 +75,17 @@ public class DubboServiceRegistrationNonWebApplicationAutoConfiguration {
|
|||||||
|
|
||||||
@Around("execution(* org.springframework.cloud.client.serviceregistry.Registration.getPort())")
|
@Around("execution(* org.springframework.cloud.client.serviceregistry.Registration.getPort())")
|
||||||
public Object getPort(ProceedingJoinPoint pjp) throws Throwable {
|
public Object getPort(ProceedingJoinPoint pjp) throws Throwable {
|
||||||
|
/**
|
||||||
|
* move setServerPort from onApplicationStarted() to here for this issue :
|
||||||
|
* https://github.com/alibaba/spring-cloud-alibaba/issues/1383
|
||||||
|
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
|
||||||
|
*/
|
||||||
|
setServerPort();
|
||||||
return serverPort != null ? serverPort : pjp.proceed();
|
return serverPort != null ? serverPort : pjp.proceed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventListener(ApplicationStartedEvent.class)
|
@EventListener(ApplicationStartedEvent.class)
|
||||||
public void onApplicationStarted() {
|
public void onApplicationStarted() {
|
||||||
setServerPort();
|
|
||||||
register();
|
register();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,18 +103,22 @@ public class DubboServiceRegistrationNonWebApplicationAutoConfiguration {
|
|||||||
*/
|
*/
|
||||||
private void setServerPort() {
|
private void setServerPort() {
|
||||||
if (serverPort == null) {
|
if (serverPort == null) {
|
||||||
for (List<URL> urls : repository.getAllExportedUrls().values()) {
|
synchronized (this) {
|
||||||
urls.stream()
|
|
||||||
.filter(url -> REST_PROTOCOL.equalsIgnoreCase(url.getProtocol()))
|
|
||||||
.findFirst().ifPresent(url -> {
|
|
||||||
serverPort = url.getPort();
|
|
||||||
});
|
|
||||||
|
|
||||||
// If REST protocol is not present, use any applied port.
|
|
||||||
if (serverPort == null) {
|
if (serverPort == null) {
|
||||||
urls.stream().findAny().ifPresent(url -> {
|
for (List<URL> urls : repository.getAllExportedUrls().values()) {
|
||||||
serverPort = url.getPort();
|
urls.stream().filter(
|
||||||
});
|
url -> REST_PROTOCOL.equalsIgnoreCase(url.getProtocol()))
|
||||||
|
.findFirst().ifPresent(url -> {
|
||||||
|
serverPort = url.getPort();
|
||||||
|
});
|
||||||
|
|
||||||
|
// If REST protocol is not present, use any applied port.
|
||||||
|
if (serverPort == null) {
|
||||||
|
urls.stream().findAny().ifPresent(url -> {
|
||||||
|
serverPort = url.getPort();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,6 +134,7 @@ public class DubboServiceRegistrationNonWebApplicationAutoConfiguration {
|
|||||||
@EventListener(ServiceInstancePreRegisteredEvent.class)
|
@EventListener(ServiceInstancePreRegisteredEvent.class)
|
||||||
public void onServiceInstancePreRegistered(
|
public void onServiceInstancePreRegistered(
|
||||||
ServiceInstancePreRegisteredEvent event) {
|
ServiceInstancePreRegisteredEvent event) {
|
||||||
|
setServerPort();
|
||||||
registration.setPort(serverPort);
|
registration.setPort(serverPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ public class MissingSpringCloudRegistryConfigPropertyCondition
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
||||||
AnnotatedTypeMetadata metadata) {
|
AnnotatedTypeMetadata metadata) {
|
||||||
ConfigurableEnvironment environment = (ConfigurableEnvironment) context
|
ConfigurableEnvironment environment = (ConfigurableEnvironment) context
|
||||||
.getEnvironment();
|
.getEnvironment();
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ class DubboClientHttpResponse implements ClientHttpResponse {
|
|||||||
private final DubboHttpOutputMessage httpOutputMessage;
|
private final DubboHttpOutputMessage httpOutputMessage;
|
||||||
|
|
||||||
DubboClientHttpResponse(DubboHttpOutputMessage httpOutputMessage,
|
DubboClientHttpResponse(DubboHttpOutputMessage httpOutputMessage,
|
||||||
GenericException exception) {
|
GenericException exception) {
|
||||||
this.httpStatus = exception != null ? HttpStatus.INTERNAL_SERVER_ERROR
|
this.httpStatus = exception != null ? HttpStatus.INTERNAL_SERVER_ERROR
|
||||||
: HttpStatus.OK;
|
: HttpStatus.OK;
|
||||||
this.statusText = exception != null ? exception.getExceptionMessage()
|
this.statusText = exception != null ? exception.getExceptionMessage()
|
||||||
|
@ -45,7 +45,7 @@ class DubboClientHttpResponseFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ClientHttpResponse build(Object result, GenericException exception,
|
public ClientHttpResponse build(Object result, GenericException exception,
|
||||||
RequestMetadata requestMetadata, RestMethodMetadata restMethodMetadata) {
|
RequestMetadata requestMetadata, RestMethodMetadata restMethodMetadata) {
|
||||||
|
|
||||||
DubboHttpOutputMessage httpOutputMessage = new DubboHttpOutputMessage();
|
DubboHttpOutputMessage httpOutputMessage = new DubboHttpOutputMessage();
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ public class DubboMetadataInitializerInterceptor implements ClientHttpRequestInt
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
||||||
ClientHttpRequestExecution execution) throws IOException {
|
ClientHttpRequestExecution execution) throws IOException {
|
||||||
|
|
||||||
URI originalUri = request.getURI();
|
URI originalUri = request.getURI();
|
||||||
|
|
||||||
|
@ -66,11 +66,11 @@ public class DubboTransporterInterceptor implements ClientHttpRequestInterceptor
|
|||||||
private final PathMatcher pathMatcher = new AntPathMatcher();
|
private final PathMatcher pathMatcher = new AntPathMatcher();
|
||||||
|
|
||||||
public DubboTransporterInterceptor(
|
public DubboTransporterInterceptor(
|
||||||
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
||||||
List<HttpMessageConverter<?>> messageConverters, ClassLoader classLoader,
|
List<HttpMessageConverter<?>> messageConverters, ClassLoader classLoader,
|
||||||
Map<String, Object> dubboTranslatedAttributes,
|
Map<String, Object> dubboTranslatedAttributes,
|
||||||
DubboGenericServiceFactory serviceFactory,
|
DubboGenericServiceFactory serviceFactory,
|
||||||
DubboGenericServiceExecutionContextFactory contextFactory) {
|
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||||
this.repository = dubboServiceMetadataRepository;
|
this.repository = dubboServiceMetadataRepository;
|
||||||
this.dubboTranslatedAttributes = dubboTranslatedAttributes;
|
this.dubboTranslatedAttributes = dubboTranslatedAttributes;
|
||||||
this.clientHttpResponseFactory = new DubboClientHttpResponseFactory(
|
this.clientHttpResponseFactory = new DubboClientHttpResponseFactory(
|
||||||
@ -81,7 +81,7 @@ public class DubboTransporterInterceptor implements ClientHttpRequestInterceptor
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
|
||||||
ClientHttpRequestExecution execution) throws IOException {
|
ClientHttpRequestExecution execution) throws IOException {
|
||||||
|
|
||||||
URI originalUri = request.getURI();
|
URI originalUri = request.getURI();
|
||||||
|
|
||||||
@ -125,7 +125,7 @@ public class DubboTransporterInterceptor implements ClientHttpRequestInterceptor
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void customizeRequest(MutableHttpServerRequest httpServerRequest,
|
protected void customizeRequest(MutableHttpServerRequest httpServerRequest,
|
||||||
RestMethodMetadata dubboRestMethodMetadata, RequestMetadata clientMetadata) {
|
RestMethodMetadata dubboRestMethodMetadata, RequestMetadata clientMetadata) {
|
||||||
|
|
||||||
RequestMetadata dubboRequestMetadata = dubboRestMethodMetadata.getRequest();
|
RequestMetadata dubboRequestMetadata = dubboRestMethodMetadata.getRequest();
|
||||||
String pathPattern = dubboRequestMetadata.getPath();
|
String pathPattern = dubboRequestMetadata.getPath();
|
||||||
|
@ -22,6 +22,8 @@ import java.util.Set;
|
|||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
import static com.alibaba.cloud.dubbo.util.DubboCloudConstants.CONFIG_PROPERTY_PREFIX;
|
||||||
|
import static com.alibaba.cloud.dubbo.util.DubboCloudConstants.DUBBO_CLOUD_REGISTRY_PROPERTY_VALUE;
|
||||||
import static org.springframework.util.StringUtils.commaDelimitedListToStringArray;
|
import static org.springframework.util.StringUtils.commaDelimitedListToStringArray;
|
||||||
import static org.springframework.util.StringUtils.hasText;
|
import static org.springframework.util.StringUtils.hasText;
|
||||||
import static org.springframework.util.StringUtils.trimAllWhitespace;
|
import static org.springframework.util.StringUtils.trimAllWhitespace;
|
||||||
@ -31,7 +33,7 @@ import static org.springframework.util.StringUtils.trimAllWhitespace;
|
|||||||
*
|
*
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties(prefix = "dubbo.cloud")
|
@ConfigurationProperties(prefix = CONFIG_PROPERTY_PREFIX)
|
||||||
public class DubboCloudProperties {
|
public class DubboCloudProperties {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,6 +49,8 @@ public class DubboCloudProperties {
|
|||||||
*/
|
*/
|
||||||
private String subscribedServices = ALL_DUBBO_SERVICES;
|
private String subscribedServices = ALL_DUBBO_SERVICES;
|
||||||
|
|
||||||
|
private String registryType = DUBBO_CLOUD_REGISTRY_PROPERTY_VALUE;
|
||||||
|
|
||||||
public String getSubscribedServices() {
|
public String getSubscribedServices() {
|
||||||
return subscribedServices;
|
return subscribedServices;
|
||||||
}
|
}
|
||||||
@ -79,4 +83,12 @@ public class DubboCloudProperties {
|
|||||||
return Collections.unmodifiableSet(subscribedServices);
|
return Collections.unmodifiableSet(subscribedServices);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getRegistryType() {
|
||||||
|
return registryType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistryType(String registryType) {
|
||||||
|
this.registryType = registryType;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -34,8 +34,8 @@ import org.springframework.core.env.PropertySource;
|
|||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_PROTOCOL;
|
|
||||||
import static com.alibaba.spring.util.PropertySourcesUtils.getSubProperties;
|
import static com.alibaba.spring.util.PropertySourcesUtils.getSubProperties;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_PROTOCOL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dubbo {@link WebApplicationType#NONE Non-Web Application}
|
* Dubbo {@link WebApplicationType#NONE Non-Web Application}
|
||||||
@ -84,7 +84,7 @@ public class DubboNonWebApplicationEnvironmentPostProcessor
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessEnvironment(ConfigurableEnvironment environment,
|
public void postProcessEnvironment(ConfigurableEnvironment environment,
|
||||||
SpringApplication application) {
|
SpringApplication application) {
|
||||||
WebApplicationType webApplicationType = application.getWebApplicationType();
|
WebApplicationType webApplicationType = application.getWebApplicationType();
|
||||||
|
|
||||||
if (!WebApplicationType.NONE.equals(webApplicationType)) { // Just works in
|
if (!WebApplicationType.NONE.equals(webApplicationType)) { // Just works in
|
||||||
@ -117,7 +117,7 @@ public class DubboNonWebApplicationEnvironmentPostProcessor
|
|||||||
* @param defaultProperties defaultProperties
|
* @param defaultProperties defaultProperties
|
||||||
*/
|
*/
|
||||||
private void resetServerPort(ConfigurableEnvironment environment,
|
private void resetServerPort(ConfigurableEnvironment environment,
|
||||||
Map<String, Object> defaultProperties) {
|
Map<String, Object> defaultProperties) {
|
||||||
|
|
||||||
String serverPort = environment.getProperty(SERVER_PORT_PROPERTY_NAME,
|
String serverPort = environment.getProperty(SERVER_PORT_PROPERTY_NAME,
|
||||||
environment.getProperty(PORT_PROPERTY_NAME));
|
environment.getProperty(PORT_PROPERTY_NAME));
|
||||||
@ -181,7 +181,7 @@ public class DubboNonWebApplicationEnvironmentPostProcessor
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setServerPort(ConfigurableEnvironment environment, String serverPort,
|
private void setServerPort(ConfigurableEnvironment environment, String serverPort,
|
||||||
Map<String, Object> defaultProperties) {
|
Map<String, Object> defaultProperties) {
|
||||||
if (serverPort == null) {
|
if (serverPort == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -196,7 +196,7 @@ public class DubboNonWebApplicationEnvironmentPostProcessor
|
|||||||
* @param map Default Dubbo Properties
|
* @param map Default Dubbo Properties
|
||||||
*/
|
*/
|
||||||
private void addOrReplace(MutablePropertySources propertySources,
|
private void addOrReplace(MutablePropertySources propertySources,
|
||||||
Map<String, Object> map) {
|
Map<String, Object> map) {
|
||||||
MapPropertySource target = null;
|
MapPropertySource target = null;
|
||||||
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
|
if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
|
||||||
PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
|
PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
|
||||||
|
@ -31,7 +31,7 @@ public class HttpMessageConverterHolder {
|
|||||||
private final HttpMessageConverter<?> converter;
|
private final HttpMessageConverter<?> converter;
|
||||||
|
|
||||||
public HttpMessageConverterHolder(MediaType mediaType,
|
public HttpMessageConverterHolder(MediaType mediaType,
|
||||||
HttpMessageConverter<?> converter) {
|
HttpMessageConverter<?> converter) {
|
||||||
this.mediaType = mediaType;
|
this.mediaType = mediaType;
|
||||||
this.converter = converter;
|
this.converter = converter;
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ public class HttpRequestConsumersMatcher extends AbstractHttpRequestMatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Set<ConsumeMediaTypeExpression> parseExpressions(String[] consumes,
|
private static Set<ConsumeMediaTypeExpression> parseExpressions(String[] consumes,
|
||||||
String[] headers) {
|
String[] headers) {
|
||||||
Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>();
|
Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>();
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
for (String header : headers) {
|
for (String header : headers) {
|
||||||
|
@ -59,7 +59,7 @@ public class HttpMessageConverterResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HttpMessageConverterHolder resolve(HttpRequest request,
|
public HttpMessageConverterHolder resolve(HttpRequest request,
|
||||||
Class<?> parameterType) {
|
Class<?> parameterType) {
|
||||||
|
|
||||||
HttpMessageConverterHolder httpMessageConverterHolder = null;
|
HttpMessageConverterHolder httpMessageConverterHolder = null;
|
||||||
|
|
||||||
@ -100,7 +100,7 @@ public class HttpMessageConverterResolver {
|
|||||||
* @return instance of {@link HttpMessageConverterHolder}
|
* @return instance of {@link HttpMessageConverterHolder}
|
||||||
*/
|
*/
|
||||||
public HttpMessageConverterHolder resolve(RequestMetadata requestMetadata,
|
public HttpMessageConverterHolder resolve(RequestMetadata requestMetadata,
|
||||||
RestMethodMetadata restMethodMetadata) {
|
RestMethodMetadata restMethodMetadata) {
|
||||||
|
|
||||||
HttpMessageConverterHolder httpMessageConverterHolder = null;
|
HttpMessageConverterHolder httpMessageConverterHolder = null;
|
||||||
|
|
||||||
@ -190,7 +190,7 @@ public class HttpMessageConverterResolver {
|
|||||||
* @return non-null {@link List}
|
* @return non-null {@link List}
|
||||||
*/
|
*/
|
||||||
private List<MediaType> getProducibleMediaTypes(RestMethodMetadata restMethodMetadata,
|
private List<MediaType> getProducibleMediaTypes(RestMethodMetadata restMethodMetadata,
|
||||||
Class<?> returnValueClass) {
|
Class<?> returnValueClass) {
|
||||||
RequestMetadata serverRequestMetadata = restMethodMetadata.getRequest();
|
RequestMetadata serverRequestMetadata = restMethodMetadata.getRequest();
|
||||||
List<MediaType> mediaTypes = serverRequestMetadata.getProduceMediaTypes();
|
List<MediaType> mediaTypes = serverRequestMetadata.getProduceMediaTypes();
|
||||||
if (!CollectionUtils.isEmpty(mediaTypes)) { // Empty
|
if (!CollectionUtils.isEmpty(mediaTypes)) { // Empty
|
||||||
@ -232,7 +232,7 @@ public class HttpMessageConverterResolver {
|
|||||||
* q-value of the former.
|
* q-value of the former.
|
||||||
*/
|
*/
|
||||||
private MediaType getMostSpecificMediaType(MediaType acceptType,
|
private MediaType getMostSpecificMediaType(MediaType acceptType,
|
||||||
MediaType produceType) {
|
MediaType produceType) {
|
||||||
MediaType produceTypeToUse = produceType.copyQualityValue(acceptType);
|
MediaType produceTypeToUse = produceType.copyQualityValue(acceptType);
|
||||||
return (MediaType.SPECIFICITY_COMPARATOR.compare(acceptType,
|
return (MediaType.SPECIFICITY_COMPARATOR.compare(acceptType,
|
||||||
produceTypeToUse) <= 0 ? acceptType : produceTypeToUse);
|
produceTypeToUse) <= 0 ? acceptType : produceTypeToUse);
|
||||||
|
@ -244,7 +244,7 @@ public abstract class HttpUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void addParam(MultiValueMap<String, String> paramsMap, String name,
|
private static void addParam(MultiValueMap<String, String> paramsMap, String name,
|
||||||
String value) {
|
String value) {
|
||||||
String paramValue = trimAllWhitespace(value);
|
String paramValue = trimAllWhitespace(value);
|
||||||
if (!StringUtils.hasText(paramValue)) {
|
if (!StringUtils.hasText(paramValue)) {
|
||||||
paramValue = EMPTY_VALUE;
|
paramValue = EMPTY_VALUE;
|
||||||
|
@ -65,7 +65,6 @@ public class DubboProtocolConfigSupplier implements Supplier<ProtocolConfig> {
|
|||||||
if (protocolConfig == null) {
|
if (protocolConfig == null) {
|
||||||
protocolConfig = new ProtocolConfig();
|
protocolConfig = new ProtocolConfig();
|
||||||
protocolConfig.setName(DEFAULT_PROTOCOL);
|
protocolConfig.setName(DEFAULT_PROTOCOL);
|
||||||
protocolConfig.setPort(-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return protocolConfig;
|
return protocolConfig;
|
||||||
|
@ -30,7 +30,7 @@ public class DubboRestServiceMetadata {
|
|||||||
private final RestMethodMetadata restMethodMetadata;
|
private final RestMethodMetadata restMethodMetadata;
|
||||||
|
|
||||||
public DubboRestServiceMetadata(ServiceRestMetadata serviceRestMetadata,
|
public DubboRestServiceMetadata(ServiceRestMetadata serviceRestMetadata,
|
||||||
RestMethodMetadata restMethodMetadata) {
|
RestMethodMetadata restMethodMetadata) {
|
||||||
this.serviceRestMetadata = serviceRestMetadata;
|
this.serviceRestMetadata = serviceRestMetadata;
|
||||||
this.restMethodMetadata = restMethodMetadata;
|
this.restMethodMetadata = restMethodMetadata;
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,7 @@ public class MethodMetadata {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private MethodParameterMetadata toMethodParameterMetadata(int index,
|
private MethodParameterMetadata toMethodParameterMetadata(int index,
|
||||||
Parameter parameter) {
|
Parameter parameter) {
|
||||||
MethodParameterMetadata metadata = new MethodParameterMetadata();
|
MethodParameterMetadata metadata = new MethodParameterMetadata();
|
||||||
metadata.setIndex(index);
|
metadata.setIndex(index);
|
||||||
metadata.setName(parameter.getName());
|
metadata.setName(parameter.getName());
|
||||||
|
@ -111,7 +111,7 @@ public class RequestMetadata {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void mediaTypes(HttpHeaders httpHeaders, String headerName,
|
private static void mediaTypes(HttpHeaders httpHeaders, String headerName,
|
||||||
Collection<String> destination) {
|
Collection<String> destination) {
|
||||||
List<String> value = httpHeaders.get(headerName);
|
List<String> value = httpHeaders.get(headerName);
|
||||||
List<MediaType> mediaTypes = parseMediaTypes(value);
|
List<MediaType> mediaTypes = parseMediaTypes(value);
|
||||||
destination.addAll(toMediaTypeValues(mediaTypes));
|
destination.addAll(toMediaTypeValues(mediaTypes));
|
||||||
|
@ -22,9 +22,7 @@ import java.util.LinkedHashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
@ -38,6 +36,7 @@ import com.alibaba.cloud.dubbo.registry.event.SubscribedServicesChangedEvent;
|
|||||||
import com.alibaba.cloud.dubbo.service.DubboMetadataService;
|
import com.alibaba.cloud.dubbo.service.DubboMetadataService;
|
||||||
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceExporter;
|
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceExporter;
|
||||||
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
|
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
|
||||||
|
import com.alibaba.cloud.dubbo.util.DubboMetadataUtils;
|
||||||
import com.alibaba.cloud.dubbo.util.JSONUtils;
|
import com.alibaba.cloud.dubbo.util.JSONUtils;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||||
@ -61,15 +60,15 @@ import org.springframework.util.MultiValueMap;
|
|||||||
|
|
||||||
import static com.alibaba.cloud.dubbo.env.DubboCloudProperties.ALL_DUBBO_SERVICES;
|
import static com.alibaba.cloud.dubbo.env.DubboCloudProperties.ALL_DUBBO_SERVICES;
|
||||||
import static com.alibaba.cloud.dubbo.http.DefaultHttpRequest.builder;
|
import static com.alibaba.cloud.dubbo.http.DefaultHttpRequest.builder;
|
||||||
import static java.lang.String.format;
|
|
||||||
import static java.lang.String.valueOf;
|
import static java.lang.String.valueOf;
|
||||||
import static java.util.Collections.emptyList;
|
|
||||||
import static java.util.Collections.emptySet;
|
import static java.util.Collections.emptySet;
|
||||||
import static java.util.Collections.unmodifiableList;
|
|
||||||
import static java.util.Collections.unmodifiableMap;
|
import static java.util.Collections.unmodifiableMap;
|
||||||
import static java.util.Collections.unmodifiableSet;
|
import static java.util.Collections.unmodifiableSet;
|
||||||
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
|
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
|
||||||
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
|
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
|
||||||
|
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_PREFIX;
|
||||||
|
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URLS_PROPERTY_NAME;
|
||||||
import static org.springframework.util.CollectionUtils.isEmpty;
|
import static org.springframework.util.CollectionUtils.isEmpty;
|
||||||
import static org.springframework.util.StringUtils.hasText;
|
import static org.springframework.util.StringUtils.hasText;
|
||||||
|
|
||||||
@ -85,14 +84,15 @@ public class DubboServiceMetadataRepository
|
|||||||
/**
|
/**
|
||||||
* The prefix of {@link DubboMetadataService} : "dubbo.metadata-service.".
|
* The prefix of {@link DubboMetadataService} : "dubbo.metadata-service.".
|
||||||
*/
|
*/
|
||||||
public static final String DUBBO_METADATA_SERVICE_PREFIX = "dubbo.metadata-service.";
|
@Deprecated
|
||||||
|
public static final String DUBBO_METADATA_SERVICE_PREFIX = METADATA_SERVICE_PREFIX;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link URL URLs} property name of {@link DubboMetadataService} :
|
* The {@link URL URLs} property name of {@link DubboMetadataService} :
|
||||||
* "dubbo.metadata-service.urls".
|
* "dubbo.metadata-service.urls".
|
||||||
*/
|
*/
|
||||||
public static final String DUBBO_METADATA_SERVICE_URLS_PROPERTY_NAME = DUBBO_METADATA_SERVICE_PREFIX
|
@Deprecated
|
||||||
+ "urls";
|
public static final String DUBBO_METADATA_SERVICE_URLS_PROPERTY_NAME = METADATA_SERVICE_URLS_PROPERTY_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link String#format(String, Object...) pattern} of dubbo protocols port.
|
* The {@link String#format(String, Object...) pattern} of dubbo protocols port.
|
||||||
@ -108,11 +108,6 @@ public class DubboServiceMetadataRepository
|
|||||||
*/
|
*/
|
||||||
private final Object monitor = new Object();
|
private final Object monitor = new Object();
|
||||||
|
|
||||||
/**
|
|
||||||
* A {@link Set} of service names that had been initialized.
|
|
||||||
*/
|
|
||||||
private final Set<String> initializedServices = new LinkedHashSet<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All exported {@link URL urls} {@link Map} whose key is the return value of
|
* All exported {@link URL urls} {@link Map} whose key is the return value of
|
||||||
* {@link URL#getServiceKey()} method and value is the {@link List} of {@link URL
|
* {@link URL#getServiceKey()} method and value is the {@link List} of {@link URL
|
||||||
@ -122,12 +117,6 @@ public class DubboServiceMetadataRepository
|
|||||||
|
|
||||||
// =================================== Registration
|
// =================================== Registration
|
||||||
// =================================== //
|
// =================================== //
|
||||||
/**
|
|
||||||
* The subscribed {@link URL urls} {@link Map} of {@link DubboMetadataService}, whose
|
|
||||||
* key is the return value of {@link URL#getServiceKey()} method and value is the
|
|
||||||
* {@link List} of {@link URL URLs}.
|
|
||||||
*/
|
|
||||||
private final MultiValueMap<String, URL> subscribedDubboMetadataServiceURLs = new LinkedMultiValueMap<>();
|
|
||||||
|
|
||||||
// ====================================================================================
|
// ====================================================================================
|
||||||
// //
|
// //
|
||||||
@ -172,7 +161,7 @@ public class DubboServiceMetadataRepository
|
|||||||
private DiscoveryClient discoveryClient;
|
private DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MetadataServiceInstanceSelector metadataServiceInstanceSelector;
|
private ServiceInstanceSelector serviceInstanceSelector;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private JSONUtils jsonUtils;
|
private JSONUtils jsonUtils;
|
||||||
@ -180,6 +169,9 @@ public class DubboServiceMetadataRepository
|
|||||||
@Autowired
|
@Autowired
|
||||||
private InetUtils inetUtils;
|
private InetUtils inetUtils;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DubboMetadataUtils dubboMetadataUtils;
|
||||||
|
|
||||||
@Value("${spring.application.name}")
|
@Value("${spring.application.name}")
|
||||||
private String currentApplicationName;
|
private String currentApplicationName;
|
||||||
|
|
||||||
@ -275,28 +267,11 @@ public class DubboServiceMetadataRepository
|
|||||||
* @param serviceName service of name
|
* @param serviceName service of name
|
||||||
*/
|
*/
|
||||||
public void initializeMetadata(String serviceName) {
|
public void initializeMetadata(String serviceName) {
|
||||||
synchronized (monitor) {
|
initDubboRestServiceMetadataRepository(serviceName);
|
||||||
if (initializedServices.contains(serviceName)) {
|
}
|
||||||
if (logger.isDebugEnabled()) {
|
|
||||||
logger.debug(
|
|
||||||
"The metadata of Dubbo service[name : {}] has been initialized",
|
|
||||||
serviceName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (logger.isInfoEnabled()) {
|
|
||||||
logger.info(
|
|
||||||
"The metadata of Dubbo service[name : {}] is about to be initialized",
|
|
||||||
serviceName);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (initSubscribedDubboMetadataService(serviceName)) {
|
private DubboMetadataService getProxy(String serviceName) {
|
||||||
// mark this service name having been initialized
|
return dubboMetadataConfigServiceProxy.getProxy(serviceName);
|
||||||
initializedServices.add(serviceName);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -307,15 +282,8 @@ public class DubboServiceMetadataRepository
|
|||||||
*/
|
*/
|
||||||
public void removeMetadataAndInitializedService(String serviceName, URL url) {
|
public void removeMetadataAndInitializedService(String serviceName, URL url) {
|
||||||
synchronized (monitor) {
|
synchronized (monitor) {
|
||||||
initializedServices.remove(serviceName);
|
dubboMetadataConfigServiceProxy.removeProxy(serviceName);
|
||||||
dubboRestServiceMetadataRepository.remove(serviceName);
|
dubboRestServiceMetadataRepository.remove(serviceName);
|
||||||
// fix #1260 if the subscribedDubboMetadataServiceURLs removed fail,old meta
|
|
||||||
// information will be retained
|
|
||||||
if (DubboMetadataService.class.getName().equals(url.getServiceInterface())) {
|
|
||||||
String serviceKey = url.getServiceKey();
|
|
||||||
subscribedDubboMetadataServiceURLs.remove(serviceKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -345,8 +313,7 @@ public class DubboServiceMetadataRepository
|
|||||||
private void addDubboMetadataServiceURLsMetadata(Map<String, String> metadata,
|
private void addDubboMetadataServiceURLsMetadata(Map<String, String> metadata,
|
||||||
List<URL> dubboMetadataServiceURLs) {
|
List<URL> dubboMetadataServiceURLs) {
|
||||||
String dubboMetadataServiceURLsJSON = jsonUtils.toJSON(dubboMetadataServiceURLs);
|
String dubboMetadataServiceURLsJSON = jsonUtils.toJSON(dubboMetadataServiceURLs);
|
||||||
metadata.put(DUBBO_METADATA_SERVICE_URLS_PROPERTY_NAME,
|
metadata.put(METADATA_SERVICE_URLS_PROPERTY_NAME, dubboMetadataServiceURLsJSON);
|
||||||
dubboMetadataServiceURLsJSON);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addDubboProtocolsPortMetadata(Map<String, String> metadata) {
|
private void addDubboProtocolsPortMetadata(Map<String, String> metadata) {
|
||||||
@ -359,15 +326,6 @@ public class DubboServiceMetadataRepository
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the property name of Dubbo Protocol.
|
|
||||||
* @param protocol Dubbo Protocol
|
|
||||||
* @return non-null
|
|
||||||
*/
|
|
||||||
public String getDubboProtocolPropertyName(String protocol) {
|
|
||||||
return format(DUBBO_PROTOCOLS_PORT_PROPERTY_NAME_PATTERN, protocol);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Publish the {@link Set} of {@link ServiceRestMetadata}.
|
* Publish the {@link Set} of {@link ServiceRestMetadata}.
|
||||||
* @param serviceRestMetadataSet the {@link Set} of {@link ServiceRestMetadata}
|
* @param serviceRestMetadataSet the {@link Set} of {@link ServiceRestMetadata}
|
||||||
@ -389,27 +347,6 @@ public class DubboServiceMetadataRepository
|
|||||||
return unmodifiableSet(serviceRestMetadata);
|
return unmodifiableSet(serviceRestMetadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<URL> findSubscribedDubboMetadataServiceURLs(String serviceName,
|
|
||||||
String group, String version, String protocol) {
|
|
||||||
String serviceKey = URL.buildKey(serviceName, group, version);
|
|
||||||
|
|
||||||
List<URL> urls = null;
|
|
||||||
|
|
||||||
synchronized (monitor) {
|
|
||||||
urls = subscribedDubboMetadataServiceURLs.get(serviceKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isEmpty(urls)) {
|
|
||||||
return emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
return hasText(protocol)
|
|
||||||
? urls.stream()
|
|
||||||
.filter(url -> url.getProtocol().equalsIgnoreCase(protocol))
|
|
||||||
.collect(Collectors.toList())
|
|
||||||
: unmodifiableList(urls);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The specified service is subscribe or not.
|
* The specified service is subscribe or not.
|
||||||
* @param serviceName the service name
|
* @param serviceName the service name
|
||||||
@ -459,24 +396,13 @@ public class DubboServiceMetadataRepository
|
|||||||
return allExportedURLs.keySet();
|
return allExportedURLs.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public Integer getDubboProtocolPort(ServiceInstance serviceInstance,
|
||||||
* Get the {@link URL urls} that {@link DubboMetadataService} exported by the
|
String protocol) {
|
||||||
* specified {@link ServiceInstance}.
|
return dubboMetadataUtils.getDubboProtocolPort(serviceInstance, protocol);
|
||||||
* @param serviceInstance {@link ServiceInstance}
|
|
||||||
* @return the mutable {@link URL urls}
|
|
||||||
*/
|
|
||||||
public List<URL> getDubboMetadataServiceURLs(ServiceInstance serviceInstance) {
|
|
||||||
Map<String, String> metadata = serviceInstance.getMetadata();
|
|
||||||
String dubboURLsJSON = metadata.get(DUBBO_METADATA_SERVICE_URLS_PROPERTY_NAME);
|
|
||||||
return jsonUtils.toURLs(dubboURLsJSON);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getDubboProtocolPort(ServiceInstance serviceInstance,
|
private String getDubboProtocolPropertyName(String protocol) {
|
||||||
String protocol) {
|
return dubboMetadataUtils.getDubboProtocolPropertyName(protocol);
|
||||||
String protocolProperty = getDubboProtocolPropertyName(protocol);
|
|
||||||
Map<String, String> metadata = serviceInstance.getMetadata();
|
|
||||||
String protocolPort = metadata.get(protocolProperty);
|
|
||||||
return hasText(protocolPort) ? Integer.valueOf(protocolPort) : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<URL> getExportedURLs(String serviceInterface, String group,
|
public List<URL> getExportedURLs(String serviceInterface, String group,
|
||||||
@ -492,6 +418,11 @@ public class DubboServiceMetadataRepository
|
|||||||
protected void initDubboRestServiceMetadataRepository(String serviceName) {
|
protected void initDubboRestServiceMetadataRepository(String serviceName) {
|
||||||
|
|
||||||
if (dubboRestServiceMetadataRepository.containsKey(serviceName)) {
|
if (dubboRestServiceMetadataRepository.containsKey(serviceName)) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug(
|
||||||
|
"The metadata of Dubbo service[name : {}] has been initialized",
|
||||||
|
serviceName);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -600,8 +531,7 @@ public class DubboServiceMetadataRepository
|
|||||||
|
|
||||||
Set<ServiceRestMetadata> metadata = emptySet();
|
Set<ServiceRestMetadata> metadata = emptySet();
|
||||||
|
|
||||||
DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy
|
DubboMetadataService dubboMetadataService = getProxy(serviceName);
|
||||||
.getProxy(serviceName);
|
|
||||||
|
|
||||||
if (dubboMetadataService != null) {
|
if (dubboMetadataService != null) {
|
||||||
try {
|
try {
|
||||||
@ -626,68 +556,29 @@ public class DubboServiceMetadataRepository
|
|||||||
subscribedServices.remove(currentApplicationName);
|
subscribedServices.remove(currentApplicationName);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Boolean initSubscribedDubboMetadataService(String serviceName) {
|
|
||||||
// this need to judge whether the initialization is successful or not. The failed
|
|
||||||
// initialization will not change the initializedServices
|
|
||||||
Optional<ServiceInstance> optionalServiceInstance = metadataServiceInstanceSelector
|
|
||||||
.choose(discoveryClient.getInstances(serviceName));
|
|
||||||
if (!((Optional) optionalServiceInstance).isPresent()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ServiceInstance serviceInstance = optionalServiceInstance.get();
|
|
||||||
if (null == serviceInstance) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
List<URL> dubboMetadataServiceURLs = getDubboMetadataServiceURLs(serviceInstance);
|
|
||||||
if (dubboMetadataServiceURLs.isEmpty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for (URL dubboMetadataServiceURL : dubboMetadataServiceURLs) {
|
|
||||||
try {
|
|
||||||
initSubscribedDubboMetadataServiceURL(dubboMetadataServiceURL);
|
|
||||||
DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy
|
|
||||||
.getProxy(serviceName);
|
|
||||||
if (dubboMetadataService == null) {
|
|
||||||
dubboMetadataService = initDubboMetadataServiceProxy(
|
|
||||||
dubboMetadataServiceURL);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dubboMetadataService == null) {
|
|
||||||
removeMetadataAndInitializedService(serviceName,
|
|
||||||
dubboMetadataServiceURL);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Throwable e) {
|
|
||||||
if (logger.isErrorEnabled()) {
|
|
||||||
logger.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
initDubboRestServiceMetadataRepository(serviceName);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initSubscribedDubboMetadataServiceURL(URL dubboMetadataServiceURL) {
|
|
||||||
// add subscriptions
|
|
||||||
String serviceKey = dubboMetadataServiceURL.getServiceKey();
|
|
||||||
subscribedDubboMetadataServiceURLs.add(serviceKey, dubboMetadataServiceURL);
|
|
||||||
}
|
|
||||||
|
|
||||||
private DubboMetadataService initDubboMetadataServiceProxy(
|
|
||||||
URL dubboMetadataServiceURL) {
|
|
||||||
String serviceName = dubboMetadataServiceURL.getParameter(APPLICATION_KEY);
|
|
||||||
String version = dubboMetadataServiceURL.getParameter(VERSION_KEY);
|
|
||||||
// Initialize DubboMetadataService with right version
|
|
||||||
return dubboMetadataConfigServiceProxy.initProxy(serviceName, version);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setApplicationEventPublisher(
|
public void setApplicationEventPublisher(
|
||||||
ApplicationEventPublisher applicationEventPublisher) {
|
ApplicationEventPublisher applicationEventPublisher) {
|
||||||
this.applicationEventPublisher = applicationEventPublisher;
|
this.applicationEventPublisher = applicationEventPublisher;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
public List<URL> findSubscribedDubboMetadataServiceURLs(URL subscribedURL) {
|
||||||
|
// The parameter of "group" as the service name
|
||||||
|
String serviceInterface = subscribedURL.getServiceInterface();
|
||||||
|
String group = subscribedURL.getParameter(GROUP_KEY);
|
||||||
|
String version = subscribedURL.getParameter(VERSION_KEY);
|
||||||
|
String protocol = subscribedURL.getParameter(PROTOCOL_KEY);
|
||||||
|
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(group);
|
||||||
|
List<URL> urls = dubboMetadataUtils.getDubboMetadataServiceURLs(serviceInstances,
|
||||||
|
serviceInterface, version, protocol);
|
||||||
|
|
||||||
|
if (logger.isInfoEnabled()) {
|
||||||
|
logger.info(
|
||||||
|
"The DubboMetadataService of service [name : {} , instances : {}] URLs[protocol : {} , size : {}] has been subscribed.",
|
||||||
|
group, serviceInstances.size(), protocol, urls.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
return urls;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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.metadata.repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
|
||||||
|
import static java.util.Optional.of;
|
||||||
|
import static org.springframework.util.CollectionUtils.isEmpty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Random {@link ServiceInstanceSelector}.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
|
*/
|
||||||
|
public class RandomServiceInstanceSelector implements ServiceInstanceSelector {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<ServiceInstance> select(List<ServiceInstance> serviceInstances) {
|
||||||
|
if (isEmpty(serviceInstances)) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
ThreadLocalRandom random = ThreadLocalRandom.current();
|
||||||
|
return of(serviceInstances.get(random.nextInt(serviceInstances.size())));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -26,13 +26,13 @@ import org.springframework.cloud.client.ServiceInstance;
|
|||||||
*
|
*
|
||||||
* @author <a href="mailto:liuxx-u@outlook.com">liuxx</a>
|
* @author <a href="mailto:liuxx-u@outlook.com">liuxx</a>
|
||||||
*/
|
*/
|
||||||
public interface MetadataServiceInstanceSelector {
|
public interface ServiceInstanceSelector {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* choose a service instance to get metadata.
|
* Select a service instance to get metadata.
|
||||||
* @param serviceInstances all service instance
|
* @param serviceInstances all service instance
|
||||||
* @return the service instance to get metadata
|
* @return the service instance to get metadata
|
||||||
*/
|
*/
|
||||||
Optional<ServiceInstance> choose(List<ServiceInstance> serviceInstances);
|
Optional<ServiceInstance> select(List<ServiceInstance> serviceInstances);
|
||||||
|
|
||||||
}
|
}
|
@ -138,7 +138,7 @@ public class DubboServiceBeanMetadataResolver
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<MethodMetadata> parseAndValidateMetadata(Contract contract,
|
private List<MethodMetadata> parseAndValidateMetadata(Contract contract,
|
||||||
Class<?> targetType) {
|
Class<?> targetType) {
|
||||||
List<MethodMetadata> methodMetadataList = Collections.emptyList();
|
List<MethodMetadata> methodMetadataList = Collections.emptyList();
|
||||||
try {
|
try {
|
||||||
methodMetadataList = contract.parseAndValidatateMetadata(targetType);
|
methodMetadataList = contract.parseAndValidatateMetadata(targetType);
|
||||||
@ -171,7 +171,7 @@ public class DubboServiceBeanMetadataResolver
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected RestMethodMetadata resolveMethodRestMetadata(MethodMetadata methodMetadata,
|
protected RestMethodMetadata resolveMethodRestMetadata(MethodMetadata methodMetadata,
|
||||||
Class<?> targetType, List<Method> feignContractMethods) {
|
Class<?> targetType, List<Method> feignContractMethods) {
|
||||||
String configKey = methodMetadata.configKey();
|
String configKey = methodMetadata.configKey();
|
||||||
Method feignContractMethod = getMatchedFeignContractMethod(targetType,
|
Method feignContractMethod = getMatchedFeignContractMethod(targetType,
|
||||||
feignContractMethods, configKey);
|
feignContractMethods, configKey);
|
||||||
|
@ -49,7 +49,7 @@ public class DubboTransportedMethodMetadataResolver {
|
|||||||
private final Contract contract;
|
private final Contract contract;
|
||||||
|
|
||||||
public DubboTransportedMethodMetadataResolver(PropertyResolver propertyResolver,
|
public DubboTransportedMethodMetadataResolver(PropertyResolver propertyResolver,
|
||||||
Contract contract) {
|
Contract contract) {
|
||||||
this.attributesResolver = new DubboTransportedAttributesResolver(
|
this.attributesResolver = new DubboTransportedAttributesResolver(
|
||||||
propertyResolver);
|
propertyResolver);
|
||||||
this.contract = contract;
|
this.contract = contract;
|
||||||
|
@ -50,9 +50,9 @@ public class TargeterBeanPostProcessor
|
|||||||
private ClassLoader classLoader;
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
public TargeterBeanPostProcessor(Environment environment,
|
public TargeterBeanPostProcessor(Environment environment,
|
||||||
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
||||||
DubboGenericServiceFactory dubboGenericServiceFactory,
|
DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||||
DubboGenericServiceExecutionContextFactory contextFactory) {
|
DubboGenericServiceExecutionContextFactory contextFactory) {
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
this.dubboServiceMetadataRepository = dubboServiceMetadataRepository;
|
this.dubboServiceMetadataRepository = dubboServiceMetadataRepository;
|
||||||
this.dubboGenericServiceFactory = dubboGenericServiceFactory;
|
this.dubboGenericServiceFactory = dubboGenericServiceFactory;
|
||||||
|
@ -98,7 +98,7 @@ class TargeterInvocationHandler implements InvocationHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object createDubboProxyIfRequired(FeignContext feignContext, Target target,
|
private Object createDubboProxyIfRequired(FeignContext feignContext, Target target,
|
||||||
Object defaultProxy) {
|
Object defaultProxy) {
|
||||||
|
|
||||||
DubboInvocationHandler dubboInvocationHandler = createDubboInvocationHandler(
|
DubboInvocationHandler dubboInvocationHandler = createDubboInvocationHandler(
|
||||||
feignContext, target, defaultProxy);
|
feignContext, target, defaultProxy);
|
||||||
@ -112,7 +112,7 @@ class TargeterInvocationHandler implements InvocationHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private DubboInvocationHandler createDubboInvocationHandler(FeignContext feignContext,
|
private DubboInvocationHandler createDubboInvocationHandler(FeignContext feignContext,
|
||||||
Target target, Object defaultFeignClientProxy) {
|
Target target, Object defaultFeignClientProxy) {
|
||||||
|
|
||||||
// Service name equals @FeignClient.name()
|
// Service name equals @FeignClient.name()
|
||||||
String serviceName = target.name();
|
String serviceName = target.name();
|
||||||
@ -153,7 +153,7 @@ class TargeterInvocationHandler implements InvocationHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Map<Method, FeignMethodMetadata> getFeignMethodMetadataMap(String serviceName,
|
private Map<Method, FeignMethodMetadata> getFeignMethodMetadataMap(String serviceName,
|
||||||
Map<DubboTransportedMethodMetadata, RestMethodMetadata> feignRestMethodMetadataMap) {
|
Map<DubboTransportedMethodMetadata, RestMethodMetadata> feignRestMethodMetadataMap) {
|
||||||
Map<Method, FeignMethodMetadata> feignMethodMetadataMap = new HashMap<>();
|
Map<Method, FeignMethodMetadata> feignMethodMetadataMap = new HashMap<>();
|
||||||
|
|
||||||
for (Map.Entry<DubboTransportedMethodMetadata, RestMethodMetadata> entry : feignRestMethodMetadataMap
|
for (Map.Entry<DubboTransportedMethodMetadata, RestMethodMetadata> entry : feignRestMethodMetadataMap
|
||||||
|
@ -63,6 +63,7 @@ import static org.springframework.util.StringUtils.hasText;
|
|||||||
*
|
*
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -98,10 +99,10 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
|||||||
private final ConfigurableApplicationContext applicationContext;
|
private final ConfigurableApplicationContext applicationContext;
|
||||||
|
|
||||||
public AbstractSpringCloudRegistry(URL url, DiscoveryClient discoveryClient,
|
public AbstractSpringCloudRegistry(URL url, DiscoveryClient discoveryClient,
|
||||||
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
||||||
DubboMetadataServiceProxy dubboMetadataConfigServiceProxy,
|
DubboMetadataServiceProxy dubboMetadataConfigServiceProxy,
|
||||||
JSONUtils jsonUtils, DubboGenericServiceFactory dubboGenericServiceFactory,
|
JSONUtils jsonUtils, DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||||
ConfigurableApplicationContext applicationContext) {
|
ConfigurableApplicationContext applicationContext) {
|
||||||
super(url);
|
super(url);
|
||||||
this.servicesLookupInterval = url
|
this.servicesLookupInterval = url
|
||||||
.getParameter(SERVICES_LOOKUP_INTERVAL_PARAM_NAME, 60L);
|
.getParameter(SERVICES_LOOKUP_INTERVAL_PARAM_NAME, 60L);
|
||||||
@ -355,7 +356,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<URL> getExportedURLs(DubboMetadataService dubboMetadataService,
|
private List<URL> getExportedURLs(DubboMetadataService dubboMetadataService,
|
||||||
URL url) {
|
URL url) {
|
||||||
String serviceInterface = url.getServiceInterface();
|
String serviceInterface = url.getServiceInterface();
|
||||||
String group = url.getParameter(GROUP_KEY);
|
String group = url.getParameter(GROUP_KEY);
|
||||||
String version = url.getParameter(VERSION_KEY);
|
String version = url.getParameter(VERSION_KEY);
|
||||||
@ -370,12 +371,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void subscribeDubboMetadataServiceURLs(URL url, NotifyListener listener) {
|
private void subscribeDubboMetadataServiceURLs(URL url, NotifyListener listener) {
|
||||||
String serviceInterface = url.getServiceInterface();
|
List<URL> urls = repository.findSubscribedDubboMetadataServiceURLs(url);
|
||||||
String group = url.getParameter(GROUP_KEY);
|
|
||||||
String version = url.getParameter(VERSION_KEY);
|
|
||||||
String protocol = url.getParameter(PROTOCOL_KEY);
|
|
||||||
List<URL> urls = repository.findSubscribedDubboMetadataServiceURLs(
|
|
||||||
serviceInterface, group, version, protocol);
|
|
||||||
listener.notify(urls);
|
listener.notify(urls);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -398,4 +394,4 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
|||||||
return DUBBO_METADATA_SERVICE_CLASS_NAME.equals(url.getServiceInterface());
|
return DUBBO_METADATA_SERVICE_CLASS_NAME.equals(url.getServiceInterface());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,511 @@
|
|||||||
|
/*
|
||||||
|
* 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 java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
||||||
|
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent;
|
||||||
|
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
|
||||||
|
import com.alibaba.cloud.dubbo.service.DubboMetadataService;
|
||||||
|
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
|
||||||
|
import com.alibaba.cloud.dubbo.util.DubboMetadataUtils;
|
||||||
|
import com.alibaba.cloud.dubbo.util.JSONUtils;
|
||||||
|
import org.apache.dubbo.common.URL;
|
||||||
|
import org.apache.dubbo.common.URLBuilder;
|
||||||
|
import org.apache.dubbo.registry.NotifyListener;
|
||||||
|
import org.apache.dubbo.registry.support.FailbackRegistry;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
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;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
|
||||||
|
import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY;
|
||||||
|
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
|
||||||
|
import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty;
|
||||||
|
import static org.apache.dubbo.registry.Constants.ADMIN_PROTOCOL;
|
||||||
|
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URLS_PROPERTY_NAME;
|
||||||
|
import static org.springframework.util.StringUtils.hasText;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dubbo Cloud {@link FailbackRegistry} is based on Spring Cloud {@link DiscoveryClient}.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
|
*/
|
||||||
|
public class DubboCloudRegistry extends FailbackRegistry {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The parameter name of {@link #servicesLookupInterval}.
|
||||||
|
*/
|
||||||
|
public static final String SERVICES_LOOKUP_INTERVAL_PARAM_NAME = "dubbo.services.lookup.interval";
|
||||||
|
|
||||||
|
protected static final String DUBBO_METADATA_SERVICE_CLASS_NAME = DubboMetadataService.class
|
||||||
|
.getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Caches the IDs of {@link ApplicationListener}.
|
||||||
|
*/
|
||||||
|
private static final Set<String> registerListeners = new HashSet<>();
|
||||||
|
|
||||||
|
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
private final DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
|
private final DubboServiceMetadataRepository repository;
|
||||||
|
|
||||||
|
private final DubboMetadataServiceProxy dubboMetadataConfigServiceProxy;
|
||||||
|
|
||||||
|
private final JSONUtils jsonUtils;
|
||||||
|
|
||||||
|
private final DubboGenericServiceFactory dubboGenericServiceFactory;
|
||||||
|
|
||||||
|
private final DubboMetadataUtils dubboMetadataUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interval in second of lookup service names(only for Dubbo-OPS).
|
||||||
|
*/
|
||||||
|
private final long servicesLookupInterval;
|
||||||
|
|
||||||
|
private final ConfigurableApplicationContext applicationContext;
|
||||||
|
|
||||||
|
private final String currentApplicationName;
|
||||||
|
|
||||||
|
public DubboCloudRegistry(URL url, DiscoveryClient discoveryClient,
|
||||||
|
DubboServiceMetadataRepository repository,
|
||||||
|
DubboMetadataServiceProxy dubboMetadataConfigServiceProxy,
|
||||||
|
JSONUtils jsonUtils, DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||||
|
ConfigurableApplicationContext applicationContext) {
|
||||||
|
|
||||||
|
super(url);
|
||||||
|
this.servicesLookupInterval = url
|
||||||
|
.getParameter(SERVICES_LOOKUP_INTERVAL_PARAM_NAME, 60L);
|
||||||
|
this.discoveryClient = discoveryClient;
|
||||||
|
this.repository = repository;
|
||||||
|
this.dubboMetadataConfigServiceProxy = dubboMetadataConfigServiceProxy;
|
||||||
|
this.jsonUtils = jsonUtils;
|
||||||
|
this.dubboGenericServiceFactory = dubboGenericServiceFactory;
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
this.dubboMetadataUtils = getBean(DubboMetadataUtils.class);
|
||||||
|
this.currentApplicationName = dubboMetadataUtils.getCurrentApplicationName();
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T getBean(Class<T> beanClass) {
|
||||||
|
return this.applicationContext.getBean(beanClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean shouldRegister(URL url) {
|
||||||
|
String side = url.getParameter(SIDE_KEY);
|
||||||
|
|
||||||
|
boolean should = PROVIDER_SIDE.equals(side); // Only register the Provider.
|
||||||
|
|
||||||
|
if (!should) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("The URL[{}] should not be registered.", url.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return should;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void doRegister(URL url) {
|
||||||
|
if (!shouldRegister(url)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
repository.exportURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void doUnregister(URL url) {
|
||||||
|
if (!shouldRegister(url)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
repository.unexportURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void doSubscribe(URL url, NotifyListener listener) {
|
||||||
|
|
||||||
|
if (isAdminURL(url)) {
|
||||||
|
// TODO in future
|
||||||
|
if (logger.isWarnEnabled()) {
|
||||||
|
logger.warn("This feature about admin will be supported in the future.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (isDubboMetadataServiceURL(url)) { // for DubboMetadataService
|
||||||
|
subscribeDubboMetadataServiceURLs(url, listener);
|
||||||
|
}
|
||||||
|
else { // for general Dubbo Services
|
||||||
|
subscribeURLs(url, listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void subscribeURLs(URL url, NotifyListener listener) {
|
||||||
|
|
||||||
|
// Sync subscription
|
||||||
|
subscribeURLs(url, getServices(url), listener);
|
||||||
|
|
||||||
|
// Async subscription
|
||||||
|
registerServiceInstancesChangedListener(url, event -> {
|
||||||
|
|
||||||
|
Set<String> serviceNames = getServices(url);
|
||||||
|
|
||||||
|
String serviceName = event.getServiceName();
|
||||||
|
|
||||||
|
if (serviceNames.contains(serviceName)) {
|
||||||
|
subscribeURLs(url, serviceNames, listener);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void subscribeURLs(URL url, Set<String> serviceNames,
|
||||||
|
NotifyListener listener) {
|
||||||
|
|
||||||
|
List<URL> subscribedURLs = new LinkedList<>();
|
||||||
|
|
||||||
|
serviceNames.forEach(serviceName -> {
|
||||||
|
|
||||||
|
subscribeURLs(url, subscribedURLs, serviceName,
|
||||||
|
() -> getServiceInstances(serviceName));
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
// Notify all
|
||||||
|
notifyAllSubscribedURLs(url, subscribedURLs, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerServiceInstancesChangedListener(URL url,
|
||||||
|
ApplicationListener<ServiceInstancesChangedEvent> listener) {
|
||||||
|
String listenerId = generateId(url);
|
||||||
|
if (registerListeners.add(listenerId)) {
|
||||||
|
applicationContext.addApplicationListener(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void subscribeURLs(URL subscribedURL, List<URL> subscribedURLs,
|
||||||
|
String serviceName,
|
||||||
|
Supplier<List<ServiceInstance>> serviceInstancesSupplier) {
|
||||||
|
List<ServiceInstance> serviceInstances = serviceInstancesSupplier.get();
|
||||||
|
subscribeURLs(subscribedURL, subscribedURLs, serviceName, serviceInstances);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void subscribeURLs(URL subscribedURL, List<URL> subscribedURLs,
|
||||||
|
String serviceName, List<ServiceInstance> serviceInstances) {
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(serviceInstances)) {
|
||||||
|
if (logger.isWarnEnabled()) {
|
||||||
|
logger.warn(format("There is no instance in service[name : %s]",
|
||||||
|
serviceName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<URL> exportedURLs = getExportedURLs(subscribedURL, serviceName,
|
||||||
|
serviceInstances);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the exported URLs from {@link MetadataService}
|
||||||
|
*/
|
||||||
|
subscribedURLs.addAll(exportedURLs);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<URL> getExportedURLs(URL subscribedURL, String serviceName,
|
||||||
|
List<ServiceInstance> serviceInstances) {
|
||||||
|
|
||||||
|
List<ServiceInstance> validServiceInstances = filter(serviceInstances);
|
||||||
|
|
||||||
|
// If there is no valid ServiceInstance, return empty result
|
||||||
|
if (isEmpty(validServiceInstances)) {
|
||||||
|
if (logger.isWarnEnabled()) {
|
||||||
|
logger.warn(
|
||||||
|
"There is no instance from service[name : {}], and then Dubbo Service[key : {}] will not be "
|
||||||
|
+ "available , please make sure the further impact",
|
||||||
|
serviceName, subscribedURL.getServiceKey());
|
||||||
|
}
|
||||||
|
return emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<URL> subscribedURLs = cloneExportedURLs(subscribedURL, serviceInstances);
|
||||||
|
|
||||||
|
// clear local service instances, help GC
|
||||||
|
validServiceInstances.clear();
|
||||||
|
|
||||||
|
return subscribedURLs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clone the subscribed URLs based on the template URLs.
|
||||||
|
* @param subscribedURL the URL to be subscribed
|
||||||
|
* @param serviceInstances the list of {@link ServiceInstance service instances}
|
||||||
|
* @return non-null
|
||||||
|
*/
|
||||||
|
private List<URL> cloneExportedURLs(URL subscribedURL,
|
||||||
|
List<ServiceInstance> serviceInstances) {
|
||||||
|
|
||||||
|
List<URL> clonedExportedURLs = new LinkedList<>();
|
||||||
|
|
||||||
|
serviceInstances.forEach(serviceInstance -> {
|
||||||
|
|
||||||
|
String host = serviceInstance.getHost();
|
||||||
|
|
||||||
|
getTemplateExportedURLs(subscribedURL, serviceInstances).stream()
|
||||||
|
.map(templateURL -> templateURL.removeParameter(TIMESTAMP_KEY))
|
||||||
|
.map(templateURL -> templateURL.removeParameter(PID_KEY))
|
||||||
|
.map(templateURL -> {
|
||||||
|
String protocol = templateURL.getProtocol();
|
||||||
|
int port = repository.getDubboProtocolPort(serviceInstance,
|
||||||
|
protocol);
|
||||||
|
if (Objects.equals(templateURL.getHost(), host)
|
||||||
|
&& Objects.equals(templateURL.getPort(), port)) { // use
|
||||||
|
// templateURL
|
||||||
|
// if
|
||||||
|
// equals
|
||||||
|
return templateURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
URLBuilder clonedURLBuilder = from(templateURL) // remove the
|
||||||
|
// parameters from
|
||||||
|
// the template
|
||||||
|
// URL
|
||||||
|
.setHost(host) // reset the host
|
||||||
|
.setPort(port); // reset the port
|
||||||
|
|
||||||
|
return clonedURLBuilder.build();
|
||||||
|
}).forEach(clonedExportedURLs::add);
|
||||||
|
});
|
||||||
|
return clonedExportedURLs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<URL> getTemplateExportedURLs(URL subscribedURL,
|
||||||
|
List<ServiceInstance> serviceInstances) {
|
||||||
|
|
||||||
|
DubboMetadataService dubboMetadataService = getProxy(serviceInstances);
|
||||||
|
|
||||||
|
List<URL> templateExportedURLs = emptyList();
|
||||||
|
|
||||||
|
if (dubboMetadataService != null) {
|
||||||
|
templateExportedURLs = getExportedURLs(dubboMetadataService, subscribedURL);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (logger.isWarnEnabled()) {
|
||||||
|
logger.warn(
|
||||||
|
"The metadata of Dubbo service[key : {}] still can't be found, it could effect the further "
|
||||||
|
+ "Dubbo service invocation",
|
||||||
|
subscribedURL.getServiceKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return templateExportedURLs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DubboMetadataService getProxy(List<ServiceInstance> serviceInstances) {
|
||||||
|
return dubboMetadataConfigServiceProxy.getProxy(serviceInstances);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ServiceInstance> filter(Collection<ServiceInstance> serviceInstances) {
|
||||||
|
return serviceInstances.stream().filter(this::isDubboServiceInstance)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isDubboServiceInstance(ServiceInstance serviceInstance) {
|
||||||
|
Map<String, String> metadata = serviceInstance.getMetadata();
|
||||||
|
return metadata.containsKey(METADATA_SERVICE_URLS_PROPERTY_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<String> getServices(URL url) {
|
||||||
|
Set<String> subscribedServices = repository.getSubscribedServices();
|
||||||
|
// TODO Add the filter feature
|
||||||
|
return subscribedServices;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void notifyAllSubscribedURLs(URL url, List<URL> subscribedURLs,
|
||||||
|
NotifyListener listener) {
|
||||||
|
|
||||||
|
if (isEmpty(subscribedURLs)) {
|
||||||
|
// Add the EMPTY_PROTOCOL URL
|
||||||
|
subscribedURLs.add(emptyURL(url));
|
||||||
|
|
||||||
|
if (isDubboMetadataServiceURL(url)) {
|
||||||
|
// if meta service change, and serviceInstances is zero, will clean up
|
||||||
|
// information about this client
|
||||||
|
String serviceName = url.getParameter(GROUP_KEY);
|
||||||
|
repository.removeMetadataAndInitializedService(serviceName, url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("The subscribed URL[{}] will notify all URLs : {}", url,
|
||||||
|
subscribedURLs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify all
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ServiceInstance> doGetServiceInstances(String serviceName) {
|
||||||
|
List<ServiceInstance> serviceInstances = emptyList();
|
||||||
|
try {
|
||||||
|
serviceInstances = discoveryClient.getInstances(serviceName);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
if (logger.isErrorEnabled()) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return serviceInstances;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String generateId(URL url) {
|
||||||
|
return url.getServiceKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
private URL emptyURL(URL url) {
|
||||||
|
// issue : When the last service provider is closed, the client still periodically
|
||||||
|
// connects to the last provider.n
|
||||||
|
// fix https://github.com/alibaba/spring-cloud-alibaba/issues/1259
|
||||||
|
return from(url).setProtocol(EMPTY_PROTOCOL).removeParameter(CATEGORY_KEY)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<URL> getExportedURLs(DubboMetadataService dubboMetadataService,
|
||||||
|
URL subscribedURL) {
|
||||||
|
String serviceInterface = subscribedURL.getServiceInterface();
|
||||||
|
String group = subscribedURL.getParameter(GROUP_KEY);
|
||||||
|
String version = subscribedURL.getParameter(VERSION_KEY);
|
||||||
|
// The subscribed protocol may be null
|
||||||
|
String subscribedProtocol = subscribedURL.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 subscribedURL,
|
||||||
|
NotifyListener listener) {
|
||||||
|
|
||||||
|
// Sync subscription
|
||||||
|
subscribeDubboMetadataServiceURLs(subscribedURL, listener,
|
||||||
|
getServiceName(subscribedURL));
|
||||||
|
|
||||||
|
// Sync subscription
|
||||||
|
if (containsProviderCategory(subscribedURL)) {
|
||||||
|
registerServiceInstancesChangedListener(subscribedURL, event -> {
|
||||||
|
|
||||||
|
String sourceServiceName = event.getServiceName();
|
||||||
|
String serviceName = getServiceName(subscribedURL);
|
||||||
|
|
||||||
|
if (Objects.equals(sourceServiceName, serviceName)) {
|
||||||
|
subscribeDubboMetadataServiceURLs(subscribedURL, listener,
|
||||||
|
sourceServiceName);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getServiceName(URL subscribedURL) {
|
||||||
|
return subscribedURL.getParameter(GROUP_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void subscribeDubboMetadataServiceURLs(URL subscribedURL,
|
||||||
|
NotifyListener listener, String serviceName) {
|
||||||
|
|
||||||
|
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 boolean containsProviderCategory(URL subscribedURL) {
|
||||||
|
String category = subscribedURL.getParameter(CATEGORY_KEY);
|
||||||
|
return category == null ? false : category.contains(PROVIDER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void doUnsubscribe(URL url, NotifyListener listener) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAvailable() {
|
||||||
|
return !discoveryClient.getServices().isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isAdminURL(URL url) {
|
||||||
|
return ADMIN_PROTOCOL.equals(url.getProtocol());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isDubboMetadataServiceURL(URL url) {
|
||||||
|
return DUBBO_METADATA_SERVICE_CLASS_NAME.equals(url.getServiceInterface());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -31,16 +31,19 @@ import org.springframework.context.ConfigurableApplicationContext;
|
|||||||
* protocol is "spring-cloud".
|
* protocol is "spring-cloud".
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
|
* @deprecated It's a legacy and not recommended implementation, being replacing to be
|
||||||
|
* {@link DubboCloudRegistry}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public class SpringCloudRegistry extends AbstractSpringCloudRegistry {
|
public class SpringCloudRegistry extends AbstractSpringCloudRegistry {
|
||||||
|
|
||||||
private final DubboServiceMetadataRepository dubboServiceMetadataRepository;
|
private final DubboServiceMetadataRepository dubboServiceMetadataRepository;
|
||||||
|
|
||||||
public SpringCloudRegistry(URL url, DiscoveryClient discoveryClient,
|
public SpringCloudRegistry(URL url, DiscoveryClient discoveryClient,
|
||||||
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
DubboServiceMetadataRepository dubboServiceMetadataRepository,
|
||||||
DubboMetadataServiceProxy dubboMetadataConfigServiceProxy,
|
DubboMetadataServiceProxy dubboMetadataConfigServiceProxy,
|
||||||
JSONUtils jsonUtils, DubboGenericServiceFactory dubboGenericServiceFactory,
|
JSONUtils jsonUtils, DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||||
ConfigurableApplicationContext applicationContext) {
|
ConfigurableApplicationContext applicationContext) {
|
||||||
super(url, discoveryClient, dubboServiceMetadataRepository,
|
super(url, discoveryClient, dubboServiceMetadataRepository,
|
||||||
dubboMetadataConfigServiceProxy, jsonUtils, dubboGenericServiceFactory,
|
dubboMetadataConfigServiceProxy, jsonUtils, dubboGenericServiceFactory,
|
||||||
applicationContext);
|
applicationContext);
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.dubbo.registry;
|
package com.alibaba.cloud.dubbo.registry;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.dubbo.env.DubboCloudProperties;
|
||||||
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
|
||||||
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
|
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
|
||||||
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
|
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
|
||||||
@ -23,11 +24,12 @@ import com.alibaba.cloud.dubbo.util.JSONUtils;
|
|||||||
import org.apache.dubbo.common.URL;
|
import org.apache.dubbo.common.URL;
|
||||||
import org.apache.dubbo.registry.Registry;
|
import org.apache.dubbo.registry.Registry;
|
||||||
import org.apache.dubbo.registry.RegistryFactory;
|
import org.apache.dubbo.registry.RegistryFactory;
|
||||||
|
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
|
||||||
|
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
|
||||||
import static java.lang.System.getProperty;
|
import static com.alibaba.cloud.dubbo.util.DubboCloudConstants.SPRING_CLOUD_REGISTRY_PROPERTY_VALUE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration abstraction, whose
|
* Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration abstraction, whose
|
||||||
@ -37,7 +39,7 @@ import static java.lang.System.getProperty;
|
|||||||
* @see RegistryFactory
|
* @see RegistryFactory
|
||||||
* @see SpringCloudRegistry
|
* @see SpringCloudRegistry
|
||||||
*/
|
*/
|
||||||
public class SpringCloudRegistryFactory implements RegistryFactory {
|
public class SpringCloudRegistryFactory extends AbstractRegistryFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring Cloud Protocol.
|
* Spring Cloud Protocol.
|
||||||
@ -49,10 +51,6 @@ public class SpringCloudRegistryFactory implements RegistryFactory {
|
|||||||
*/
|
*/
|
||||||
public static String ADDRESS = "localhost";
|
public static String ADDRESS = "localhost";
|
||||||
|
|
||||||
private static String SERVICES_LOOKUP_SCHEDULER_THREAD_NAME_PREFIX = getProperty(
|
|
||||||
"dubbo.services.lookup.scheduler.thread.name.prefix ",
|
|
||||||
"dubbo-services-lookup-");
|
|
||||||
|
|
||||||
private static ConfigurableApplicationContext applicationContext;
|
private static ConfigurableApplicationContext applicationContext;
|
||||||
|
|
||||||
private DiscoveryClient discoveryClient;
|
private DiscoveryClient discoveryClient;
|
||||||
@ -65,8 +63,6 @@ public class SpringCloudRegistryFactory implements RegistryFactory {
|
|||||||
|
|
||||||
private DubboGenericServiceFactory dubboGenericServiceFactory;
|
private DubboGenericServiceFactory dubboGenericServiceFactory;
|
||||||
|
|
||||||
private volatile boolean initialized = false;
|
|
||||||
|
|
||||||
public SpringCloudRegistryFactory() {
|
public SpringCloudRegistryFactory() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,9 +72,6 @@ public class SpringCloudRegistryFactory implements RegistryFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void init() {
|
protected void init() {
|
||||||
if (initialized || applicationContext == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.discoveryClient = applicationContext.getBean(DiscoveryClient.class);
|
this.discoveryClient = applicationContext.getBean(DiscoveryClient.class);
|
||||||
this.dubboServiceMetadataRepository = applicationContext
|
this.dubboServiceMetadataRepository = applicationContext
|
||||||
.getBean(DubboServiceMetadataRepository.class);
|
.getBean(DubboServiceMetadataRepository.class);
|
||||||
@ -90,11 +83,28 @@ public class SpringCloudRegistryFactory implements RegistryFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Registry getRegistry(URL url) {
|
public Registry createRegistry(URL url) {
|
||||||
init();
|
init();
|
||||||
return new SpringCloudRegistry(url, discoveryClient,
|
|
||||||
dubboServiceMetadataRepository, dubboMetadataConfigServiceProxy,
|
DubboCloudProperties dubboCloudProperties = applicationContext
|
||||||
jsonUtils, dubboGenericServiceFactory, applicationContext);
|
.getBean(DubboCloudProperties.class);
|
||||||
|
|
||||||
|
Registry registry = null;
|
||||||
|
|
||||||
|
switch (dubboCloudProperties.getRegistryType()) {
|
||||||
|
case SPRING_CLOUD_REGISTRY_PROPERTY_VALUE:
|
||||||
|
registry = new SpringCloudRegistry(url, discoveryClient,
|
||||||
|
dubboServiceMetadataRepository, dubboMetadataConfigServiceProxy,
|
||||||
|
jsonUtils, dubboGenericServiceFactory, applicationContext);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
registry = new DubboCloudRegistry(url, discoveryClient,
|
||||||
|
dubboServiceMetadataRepository, dubboMetadataConfigServiceProxy,
|
||||||
|
jsonUtils, dubboGenericServiceFactory, applicationContext);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,14 +16,14 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.dubbo.registry.event;
|
package com.alibaba.cloud.dubbo.registry.event;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
import org.springframework.context.event.ApplicationEventMulticaster;
|
import org.springframework.context.event.ApplicationEventMulticaster;
|
||||||
import org.springframework.context.event.SimpleApplicationEventMulticaster;
|
import org.springframework.context.event.SimpleApplicationEventMulticaster;
|
||||||
|
|
||||||
import static java.util.Collections.unmodifiableCollection;
|
import static java.util.Collections.unmodifiableList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An event raised after the {@link ServiceInstance instances} of one service has been
|
* An event raised after the {@link ServiceInstance instances} of one service has been
|
||||||
@ -35,7 +35,7 @@ public class ServiceInstancesChangedEvent extends ApplicationEvent {
|
|||||||
|
|
||||||
private final String serviceName;
|
private final String serviceName;
|
||||||
|
|
||||||
private final Collection<ServiceInstance> serviceInstances;
|
private final List<ServiceInstance> serviceInstances;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Current event has been processed or not. Typically, Spring Event was based on sync
|
* Current event has been processed or not. Typically, Spring Event was based on sync
|
||||||
@ -51,10 +51,10 @@ public class ServiceInstancesChangedEvent extends ApplicationEvent {
|
|||||||
* @throws IllegalArgumentException if source is null.
|
* @throws IllegalArgumentException if source is null.
|
||||||
*/
|
*/
|
||||||
public ServiceInstancesChangedEvent(String serviceName,
|
public ServiceInstancesChangedEvent(String serviceName,
|
||||||
Collection<ServiceInstance> serviceInstances) {
|
List<ServiceInstance> serviceInstances) {
|
||||||
super(serviceName);
|
super(serviceName);
|
||||||
this.serviceName = serviceName;
|
this.serviceName = serviceName;
|
||||||
this.serviceInstances = unmodifiableCollection(serviceInstances);
|
this.serviceInstances = unmodifiableList(serviceInstances);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,7 +67,7 @@ public class ServiceInstancesChangedEvent extends ApplicationEvent {
|
|||||||
/**
|
/**
|
||||||
* @return all {@link ServiceInstance service instances}.
|
* @return all {@link ServiceInstance service instances}.
|
||||||
*/
|
*/
|
||||||
public Collection<ServiceInstance> getServiceInstances() {
|
public List<ServiceInstance> getServiceInstances() {
|
||||||
return serviceInstances;
|
return serviceInstances;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ public class DubboGenericServiceFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ReferenceBean<GenericService> build(ServiceRestMetadata serviceRestMetadata,
|
private ReferenceBean<GenericService> build(ServiceRestMetadata serviceRestMetadata,
|
||||||
Map<String, Object> dubboTranslatedAttributes) {
|
Map<String, Object> dubboTranslatedAttributes) {
|
||||||
String urlValue = serviceRestMetadata.getUrl();
|
String urlValue = serviceRestMetadata.getUrl();
|
||||||
URL url = URL.valueOf(urlValue);
|
URL url = URL.valueOf(urlValue);
|
||||||
String interfaceName = url.getServiceInterface();
|
String interfaceName = url.getServiceInterface();
|
||||||
@ -95,10 +95,9 @@ public class DubboGenericServiceFactory {
|
|||||||
private ReferenceBean<GenericService> build(String interfaceName, String version,
|
private ReferenceBean<GenericService> build(String interfaceName, String version,
|
||||||
String group, Map<String, Object> dubboTranslatedAttributes) {
|
String group, Map<String, Object> dubboTranslatedAttributes) {
|
||||||
|
|
||||||
Integer key = Objects.hash(interfaceName, version, group,
|
String key = createKey(interfaceName, version, group, dubboTranslatedAttributes);
|
||||||
dubboTranslatedAttributes);
|
|
||||||
|
|
||||||
return cache.computeIfAbsent(group + key, k -> {
|
return cache.computeIfAbsent(key, k -> {
|
||||||
ReferenceBean<GenericService> referenceBean = new ReferenceBean<>();
|
ReferenceBean<GenericService> referenceBean = new ReferenceBean<>();
|
||||||
referenceBean.setGeneric(true);
|
referenceBean.setGeneric(true);
|
||||||
referenceBean.setInterface(interfaceName);
|
referenceBean.setInterface(interfaceName);
|
||||||
@ -110,6 +109,12 @@ public class DubboGenericServiceFactory {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String createKey(String interfaceName, String version, String group,
|
||||||
|
Map<String, Object> dubboTranslatedAttributes) {
|
||||||
|
return group + "#"
|
||||||
|
+ Objects.hash(interfaceName, version, group, dubboTranslatedAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
private void bindReferenceBean(ReferenceBean<GenericService> referenceBean,
|
private void bindReferenceBean(ReferenceBean<GenericService> referenceBean,
|
||||||
Map<String, Object> dubboTranslatedAttributes) {
|
Map<String, Object> dubboTranslatedAttributes) {
|
||||||
DataBinder dataBinder = new DataBinder(referenceBean);
|
DataBinder dataBinder = new DataBinder(referenceBean);
|
||||||
@ -123,11 +128,11 @@ public class DubboGenericServiceFactory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAsText(String text)
|
public void setAsText(String text)
|
||||||
throws java.lang.IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
// Trim all whitespace
|
// Trim all whitespace
|
||||||
String content = StringUtils.trimAllWhitespace(text);
|
String content = StringUtils.trimAllWhitespace(text);
|
||||||
if (!StringUtils.hasText(content)) { // No content , ignore
|
if (!StringUtils.hasText(content)) { // No content , ignore
|
||||||
// directly
|
// directly
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// replace "=" to ","
|
// replace "=" to ","
|
||||||
@ -155,7 +160,7 @@ public class DubboGenericServiceFactory {
|
|||||||
cache.clear();
|
cache.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void destroy(String serviceName) {
|
public void destroy(String serviceName) {
|
||||||
Set<String> removeGroups = new HashSet<>(cache.keySet());
|
Set<String> removeGroups = new HashSet<>(cache.keySet());
|
||||||
for (String key : removeGroups) {
|
for (String key : removeGroups) {
|
||||||
if (key.contains(serviceName)) {
|
if (key.contains(serviceName)) {
|
||||||
|
@ -16,13 +16,25 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.dubbo.service;
|
package com.alibaba.cloud.dubbo.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.dubbo.metadata.repository.ServiceInstanceSelector;
|
||||||
|
import com.alibaba.cloud.dubbo.util.DubboMetadataUtils;
|
||||||
|
import org.apache.dubbo.common.URL;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
|
|
||||||
import static java.lang.reflect.Proxy.newProxyInstance;
|
import static java.lang.reflect.Proxy.newProxyInstance;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The proxy of {@link DubboMetadataService}.
|
* The proxy of {@link DubboMetadataService}.
|
||||||
@ -31,26 +43,29 @@ import static java.lang.reflect.Proxy.newProxyInstance;
|
|||||||
*/
|
*/
|
||||||
public class DubboMetadataServiceProxy implements BeanClassLoaderAware, DisposableBean {
|
public class DubboMetadataServiceProxy implements BeanClassLoaderAware, DisposableBean {
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private final DubboGenericServiceFactory dubboGenericServiceFactory;
|
private final DubboGenericServiceFactory dubboGenericServiceFactory;
|
||||||
|
|
||||||
|
private final DubboMetadataUtils dubboMetadataUtils;
|
||||||
|
|
||||||
|
private final ServiceInstanceSelector serviceInstanceSelector;
|
||||||
|
|
||||||
|
private final DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
private final Map<String, DubboMetadataService> dubboMetadataServiceCache = new ConcurrentHashMap<>();
|
private final Map<String, DubboMetadataService> dubboMetadataServiceCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private ClassLoader classLoader;
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
public DubboMetadataServiceProxy(
|
public DubboMetadataServiceProxy(
|
||||||
DubboGenericServiceFactory dubboGenericServiceFactory) {
|
DubboGenericServiceFactory dubboGenericServiceFactory,
|
||||||
|
DubboMetadataUtils dubboMetadataUtils,
|
||||||
|
ServiceInstanceSelector serviceInstanceSelector,
|
||||||
|
DiscoveryClient discoveryClient) {
|
||||||
this.dubboGenericServiceFactory = dubboGenericServiceFactory;
|
this.dubboGenericServiceFactory = dubboGenericServiceFactory;
|
||||||
}
|
this.dubboMetadataUtils = dubboMetadataUtils;
|
||||||
|
this.serviceInstanceSelector = serviceInstanceSelector;
|
||||||
/**
|
this.discoveryClient = discoveryClient;
|
||||||
* Initializes {@link DubboMetadataService}'s Proxy.
|
|
||||||
* @param serviceName the service name
|
|
||||||
* @param version the service version
|
|
||||||
* @return a {@link DubboMetadataService} proxy
|
|
||||||
*/
|
|
||||||
public DubboMetadataService initProxy(String serviceName, String version) {
|
|
||||||
return dubboMetadataServiceCache.computeIfAbsent(serviceName,
|
|
||||||
name -> newProxy(name, version));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,6 +74,79 @@ public class DubboMetadataServiceProxy implements BeanClassLoaderAware, Disposab
|
|||||||
*/
|
*/
|
||||||
public void removeProxy(String serviceName) {
|
public void removeProxy(String serviceName) {
|
||||||
dubboMetadataServiceCache.remove(serviceName);
|
dubboMetadataServiceCache.remove(serviceName);
|
||||||
|
dubboGenericServiceFactory.destroy(serviceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the proxy of {@link DubboMetadataService} if possible.
|
||||||
|
* @param serviceInstances the instances of {@link DubboMetadataService}
|
||||||
|
* @return <code>null</code> if initialization can't be done
|
||||||
|
*/
|
||||||
|
public DubboMetadataService getProxy(List<ServiceInstance> serviceInstances) {
|
||||||
|
|
||||||
|
DubboMetadataService dubboMetadataService = null;
|
||||||
|
|
||||||
|
// attempt to get the proxy of DubboMetadataService in maximum times
|
||||||
|
int attempts = serviceInstances.size();
|
||||||
|
|
||||||
|
for (int i = 0; i < attempts; i++) {
|
||||||
|
Optional<ServiceInstance> serviceInstance = select(serviceInstances);
|
||||||
|
|
||||||
|
if (serviceInstance.isPresent()) {
|
||||||
|
|
||||||
|
List<URL> dubboMetadataServiceURLs = getDubboMetadataServiceURLs(
|
||||||
|
serviceInstance.get());
|
||||||
|
|
||||||
|
for (URL dubboMetadataServiceURL : dubboMetadataServiceURLs) {
|
||||||
|
dubboMetadataService = createProxyIfAbsent(dubboMetadataServiceURL);
|
||||||
|
if (dubboMetadataService != null) {
|
||||||
|
return dubboMetadataService;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dubboMetadataService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the {@link DubboMetadataService}'s Proxy initialized or not.
|
||||||
|
* @param serviceName the service name
|
||||||
|
* @return <code>true</code> if initialized , or return <code>false</code>
|
||||||
|
*/
|
||||||
|
public boolean isInitialized(String serviceName) {
|
||||||
|
return dubboMetadataServiceCache.containsKey(serviceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a {@link DubboMetadataService}'s Proxy If abstract.
|
||||||
|
* @param dubboMetadataServiceURL the {@link URL} of {@link DubboMetadataService}
|
||||||
|
* @return a {@link DubboMetadataService} proxy
|
||||||
|
*/
|
||||||
|
private DubboMetadataService createProxyIfAbsent(URL dubboMetadataServiceURL) {
|
||||||
|
String serviceName = dubboMetadataServiceURL.getParameter(APPLICATION_KEY);
|
||||||
|
String version = dubboMetadataServiceURL.getParameter(VERSION_KEY);
|
||||||
|
// Initialize DubboMetadataService with right version
|
||||||
|
return createProxyIfAbsent(serviceName, version);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes {@link DubboMetadataService}'s Proxy.
|
||||||
|
* @param serviceName the service name
|
||||||
|
* @param version the service version
|
||||||
|
* @return a {@link DubboMetadataService} proxy
|
||||||
|
*/
|
||||||
|
private DubboMetadataService createProxyIfAbsent(String serviceName, String version) {
|
||||||
|
return dubboMetadataServiceCache.computeIfAbsent(serviceName,
|
||||||
|
name -> createProxy(name, version));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<ServiceInstance> select(List<ServiceInstance> serviceInstances) {
|
||||||
|
return serviceInstanceSelector.select(serviceInstances);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<URL> getDubboMetadataServiceURLs(ServiceInstance serviceInstance) {
|
||||||
|
return dubboMetadataUtils.getDubboMetadataServiceURLs(serviceInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,7 +156,12 @@ public class DubboMetadataServiceProxy implements BeanClassLoaderAware, Disposab
|
|||||||
* @return a {@link DubboMetadataService} proxy
|
* @return a {@link DubboMetadataService} proxy
|
||||||
*/
|
*/
|
||||||
public DubboMetadataService getProxy(String serviceName) {
|
public DubboMetadataService getProxy(String serviceName) {
|
||||||
return dubboMetadataServiceCache.get(serviceName);
|
return dubboMetadataServiceCache.getOrDefault(serviceName,
|
||||||
|
getProxy0(serviceName));
|
||||||
|
}
|
||||||
|
|
||||||
|
private DubboMetadataService getProxy0(String serviceName) {
|
||||||
|
return getProxy(discoveryClient.getInstances(serviceName));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -88,7 +181,14 @@ public class DubboMetadataServiceProxy implements BeanClassLoaderAware, Disposab
|
|||||||
* @param version the service version
|
* @param version the service version
|
||||||
* @return a {@link DubboMetadataService} proxy
|
* @return a {@link DubboMetadataService} proxy
|
||||||
*/
|
*/
|
||||||
protected DubboMetadataService newProxy(String serviceName, String version) {
|
protected DubboMetadataService createProxy(String serviceName, String version) {
|
||||||
|
|
||||||
|
if (logger.isInfoEnabled()) {
|
||||||
|
logger.info(
|
||||||
|
"The metadata of Dubbo service[name : {}] is about to be initialized",
|
||||||
|
serviceName);
|
||||||
|
}
|
||||||
|
|
||||||
return (DubboMetadataService) newProxyInstance(classLoader,
|
return (DubboMetadataService) newProxyInstance(classLoader,
|
||||||
new Class[] { DubboMetadataService.class },
|
new Class[] { DubboMetadataService.class },
|
||||||
new DubboMetadataServiceInvocationHandler(serviceName, version,
|
new DubboMetadataServiceInvocationHandler(serviceName, version,
|
||||||
|
@ -48,7 +48,7 @@ public abstract class AbstractNamedValueServiceParameterResolver
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object resolve(RestMethodMetadata restMethodMetadata,
|
public Object resolve(RestMethodMetadata restMethodMetadata,
|
||||||
MethodParameterMetadata methodParameterMetadata, HttpServerRequest request) {
|
MethodParameterMetadata methodParameterMetadata, HttpServerRequest request) {
|
||||||
|
|
||||||
Collection<String> names = getNames(restMethodMetadata, methodParameterMetadata);
|
Collection<String> names = getNames(restMethodMetadata, methodParameterMetadata);
|
||||||
|
|
||||||
@ -87,8 +87,8 @@ public abstract class AbstractNamedValueServiceParameterResolver
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object resolve(RestMethodMetadata restMethodMetadata,
|
public Object resolve(RestMethodMetadata restMethodMetadata,
|
||||||
MethodParameterMetadata methodParameterMetadata,
|
MethodParameterMetadata methodParameterMetadata,
|
||||||
RestMethodMetadata clientRestMethodMetadata, Object[] arguments) {
|
RestMethodMetadata clientRestMethodMetadata, Object[] arguments) {
|
||||||
|
|
||||||
Collection<String> names = getNames(restMethodMetadata, methodParameterMetadata);
|
Collection<String> names = getNames(restMethodMetadata, methodParameterMetadata);
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ public abstract class AbstractNamedValueServiceParameterResolver
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected Collection<String> getNames(RestMethodMetadata restMethodMetadata,
|
protected Collection<String> getNames(RestMethodMetadata restMethodMetadata,
|
||||||
MethodParameterMetadata methodParameterMetadata) {
|
MethodParameterMetadata methodParameterMetadata) {
|
||||||
|
|
||||||
Map<Integer, Collection<String>> indexToName = restMethodMetadata
|
Map<Integer, Collection<String>> indexToName = restMethodMetadata
|
||||||
.getIndexToName();
|
.getIndexToName();
|
||||||
|
@ -38,10 +38,10 @@ public interface DubboGenericServiceParameterResolver extends Ordered {
|
|||||||
* @return the result of resolve
|
* @return the result of resolve
|
||||||
*/
|
*/
|
||||||
Object resolve(RestMethodMetadata restMethodMetadata,
|
Object resolve(RestMethodMetadata restMethodMetadata,
|
||||||
MethodParameterMetadata methodParameterMetadata, HttpServerRequest request);
|
MethodParameterMetadata methodParameterMetadata, HttpServerRequest request);
|
||||||
|
|
||||||
Object resolve(RestMethodMetadata restMethodMetadata,
|
Object resolve(RestMethodMetadata restMethodMetadata,
|
||||||
MethodParameterMetadata methodParameterMetadata,
|
MethodParameterMetadata methodParameterMetadata,
|
||||||
RestMethodMetadata clientRestMethodMetadata, Object[] arguments);
|
RestMethodMetadata clientRestMethodMetadata, Object[] arguments);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ public class RequestBodyServiceParameterResolver
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean supportParameter(RestMethodMetadata restMethodMetadata,
|
private boolean supportParameter(RestMethodMetadata restMethodMetadata,
|
||||||
MethodParameterMetadata methodParameterMetadata) {
|
MethodParameterMetadata methodParameterMetadata) {
|
||||||
|
|
||||||
Integer index = methodParameterMetadata.getIndex();
|
Integer index = methodParameterMetadata.getIndex();
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ public class RequestBodyServiceParameterResolver
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object resolve(RestMethodMetadata restMethodMetadata,
|
public Object resolve(RestMethodMetadata restMethodMetadata,
|
||||||
MethodParameterMetadata methodParameterMetadata, HttpServerRequest request) {
|
MethodParameterMetadata methodParameterMetadata, HttpServerRequest request) {
|
||||||
|
|
||||||
if (!supportParameter(restMethodMetadata, methodParameterMetadata)) {
|
if (!supportParameter(restMethodMetadata, methodParameterMetadata)) {
|
||||||
return null;
|
return null;
|
||||||
@ -117,8 +117,8 @@ public class RequestBodyServiceParameterResolver
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object resolve(RestMethodMetadata restMethodMetadata,
|
public Object resolve(RestMethodMetadata restMethodMetadata,
|
||||||
MethodParameterMetadata methodParameterMetadata,
|
MethodParameterMetadata methodParameterMetadata,
|
||||||
RestMethodMetadata clientRestMethodMetadata, Object[] arguments) {
|
RestMethodMetadata clientRestMethodMetadata, Object[] arguments) {
|
||||||
|
|
||||||
if (!supportParameter(restMethodMetadata, methodParameterMetadata)) {
|
if (!supportParameter(restMethodMetadata, methodParameterMetadata)) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* 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.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constants for Dubbo Spring Cloud.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
|
*/
|
||||||
|
public final class DubboCloudConstants {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property prefix of Configuration.
|
||||||
|
*/
|
||||||
|
public static final String CONFIG_PROPERTY_PREFIX = "dubbo.cloud";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property name of Registry type.
|
||||||
|
*/
|
||||||
|
public static final String REGISTRY_TYPE_PROPERTY_NAME = CONFIG_PROPERTY_PREFIX
|
||||||
|
+ ".registry-type";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property value of Spring Cloud Registry.
|
||||||
|
*/
|
||||||
|
public static final String SPRING_CLOUD_REGISTRY_PROPERTY_VALUE = "spring-cloud";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property value of Dubbo Cloud Registry.
|
||||||
|
*/
|
||||||
|
public static final String DUBBO_CLOUD_REGISTRY_PROPERTY_VALUE = "dubbo-cloud";
|
||||||
|
|
||||||
|
private DubboCloudConstants() {
|
||||||
|
throw new AssertionError("Must not instantiate constant utility class");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,118 @@
|
|||||||
|
/*
|
||||||
|
* 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.util;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.dubbo.service.DubboMetadataService;
|
||||||
|
import org.apache.dubbo.common.URL;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
import static java.lang.String.format;
|
||||||
|
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
|
||||||
|
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URLS_PROPERTY_NAME;
|
||||||
|
import static org.springframework.util.StringUtils.hasText;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The utilities class of Dubbo Metadata.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||||
|
*/
|
||||||
|
public class DubboMetadataUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link String#format(String, Object...) pattern} of dubbo protocols port.
|
||||||
|
*/
|
||||||
|
public static final String DUBBO_PROTOCOLS_PORT_PROPERTY_NAME_PATTERN = "dubbo.protocols.%s.port";
|
||||||
|
|
||||||
|
private final JSONUtils jsonUtils;
|
||||||
|
|
||||||
|
private final Environment environment;
|
||||||
|
|
||||||
|
private final String currentApplicationName;
|
||||||
|
|
||||||
|
public DubboMetadataUtils(JSONUtils jsonUtils, Environment environment) {
|
||||||
|
this.jsonUtils = jsonUtils;
|
||||||
|
this.environment = environment;
|
||||||
|
this.currentApplicationName = environment.getProperty("spring.application.name",
|
||||||
|
environment.getProperty("dubbo.application.name", "application"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current application name.
|
||||||
|
* @return non-null
|
||||||
|
*/
|
||||||
|
public String getCurrentApplicationName() {
|
||||||
|
return currentApplicationName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the {@link URL urls} that {@link DubboMetadataService} exported by the
|
||||||
|
* specified {@link ServiceInstance}.
|
||||||
|
* @param serviceInstance {@link ServiceInstance}
|
||||||
|
* @return the mutable {@link URL urls}
|
||||||
|
*/
|
||||||
|
public List<URL> getDubboMetadataServiceURLs(ServiceInstance serviceInstance) {
|
||||||
|
Map<String, String> metadata = serviceInstance.getMetadata();
|
||||||
|
String dubboURLsJSON = metadata.get(METADATA_SERVICE_URLS_PROPERTY_NAME);
|
||||||
|
return jsonUtils.toURLs(dubboURLsJSON);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the {@link URL urls} that {@link DubboMetadataService} exported by the
|
||||||
|
* specified {@link ServiceInstance ServiceInstances}.
|
||||||
|
* @param serviceInstances the list of {@link ServiceInstance ServiceInstances}
|
||||||
|
* @param serviceInterface the interface name of Dubbo service
|
||||||
|
* @param version the version of Dubbo service
|
||||||
|
* @param protocol the protocol that Dubbo Service exports
|
||||||
|
* @return the mutable {@link URL urls}
|
||||||
|
*/
|
||||||
|
public List<URL> getDubboMetadataServiceURLs(List<ServiceInstance> serviceInstances,
|
||||||
|
String serviceInterface, String version, String protocol) {
|
||||||
|
return serviceInstances.stream().map(this::getDubboMetadataServiceURLs)
|
||||||
|
.flatMap(List::stream)
|
||||||
|
.filter(url -> protocol == null
|
||||||
|
|| Objects.equals(protocol, url.getProtocol()))
|
||||||
|
.filter(url -> Objects.equals(serviceInterface,
|
||||||
|
url.getServiceInterface()))
|
||||||
|
.filter(url -> Objects.equals(version, url.getParameter(VERSION_KEY)))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the property name of Dubbo Protocol.
|
||||||
|
* @param protocol Dubbo Protocol
|
||||||
|
* @return non-null
|
||||||
|
*/
|
||||||
|
public String getDubboProtocolPropertyName(String protocol) {
|
||||||
|
return format(DUBBO_PROTOCOLS_PORT_PROPERTY_NAME_PATTERN, protocol);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getDubboProtocolPort(ServiceInstance serviceInstance,
|
||||||
|
String protocol) {
|
||||||
|
String protocolProperty = getDubboProtocolPropertyName(protocol);
|
||||||
|
Map<String, String> metadata = serviceInstance.getMetadata();
|
||||||
|
String protocolPort = metadata.get(protocolProperty);
|
||||||
|
return hasText(protocolPort) ? Integer.valueOf(protocolPort) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
# Dubbo Endpoints Default Properties is loaded by @PropertySource with low order,
|
# Dubbo Endpoints Default Properties is loaded by @PropertySource with low order,
|
||||||
# Set enabled for Dubbo Endpoints
|
# Set enabled for Dubbo Endpoints
|
||||||
management.endpoint.dubborestmetadata.enabled = true
|
management.endpoint.dubborestmetadata.enabled=true
|
||||||
|
|
||||||
# "management.endpoints.web.base-path" should not be configured in this file
|
# "management.endpoints.web.base-path" should not be configured in this file
|
||||||
# Re-defines path-mapping of Dubbo Web Endpoints
|
# Re-defines path-mapping of Dubbo Web Endpoints
|
||||||
management.endpoints.web.path-mapping.dubborestmetadata = dubbo/rest/metadata
|
management.endpoints.web.path-mapping.dubborestmetadata=dubbo/rest/metadata
|
||||||
|
@ -6,12 +6,9 @@ com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationNonWebApplicationA
|
|||||||
com.alibaba.cloud.dubbo.autoconfigure.DubboLoadBalancedRestTemplateAutoConfiguration,\
|
com.alibaba.cloud.dubbo.autoconfigure.DubboLoadBalancedRestTemplateAutoConfiguration,\
|
||||||
com.alibaba.cloud.dubbo.autoconfigure.DubboServiceAutoConfiguration,\
|
com.alibaba.cloud.dubbo.autoconfigure.DubboServiceAutoConfiguration,\
|
||||||
com.alibaba.cloud.dubbo.autoconfigure.DubboServiceDiscoveryAutoConfiguration
|
com.alibaba.cloud.dubbo.autoconfigure.DubboServiceDiscoveryAutoConfiguration
|
||||||
|
|
||||||
org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=\
|
org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=\
|
||||||
com.alibaba.cloud.dubbo.actuate.DubboMetadataEndpointAutoConfiguration
|
com.alibaba.cloud.dubbo.actuate.DubboMetadataEndpointAutoConfiguration
|
||||||
|
|
||||||
org.springframework.context.ApplicationContextInitializer=\
|
org.springframework.context.ApplicationContextInitializer=\
|
||||||
com.alibaba.cloud.dubbo.context.DubboServiceRegistrationApplicationContextInitializer
|
com.alibaba.cloud.dubbo.context.DubboServiceRegistrationApplicationContextInitializer
|
||||||
|
|
||||||
org.springframework.boot.env.EnvironmentPostProcessor=\
|
org.springframework.boot.env.EnvironmentPostProcessor=\
|
||||||
com.alibaba.cloud.dubbo.env.DubboNonWebApplicationEnvironmentPostProcessor
|
com.alibaba.cloud.dubbo.env.DubboNonWebApplicationEnvironmentPostProcessor
|
@ -3,8 +3,6 @@ spring:
|
|||||||
name: spring-cloud-alibaba-dubbo
|
name: spring-cloud-alibaba-dubbo
|
||||||
cloud:
|
cloud:
|
||||||
nacos:
|
nacos:
|
||||||
username: nacos
|
|
||||||
password: nacos
|
|
||||||
discovery:
|
discovery:
|
||||||
server-addr: 127.0.0.1:8848
|
server-addr: 127.0.0.1:8848
|
||||||
config:
|
config:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user