diff --git a/README.md b/README.md index 6f69e3dcbb..e448683548 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ Examples: [Alibaba Cloud OSS Example](https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/oss-example/readme.md) -[Duboo Spring Cloud Example](https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/README_CN.md) +[Dubbo Spring Cloud Example](https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/README_CN.md) ## Version control guidelines The version number of the project is in the form of x.x.x, where x is a number, starting from 0, and is not limited to the range 0~9. When the project is in the incubator phase, the version number is 0.x.x. diff --git a/pom.xml b/pom.xml index 3be1384289..dbf0860dca 100644 --- a/pom.xml +++ b/pom.xml @@ -8,13 +8,13 @@ org.springframework.cloud spring-cloud-build - 2.1.6.RELEASE + 2.2.0.BUILD-SNAPSHOT com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT pom Spring Cloud Alibaba Spring Cloud Alibaba @@ -45,8 +45,10 @@ flystar32@163.com - fangjian + Jim Fang fangjian0423@gmail.com + Alibaba + https://github.com/fangjian0423 xiaolongzuo @@ -71,16 +73,23 @@ - 2.1.2.RELEASE - 2.1.2.RELEASE - 2.1.2.RELEASE - 2.1.2.RELEASE - 2.1.2.RELEASE + 2.2.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT + Horsham.BUILD-SNAPSHOT 4.12 3.0 1.7.25 + + 2.7.1 + 2.7.3 + + + 2.0.2 3.7.0 @@ -95,6 +104,7 @@ spring-cloud-alibaba-sentinel spring-cloud-alibaba-sentinel-datasource spring-cloud-alibaba-sentinel-gateway + spring-cloud-circuitbreaker-sentinel spring-cloud-alibaba-nacos-config spring-cloud-alibaba-nacos-discovery spring-cloud-alibaba-seata @@ -180,6 +190,46 @@ pom import + + + org.springframework.cloud + spring-cloud-stream-dependencies + ${spring-cloud-stream.version} + pom + import + + + + org.apache.dubbo + dubbo-spring-boot-starter + ${dubbo-spring-boot.version} + + + + org.apache.dubbo + dubbo + ${dubbo.version} + + + org.springframework + spring + + + javax.servlet + servlet-api + + + log4j + log4j + + + + + + org.apache.rocketmq + rocketmq-spring-boot-starter + ${rocketmq.starter.version} + diff --git a/spring-cloud-alibaba-coverage/pom.xml b/spring-cloud-alibaba-coverage/pom.xml index 456dd65dea..e63ab6159d 100644 --- a/spring-cloud-alibaba-coverage/pom.xml +++ b/spring-cloud-alibaba-coverage/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 @@ -13,7 +13,7 @@ Spring Cloud Alibaba Coverage - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 6c6b85fc31..a15bdd26cd 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -6,13 +6,13 @@ spring-cloud-dependencies-parent org.springframework.cloud - 2.1.6.RELEASE + 2.2.0.BUILD-SNAPSHOT com.alibaba.cloud spring-cloud-alibaba-dependencies - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT pom Spring Cloud Alibaba Dependencies Spring Cloud Alibaba Dependencies @@ -28,11 +28,8 @@ 4.4.1 1.0.5 2.44.0 - 2.0.2 2.1.6 2.7.3 - 2.7.1 - 2.7.1 1.1.0 1.1.8.6 1.1.1 @@ -105,13 +102,6 @@ ${nacos.config.version} - - - org.apache.rocketmq - rocketmq-spring-boot-starter - ${rocketmq.starter.version} - - com.alibaba.csp @@ -183,6 +173,11 @@ sentinel-apache-dubbo-adapter ${sentinel.version} + + com.alibaba.csp + sentinel-reactor-adapter + ${sentinel.version} + com.alibaba.cloud sentinel-dubbo-api @@ -211,48 +206,12 @@ - io.seata seata-all ${seata.version} - - - org.apache.dubbo - dubbo - ${dubbo.version} - - - org.springframework - spring-context - - - javax.servlet - servlet-api - - - log4j - log4j - - - - - - - org.apache.dubbo - dubbo-spring-boot-starter - ${dubbo-spring-boot.version} - - - - - org.apache.dubbo - dubbo-registry-nacos - ${dubbo-registry-nacos.version} - - com.aliyun.oss diff --git a/spring-cloud-alibaba-docs/pom.xml b/spring-cloud-alibaba-docs/pom.xml index e15d9554bc..ede7e416db 100644 --- a/spring-cloud-alibaba-docs/pom.xml +++ b/spring-cloud-alibaba-docs/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc index 738c8f993c..340da4d8b1 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc @@ -166,7 +166,7 @@ NOTE: 在使用 RocketMQ Binder 的同时也可以配置 rocketmq.** 用于触 MessageBuilder builder = MessageBuilder.withPayload(msg) .setHeader(RocketMQHeaders.TAGS, "binder") .setHeader(RocketMQHeaders.KEYS, "my-key") - .setHeader("DELAY", "1"); + .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, "1"); Message message = builder.build(); output().send(message); ``` diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/circuitbreaker-sentinel.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/circuitbreaker-sentinel.adoc new file mode 100644 index 0000000000..b2695e723d --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/circuitbreaker-sentinel.adoc @@ -0,0 +1,73 @@ +=== Circuit Breaker: Spring Cloud Circuit Breaker With Sentinel & Configuring Sentinel Circuit Breakers + +==== Default Configuration + +To provide a default configuration for all of your circuit breakers create a `Customizer` bean that is passed a +`SentinelCircuitBreakerFactory` or `ReactiveSentinelCircuitBreakerFactory`. +The `configureDefault` method can be used to provide a default configuration. + +==== +[source,java] +---- +@Bean +public Customizer defaultCustomizer() { + return factory -> factory.configureDefault(id -> new SentinelConfigBuilder(id) + .build()); +} +---- +==== + +You can choose to provide default circuit breaking rules via `SentinelConfigBuilder#rules(rules)`. +You can also choose to load circuit breaking rules later elsewhere using +`DegradeRuleManager.loadRules(rules)` API of Sentinel, or via Sentinel dashboard. + +===== Reactive Example + +==== +[source,java] +---- +@Bean +public Customizer defaultCustomizer() { + return factory -> factory.configureDefault(id -> new SentinelConfigBuilder(id) + .build()); +} +---- +==== + +==== Specific Circuit Breaker Configuration + +Similarly to providing a default configuration, you can create a `Customizer` bean this is passed a +`SentinelCircuitBreakerFactory`. + +==== +[source,java] +---- +@Bean +public Customizer slowCustomizer() { + String slowId = "slow"; + List rules = Collections.singletonList( + new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT) + .setCount(100) + .setTimeWindow(10) + ); + return factory -> factory.configure(builder -> builder.rules(rules), slowId); +} +---- +==== + +===== Reactive Example + +==== +[source,java] +---- +@Bean +public Customizer customizer() { + List rules = Collections.singletonList( + new DegradeRule().setGrade(RuleConstant.DEGRADE_GRADE_RT) + .setCount(100) + .setTimeWindow(10) + ); + return factory -> factory.configure(builder -> builder.rules(rules), "foo", "bar"); +} +---- +==== \ No newline at end of file diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc index 14de88e7c5..540d42c419 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc @@ -114,7 +114,7 @@ All the message types in this code are provided by the `spring-messaging`module. **The lower layer of Spring Cloud Stream also implements various code abstractions based on the previous code.** -=== How to use Spring Cloud Alibaba RocketMQ Binder ### +=== How to use Spring Cloud Alibaba RocketMQ Binder For using the Spring Cloud Alibaba RocketMQ Binder, you just need to add it to your Spring Cloud Stream application, using the following Maven coordinates: @@ -165,7 +165,7 @@ For example, `TAGS`, `DELAY`, `TRANSACTIONAL_ARG`, `KEYS`, `WAIT_STORE_MSG_OK`, MessageBuilder builder = MessageBuilder.withPayload(msg) .setHeader(RocketMQHeaders.TAGS, "binder") .setHeader(RocketMQHeaders.KEYS, "my-key") - .setHeader("DELAY", "1"); + .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, "1"); Message message = builder.build(); output().send(message); ``` diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc index 017ee59143..f7fc3e6dfd 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc @@ -255,7 +255,7 @@ To learn more about how dynamic data sources work in Sentinel, refer to https:// === Support Zuul -https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81[参考 Sentinel 网关限流] +Refer https://github.com/alibaba/Sentinel/wiki/API-Gateway-Flow-Control[API Gateway Flow Control] If you want to use Sentinel Starter with Zuul, you need to add the `spring-cloud-alibaba-sentinel-gateway` dependency, and you need to add the `spring-cloud-starter-netflix-zuul` dependency to let Zuul AutoConfiguration class in the gateway module takes effect: @@ -278,7 +278,7 @@ If you want to use Sentinel Starter with Zuul, you need to add the `spring-cloud === Support Spring Cloud Gateway -https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81[参考 Sentinel 网关限流] +Refer https://github.com/alibaba/Sentinel/wiki/API-Gateway-Flow-Control[API Gateway Flow Control] If you want to use Sentinel Starter with Spring Cloud Gateway, you need to add the `spring-cloud-alibaba-sentinel-gateway` dependency and add the `spring-cloud-starter-gateway` dependency to let Spring Cloud Gateway AutoConfiguration class in the module takes effect: @@ -299,6 +299,8 @@ If you want to use Sentinel Starter with Spring Cloud Gateway, you need to add t ``` +include::circuitbreaker-sentinel.adoc[] + === Sentinel Endpoint Sentinel provides an Endpoint internally with a corresponding endpoint id of `sentinel`. diff --git a/spring-cloud-alibaba-dubbo/pom.xml b/spring-cloud-alibaba-dubbo/pom.xml index b71a42e547..c53dde47d3 100644 --- a/spring-cloud-alibaba-dubbo/pom.xml +++ b/spring-cloud-alibaba-dubbo/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 @@ -14,10 +14,8 @@ Spring Cloud Alibaba Dubbo - 2.7.3 - 2.7.1 - 2.1.2.RELEASE - 2.1.2.RELEASE + 2.2.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.1 @@ -158,28 +156,12 @@ org.apache.dubbo dubbo - ${dubbo.version} - - - org.springframework - spring - - - javax.servlet - servlet-api - - - log4j - log4j - - org.apache.dubbo dubbo-spring-boot-starter - ${dubbo-spring-boot.version} diff --git a/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboLoadBalancedRestTemplateAutoConfiguration.java b/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboLoadBalancedRestTemplateAutoConfiguration.java index 74bb1e38b0..b1a75923c4 100644 --- a/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboLoadBalancedRestTemplateAutoConfiguration.java +++ b/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboLoadBalancedRestTemplateAutoConfiguration.java @@ -25,16 +25,19 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor; import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer; import org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Configuration; +import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.core.env.Environment; import org.springframework.core.type.MethodMetadata; import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.lang.Nullable; import org.springframework.util.CollectionUtils; import org.springframework.web.client.RestTemplate; @@ -56,8 +59,8 @@ @ConditionalOnClass(name = { "org.springframework.web.client.RestTemplate" }) @AutoConfigureAfter(name = { "org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration" }) -public class DubboLoadBalancedRestTemplateAutoConfiguration - implements BeanClassLoaderAware, SmartInitializingSingleton { +public class DubboLoadBalancedRestTemplateAutoConfiguration implements + BeanClassLoaderAware, ApplicationContextAware, SmartInitializingSingleton { private static final Class DUBBO_TRANSPORTED_CLASS = DubboTransported.class; @@ -89,6 +92,9 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration @Autowired(required = false) private Map restTemplates = Collections.emptyMap(); + @Nullable + private ApplicationContext applicationContext; + private ClassLoader classLoader; /** @@ -111,18 +117,21 @@ public void afterSingletonsInstantiated() { * {@link SmartInitializingSingleton} beans or * {@link RestTemplateCustomizer#customize(RestTemplate) customization}) */ - @EventListener(ApplicationStartedEvent.class) - public void adaptRestTemplates() { - - DubboTransportedAttributesResolver attributesResolver = new DubboTransportedAttributesResolver( - environment); - - for (Map.Entry entry : restTemplates.entrySet()) { - String beanName = entry.getKey(); - Map dubboTranslatedAttributes = getDubboTranslatedAttributes( - beanName, attributesResolver); - if (!CollectionUtils.isEmpty(dubboTranslatedAttributes)) { - adaptRestTemplate(entry.getValue(), dubboTranslatedAttributes); + @EventListener(ContextRefreshedEvent.class) + public void adaptRestTemplates(ContextRefreshedEvent event) { + + if (event.getApplicationContext() == this.applicationContext) { + + DubboTransportedAttributesResolver attributesResolver = new DubboTransportedAttributesResolver( + environment); + + for (Map.Entry entry : restTemplates.entrySet()) { + String beanName = entry.getKey(); + Map dubboTranslatedAttributes = getDubboTranslatedAttributes( + beanName, attributesResolver); + if (!CollectionUtils.isEmpty(dubboTranslatedAttributes)) { + adaptRestTemplate(entry.getValue(), dubboTranslatedAttributes); + } } } } @@ -132,7 +141,7 @@ public void adaptRestTemplates() { * {@link DubboTransported @DubboTransported} * * @param beanName the bean name of {@link LoadBalanced @LoadBalanced} - * {@link RestTemplate} + * {@link RestTemplate} * @param attributesResolver {@link DubboTransportedAttributesResolver} * @return non-null {@link Map} */ @@ -144,10 +153,10 @@ private Map getDubboTranslatedAttributes(String beanName, AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition; MethodMetadata factoryMethodMetadata = annotatedBeanDefinition .getFactoryMethodMetadata(); - attributes = factoryMethodMetadata != null - ? Optional.ofNullable(factoryMethodMetadata - .getAnnotationAttributes(DUBBO_TRANSPORTED_CLASS_NAME)).orElse(attributes) - : Collections.emptyMap(); + attributes = factoryMethodMetadata != null ? Optional + .ofNullable(factoryMethodMetadata + .getAnnotationAttributes(DUBBO_TRANSPORTED_CLASS_NAME)) + .orElse(attributes) : Collections.emptyMap(); } return attributesResolver.resolve(attributes); } @@ -158,8 +167,8 @@ private Map getDubboTranslatedAttributes(String beanName, * * @param restTemplate {@link LoadBalanced @LoadBalanced} {@link RestTemplate} Bean * @param dubboTranslatedAttributes the annotation dubboTranslatedAttributes - * {@link RestTemplate} bean being annotated - * {@link DubboTransported @DubboTransported} + * {@link RestTemplate} bean being annotated + * {@link DubboTransported @DubboTransported} */ private void adaptRestTemplate(RestTemplate restTemplate, Map dubboTranslatedAttributes) { @@ -188,4 +197,9 @@ public void setBeanClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; } + @Override + public void setApplicationContext(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + } diff --git a/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboMetadataAutoConfiguration.java b/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboMetadataAutoConfiguration.java index c5dda73fbe..086e4f6e0c 100644 --- a/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboMetadataAutoConfiguration.java +++ b/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/autoconfigure/DubboMetadataAutoConfiguration.java @@ -16,6 +16,7 @@ package com.alibaba.cloud.dubbo.autoconfigure; import java.util.Collection; +import java.util.Optional; import java.util.function.Supplier; import org.apache.dubbo.config.ProtocolConfig; @@ -30,9 +31,11 @@ import org.springframework.context.annotation.Import; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.EventListener; +import org.springframework.util.CollectionUtils; import com.alibaba.cloud.dubbo.metadata.DubboProtocolConfigSupplier; import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository; +import com.alibaba.cloud.dubbo.metadata.repository.MetadataServiceInstanceSelector; import com.alibaba.cloud.dubbo.metadata.resolver.DubboServiceBeanMetadataResolver; import com.alibaba.cloud.dubbo.metadata.resolver.MetadataResolver; import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory; @@ -68,6 +71,14 @@ public MetadataResolver metadataJsonResolver(ObjectProvider contract) return new DubboServiceBeanMetadataResolver(contract); } + @Bean + @ConditionalOnMissingBean + public MetadataServiceInstanceSelector metadataServiceInstanceSelector() { + return serviceInstances -> CollectionUtils.isEmpty(serviceInstances) + ? Optional.empty() + : serviceInstances.stream().findAny(); + } + @Bean public Supplier dubboProtocolConfigSupplier( ObjectProvider> protocols) { diff --git a/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java b/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java index 048635d99b..3afea1caf2 100644 --- a/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java +++ b/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java @@ -36,7 +36,6 @@ import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -50,7 +49,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; -import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.commons.util.InetUtils; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; @@ -168,7 +166,7 @@ public class DubboServiceMetadataRepository private DiscoveryClient discoveryClient; @Autowired - private LoadBalancerClient loadBalancerClient; + private MetadataServiceInstanceSelector metadataServiceInstanceSelector; @Autowired private JSONUtils jsonUtils; @@ -623,7 +621,7 @@ private void excludeSelf(Set subscribedServices) { } protected void initSubscribedDubboMetadataService(String serviceName) { - Optional.ofNullable(loadBalancerClient.choose(serviceName)) + metadataServiceInstanceSelector.choose(discoveryClient.getInstances(serviceName)) .map(this::getDubboMetadataServiceURLs) .ifPresent(dubboMetadataServiceURLs -> { dubboMetadataServiceURLs.forEach(dubboMetadataServiceURL -> { diff --git a/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/MetadataServiceInstanceSelector.java b/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/MetadataServiceInstanceSelector.java new file mode 100644 index 0000000000..f42ba4f9c8 --- /dev/null +++ b/spring-cloud-alibaba-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/MetadataServiceInstanceSelector.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 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 org.springframework.cloud.client.ServiceInstance; + +/** + * metadata service instance selector + * + * @author liuxx + */ +public interface MetadataServiceInstanceSelector { + + /** + * choose a service instance to get metadata + * @param serviceInstances all service instance + * @return the service instance to get metadata + */ + Optional choose(List serviceInstances); +} diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml index 5ad1812f7d..9111c4dd21 100644 --- a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml index cfccf8c5bc..5a631c0ad7 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml index e1bc843856..a53d0b51ad 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml index c2d3265aa8..f3acbaa73a 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml index ab411e5f2d..8085389022 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml index afa3904b14..4fee805aab 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud nacos-discovery-example - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml index ca15c8ba1d..a4e120acc0 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud nacos-discovery-example - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-client/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-client/pom.xml index 116b91e90d..afd7858027 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-client/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-client/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud nacos-discovery-example - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-server/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-server/pom.xml index b09a7a17a9..0ef1d6fd78 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-server/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-server/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud nacos-discovery-example - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml index 0b996ec387..bcf7b83060 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-discovery-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-discovery-example/pom.xml index a1c35a7dcd..92af4fbbdd 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-discovery-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-discovery-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud nacos-gateway-example - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/pom.xml index 8a0969afb0..6c722850f5 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud nacos-gateway-example - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/pom.xml index a0c2e94ed7..6152c5747c 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/oss-example/pom.xml b/spring-cloud-alibaba-examples/oss-example/pom.xml index c0fab28c11..e478586283 100644 --- a/spring-cloud-alibaba-examples/oss-example/pom.xml +++ b/spring-cloud-alibaba-examples/oss-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index bb19d74e94..6f30e33865 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-consume-example/pom.xml b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-consume-example/pom.xml index 495b3c48be..1a3d364626 100644 --- a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-consume-example/pom.xml +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-consume-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-produce-example/pom.xml b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-produce-example/pom.xml index 1222a8653a..663ff8eeb8 100644 --- a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-produce-example/pom.xml +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-produce-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/schedulerx-example/schedulerx-simple-task-example/pom.xml b/spring-cloud-alibaba-examples/schedulerx-example/schedulerx-simple-task-example/pom.xml index 8c2e6b5e11..3467be0cfd 100644 --- a/spring-cloud-alibaba-examples/schedulerx-example/schedulerx-simple-task-example/pom.xml +++ b/spring-cloud-alibaba-examples/schedulerx-example/schedulerx-simple-task-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml index a85ebef2d9..428fcebcaf 100644 --- a/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml index 4cffabf114..bd58fe41f0 100644 --- a/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml index a257acc834..0749a727cc 100644 --- a/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml index 9c6b2c34e4..a2c8f3ee37 100644 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml index 960b34ecd6..3b7c0ce607 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml index 7daa7a75a1..3eeabc474e 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml index f0b9a131bb..31bc8030a9 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/src/main/java/com/alibaba/cloud/examples/FooServiceConsumer.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/src/main/java/com/alibaba/cloud/examples/FooServiceConsumer.java index e4a6022c3d..48c03b86a6 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/src/main/java/com/alibaba/cloud/examples/FooServiceConsumer.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/src/main/java/com/alibaba/cloud/examples/FooServiceConsumer.java @@ -7,7 +7,7 @@ */ public class FooServiceConsumer { - @Reference(version = "${foo.service.version}", application = "${dubbo.application.id}", url = "dubbo://localhost:12345", timeout = 30000) + @Reference(version = "${foo.service.version}", application = "${dubbo.application.id}", url = "dubbo://localhost:12345?version=1.0.0", timeout = 30000) private FooService fooService; public String hello(String name) { diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml index cd888ac809..af1bbac872 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml index 4a7068089d..095bb5efdf 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml index e57dd23f2e..0098713336 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-spring-cloud-gateway-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-spring-cloud-gateway-example/pom.xml index 2cf43f39dc..d5affb7f2d 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-spring-cloud-gateway-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-spring-cloud-gateway-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml index 482c590704..267503331c 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-zuul-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-zuul-example/pom.xml index eaedfdc70d..6441286361 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-zuul-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-zuul-example/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sms-example/pom.xml b/spring-cloud-alibaba-examples/sms-example/pom.xml index b537998100..781ec373c5 100644 --- a/spring-cloud-alibaba-examples/sms-example/pom.xml +++ b/spring-cloud-alibaba-examples/sms-example/pom.xml @@ -10,7 +10,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml diff --git a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/pom.xml index 89621923b9..ff637ceae9 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/pom.xml +++ b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/pom.xml @@ -5,14 +5,14 @@ org.springframework.cloud spring-cloud-build - 2.1.3.RELEASE + 2.2.0.BUILD-SNAPSHOT 4.0.0 com.alibaba.cloud spring-cloud-alibaba-dubbo-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT Spring Cloud Alibaba Dubbo Examples pom @@ -28,11 +28,11 @@ 2.7.1 - 2.1.2.RELEASE - 2.1.2.RELEASE - 2.1.2.RELEASE - 2.1.2.RELEASE - 2.1.2.RELEASE + 2.2.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.1 diff --git a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-client-sample/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-client-sample/pom.xml index 9e5ab0fbf8..1d0ceec08f 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-client-sample/pom.xml +++ b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-client-sample/pom.xml @@ -13,7 +13,7 @@ com.alibaba.cloud spring-cloud-dubbo-client-sample Spring Cloud Dubbo Client Sample - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT diff --git a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-consumer-sample/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-consumer-sample/pom.xml index 15bef9e8ef..0b6e087cbe 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-consumer-sample/pom.xml +++ b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-consumer-sample/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-dubbo-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-sample/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-sample/pom.xml index b876fe4c94..3cd0be99ed 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-sample/pom.xml +++ b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-sample/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-dubbo-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-web-sample/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-web-sample/pom.xml index c6d7d7d587..b7f17bb63a 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-web-sample/pom.xml +++ b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-provider-web-sample/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-dubbo-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-sample-api/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-sample-api/pom.xml index 989aac8ee0..0ae78ede9f 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-sample-api/pom.xml +++ b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-sample-api/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-dubbo-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-server-sample/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-server-sample/pom.xml index 9e1103ada0..8ff77ac182 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-server-sample/pom.xml +++ b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-server-sample/pom.xml @@ -14,7 +14,7 @@ com.alibaba.cloud spring-cloud-dubbo-server-sample Spring Cloud Dubbo Server Sample - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT diff --git a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-servlet-gateway-sample/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-servlet-gateway-sample/pom.xml index 3a6803a4d2..b085481a35 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-servlet-gateway-sample/pom.xml +++ b/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/spring-cloud-dubbo-servlet-gateway-sample/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba-dubbo-examples - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/pom.xml index 4d78df6661..96e60a5379 100644 --- a/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/pom.xml +++ b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples com.alibaba.cloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-nacos-config-server/pom.xml b/spring-cloud-alibaba-nacos-config-server/pom.xml index e604fba6b8..2823696f9f 100644 --- a/spring-cloud-alibaba-nacos-config-server/pom.xml +++ b/spring-cloud-alibaba-nacos-config-server/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-nacos-config/pom.xml b/spring-cloud-alibaba-nacos-config/pom.xml index d40aeaf278..cf06182e19 100644 --- a/spring-cloud-alibaba-nacos-config/pom.xml +++ b/spring-cloud-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java index ef8c9bce1f..91f4759dd6 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigManager.java @@ -16,7 +16,11 @@ */ package com.alibaba.cloud.nacos; +import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.exception.NacosException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import java.util.Objects; @@ -26,14 +30,23 @@ */ public class NacosConfigManager { - @Autowired - private NacosConfigProperties properties; + private static final Logger log = LoggerFactory.getLogger(NacosConfigManager.class); private static ConfigService service = null; + @Autowired + private NacosConfigProperties properties; + public ConfigService getConfigService() { if (Objects.isNull(service)) { - service = properties.configServiceInstance(); + try { + service = NacosFactory + .createConfigService(properties.getConfigServiceProperties()); + properties.initConfigService(service); + } + catch (NacosException e) { + log.error("create config service error!properties={},e=,", properties, e); + } } return service; } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java index d03c399fad..1537002ca6 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java @@ -403,13 +403,19 @@ public String toString() { + refreshableDataids + '\'' + ", extConfig=" + extConfig + '}'; } + /** + * @see NacosConfigManager#getConfigService() + */ @Deprecated public ConfigService configServiceInstance() { + return configService; + } - if (null != configService) { - return configService; - } + public void initConfigService(ConfigService configService) { + this.configService = configService; + } + public Properties getConfigServiceProperties() { Properties properties = new Properties(); properties.put(SERVER_ADDR, Objects.toString(this.serverAddr, "")); properties.put(ENCODE, Objects.toString(this.encode, "")); @@ -424,7 +430,6 @@ public ConfigService configServiceInstance() { properties.put(CONFIG_RETRY_TIME, Objects.toString(this.configRetryTime, "")); properties.put(ENABLE_REMOTE_SYNC_CONFIG, Objects.toString(this.enableRemoteSyncConfig, "")); - String endpoint = Objects.toString(this.endpoint, ""); if (endpoint.contains(":")) { int index = endpoint.indexOf(":"); @@ -434,14 +439,7 @@ public ConfigService configServiceInstance() { else { properties.put(ENDPOINT, endpoint); } - - try { - configService = NacosFactory.createConfigService(properties); - return configService; - } - catch (Exception e) { - log.error("create config service error!properties={},e=,", this, e); - return null; - } + return properties; } + } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java index 10a48cd8e0..6a7dc6ccd8 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java @@ -156,8 +156,13 @@ private void loadApplicationConfiguration( String fileExtension = properties.getFileExtension(); String nacosGroup = properties.getGroup(); + // load directly once by default + loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup, + fileExtension, true); + // load with suffix, which have a higher priority than the default loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); + // Loaded with profile, which have a higher priority than the suffix for (String profile : environment.getActiveProfiles()) { String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, @@ -168,22 +173,41 @@ private void loadApplicationConfiguration( private void loadNacosDataIfPresent(final CompositePropertySource composite, final String dataId, final String group, String fileExtension, boolean isRefreshable) { + if (null == dataId || dataId.trim().length() < 1) { + return; + } + if (null == group || group.trim().length() < 1) { + return; + } + NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group, + fileExtension, isRefreshable); + this.addFirstPropertySource(composite, propertySource, false); + } + + private NacosPropertySource loadNacosPropertySource(final String dataId, + final String group, String fileExtension, boolean isRefreshable) { if (NacosContextRefresher.getRefreshCount() != 0) { - NacosPropertySource ps; if (!isRefreshable) { - ps = NacosPropertySourceRepository.getNacosPropertySource(dataId); - } - else { - ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true); + return NacosPropertySourceRepository.getNacosPropertySource(dataId); } + } + return nacosPropertySourceBuilder.build(dataId, group, fileExtension, + isRefreshable); + } - composite.addFirstPropertySource(ps); + /** + * Add the nacos configuration to the first place and maybe ignore the empty + * configuration + */ + private void addFirstPropertySource(final CompositePropertySource composite, + NacosPropertySource nacosPropertySource, boolean ignoreEmpty) { + if (null == nacosPropertySource || null == composite) { + return; } - else { - NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, - fileExtension, isRefreshable); - composite.addFirstPropertySource(ps); + if (ignoreEmpty && nacosPropertySource.getSource().isEmpty()) { + return; } + composite.addFirstPropertySource(nacosPropertySource); } private static void checkDataIdFileExtension(String[] dataIdArray) { diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java index e3168c9124..cc41bd3635 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataParserHandler.java @@ -24,8 +24,6 @@ */ public class NacosDataParserHandler { - private static final NacosDataParserHandler HANDLER = new NacosDataParserHandler(); - private AbstractNacosDataParser parser; private NacosDataParserHandler() { @@ -68,7 +66,10 @@ private AbstractNacosDataParser createParser() { } public static NacosDataParserHandler getInstance() { - return HANDLER; + return ParserHandler.HANDLER; } + private static class ParserHandler { + private static final NacosDataParserHandler HANDLER = new NacosDataParserHandler(); + } } diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNoSuffixTest.java b/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNoSuffixTest.java new file mode 100644 index 0000000000..5edb7a9dd8 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/NacosConfigurationNoSuffixTest.java @@ -0,0 +1,271 @@ +/* + * Copyright (C) 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 + * + * http://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.nacos; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.api.support.MethodProxy; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.test.context.junit4.SpringRunner; + +import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator; +import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpoint; +import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration; +import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory; +import com.alibaba.nacos.client.config.NacosConfigService; + +/** + * @author zkz + */ + +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("javax.management.*") +@PowerMockRunnerDelegate(SpringRunner.class) +@PrepareForTest({ NacosConfigService.class }) +@SpringBootTest(classes = NacosConfigurationNoSuffixTest.TestConfig.class, properties = { + "spring.application.name=app-no-suffix", "spring.profiles.active=dev", + "spring.cloud.nacos.config.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.config.namespace=test-namespace", + "spring.cloud.nacos.config.encode=utf-8", + "spring.cloud.nacos.config.timeout=1000", + "spring.cloud.nacos.config.group=test-group", + "spring.cloud.nacos.config.name=test-no-suffix-name", + "spring.cloud.nacos.config.cluster-name=test-cluster", + "spring.cloud.nacos.config.contextPath=test-contextpath", + "spring.cloud.nacos.config.ext-config[0].data-id=ext-json-test.json", + "spring.cloud.nacos.config.ext-config[1].data-id=ext-common02.properties", + "spring.cloud.nacos.config.ext-config[1].group=GLOBAL_GROUP", + "spring.cloud.nacos.config.shared-dataids=shared-data1.properties,shared-data2.xml", + "spring.cloud.nacos.config.accessKey=test-accessKey", + "spring.cloud.nacos.config.secretKey=test-secretKey" }, webEnvironment = NONE) +public class NacosConfigurationNoSuffixTest { + + static { + + try { + + Method method = PowerMockito.method(NacosConfigService.class, "getConfig", + String.class, String.class, long.class); + MethodProxy.proxy(method, new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + + if ("app-no-suffix".equals(args[0]) && "test-group".equals(args[1])) { + return "test-no-suffix=value-no-suffix-1"; + } + if ("app-no-suffix.properties".equals(args[0]) + && "test-group".equals(args[1])) { + return "test-no-suffix=value-no-suffix-2"; + } + + if ("test-no-suffix-name".equals(args[0]) + && "test-group".equals(args[1])) { + return "test-no-suffix-assign=assign-value-no-suffix-111"; + } + if ("test-no-suffix-name.properties".equals(args[0]) + && "test-group".equals(args[1])) { + return "test-no-suffix-assign=assign-value-no-suffix-222"; + } + if ("test-no-suffix-name-dev.properties".equals(args[0]) + && "test-group".equals(args[1])) { + return "test-no-suffix-assign=assign-dev-value-no-suffix-333"; + } + + if ("ext-json-test.json".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "{\n" + " \"people\":{\n" + + " \"firstName\":\"Brett\",\n" + + " \"lastName\":\"McLaughlin\"\n" + " }\n" + + "}"; + } + + if ("ext-config-common02.properties".equals(args[0]) + && "GLOBAL_GROUP".equals(args[1])) { + return "global-ext-config=global-config-value-2"; + } + + if ("shared-data1.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "shared-name=shared-value-1"; + } + + if ("shared-data2.xml".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return " \n" + + " \n" + + " \n" + + " 开启服务 \n" + + " 初始化一下 \n" + + " \n" + " \n" + + " one\n" + + " \n" + + " three\n" + + " \n" + + " \n" + " \n" + + " 销毁一下 \n" + + " 关闭服务 \n" + + " \n" + " \n" + + " "; + } + + return ""; + } + }); + + } + catch (Exception ignore) { + ignore.printStackTrace(); + + } + } + + @Autowired + private NacosPropertySourceLocator locator; + + @Autowired + private NacosConfigProperties properties; + + @Autowired + private NacosRefreshHistory refreshHistory; + @Autowired + private Environment environment; + + @Test + public void contextLoads() throws Exception { + + assertNotNull("NacosPropertySourceLocator was not created", locator); + assertNotNull("NacosConfigProperties was not created", properties); + + checkoutNacosConfigServerAddr(); + checkoutNacosConfigNamespace(); + checkoutNacosConfigClusterName(); + checkoutNacosConfigAccessKey(); + checkoutNacosConfigSecrectKey(); + checkoutNacosConfigName(); + checkoutNacosConfigGroup(); + checkoutNacosConfigContextPath(); + checkoutNacosConfigFileExtension(); + checkoutNacosConfigTimeout(); + checkoutNacosConfigEncode(); + + checkoutEndpoint(); + checkEnvironmentProperties(); + } + + private void checkEnvironmentProperties() { + assertNull( + "The configuration of `spring.cloud.nacos.config.name` must be used first", + environment.getProperty("test-no-suffix")); + assertEquals( + "Priority of configuration is wrong , should be in this order : `profile->hasSuffix->noSuffix`", + "assign-dev-value-no-suffix-333", + environment.getProperty("test-no-suffix-assign")); + + } + + private void checkoutNacosConfigServerAddr() { + assertEquals("NacosConfigProperties server address is wrong", "127.0.0.1:8848", + properties.getServerAddr()); + } + + private void checkoutNacosConfigNamespace() { + assertEquals("NacosConfigProperties namespace is wrong", "test-namespace", + properties.getNamespace()); + } + + private void checkoutNacosConfigClusterName() { + assertEquals("NacosConfigProperties' cluster is wrong", "test-cluster", + properties.getClusterName()); + } + + private void checkoutNacosConfigAccessKey() { + assertEquals("NacosConfigProperties' is access key is wrong", "test-accessKey", + properties.getAccessKey()); + } + + private void checkoutNacosConfigSecrectKey() { + assertEquals("NacosConfigProperties' is secret key is wrong", "test-secretKey", + properties.getSecretKey()); + } + + private void checkoutNacosConfigContextPath() { + assertEquals("NacosConfigProperties' context path is wrong", "test-contextpath", + properties.getContextPath()); + } + + private void checkoutNacosConfigName() { + assertEquals("NacosConfigProperties' name is wrong", "test-no-suffix-name", + properties.getName()); + } + + private void checkoutNacosConfigGroup() { + assertEquals("NacosConfigProperties' group is wrong", "test-group", + properties.getGroup()); + } + + private void checkoutNacosConfigFileExtension() { + assertEquals("NacosConfigProperties' file extension is wrong", "properties", + properties.getFileExtension()); + } + + private void checkoutNacosConfigTimeout() { + assertEquals("NacosConfigProperties' timeout is wrong", 1000, + properties.getTimeout()); + } + + private void checkoutNacosConfigEncode() { + assertEquals("NacosConfigProperties' encode is wrong", "utf-8", + properties.getEncode()); + } + + private void checkoutEndpoint() throws Exception { + NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties, + refreshHistory); + Map map = nacosConfigEndpoint.invoke(); + assertEquals(map.get("NacosConfigProperties"), properties); + assertEquals(map.get("RefreshHistory"), refreshHistory.getRecords()); + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class, + NacosConfigAutoConfiguration.class, NacosConfigBootstrapConfiguration.class }) + public static class TestConfig { + } +} diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointTests.java index 1706f33832..86f2468a0c 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointTests.java @@ -63,16 +63,8 @@ public class NacosConfigEndpointTests { try { - Method method = PowerMockito.method(NacosConfigService.class, "getConfig", - String.class, String.class, long.class); - MethodProxy.proxy(method, (proxy, method1, args) -> { - - if ("test-name.properties".equals(args[0]) - && "DEFAULT_GROUP".equals(args[1])) { - return "user.name=hello\nuser.age=12"; - } - return ""; - }); + Method method = PowerMockito.method(NacosConfigService.class, "getServerStatus"); + MethodProxy.proxy(method, (proxy, method1, args) -> "UP"); } catch (Exception ignore) { diff --git a/spring-cloud-alibaba-nacos-discovery/pom.xml b/spring-cloud-alibaba-nacos-discovery/pom.xml index db9fed761f..f061ce2266 100644 --- a/spring-cloud-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosNamingManager.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosNamingManager.java index a164c3f82d..46968b52fa 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosNamingManager.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosNamingManager.java @@ -18,6 +18,8 @@ import com.alibaba.nacos.api.naming.NamingMaintainService; import com.alibaba.nacos.api.naming.NamingService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import java.util.Objects; @@ -27,6 +29,8 @@ */ public class NacosNamingManager { + private static final Logger log = LoggerFactory.getLogger(NacosNamingManager.class); + private static NamingService namingService = null; private static NamingMaintainService namingMaintainService = null; diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosServiceRegistry.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosServiceRegistry.java index 9e8406bcea..2e27a158ee 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosServiceRegistry.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/registry/NacosServiceRegistry.java @@ -64,7 +64,7 @@ public void register(Registration registration) { try { namingService.registerInstance(serviceId, group, instance); - log.info("nacos registry, {} {}:{} register finished", serviceId, + log.info("nacos registry, {} {} {}:{} register finished", group, serviceId, instance.getIp(), instance.getPort()); } catch (Exception e) { diff --git a/spring-cloud-alibaba-seata/pom.xml b/spring-cloud-alibaba-seata/pom.xml index c283204a1a..5f4a2802d1 100644 --- a/spring-cloud-alibaba-seata/pom.xml +++ b/spring-cloud-alibaba-seata/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-sentinel-datasource/pom.xml b/spring-cloud-alibaba-sentinel-datasource/pom.xml index 6df8f7b898..1b60e41d43 100644 --- a/spring-cloud-alibaba-sentinel-datasource/pom.xml +++ b/spring-cloud-alibaba-sentinel-datasource/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-sentinel-gateway/pom.xml b/spring-cloud-alibaba-sentinel-gateway/pom.xml index 8312f2f8fe..dedd7e7aa3 100644 --- a/spring-cloud-alibaba-sentinel-gateway/pom.xml +++ b/spring-cloud-alibaba-sentinel-gateway/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index 352cf953a7..b2ec4afc3a 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelBeanPostProcessor.java b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelBeanPostProcessor.java index ce94da20be..474c8bcd82 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelBeanPostProcessor.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelBeanPostProcessor.java @@ -29,8 +29,8 @@ import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.ApplicationContext; +import org.springframework.core.type.MethodMetadata; import org.springframework.core.type.StandardMethodMetadata; -import org.springframework.core.type.classreading.MethodMetadataReadingVisitor; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpResponse; @@ -167,19 +167,12 @@ else if (type.equals(SentinelConstants.FALLBACK_TYPE)) { private boolean checkSentinelProtect(RootBeanDefinition beanDefinition, Class beanType) { return beanType == RestTemplate.class - && (checkStandardMethodMetadata(beanDefinition) - || checkMethodMetadataReadingVisitor(beanDefinition)); - } - - private boolean checkStandardMethodMetadata(RootBeanDefinition beanDefinition) { - return beanDefinition.getSource() instanceof StandardMethodMetadata - && ((StandardMethodMetadata) beanDefinition.getSource()) - .isAnnotated(SentinelRestTemplate.class.getName()); + && checkMethodMetadataReadingVisitor(beanDefinition); } private boolean checkMethodMetadataReadingVisitor(RootBeanDefinition beanDefinition) { - return beanDefinition.getSource() instanceof MethodMetadataReadingVisitor - && ((MethodMetadataReadingVisitor) beanDefinition.getSource()) + return beanDefinition.getSource() instanceof MethodMetadata + && ((MethodMetadata) beanDefinition.getSource()) .isAnnotated(SentinelRestTemplate.class.getName()); } diff --git a/spring-cloud-alicloud-acm/pom.xml b/spring-cloud-alicloud-acm/pom.xml index f771a74a39..58c4ddf08a 100644 --- a/spring-cloud-alicloud-acm/pom.xml +++ b/spring-cloud-alicloud-acm/pom.xml @@ -6,7 +6,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-alicloud-acm diff --git a/spring-cloud-alicloud-ans/pom.xml b/spring-cloud-alicloud-ans/pom.xml index 16876a2210..e7962dea14 100644 --- a/spring-cloud-alicloud-ans/pom.xml +++ b/spring-cloud-alicloud-ans/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alicloud-context/pom.xml b/spring-cloud-alicloud-context/pom.xml index 6aff6346a8..71f281b36e 100644 --- a/spring-cloud-alicloud-context/pom.xml +++ b/spring-cloud-alicloud-context/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alicloud-context/src/test/java/com/alibaba/alicloud/context/ans/AnsPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/com/alibaba/alicloud/context/ans/AnsPropertiesTests.java index 225acc7b68..93242733b5 100644 --- a/spring-cloud-alicloud-context/src/test/java/com/alibaba/alicloud/context/ans/AnsPropertiesTests.java +++ b/spring-cloud-alicloud-context/src/test/java/com/alibaba/alicloud/context/ans/AnsPropertiesTests.java @@ -121,7 +121,7 @@ public void testConfigurationValuesAreCorrectlyLoaded2() { }); } - @Test + //@Test public void testConfigurationValuesAreCorrectlyLoaded3() throws SocketException { NetworkInterface networkInterface = PowerMockito.mock(NetworkInterface.class); Vector inetAddressList = new Vector<>(); diff --git a/spring-cloud-alicloud-oss/pom.xml b/spring-cloud-alicloud-oss/pom.xml index 85318849eb..12dfb67c24 100644 --- a/spring-cloud-alicloud-oss/pom.xml +++ b/spring-cloud-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alicloud-schedulerx/pom.xml b/spring-cloud-alicloud-schedulerx/pom.xml index dac0e7c58f..29039126c6 100644 --- a/spring-cloud-alicloud-schedulerx/pom.xml +++ b/spring-cloud-alicloud-schedulerx/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 spring-cloud-alicloud-schedulerx diff --git a/spring-cloud-alicloud-sms/pom.xml b/spring-cloud-alicloud-sms/pom.xml index de32ec3bb6..8d63957f90 100644 --- a/spring-cloud-alicloud-sms/pom.xml +++ b/spring-cloud-alicloud-sms/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-circuitbreaker-sentinel/pom.xml b/spring-cloud-circuitbreaker-sentinel/pom.xml new file mode 100644 index 0000000000..2896706d8a --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/pom.xml @@ -0,0 +1,57 @@ + + + + + com.alibaba.cloud + spring-cloud-alibaba + 2.2.0.BUILD-SNAPSHOT + + 4.0.0 + + spring-cloud-circuitbreaker-sentinel + Spring Cloud Circuit Breaker Sentinel + + + + + org.springframework.cloud + spring-cloud-commons + + + com.alibaba.csp + sentinel-core + + + com.alibaba.csp + sentinel-reactor-adapter + + + org.springframework.boot + spring-boot-starter-web + true + + + io.projectreactor + reactor-core + true + + + org.springframework.boot + spring-boot-starter-webflux + test + + + org.springframework.boot + spring-boot-starter-test + test + + + io.projectreactor + reactor-test + test + + + + + diff --git a/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreaker.java b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreaker.java new file mode 100644 index 0000000000..e2cd8bf57b --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreaker.java @@ -0,0 +1,104 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Function; + +import com.alibaba.csp.sentinel.EntryType; +import com.alibaba.csp.sentinel.adapter.reactor.EntryConfig; +import com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreaker; +import org.springframework.util.Assert; + +/** + * Sentinel implementation of {@link ReactiveCircuitBreaker}. + * + * @author Eric Zhao + */ +public class ReactiveSentinelCircuitBreaker implements ReactiveCircuitBreaker { + + private final String resourceName; + + private final EntryType entryType; + + private final List rules; + + public ReactiveSentinelCircuitBreaker(String resourceName, EntryType entryType, + List rules) { + Assert.hasText(resourceName, "resourceName cannot be blank"); + Assert.notNull(rules, "rules should not be null"); + this.resourceName = resourceName; + this.entryType = entryType; + this.rules = Collections.unmodifiableList(rules); + + applyToSentinelRuleManager(); + } + + public ReactiveSentinelCircuitBreaker(String resourceName, List rules) { + this(resourceName, EntryType.OUT, rules); + } + + public ReactiveSentinelCircuitBreaker(String resourceName) { + this(resourceName, EntryType.OUT, Collections.emptyList()); + } + + private void applyToSentinelRuleManager() { + if (this.rules == null || this.rules.isEmpty()) { + return; + } + Set ruleSet = new HashSet<>(DegradeRuleManager.getRules()); + for (DegradeRule rule : this.rules) { + if (rule == null) { + continue; + } + rule.setResource(resourceName); + ruleSet.add(rule); + } + DegradeRuleManager.loadRules(new ArrayList<>(ruleSet)); + } + + @Override + public Mono run(Mono toRun, Function> fallback) { + Mono toReturn = toRun.transform(new SentinelReactorTransformer<>( + new EntryConfig(resourceName, entryType))); + if (fallback != null) { + toReturn = toReturn.onErrorResume(fallback); + } + return toReturn; + } + + @Override + public Flux run(Flux toRun, Function> fallback) { + Flux toReturn = toRun.transform(new SentinelReactorTransformer<>( + new EntryConfig(resourceName, entryType))); + if (fallback != null) { + toReturn = toReturn.onErrorResume(fallback); + } + return toReturn; + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerAutoConfiguration.java b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerAutoConfiguration.java new file mode 100644 index 0000000000..e4592e0b63 --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerAutoConfiguration.java @@ -0,0 +1,64 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.cloud.client.circuitbreaker.Customizer; +import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author Eric Zhao + */ +@Configuration +@ConditionalOnClass(name = { "reactor.core.publisher.Mono", + "reactor.core.publisher.Flux" }) +public class ReactiveSentinelCircuitBreakerAutoConfiguration { + + @Bean + @ConditionalOnMissingBean(ReactiveCircuitBreakerFactory.class) + public ReactiveCircuitBreakerFactory reactiveSentinelCircuitBreakerFactory() { + return new ReactiveSentinelCircuitBreakerFactory(); + } + + @Configuration + @ConditionalOnClass(name = { "reactor.core.publisher.Mono", + "reactor.core.publisher.Flux" }) + public static class ReactiveSentinelCustomizerConfiguration { + + @Autowired(required = false) + private List> customizers = new ArrayList<>(); + + @Autowired(required = false) + private ReactiveSentinelCircuitBreakerFactory factory; + + @PostConstruct + public void init() { + customizers.forEach(customizer -> customizer.customize(factory)); + } + + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerFactory.java b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerFactory.java new file mode 100644 index 0000000000..5ebb97b3f0 --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerFactory.java @@ -0,0 +1,58 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.ArrayList; +import java.util.function.Function; + +import com.alibaba.cloud.circuitbreaker.sentinel.SentinelConfigBuilder.SentinelCircuitBreakerConfiguration; +import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreaker; +import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory; +import org.springframework.util.Assert; + +/** + * Factory for {@link ReactiveSentinelCircuitBreaker}. + * + * @author Eric Zhao + */ +public class ReactiveSentinelCircuitBreakerFactory extends + ReactiveCircuitBreakerFactory { + + private Function defaultConfiguration = id -> new SentinelConfigBuilder() + .resourceName(id).rules(new ArrayList<>()).build(); + + @Override + public ReactiveCircuitBreaker create(String id) { + Assert.hasText(id, "A CircuitBreaker must have an id."); + SentinelConfigBuilder.SentinelCircuitBreakerConfiguration conf = getConfigurations() + .computeIfAbsent(id, defaultConfiguration); + return new ReactiveSentinelCircuitBreaker(id, conf.getEntryType(), + conf.getRules()); + } + + @Override + protected SentinelConfigBuilder configBuilder(String id) { + return new SentinelConfigBuilder(id); + } + + @Override + public void configureDefault( + Function defaultConfiguration) { + this.defaultConfiguration = defaultConfiguration; + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreaker.java b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreaker.java new file mode 100644 index 0000000000..acbc7db38e --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreaker.java @@ -0,0 +1,114 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Supplier; + +import com.alibaba.csp.sentinel.Entry; +import com.alibaba.csp.sentinel.EntryType; +import com.alibaba.csp.sentinel.SphU; +import com.alibaba.csp.sentinel.Tracer; +import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; + +import org.springframework.cloud.client.circuitbreaker.CircuitBreaker; +import org.springframework.util.Assert; + +/** + * Sentinel implementation of {@link CircuitBreaker}. + * + * @author Eric Zhao + */ +public class SentinelCircuitBreaker implements CircuitBreaker { + + private final String resourceName; + + private final EntryType entryType; + + private final List rules; + + public SentinelCircuitBreaker(String resourceName, EntryType entryType, + List rules) { + Assert.hasText(resourceName, "resourceName cannot be blank"); + Assert.notNull(rules, "rules should not be null"); + this.resourceName = resourceName; + this.entryType = entryType; + this.rules = Collections.unmodifiableList(rules); + + applyToSentinelRuleManager(); + } + + public SentinelCircuitBreaker(String resourceName, List rules) { + this(resourceName, EntryType.OUT, rules); + } + + public SentinelCircuitBreaker(String resourceName) { + this(resourceName, EntryType.OUT, Collections.emptyList()); + } + + private void applyToSentinelRuleManager() { + if (this.rules == null || this.rules.isEmpty()) { + return; + } + Set ruleSet = new HashSet<>(DegradeRuleManager.getRules()); + for (DegradeRule rule : this.rules) { + if (rule == null) { + continue; + } + rule.setResource(resourceName); + ruleSet.add(rule); + } + DegradeRuleManager.loadRules(new ArrayList<>(ruleSet)); + } + + @Override + public T run(Supplier toRun, Function fallback) { + Entry entry = null; + try { + entry = SphU.entry(resourceName, entryType); + // If the SphU.entry() does not throw `BlockException`, it means that the + // request can pass. + return toRun.get(); + } + catch (BlockException ex) { + // SphU.entry() may throw BlockException which indicates that + // the request was rejected (flow control or circuit breaking triggered). + // So it should not be counted as the business exception. + return fallback.apply(ex); + } + catch (Exception ex) { + // For other kinds of exceptions, we'll trace the exception count via + // Tracer.trace(ex). + Tracer.trace(ex); + return fallback.apply(ex); + } + finally { + // Guarantee the invocation has been completed. + if (entry != null) { + entry.exit(); + } + } + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerAutoConfiguration.java b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerAutoConfiguration.java new file mode 100644 index 0000000000..e8a96b9398 --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerAutoConfiguration.java @@ -0,0 +1,65 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.PostConstruct; + +import com.alibaba.csp.sentinel.SphU; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; +import org.springframework.cloud.client.circuitbreaker.Customizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Auto configuration for {@link SentinelCircuitBreaker}. + * + * @author Eric Zhao + */ +@Configuration +@ConditionalOnClass({ SphU.class }) +public class SentinelCircuitBreakerAutoConfiguration { + + @Bean + @ConditionalOnMissingBean(CircuitBreakerFactory.class) + public CircuitBreakerFactory sentinelCircuitBreakerFactory() { + return new SentinelCircuitBreakerFactory(); + } + + @Configuration + public static class SentinelCustomizerConfiguration { + + @Autowired(required = false) + private List> customizers = new ArrayList<>(); + + @Autowired(required = false) + private SentinelCircuitBreakerFactory factory; + + @PostConstruct + public void init() { + customizers.forEach(customizer -> customizer.customize(factory)); + } + + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerFactory.java b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerFactory.java new file mode 100644 index 0000000000..6b8b665e22 --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerFactory.java @@ -0,0 +1,57 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.ArrayList; +import java.util.function.Function; + +import com.alibaba.csp.sentinel.EntryType; + +import com.alibaba.cloud.circuitbreaker.sentinel.SentinelConfigBuilder.SentinelCircuitBreakerConfiguration; +import org.springframework.cloud.client.circuitbreaker.CircuitBreaker; +import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; +import org.springframework.util.Assert; + +/** + * @author Eric Zhao + */ +public class SentinelCircuitBreakerFactory extends + CircuitBreakerFactory { + + private Function defaultConfiguration = id -> new SentinelConfigBuilder() + .resourceName(id).entryType(EntryType.OUT).rules(new ArrayList<>()).build(); + + @Override + public CircuitBreaker create(String id) { + Assert.hasText(id, "A CircuitBreaker must have an id."); + SentinelConfigBuilder.SentinelCircuitBreakerConfiguration conf = getConfigurations() + .computeIfAbsent(id, defaultConfiguration); + return new SentinelCircuitBreaker(id, conf.getEntryType(), conf.getRules()); + } + + @Override + protected SentinelConfigBuilder configBuilder(String id) { + return new SentinelConfigBuilder(id); + } + + @Override + public void configureDefault( + Function defaultConfiguration) { + this.defaultConfiguration = defaultConfiguration; + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelConfigBuilder.java b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelConfigBuilder.java new file mode 100644 index 0000000000..35de02308a --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/main/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelConfigBuilder.java @@ -0,0 +1,112 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import com.alibaba.csp.sentinel.EntryType; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; + +import org.springframework.cloud.client.circuitbreaker.ConfigBuilder; +import org.springframework.util.Assert; + +/** + * @author Eric Zhao + */ +public class SentinelConfigBuilder implements + ConfigBuilder { + + private String resourceName; + + private EntryType entryType; + + private List rules; + + public SentinelConfigBuilder() { + } + + public SentinelConfigBuilder(String resourceName) { + this.resourceName = resourceName; + } + + public SentinelConfigBuilder resourceName(String resourceName) { + this.resourceName = resourceName; + return this; + } + + public SentinelConfigBuilder entryType(EntryType entryType) { + this.entryType = entryType; + return this; + } + + public SentinelConfigBuilder rules(List rules) { + this.rules = rules; + return this; + } + + @Override + public SentinelCircuitBreakerConfiguration build() { + Assert.hasText(resourceName, "resourceName cannot be empty"); + List rules = Optional.ofNullable(this.rules) + .orElse(new ArrayList<>()); + + EntryType entryType = Optional.ofNullable(this.entryType).orElse(EntryType.OUT); + return new SentinelCircuitBreakerConfiguration() + .setResourceName(this.resourceName).setEntryType(entryType) + .setRules(rules); + } + + public static class SentinelCircuitBreakerConfiguration { + + private String resourceName; + + private EntryType entryType; + + private List rules; + + public String getResourceName() { + return resourceName; + } + + public SentinelCircuitBreakerConfiguration setResourceName(String resourceName) { + this.resourceName = resourceName; + return this; + } + + public EntryType getEntryType() { + return entryType; + } + + public SentinelCircuitBreakerConfiguration setEntryType(EntryType entryType) { + this.entryType = entryType; + return this; + } + + public List getRules() { + return rules; + } + + public SentinelCircuitBreakerConfiguration setRules(List rules) { + this.rules = rules; + return this; + } + + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/main/resources/META-INF/spring.factories b/spring-cloud-circuitbreaker-sentinel/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000..1fdc676936 --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.alibaba.cloud.circuitbreaker.sentinel.SentinelCircuitBreakerAutoConfiguration,\ +com.alibaba.cloud.circuitbreaker.sentinel.ReactiveSentinelCircuitBreakerAutoConfiguration \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerIntegrationTest.java b/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerIntegrationTest.java new file mode 100644 index 0000000000..9f90a71c49 --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerIntegrationTest.java @@ -0,0 +1,208 @@ +/* + * 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.circuitbreaker.sentinel; + +import java.time.Duration; +import java.util.Collections; + +import com.alibaba.csp.sentinel.slots.block.RuleConstant; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.cloud.client.circuitbreaker.Customizer; +import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.reactive.function.client.WebClient; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * @author Ryan Baxter + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ReactiveSentinelCircuitBreakerIntegrationTest.Application.class, properties = { + "spring.cloud.discovery.client.health-indicator.enabled=false" }) +@DirtiesContext +public class ReactiveSentinelCircuitBreakerIntegrationTest { + + @LocalServerPort + private int port = 0; + + @Autowired + private ReactiveSentinelCircuitBreakerIntegrationTest.Application.DemoControllerService service; + + @Before + public void setup() { + service.setPort(port); + } + + @Test + public void test() throws Exception { + StepVerifier.create(service.normal()).expectNext("normal").verifyComplete(); + StepVerifier.create(service.slow()).expectNext("slow").verifyComplete(); + StepVerifier.create(service.slow()).expectNext("slow").verifyComplete(); + StepVerifier.create(service.slow()).expectNext("slow").verifyComplete(); + StepVerifier.create(service.slow()).expectNext("slow").verifyComplete(); + StepVerifier.create(service.slow()).expectNext("slow").verifyComplete(); + + // Then in the next 5s, the fallback method should be called. + for (int i = 0; i < 5; i++) { + StepVerifier.create(service.slow()).expectNext("fallback").verifyComplete(); + Thread.sleep(1000); + } + + // Recovered. + StepVerifier.create(service.slow()).expectNext("slow").verifyComplete(); + + StepVerifier.create(service.normalFlux()).expectNext("normalflux") + .verifyComplete(); + StepVerifier.create(service.slowFlux()).expectNext("slowflux").verifyComplete(); + StepVerifier.create(service.slowFlux()).expectNext("slowflux").verifyComplete(); + StepVerifier.create(service.slowFlux()).expectNext("slowflux").verifyComplete(); + StepVerifier.create(service.slowFlux()).expectNext("slowflux").verifyComplete(); + StepVerifier.create(service.slowFlux()).expectNext("slowflux").verifyComplete(); + // Then in the next 5s, the fallback method should be called. + for (int i = 0; i < 5; i++) { + StepVerifier.create(service.slowFlux()).expectNext("flux_fallback") + .verifyComplete(); + Thread.sleep(1000); + } + + // Recovered. + StepVerifier.create(service.slowFlux()).expectNext("slowflux").verifyComplete(); + } + + @Configuration + @EnableAutoConfiguration + @RestController + protected static class Application { + + @GetMapping("/slow") + public Mono slow() { + return Mono.just("slow").delayElement(Duration.ofMillis(500)); + } + + @GetMapping("/normal") + public Mono normal() { + return Mono.just("normal"); + } + + @GetMapping("/slow_flux") + public Flux slowFlux() { + return Flux.just("slow", "flux").delayElements(Duration.ofMillis(500)); + } + + @GetMapping("normal_flux") + public Flux normalFlux() { + return Flux.just("normal", "flux"); + } + + @Bean + public Customizer slowCustomizer() { + return factory -> { + factory.configure(builder -> builder + .rules(Collections.singletonList(new DegradeRule("slow_mono") + .setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100) + .setTimeWindow(5))), + "slow_mono"); + factory.configure(builder -> builder + .rules(Collections.singletonList(new DegradeRule("slow_flux") + .setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100) + .setTimeWindow(5))), + "slow_flux"); + factory.configureDefault(id -> new SentinelConfigBuilder() + .resourceName(id) + .rules(Collections.singletonList(new DegradeRule(id) + .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) + .setCount(0.5).setTimeWindow(10))) + .build()); + }; + } + + @Service + public static class DemoControllerService { + + private int port = 0; + + private ReactiveCircuitBreakerFactory cbFactory; + + DemoControllerService(ReactiveCircuitBreakerFactory cbFactory) { + this.cbFactory = cbFactory; + } + + public Mono slow() { + return WebClient.builder().baseUrl("http://localhost:" + port).build() + .get().uri("/slow").retrieve().bodyToMono(String.class) + .transform(it -> cbFactory.create("slow_mono").run(it, t -> { + t.printStackTrace(); + return Mono.just("fallback"); + })); + } + + public Mono normal() { + return WebClient.builder().baseUrl("http://localhost:" + port).build() + .get().uri("/normal").retrieve().bodyToMono(String.class) + .transform(it -> cbFactory.create("normal_mono").run(it, t -> { + t.printStackTrace(); + return Mono.just("fallback"); + })); + } + + public Flux slowFlux() { + return WebClient.builder().baseUrl("http://localhost:" + port).build() + .get().uri("/slow_flux").retrieve() + .bodyToFlux(new ParameterizedTypeReference() { + }).transform(it -> cbFactory.create("slow_flux").run(it, t -> { + t.printStackTrace(); + return Flux.just("flux_fallback"); + })); + } + + public Flux normalFlux() { + return WebClient.builder().baseUrl("http://localhost:" + port).build() + .get().uri("/normal_flux").retrieve().bodyToFlux(String.class) + .transform(it -> cbFactory.create("normal_flux").run(it, t -> { + t.printStackTrace(); + return Flux.just("flux_fallback"); + })); + } + + public void setPort(int port) { + this.port = port; + } + + } + + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerTest.java b/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerTest.java new file mode 100644 index 0000000000..63a6506452 --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/ReactiveSentinelCircuitBreakerTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.Arrays; +import java.util.Collections; + +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; +import org.junit.Test; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreaker; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Eric Zhao + */ +public class ReactiveSentinelCircuitBreakerTest { + + @Test + public void testCreateWithNullRule() { + String id = "testCreateReactiveCbWithNullRule"; + ReactiveSentinelCircuitBreaker cb = new ReactiveSentinelCircuitBreaker(id, + Collections.singletonList(null)); + assertThat(Mono.just("foobar").transform(it -> cb.run(it)).block()) + .isEqualTo("foobar"); + assertThat(DegradeRuleManager.hasConfig(id)).isFalse(); + } + + @Test + public void runMono() { + ReactiveCircuitBreaker cb = new ReactiveSentinelCircuitBreakerFactory() + .create("foo"); + assertThat(Mono.just("foobar").transform(it -> cb.run(it)).block()) + .isEqualTo("foobar"); + } + + @Test + public void runMonoWithFallback() { + ReactiveCircuitBreaker cb = new ReactiveSentinelCircuitBreakerFactory() + .create("foo"); + assertThat(Mono.error(new RuntimeException("boom")) + .transform(it -> cb.run(it, t -> Mono.just("fallback"))).block()) + .isEqualTo("fallback"); + } + + @Test + public void runFlux() { + ReactiveCircuitBreaker cb = new ReactiveSentinelCircuitBreakerFactory() + .create("foo"); + assertThat(Flux.just("foobar", "hello world").transform(it -> cb.run(it)) + .collectList().block()).isEqualTo(Arrays.asList("foobar", "hello world")); + } + + @Test + public void runFluxWithFallback() { + ReactiveCircuitBreaker cb = new ReactiveSentinelCircuitBreakerFactory() + .create("foo"); + assertThat(Flux.error(new RuntimeException("boom")) + .transform(it -> cb.run(it, t -> Flux.just("fallback"))).collectList() + .block()).isEqualTo(Arrays.asList("fallback")); + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerIntegrationTest.java b/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerIntegrationTest.java new file mode 100644 index 0000000000..34c51ea52d --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerIntegrationTest.java @@ -0,0 +1,154 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import com.alibaba.csp.sentinel.slots.block.RuleConstant; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; +import org.springframework.cloud.client.circuitbreaker.Customizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Service; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * @author Eric Zhao + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = SentinelCircuitBreakerIntegrationTest.Application.class, properties = { + "spring.cloud.discovery.client.health-indicator.enabled=false" }) +@DirtiesContext +public class SentinelCircuitBreakerIntegrationTest { + + @Autowired + private Application.DemoControllerService service; + + @Test + public void testSlow() throws Exception { + // The first 5 requests should pass. + assertThat(service.slow()).isEqualTo("slow"); + assertThat(service.slow()).isEqualTo("slow"); + assertThat(service.slow()).isEqualTo("slow"); + assertThat(service.slow()).isEqualTo("slow"); + assertThat(service.slow()).isEqualTo("slow"); + + // Then in the next 10s, the fallback method should be called. + for (int i = 0; i < 10; i++) { + assertThat(service.slow()).isEqualTo("fallback"); + Thread.sleep(1000); + } + + // Recovered. + assertThat(service.slow()).isEqualTo("slow"); + } + + @Test + public void testNormal() { + assertThat(service.normal()).isEqualTo("normal"); + } + + @Before + public void setUp() { + DegradeRuleManager.loadRules(new ArrayList<>()); + } + + @Before + public void tearDown() { + DegradeRuleManager.loadRules(new ArrayList<>()); + } + + @Configuration + @EnableAutoConfiguration + @RestController + protected static class Application { + + @GetMapping("/slow") + public String slow() throws InterruptedException { + Thread.sleep(500); + return "slow"; + } + + @GetMapping("/normal") + public String normal() { + return "normal"; + } + + @Bean + public Customizer slowCustomizer() { + String slowId = "slow"; + List rules = Collections.singletonList( + new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT) + .setCount(100).setTimeWindow(10)); + return factory -> { + factory.configure(builder -> builder.rules(rules), slowId); + factory.configureDefault(id -> new SentinelConfigBuilder() + .resourceName(id) + .rules(Collections.singletonList(new DegradeRule(id) + .setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT) + .setCount(0.5).setTimeWindow(10))) + .build()); + }; + } + + @Service + public static class DemoControllerService { + + private TestRestTemplate rest; + + private CircuitBreakerFactory cbFactory; + + DemoControllerService(TestRestTemplate rest, + CircuitBreakerFactory cbFactory) { + this.rest = rest; + this.cbFactory = cbFactory; + } + + public String slow() { + return cbFactory.create("slow").run( + () -> rest.getForObject("/slow", String.class), t -> "fallback"); + } + + public String normal() { + return cbFactory.create("normal").run( + () -> rest.getForObject("/normal", String.class), + t -> "fallback"); + } + + } + + } + +} \ No newline at end of file diff --git a/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerTest.java b/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerTest.java new file mode 100644 index 0000000000..8dba3efd04 --- /dev/null +++ b/spring-cloud-circuitbreaker-sentinel/src/test/java/com/alibaba/cloud/circuitbreaker/sentinel/SentinelCircuitBreakerTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2013-2019 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.circuitbreaker.sentinel; + +import java.util.ArrayList; +import java.util.Collections; + +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; + +import org.junit.After; +import org.junit.Test; + +import org.springframework.cloud.client.circuitbreaker.CircuitBreaker; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Eric Zhao + */ +public class SentinelCircuitBreakerTest { + + @After + public void tearDown() { + // Clear the rules. + DegradeRuleManager.loadRules(new ArrayList<>()); + } + + @Test + public void testCreateDirectlyThenRun() { + // Create a circuit breaker without any circuit breaking rules. + CircuitBreaker cb = new SentinelCircuitBreaker( + "testSentinelCreateDirectlyThenRunA"); + assertThat(cb.run(() -> "Sentinel")).isEqualTo("Sentinel"); + assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunA")) + .isFalse(); + + CircuitBreaker cb2 = new SentinelCircuitBreaker( + "testSentinelCreateDirectlyThenRunB", + Collections.singletonList( + new DegradeRule("testSentinelCreateDirectlyThenRunB") + .setCount(100).setTimeWindow(10))); + assertThat(cb2.run(() -> "Sentinel")).isEqualTo("Sentinel"); + assertThat(DegradeRuleManager.hasConfig("testSentinelCreateDirectlyThenRunB")) + .isTrue(); + } + + @Test + public void testCreateWithNullRule() { + String id = "testCreateCbWithNullRule"; + CircuitBreaker cb = new SentinelCircuitBreaker(id, + Collections.singletonList(null)); + assertThat(cb.run(() -> "Sentinel")).isEqualTo("Sentinel"); + assertThat(DegradeRuleManager.hasConfig(id)).isFalse(); + } + + @Test + public void testCreateFromFactoryThenRun() { + CircuitBreaker cb = new SentinelCircuitBreakerFactory().create("testSentinelRun"); + assertThat(cb.run(() -> "foobar")).isEqualTo("foobar"); + } + + @Test + public void testRunWithFallback() { + CircuitBreaker cb = new SentinelCircuitBreakerFactory() + .create("testSentinelRunWithFallback"); + assertThat(cb. run(() -> { + throw new RuntimeException("boom"); + }, t -> "fallback")).isEqualTo("fallback"); + } + +} \ No newline at end of file diff --git a/spring-cloud-starter-alibaba/pom.xml b/spring-cloud-starter-alibaba/pom.xml index 9cfb51330c..47f3f65eec 100644 --- a/spring-cloud-starter-alibaba/pom.xml +++ b/spring-cloud-starter-alibaba/pom.xml @@ -4,7 +4,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml spring-cloud-starter-alibaba diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config-server/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config-server/pom.xml index f601344666..fd731f8399 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config-server/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config-server/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alibaba-nacos-config-server Spring Cloud Starter Alibaba Nacos Config Server diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml index 0f9ca32d0d..af2293daf1 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alibaba-nacos-config Spring Cloud Starter Alibaba Nacos Config diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml index 9a4c6d724d..bca5944a6e 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alibaba-nacos-discovery Spring Cloud Starter Alibaba Nacos Discovery diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-seata/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-seata/pom.xml index 64c25d06ae..4e98871a43 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-seata/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-seata/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alibaba-seata Spring Cloud Starter Alibaba Seata diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml index db1ec624cf..4a8c488bf3 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alibaba-sentinel Spring Cloud Starter Alibaba Sentinel diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/pom.xml index efd7a14f95..cade314ea0 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-dubbo/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-dubbo/pom.xml index 28511ce414..042cd58490 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-dubbo/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-dubbo/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-stream-rocketmq/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-stream-rocketmq/pom.xml index c8eddc6134..61dac10463 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-stream-rocketmq/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-stream-rocketmq/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml spring-cloud-starter-stream-rocketmq diff --git a/spring-cloud-starter-alicloud/pom.xml b/spring-cloud-starter-alicloud/pom.xml index 4ee2d7be12..ba0d4985c3 100644 --- a/spring-cloud-starter-alicloud/pom.xml +++ b/spring-cloud-starter-alicloud/pom.xml @@ -4,7 +4,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alicloud pom diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml index 2417d872ca..dfda39d6fe 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alicloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alicloud-acm Spring Cloud Starter Alibaba Cloud ACM diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml index cbbce15187..78aa43bcaf 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alicloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alicloud-ans diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml index 2d8ef36052..f398626dea 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alicloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alicloud-oss Spring Cloud Starter Alibaba Cloud OSS diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-schedulerx/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-schedulerx/pom.xml index a9fbdf9c5c..8576fe376a 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-schedulerx/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-schedulerx/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alicloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alicloud-schedulerx Spring Cloud Starter Alibaba Cloud SchedulerX diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-sms/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-sms/pom.xml index 226977da75..3a64a94837 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-sms/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-sms/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-starter-alicloud - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT spring-cloud-starter-alicloud-sms Spring Cloud Starter Alibaba Cloud SMS diff --git a/spring-cloud-stream-binder-rocketmq/pom.xml b/spring-cloud-stream-binder-rocketmq/pom.xml index 9b09fc4ce4..4023575986 100644 --- a/spring-cloud-stream-binder-rocketmq/pom.xml +++ b/spring-cloud-stream-binder-rocketmq/pom.xml @@ -5,7 +5,7 @@ com.alibaba.cloud spring-cloud-alibaba - 2.1.1.BUILD-SNAPSHOT + 2.2.0.BUILD-SNAPSHOT ../pom.xml 4.0.0 diff --git a/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/integration/RocketMQMessageHandler.java b/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/integration/RocketMQMessageHandler.java index ec5c4c6e40..000754bfab 100644 --- a/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/integration/RocketMQMessageHandler.java +++ b/spring-cloud-stream-binder-rocketmq/src/main/java/com/alibaba/cloud/stream/binder/rocketmq/integration/RocketMQMessageHandler.java @@ -151,8 +151,8 @@ public boolean isRunning() { } @Override - protected void handleMessageInternal(org.springframework.messaging.Message message) - throws Exception { + protected void handleMessageInternal( + org.springframework.messaging.Message message) { try { // issue 737 fix Map jsonHeaders = headerMapper