From 39db289c17fdacc2c62095420313f0a1566518ed Mon Sep 17 00:00:00 2001 From: WangCanxuan <100568933+MieAh@users.noreply.github.com> Date: Wed, 30 Nov 2022 19:38:52 +0800 Subject: [PATCH] Refactor/nacos-discovery-example_for2.2.x (#2940) * Refactor nacos discovery examples. --- .../cloud/examples/ConsumerApplication.java | 75 +----------- .../cloud/examples/TestController.java | 45 ++++--- .../configuration/FeignConfiguration.java | 36 ++++++ .../RestTemplateConfiguration.java | 48 ++++++++ .../examples/configuration}/UrlCleaner.java | 20 ++- .../cloud/examples/feign/EchoClient.java | 70 +++++++++++ .../examples/feign/EchoClientFallback.java | 44 +++++++ .../examples/ConsumerSCLBApplication.java | 2 + .../cloud/examples/RandomLoadBalancer.java | 77 ++++++++++++ .../cloud/examples/TestController.java | 115 ++++++++++++++++++ .../examples/config/FeignConfiguration.java | 36 ++++++ .../MyLoadBalancerConfiguration.java | 8 +- .../examples/config/MySCLBConfiguration.java | 32 +++++ .../config/RestTemplateConfiguration.java | 48 ++++++++ .../cloud/examples/config}/UrlCleaner.java | 20 ++- .../cloud/examples/feign/EchoClient.java | 70 +++++++++++ .../examples/feign/EchoClientFallback.java | 44 +++++++ .../cloud/examples/EchoController.java | 85 +++++++++++++ .../cloud/examples/ProviderApplication.java | 47 ------- .../cloud/examples/GetConfigController.java | 5 + .../examples/ConsumerReactiveApplication.java | 42 ------- .../alibaba/cloud/examples/MyController.java | 57 +++++++++ .../examples/WebClientConfiguration.java | 36 ++++++ .../cloud/examples/EchoController.java | 42 +++++++ .../cloud/examples/ProviderApplication.java | 19 --- 25 files changed, 912 insertions(+), 211 deletions(-) create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/FeignConfiguration.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/RestTemplateConfiguration.java rename spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/{nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples => nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration}/UrlCleaner.java (63%) create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClient.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClientFallback.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/RandomLoadBalancer.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/TestController.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/FeignConfiguration.java rename spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/{ => config}/MyLoadBalancerConfiguration.java (90%) create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/MySCLBConfiguration.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/RestTemplateConfiguration.java rename spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/{nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples => nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config}/UrlCleaner.java (64%) create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClient.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClientFallback.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/java/com/alibaba/cloud/examples/EchoController.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/MyController.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/WebClientConfiguration.java create mode 100644 spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/src/main/java/com/alibaba/cloud/examples/EchoController.java diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java index b57ca0c07..91089bf3e 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java @@ -16,92 +16,21 @@ package com.alibaba.cloud.examples; -import com.alibaba.cloud.examples.ConsumerApplication.EchoService; -import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.EnableFeignClients; -import org.springframework.cloud.openfeign.FeignClient; -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.RequestParam; -import org.springframework.web.client.RestTemplate; /** - * @author xiaojing + * @author xiaojing, fangjian0423, MieAh */ @SpringBootApplication -@EnableDiscoveryClient(autoRegister = true) +@EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { - @LoadBalanced - @Bean - @SentinelRestTemplate(urlCleanerClass = UrlCleaner.class, urlCleaner = "clean") - public RestTemplate restTemplate() { - return new RestTemplate(); - } - - @LoadBalanced - @Bean - @SentinelRestTemplate - public RestTemplate restTemplate1() { - return new RestTemplate(); - } - public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } - @FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, - configuration = FeignConfiguration.class) - 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(); - - } - -} - -class FeignConfiguration { - - @Bean - public EchoServiceFallback echoServiceFallback() { - return new EchoServiceFallback(); - } - -} - -class EchoServiceFallback implements EchoService { - - @Override - public String echo(@PathVariable("str") String str) { - return "echo fallback"; - } - - @Override - public String divide(@RequestParam Integer a, @RequestParam Integer b) { - return "divide fallback"; - } - - @Override - public String notFound() { - return "notFound fallback"; - } - } diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/TestController.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/TestController.java index 51542d0d3..e412e2304 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/TestController.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/TestController.java @@ -16,9 +16,10 @@ package com.alibaba.cloud.examples; -import com.alibaba.cloud.examples.ConsumerApplication.EchoService; +import javax.annotation.Resource; + +import com.alibaba.cloud.examples.feign.EchoClient; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -27,62 +28,68 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** - * @author xiaojing + * Example of remote invocation of service fusing and load balancing. + * + * @author xiaojing, fangjian0423, MieAh */ @RestController public class TestController { - @Autowired - private RestTemplate restTemplate; + @Resource + private RestTemplate urlCleanedRestTemplate; - @Autowired - private RestTemplate restTemplate1; + @Resource + private RestTemplate restTemplate; - @Autowired - private EchoService echoService; + @Resource + private EchoClient echoClient; - @Autowired + @Resource private DiscoveryClient discoveryClient; + private static final String SERVICE_PROVIDER_ADDRESS = "http://service-provider"; + @GetMapping("/echo-rest/{str}") public String rest(@PathVariable String str) { - return restTemplate.getForObject("http://service-provider/echo/" + str, - String.class); + return urlCleanedRestTemplate + .getForObject(SERVICE_PROVIDER_ADDRESS + "/echo/" + str, String.class); } @GetMapping("/index") public String index() { - return restTemplate1.getForObject("http://service-provider", String.class); + return restTemplate.getForObject(SERVICE_PROVIDER_ADDRESS, String.class); } @GetMapping("/test") public String test() { - return restTemplate1.getForObject("http://service-provider/test", String.class); + return restTemplate.getForObject(SERVICE_PROVIDER_ADDRESS + "/test", + String.class); } @GetMapping("/sleep") public String sleep() { - return restTemplate1.getForObject("http://service-provider/sleep", String.class); + return restTemplate.getForObject(SERVICE_PROVIDER_ADDRESS + "/sleep", + String.class); } @GetMapping("/notFound-feign") public String notFound() { - return echoService.notFound(); + return echoClient.notFound(); } @GetMapping("/divide-feign") public String divide(@RequestParam Integer a, @RequestParam Integer b) { - return echoService.divide(a, b); + return echoClient.divide(a, b); } @GetMapping("/divide-feign2") public String divide(@RequestParam Integer a) { - return echoService.divide(a); + return echoClient.divide(a); } @GetMapping("/echo-feign/{str}") public String feign(@PathVariable String str) { - return echoService.echo(str); + return echoClient.echo(str); } @GetMapping("/services/{service}") diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/FeignConfiguration.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/FeignConfiguration.java new file mode 100644 index 000000000..f93e0c4e1 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/FeignConfiguration.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.configuration; + +import com.alibaba.cloud.examples.feign.EchoClient; +import com.alibaba.cloud.examples.feign.EchoClientFallback; + +import org.springframework.context.annotation.Bean; + +/** + * Configuration for Feign. + * + * @author fangjian0423, MieAh + */ +public class FeignConfiguration { + + @Bean + public EchoClient echoClientFallback() { + return new EchoClientFallback(); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/RestTemplateConfiguration.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/RestTemplateConfiguration.java new file mode 100644 index 000000000..5d2152d11 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/RestTemplateConfiguration.java @@ -0,0 +1,48 @@ +/* + * 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.configuration; + +import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * Load balancing and sentinel configuration for RestTemplate. + * + * @author fangjian0423, MieAh + */ +@Configuration +public class RestTemplateConfiguration { + + @LoadBalanced + @Bean + @SentinelRestTemplate(urlCleanerClass = UrlCleaner.class, urlCleaner = "clean") + public RestTemplate urlCleanedRestTemplate() { + return new RestTemplate(); + } + + @LoadBalanced + @Bean + @SentinelRestTemplate + public RestTemplate restTemplate() { + return new RestTemplate(); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/UrlCleaner.java similarity index 63% rename from spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java rename to spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/UrlCleaner.java index e0c691af4..131864c8a 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/configuration/UrlCleaner.java @@ -14,14 +14,26 @@ * limitations under the License. */ -package com.alibaba.cloud.examples; +package com.alibaba.cloud.examples.configuration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Change the request path containing echo. + * + * @author fangjian0423, MieAh + */ public class UrlCleaner { + private static final Logger LOGGER = LoggerFactory.getLogger(UrlCleaner.class); + + private static final String URL_CLEAN_ECHO = ".*/echo/.*"; + public static String clean(String url) { - System.out.println("enter urlCleaner"); - if (url.matches(".*/echo/.*")) { - System.out.println("change url"); + LOGGER.info("enter urlCleaner"); + if (url.matches(URL_CLEAN_ECHO)) { + LOGGER.info("change url"); url = url.replaceAll("/echo/.*", "/echo/{str}"); } return url; diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClient.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClient.java new file mode 100644 index 000000000..7c8a6c124 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClient.java @@ -0,0 +1,70 @@ +/* + * 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.feign; + +import com.alibaba.cloud.examples.configuration.FeignConfiguration; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * Provide the external exposure interface of the service calling client. + * + * @author fangjian0423, MieAh + */ +@FeignClient(name = "service-provider", fallback = EchoClientFallback.class, + configuration = FeignConfiguration.class) +public interface EchoClient { + + /** + * Call the echo method of the remote provider or roll back when the service is blown. + * @param str str + * @return {@link String} + */ + @GetMapping("/echo/{str}") + String echo(@PathVariable("str") String str); + + /** + * Call the divide method of the remote provider or roll back when the service is + * blown. + * @param a a + * @param b b + * @return {@link String} + */ + @GetMapping("/divide") + String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b); + + /** + * Test that the default method calls the remote method is still a remote call. + * @param a a + * @return {@link String} + */ + default String divide(Integer a) { + return divide(a, 0); + } + + /** + * Call the notFound method of the remote provider or roll back when the service is + * blown. + * @return {@link String} + */ + @GetMapping("/notFound") + String notFound(); + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClientFallback.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClientFallback.java new file mode 100644 index 000000000..40c5948ca --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClientFallback.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.feign; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * When the service is blown, the fallback operation is performed. + * + * @author fangjian0423, MieAh + */ +public class EchoClientFallback implements EchoClient { + + @Override + public String echo(@PathVariable("str") String str) { + return "echo fallback"; + } + + @Override + public String divide(@RequestParam Integer a, @RequestParam Integer b) { + return "divide fallback"; + } + + @Override + public String notFound() { + return "notFound fallback"; + } + +} 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 index f157897bf..a2c0c2801 100644 --- 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 @@ -20,6 +20,8 @@ import java.util.List; import java.util.Random; import com.alibaba.cloud.examples.ConsumerSCLBApplication.EchoService; +import com.alibaba.cloud.examples.config.MyLoadBalancerConfiguration; +import com.alibaba.cloud.examples.config.UrlCleaner; import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/RandomLoadBalancer.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/RandomLoadBalancer.java new file mode 100644 index 000000000..924c3acd8 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/RandomLoadBalancer.java @@ -0,0 +1,77 @@ +/* + * 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.cloud.client.ServiceInstance; +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.core.NoopServiceInstanceListSupplier; +import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; + +/** + * Self-defined randomLoadBalancer. + * + * @author fangjian0423, MieAh + */ +public class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer { + + private static final Logger log = LoggerFactory.getLogger(RandomLoadBalancer.class); + + private ObjectProvider serviceInstanceListSupplierProvider; + + private final String serviceId; + + private final Random random; + + public 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 loadbalancer 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); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/TestController.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/TestController.java new file mode 100644 index 000000000..a6315fcbe --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/TestController.java @@ -0,0 +1,115 @@ +/* + * 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 javax.annotation.Resource; + +import com.alibaba.cloud.examples.feign.EchoClient; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.client.discovery.DiscoveryClient; +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; + +/** + * Example of remote invocation of service fusing and custom load balancing. + * + * @author fangjian0423, MieAh + */ +@RestController +public class TestController { + + @Resource + private RestTemplate urlCleanedRestTemplate; + + @Resource + private RestTemplate restTemplate; + + @Resource + private EchoClient echoClient; + + @Resource + private DiscoveryClient discoveryClient; + + @Value("${spring.cloud.loadbalancer.zone:null}") + private String zone; + + private static final String SERVICE_PROVIDER_ADDRESS = "http://service-provider"; + + @GetMapping("/echo-rest/{str}") + public String rest(@PathVariable String str) { + return urlCleanedRestTemplate + .getForObject(SERVICE_PROVIDER_ADDRESS + "/echo/" + str, String.class); + } + + @GetMapping("/zone") + public String zone() { + return "consumer zone " + zone + "\n" + urlCleanedRestTemplate + .getForObject(SERVICE_PROVIDER_ADDRESS + "/zone", String.class); + } + + @GetMapping("/echo-feign/{str}") + public String feign(@PathVariable String str) { + return echoClient.echo(str); + } + + @GetMapping("/index") + public String index() { + return restTemplate.getForObject(SERVICE_PROVIDER_ADDRESS, String.class); + } + + @GetMapping("/test") + public String test() { + return restTemplate.getForObject(SERVICE_PROVIDER_ADDRESS + "/test", + String.class); + } + + @GetMapping("/sleep") + public String sleep() { + return restTemplate.getForObject(SERVICE_PROVIDER_ADDRESS + "/sleep", + String.class); + } + + @GetMapping("/notFound-feign") + public String notFound() { + return echoClient.notFound(); + } + + @GetMapping("/divide-feign") + public String divide(@RequestParam Integer a, @RequestParam Integer b) { + return echoClient.divide(a, b); + } + + @GetMapping("/divide-feign2") + public String divide(@RequestParam Integer a) { + return echoClient.divide(a); + } + + @GetMapping("/services/{service}") + public Object client(@PathVariable String service) { + return discoveryClient.getInstances(service); + } + + @GetMapping("/services") + public Object services() { + return discoveryClient.getServices(); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/FeignConfiguration.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/FeignConfiguration.java new file mode 100644 index 000000000..1e713e139 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/FeignConfiguration.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.config; + +import com.alibaba.cloud.examples.feign.EchoClient; +import com.alibaba.cloud.examples.feign.EchoClientFallback; + +import org.springframework.context.annotation.Bean; + +/** + * Configuration for Feign. + * + * @author fangjian0423, MieAh + */ +public class FeignConfiguration { + + @Bean + public EchoClient echoClientFallback() { + return new EchoClientFallback(); + } + +} 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/config/MyLoadBalancerConfiguration.java similarity index 90% rename from spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/MyLoadBalancerConfiguration.java rename to spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/MyLoadBalancerConfiguration.java index 8ff966575..0f71847c6 100644 --- 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/config/MyLoadBalancerConfiguration.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package com.alibaba.cloud.examples; +package com.alibaba.cloud.examples.config; -import com.alibaba.cloud.examples.ConsumerSCLBApplication.RandomLoadBalancer; +import com.alibaba.cloud.examples.RandomLoadBalancer; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.cloud.client.ServiceInstance; @@ -27,7 +27,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; /** - * @author Jim + * Configure for load balancing. + * + * @author fangjian0423, MieAh */ public class MyLoadBalancerConfiguration { diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/MySCLBConfiguration.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/MySCLBConfiguration.java new file mode 100644 index 000000000..9878b273a --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/MySCLBConfiguration.java @@ -0,0 +1,32 @@ +/* + * 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.config; + +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; +import org.springframework.context.annotation.Configuration; + +/** + * Configuration for Self-defined randomLoadBalancer. + * + * @author fangjian0423, MieAh + */ +@Configuration +@LoadBalancerClient(value = "service-provider", + configuration = MyLoadBalancerConfiguration.class) +public class MySCLBConfiguration { + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/RestTemplateConfiguration.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/RestTemplateConfiguration.java new file mode 100644 index 000000000..84ee624e8 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/RestTemplateConfiguration.java @@ -0,0 +1,48 @@ +/* + * 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.config; + +import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * Load balancing and sentinel configuration for RestTemplate. + * + * @author fangjian0423, MieAh + */ +@Configuration +public class RestTemplateConfiguration { + + @LoadBalanced + @Bean + @SentinelRestTemplate(urlCleanerClass = UrlCleaner.class, urlCleaner = "clean") + public RestTemplate urlCleanedRestTemplate() { + return new RestTemplate(); + } + + @LoadBalanced + @Bean + @SentinelRestTemplate + public RestTemplate restTemplate() { + return new RestTemplate(); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/UrlCleaner.java similarity index 64% rename from spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java rename to spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/UrlCleaner.java index e0c691af4..aca1ec0bc 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/src/main/java/com/alibaba/cloud/examples/UrlCleaner.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/config/UrlCleaner.java @@ -14,14 +14,26 @@ * limitations under the License. */ -package com.alibaba.cloud.examples; +package com.alibaba.cloud.examples.config; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Change the request path containing echo. + * + * @author fangjian0423, MieAh + */ public class UrlCleaner { + private static final Logger LOGGER = LoggerFactory.getLogger(UrlCleaner.class); + + private static final String URL_CLEAN_ECHO = ".*/echo/.*"; + public static String clean(String url) { - System.out.println("enter urlCleaner"); - if (url.matches(".*/echo/.*")) { - System.out.println("change url"); + LOGGER.info("enter urlCleaner"); + if (url.matches(URL_CLEAN_ECHO)) { + LOGGER.info("change url"); url = url.replaceAll("/echo/.*", "/echo/{str}"); } return url; diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClient.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClient.java new file mode 100644 index 000000000..0e733f70e --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClient.java @@ -0,0 +1,70 @@ +/* + * 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.feign; + +import com.alibaba.cloud.examples.config.FeignConfiguration; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * Provide the external exposure interface of the service calling client. + * + * @author fangjian0423, MieAh + */ +@FeignClient(name = "service-provider", fallback = EchoClientFallback.class, + configuration = FeignConfiguration.class) +public interface EchoClient { + + /** + * Call the echo method of the remote provider or roll back when the service is blown. + * @param str str + * @return {@link String} + */ + @GetMapping("/echo/{str}") + String echo(@PathVariable("str") String str); + + /** + * Call the divide method of the remote provider or roll back when the service is + * blown. + * @param a a + * @param b b + * @return {@link String} + */ + @GetMapping("/divide") + String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b); + + /** + * Test that the default method calls the remote method is still a remote call. + * @param a a + * @return {@link String} + */ + default String divide(Integer a) { + return divide(a, 0); + } + + /** + * Call the notFound method of the remote provider or roll back when the service is + * blown. + * @return {@link String} + */ + @GetMapping("/notFound") + String notFound(); + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClientFallback.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClientFallback.java new file mode 100644 index 000000000..40c5948ca --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-sclb-example/src/main/java/com/alibaba/cloud/examples/feign/EchoClientFallback.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.feign; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * When the service is blown, the fallback operation is performed. + * + * @author fangjian0423, MieAh + */ +public class EchoClientFallback implements EchoClient { + + @Override + public String echo(@PathVariable("str") String str) { + return "echo fallback"; + } + + @Override + public String divide(@RequestParam Integer a, @RequestParam Integer b) { + return "divide fallback"; + } + + @Override + public String notFound() { + return "notFound fallback"; + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/java/com/alibaba/cloud/examples/EchoController.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/java/com/alibaba/cloud/examples/EchoController.java new file mode 100644 index 000000000..912934aa6 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/src/main/java/com/alibaba/cloud/examples/EchoController.java @@ -0,0 +1,85 @@ +/* + * 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.Map; + +import javax.annotation.Resource; + +import com.alibaba.cloud.nacos.NacosDiscoveryProperties; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +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; + +/** + * Provide interfaces to consumers. + * + * @author fangjian0423, MieAh + */ +@RestController +public class EchoController { + + @Resource + private NacosDiscoveryProperties nacosDiscoveryProperties; + + @GetMapping("/") + public ResponseEntity index() { + return new ResponseEntity<>("index error", HttpStatus.INTERNAL_SERVER_ERROR); + } + + @GetMapping("/test") + public ResponseEntity test() { + return new ResponseEntity<>("error", HttpStatus.INTERNAL_SERVER_ERROR); + } + + @GetMapping("/sleep") + public String sleep() { + try { + Thread.sleep(1000L); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + return "ok"; + } + + @GetMapping("/echo/{string}") + public String echo(@PathVariable String string) { + return "hello Nacos Discovery " + string; + } + + @GetMapping("/divide") + public String divide(@RequestParam Integer a, @RequestParam Integer b) { + if (b == 0) { + return String.valueOf(0); + } + else { + return String.valueOf(a / b); + } + } + + @GetMapping("/zone") + public String zone() { + Map metadata = nacosDiscoveryProperties.getMetadata(); + return "provider zone " + metadata.get("zone"); + } + +} 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 2cee5295f..9cdce0266 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 @@ -19,12 +19,6 @@ package com.alibaba.cloud.examples; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -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; /** * @author xiaojing @@ -37,45 +31,4 @@ public class ProviderApplication { SpringApplication.run(ProviderApplication.class, args); } - @RestController - class EchoController { - - @GetMapping("/") - public ResponseEntity index() { - return new ResponseEntity("index error", HttpStatus.INTERNAL_SERVER_ERROR); - } - - @GetMapping("/test") - public ResponseEntity test() { - return new ResponseEntity("error", HttpStatus.INTERNAL_SERVER_ERROR); - } - - @GetMapping("/sleep") - public String sleep() { - try { - Thread.sleep(1000L); - } - catch (InterruptedException e) { - e.printStackTrace(); - } - return "ok"; - } - - @GetMapping("/echo/{string}") - public String echo(@PathVariable String string) { - return "hello Nacos Discovery " + string; - } - - @GetMapping("/divide") - public String divide(@RequestParam Integer a, @RequestParam Integer b) { - if (b == 0) { - return String.valueOf(0); - } - else { - return String.valueOf(a / b); - } - } - - } - } diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-client-example/src/main/java/com/alibaba/cloud/examples/GetConfigController.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-client-example/src/main/java/com/alibaba/cloud/examples/GetConfigController.java index 32dfcff07..50d23aa4c 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-client-example/src/main/java/com/alibaba/cloud/examples/GetConfigController.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-spring-cloud-config-client-example/src/main/java/com/alibaba/cloud/examples/GetConfigController.java @@ -20,6 +20,11 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; +/** + * Provide the interface method of config. + * + * @author MieAh + */ @RestController public class GetConfigController { 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 index b9e057372..831827e04 100644 --- 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 @@ -16,19 +16,8 @@ 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 @@ -36,39 +25,8 @@ import org.springframework.web.reactive.function.client.WebClient; @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/java/com/alibaba/cloud/examples/MyController.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/MyController.java new file mode 100644 index 000000000..e6df7aec3 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/MyController.java @@ -0,0 +1,57 @@ +/* + * 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 javax.annotation.Resource; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; +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; + +/** + * Example of responsive discovery client. + * + * @author fangjian0423, MieAh + */ +@RestController +public class MyController { + + @Resource + private ReactiveDiscoveryClient reactiveDiscoveryClient; + + @Resource + 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/java/com/alibaba/cloud/examples/WebClientConfiguration.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/WebClientConfiguration.java new file mode 100644 index 000000000..0a6a42586 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/WebClientConfiguration.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.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.web.reactive.function.client.WebClient; + +/** + * Configuration for web client. + * + * @author fangjian0423, MieAh + */ +public class WebClientConfiguration { + + @Bean + @LoadBalanced + public WebClient.Builder webClient() { + return WebClient.builder(); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/src/main/java/com/alibaba/cloud/examples/EchoController.java b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/src/main/java/com/alibaba/cloud/examples/EchoController.java new file mode 100644 index 000000000..50cb9b180 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/src/main/java/com/alibaba/cloud/examples/EchoController.java @@ -0,0 +1,42 @@ +/* + * 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.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; + +/** + * Provide a service interface to the gateway for forwarding calls. + * + * @author MieAh + */ +@RestController +public class EchoController { + + @GetMapping("/echo/{string}") + public String echo(@PathVariable String string) { + return "hello Nacos Discovery " + string; + } + + @GetMapping("/divide") + public String divide(@RequestParam Integer a, @RequestParam Integer b) { + return String.valueOf(a / b); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java index a28fd9430..f13a01daf 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-gateway-example/nacos-gateway-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java @@ -19,10 +19,6 @@ package com.alibaba.cloud.examples; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -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; /** * @author xiaojing @@ -35,19 +31,4 @@ public class ProviderApplication { SpringApplication.run(ProviderApplication.class, args); } - @RestController - class EchoController { - - @GetMapping("/echo/{string}") - public String echo(@PathVariable String string) { - return "hello Nacos Discovery " + string; - } - - @GetMapping("/divide") - public String divide(@RequestParam Integer a, @RequestParam Integer b) { - return String.valueOf(a / b); - } - - } - }