diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml
index 564672510..a6df08172 100644
--- a/spring-cloud-alibaba-dependencies/pom.xml
+++ b/spring-cloud-alibaba-dependencies/pom.xml
@@ -18,7 +18,7 @@
Spring Cloud Alibaba Dependencies
- 1.6.3
+ 1.7.1
3.1.0
0.9.0
1.1.4
@@ -198,6 +198,11 @@
sentinel-api-gateway-adapter-common
${sentinel.version}
+
+ com.alibaba.csp
+ sentinel-spring-webmvc-adapter
+ ${sentinel.version}
+
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/Application.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/Application.java
index 35cec8c38..30ef8884b 100644
--- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/Application.java
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/com/alibaba/cloud/examples/Application.java
@@ -18,6 +18,7 @@ package com.alibaba.cloud.examples;
import java.io.IOException;
import java.io.StringReader;
+import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
@@ -30,7 +31,9 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
+import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -45,6 +48,52 @@ public class Application {
SpringApplication.run(Application.class, args);
}
+ @Bean
+ public UserConfig userConfig() {
+ return new UserConfig();
+ }
+
+}
+
+@ConfigurationProperties(prefix = "user")
+class UserConfig {
+
+ private int age;
+
+ private String name;
+
+ private Map map;
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Map getMap() {
+ return map;
+ }
+
+ public void setMap(Map map) {
+ this.map = map;
+ }
+
+ @Override
+ public String toString() {
+ return "UserConfig{" + "age=" + age + ", name='" + name + '\'' + ", map=" + map
+ + '}';
+ }
+
}
@Component
@@ -74,7 +123,7 @@ class SampleRunner implements ApplicationRunner {
*
* user.name=Nacos user.age=25
* @param configInfo latest config data for specific dataId in Nacos
- * server
+ * server
*/
@Override
public void receiveConfigInfo(String configInfo) {
@@ -101,19 +150,27 @@ class SampleRunner implements ApplicationRunner {
@RefreshScope
class SampleController {
+ @Autowired
+ UserConfig userConfig;
+
+ @Autowired
+ private NacosConfigManager nacosConfigManager;
+
@Value("${user.name}")
String userName;
@Value("${user.age:25}")
Integer age;
- @Autowired
- private NacosConfigManager nacosConfigManager;
-
@RequestMapping("/user")
public String simple() {
- return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!"
- + nacosConfigManager.getConfigService();
+ return "Hello Nacos Config!" + "Hello " + userName + " " + age + " [UserConfig]: "
+ + userConfig + "!" + nacosConfigManager.getConfigService();
+ }
+
+ @RequestMapping("/bool")
+ public boolean bool() {
+ return (Boolean) (userConfig.getMap().get("2"));
}
}
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties
index 2fc0eab44..71b87e610 100644
--- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties
@@ -13,6 +13,7 @@ spring.cloud.nacos.config.shared-configs[2]= common222.properties
spring.cloud.nacos.config.extension-configs[0].data-id= extension1.properties
spring.cloud.nacos.config.extension-configs[0].refresh= true
spring.cloud.nacos.config.extension-configs[1]= extension2.properties
+spring.cloud.nacos.config.extension-configs[2].data-id= extension3.json
#spring.cloud.nacos.config.refresh-enabled=true
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/pom.xml
new file mode 100644
index 000000000..dc211efeb
--- /dev/null
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/pom.xml
@@ -0,0 +1,74 @@
+
+
+
+
+ com.alibaba.cloud
+ nacos-discovery-example
+ 2.2.0.BUILD-SNAPSHOT
+
+ 4.0.0
+
+
+ nacos-discovery-consumer-sclb-example
+ jar
+ Example demonstrating how to use nacos discovery
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
+
+ org.springframework.cloud
+ spring-cloud-starter-netflix-ribbon
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-loadbalancer
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-openfeign
+
+
+ org.springframework.cloud
+ spring-cloud-starter-netflix-ribbon
+
+
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ org.apache.maven.plugins
+ maven-deploy-plugin
+ ${maven-deploy-plugin.version}
+
+ true
+
+
+
+
+
+
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/scripts/openfeign.sh b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/scripts/openfeign.sh
new file mode 100644
index 000000000..975d060d0
--- /dev/null
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/scripts/openfeign.sh
@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+n=1
+while [ $n -le 10 ]
+do
+ echo `curl -s http://localhost:18083/echo-feign/openfeign`
+ let n++
+done
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/scripts/resttemplate.sh b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/scripts/resttemplate.sh
new file mode 100644
index 000000000..e9d2e6300
--- /dev/null
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/scripts/resttemplate.sh
@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+n=1
+while [ $n -le 10 ]
+do
+ echo `curl -s http://localhost:18083/echo-rest/resttemplate`
+ let n++
+done
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/ConsumerSCLBApplication.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/ConsumerSCLBApplication.java
new file mode 100644
index 000000000..d60b21341
--- /dev/null
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/ConsumerSCLBApplication.java
@@ -0,0 +1,155 @@
+/*
+ * 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.examples;
+
+import java.util.List;
+import java.util.Random;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import reactor.core.publisher.Mono;
+
+import org.springframework.beans.factory.ObjectProvider;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.ServiceInstance;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.client.loadbalancer.LoadBalanced;
+import org.springframework.cloud.client.loadbalancer.reactive.DefaultResponse;
+import org.springframework.cloud.client.loadbalancer.reactive.EmptyResponse;
+import org.springframework.cloud.client.loadbalancer.reactive.Request;
+import org.springframework.cloud.client.loadbalancer.reactive.Response;
+import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
+import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
+import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
+import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * @author Jim
+ */
+@SpringBootApplication
+@EnableDiscoveryClient(autoRegister = false)
+@EnableFeignClients
+public class ConsumerSCLBApplication {
+
+ @LoadBalanced
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
+
+ public static void main(String[] args) {
+ SpringApplication.run(ConsumerSCLBApplication.class, args);
+ }
+
+ @Configuration
+ @LoadBalancerClient(value = "service-provider",
+ configuration = MyLoadBalancerConfiguration.class)
+ class MySCLBConfiguration {
+
+ }
+
+ static class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
+
+ private static final Logger log = LoggerFactory
+ .getLogger(RandomLoadBalancer.class);
+
+ private ObjectProvider serviceInstanceListSupplierProvider;
+
+ private final String serviceId;
+
+ private final Random random;
+
+ RandomLoadBalancer(
+ ObjectProvider serviceInstanceListSupplierProvider,
+ String serviceId) {
+ this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
+ this.serviceId = serviceId;
+ this.random = new Random();
+ }
+
+ @Override
+ public Mono> choose(Request request) {
+ log.info("random spring cloud loadbalacer active -.-");
+ ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
+ .getIfAvailable(NoopServiceInstanceListSupplier::new);
+ return supplier.get().next().map(this::getInstanceResponse);
+ }
+
+ private Response getInstanceResponse(
+ List instances) {
+ if (instances.isEmpty()) {
+ return new EmptyResponse();
+ }
+ ServiceInstance instance = instances.get(random.nextInt(instances.size()));
+
+ return new DefaultResponse(instance);
+ }
+
+ }
+
+ @FeignClient(name = "service-provider")
+ public interface EchoService {
+
+ @GetMapping("/echo/{str}")
+ String echo(@PathVariable("str") String str);
+
+ @GetMapping("/divide")
+ String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
+
+ default String divide(Integer a) {
+ return divide(a, 0);
+ }
+
+ @GetMapping("/notFound")
+ String notFound();
+
+ }
+
+ @RestController
+ class TestController {
+
+ @Autowired
+ private RestTemplate restTemplate;
+
+ @Autowired
+ private EchoService echoService;
+
+ @GetMapping("/echo-rest/{str}")
+ public String rest(@PathVariable String str) {
+ return restTemplate.getForObject("http://service-provider/echo/" + str,
+ String.class);
+ }
+
+ @GetMapping("/echo-feign/{str}")
+ public String feign(@PathVariable String str) {
+ return echoService.echo(str);
+ }
+
+ }
+
+}
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/MyLoadBalancerConfiguration.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/MyLoadBalancerConfiguration.java
new file mode 100644
index 000000000..8ff966575
--- /dev/null
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/MyLoadBalancerConfiguration.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2013-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.alibaba.cloud.examples;
+
+import com.alibaba.cloud.examples.ConsumerSCLBApplication.RandomLoadBalancer;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.cloud.client.ServiceInstance;
+import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
+import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
+import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.core.env.Environment;
+
+/**
+ * @author Jim
+ */
+public class MyLoadBalancerConfiguration {
+
+ @Bean
+ @ConditionalOnMissingBean
+ public ReactorLoadBalancer reactorServiceInstanceLoadBalancer(
+ Environment environment,
+ LoadBalancerClientFactory loadBalancerClientFactory) {
+ String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
+ return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name,
+ ServiceInstanceListSupplier.class), name);
+ }
+
+}
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/resources/application.properties
new file mode 100644
index 000000000..40c2b2f56
--- /dev/null
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/resources/application.properties
@@ -0,0 +1,6 @@
+spring.application.name=service-consumer-sclb
+server.port=18083
+management.endpoints.web.exposure.include=*
+spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
+
+spring.cloud.loadbalancer.ribbon.enabled=false
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java
index 64b43b233..94a842598 100644
--- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java
@@ -29,7 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
/**
* @author xiaojing
*/
-@EnableDiscoveryClient(autoRegister = false)
+@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/pom.xml
new file mode 100644
index 000000000..704879687
--- /dev/null
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/pom.xml
@@ -0,0 +1,63 @@
+
+
+
+
+ com.alibaba.cloud
+ nacos-discovery-example
+ 2.2.0.BUILD-SNAPSHOT
+
+ 4.0.0
+
+
+ nacos-reactivediscovery-consumer-example
+ jar
+ Example demonstrating how to use nacos discovery
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-webflux
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
+
+ org.springframework.cloud
+ spring-cloud-starter-netflix-ribbon
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-loadbalancer
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ org.apache.maven.plugins
+ maven-deploy-plugin
+ ${maven-deploy-plugin.version}
+
+ true
+
+
+
+
+
+
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerReactiveApplication.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerReactiveApplication.java
new file mode 100644
index 000000000..b9e057372
--- /dev/null
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerReactiveApplication.java
@@ -0,0 +1,74 @@
+/*
+ * 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.examples;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
+import org.springframework.cloud.client.loadbalancer.LoadBalanced;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.reactive.function.client.WebClient;
+
+/**
+ * @author Jim
+ */
+@SpringBootApplication
+public class ConsumerReactiveApplication {
+
+ @Bean
+ @LoadBalanced
+ public WebClient.Builder webClient() {
+ return WebClient.builder();
+ }
+
+ public static void main(String[] args) {
+ SpringApplication.run(ConsumerReactiveApplication.class, args);
+ }
+
+ @RestController
+ class MyController {
+
+ @Autowired
+ private ReactiveDiscoveryClient reactiveDiscoveryClient;
+
+ @Autowired
+ private WebClient.Builder webClientBuilder;
+
+ @GetMapping("/all-services")
+ public Flux allServices() {
+ return reactiveDiscoveryClient.getInstances("service-provider")
+ .map(serviceInstance -> serviceInstance.getHost() + ":"
+ + serviceInstance.getPort());
+ }
+
+ @GetMapping("/service-call/{name}")
+ public Mono serviceCall(@PathVariable("name") String name) {
+ return webClientBuilder.build().get()
+ .uri("http://service-provider/echo/" + name).retrieve()
+ .bodyToMono(String.class);
+ }
+
+ }
+
+}
diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/resources/application.properties
new file mode 100644
index 000000000..cedb93f2d
--- /dev/null
+++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/resources/application.properties
@@ -0,0 +1,6 @@
+spring.application.name=service-consumer-reactive
+server.port=18083
+management.endpoints.web.exposure.include=*
+spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
+
+spring.cloud.loadbalancer.ribbon.enabled=false
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 3c5c31fa2..a0be8ef00 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
@@ -18,6 +18,8 @@
nacos-discovery-consumer-example
+ nacos-discovery-consumer-sclb-example
+ nacos-reactivediscovery-consumer-example
nacos-discovery-provider-example
nacos-discovery-spring-cloud-config-server-example
nacos-discovery-spring-cloud-config-client-example
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 3b7c0ce60..2db02dc2a 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
@@ -30,6 +30,10 @@
org.springframework.boot
spring-boot-starter-actuator
+
+ org.springframework.boot
+ spring-boot-starter-thymeleaf
+
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ServiceApplication.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ServiceApplication.java
index 17121bd9c..7345f74c0 100644
--- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ServiceApplication.java
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ServiceApplication.java
@@ -16,11 +16,18 @@
package com.alibaba.cloud.examples;
+import java.util.Collections;
+
+import com.alibaba.cloud.circuitbreaker.sentinel.SentinelCircuitBreakerFactory;
+import com.alibaba.cloud.circuitbreaker.sentinel.SentinelConfigBuilder;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.csp.sentinel.datasource.Converter;
+import com.alibaba.csp.sentinel.slots.block.RuleConstant;
+import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.circuitbreaker.Customizer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@@ -47,6 +54,18 @@ public class ServiceApplication {
return new JsonFlowRuleListConverter();
}
+ @Bean
+ public Customizer defaultConfig() {
+ return factory -> {
+ factory.configureDefault(
+ id -> new SentinelConfigBuilder().resourceName(id)
+ .rules(Collections.singletonList(new DegradeRule(id)
+ .setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100)
+ .setTimeWindow(10)))
+ .build());
+ };
+ }
+
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/TestController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/TestController.java
index 1e7a909d9..ffacf273a 100644
--- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/TestController.java
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/TestController.java
@@ -19,6 +19,7 @@ package com.alibaba.cloud.examples;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@@ -32,6 +33,9 @@ public class TestController {
@Autowired
private RestTemplate restTemplate;
+ @Autowired
+ private CircuitBreakerFactory circuitBreakerFactory;
+
@GetMapping("/hello")
@SentinelResource("resource")
public String hello() {
@@ -54,4 +58,17 @@ public class TestController {
return restTemplate.getForObject("http://www.taobao.com/test", String.class);
}
+ @GetMapping("/slow")
+ public String slow() {
+ return circuitBreakerFactory.create("show").run(() -> {
+ try {
+ Thread.sleep(1000L);
+ }
+ catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ return "success";
+ }, throwable -> "fallback");
+ }
+
}
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/WebMvcConfiguration.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/WebMvcConfiguration.java
new file mode 100644
index 000000000..b4a562fbe
--- /dev/null
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/WebMvcConfiguration.java
@@ -0,0 +1,36 @@
+/*
+ * 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.examples;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/**
+ * @author yuhuangbin
+ */
+@Configuration
+@EnableWebMvc
+public class WebMvcConfiguration implements WebMvcConfigurer {
+
+ @Override
+ public void addViewControllers(ViewControllerRegistry registry) {
+ registry.addViewController("/errorPage").setViewName("errorPage");
+ }
+
+}
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties
index 4a60ba53b..16ee4a26a 100644
--- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties
@@ -9,6 +9,9 @@ management.health.diskspace.enabled=false
spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.eager=true
+
+#spring.cloud.sentinel.block-page=/errorPage
+#spring.cloud.sentinel.filter.enabled=false
#spring.cloud.sentinel.http-method-specify=false
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/templates/errorPage.html b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/templates/errorPage.html
new file mode 100644
index 000000000..04211d7e0
--- /dev/null
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/templates/errorPage.html
@@ -0,0 +1,10 @@
+
+
+
+
+ Title
+
+
+This is error page.
+
+
\ No newline at end of file
diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigAutoConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigAutoConfiguration.java
index e851faef5..60075c9c1 100644
--- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigAutoConfiguration.java
+++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigAutoConfiguration.java
@@ -64,7 +64,8 @@ public class NacosConfigAutoConfiguration {
public NacosContextRefresher nacosContextRefresher(
NacosConfigManager nacosConfigManager,
NacosRefreshHistory nacosRefreshHistory) {
- // Consider that it is not necessary to be compatible with the previous configuration
+ // Consider that it is not necessary to be compatible with the previous
+ // configuration
// and use the new configuration if necessary.
return new NacosContextRefresher(nacosConfigManager, nacosRefreshHistory);
}
diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceBuilder.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceBuilder.java
index 3f650b5e8..767a72c1e 100644
--- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceBuilder.java
+++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceBuilder.java
@@ -17,7 +17,6 @@
package com.alibaba.cloud.nacos.client;
import java.util.Date;
-import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@@ -111,17 +110,7 @@ public class NacosPropertySourceBuilder {
@SuppressWarnings("unchecked")
private Map propertiesToMap(Properties properties) {
Map result = new HashMap<>(16);
- Enumeration keys = (Enumeration) properties.propertyNames();
- while (keys.hasMoreElements()) {
- String key = keys.nextElement();
- String value = properties.getProperty(key);
- if (value != null) {
- result.put(key, value.trim());
- }
- else {
- result.put(key, null);
- }
- }
+ properties.forEach((k, v) -> result.put(String.valueOf(k), v));
return result;
}
diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/AbstractNacosDataParser.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/AbstractNacosDataParser.java
index 388961181..4109b049a 100644
--- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/AbstractNacosDataParser.java
+++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/AbstractNacosDataParser.java
@@ -111,12 +111,12 @@ public abstract class AbstractNacosDataParser {
/**
* Generate key-value pairs from the map.
*/
- protected Properties generateProperties(Map map) {
+ protected Properties generateProperties(Map map) {
if (null == map || map.isEmpty()) {
return null;
}
Properties properties = new Properties();
- for (Map.Entry entry : map.entrySet()) {
+ for (Map.Entry entry : map.entrySet()) {
String key = entry.getKey();
if (StringUtils.isEmpty(key)) {
continue;
@@ -130,12 +130,12 @@ public abstract class AbstractNacosDataParser {
/**
* Reload the key ending in `value` if need.
*/
- protected Map reloadMap(Map map) {
+ protected Map reloadMap(Map map) {
if (map == null || map.isEmpty()) {
return null;
}
- Map result = new HashMap<>(map);
- for (Map.Entry entry : map.entrySet()) {
+ Map result = new HashMap<>(map);
+ for (Map.Entry entry : map.entrySet()) {
String key = entry.getKey();
if (key.contains(DOT)) {
int idx = key.lastIndexOf(DOT);
diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataJsonParser.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataJsonParser.java
index 437661a39..7b22192d1 100644
--- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataJsonParser.java
+++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosDataJsonParser.java
@@ -17,18 +17,22 @@
package com.alibaba.cloud.nacos.parser;
import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
+import java.util.Set;
-import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
/**
* @author zkz
+ * @author yuhuangbin
*/
public class NacosDataJsonParser extends AbstractNacosDataParser {
@@ -41,7 +45,7 @@ public class NacosDataJsonParser extends AbstractNacosDataParser {
if (StringUtils.isEmpty(data)) {
return null;
}
- Map map = parseJSON2Map(data);
+ Map map = parseJSON2Map(data);
return this.generateProperties(this.reloadMap(map));
}
@@ -51,44 +55,48 @@ public class NacosDataJsonParser extends AbstractNacosDataParser {
* @return the map convert by json string
* @throws IOException thrown if there is a problem parsing config.
*/
- public static Map parseJSON2Map(String json) throws IOException {
- Map map = new HashMap<>(32);
+ public static Map parseJSON2Map(String json) throws IOException {
+ Map result = new HashMap<>(32);
+
ObjectMapper mapper = new ObjectMapper();
- JsonNode jsonNode = mapper.readTree(json);
- if (null == jsonNode) {
- return map;
+ Map nacosDataMap = mapper.readValue(json, Map.class);
+
+ if (CollectionUtils.isEmpty(nacosDataMap)) {
+ return result;
}
- parseJsonNode(map, jsonNode, "");
- return map;
+ parseNacosDataMap(result, nacosDataMap, "");
+ return result;
}
- private static void parseJsonNode(Map jsonMap, JsonNode jsonNode,
- String parentKey) {
- Iterator fieldNames = jsonNode.fieldNames();
- while (fieldNames.hasNext()) {
- String name = fieldNames.next();
- String fullKey = StringUtils.isEmpty(parentKey) ? name
- : parentKey + DOT + name;
- JsonNode resultValue = jsonNode.findValue(name);
- if (null == resultValue) {
+ private static void parseNacosDataMap(Map result,
+ Map dataMap, String parentKey) {
+ Set> entries = dataMap.entrySet();
+ for (Iterator> iterator = entries.iterator(); iterator
+ .hasNext();) {
+ Map.Entry entry = iterator.next();
+ String key = entry.getKey();
+ Object value = entry.getValue();
+
+ String fullKey = StringUtils.isEmpty(parentKey) ? key : key.startsWith("[")
+ ? parentKey.concat(key) : parentKey.concat(DOT).concat(key);
+
+ if (value instanceof Map) {
+ Map map = (Map) value;
+ parseNacosDataMap(result, map, fullKey);
continue;
}
- if (resultValue.isArray()) {
- Iterator iterator = resultValue.elements();
- while (iterator != null && iterator.hasNext()) {
- JsonNode next = iterator.next();
- if (null == next) {
- continue;
- }
- parseJsonNode(jsonMap, next, fullKey);
+ else if (value instanceof Collection) {
+ int count = 0;
+ Collection