Refactor/nacos-discovery-example_for2.2.x (#2940)

* Refactor nacos discovery examples.
pull/2943/head
WangCanxuan 2 years ago committed by GitHub
parent 1df238ad25
commit 39db289c17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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";
}
}

@ -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}")

@ -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();
}
}

@ -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();
}
}

@ -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;

@ -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();
}

@ -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";
}
}

@ -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;

@ -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<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;
private final String serviceId;
private final Random random;
public RandomLoadBalancer(
ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider,
String serviceId) {
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
this.serviceId = serviceId;
this.random = new Random();
}
@Override
public Mono<Response<ServiceInstance>> 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<ServiceInstance> getInstanceResponse(
List<ServiceInstance> instances) {
if (instances.isEmpty()) {
return new EmptyResponse();
}
ServiceInstance instance = instances.get(random.nextInt(instances.size()));
return new DefaultResponse(instance);
}
}

@ -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();
}
}

@ -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();
}
}

@ -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 <a href="mailto:fangjian0423@gmail.com">Jim</a>
* Configure for load balancing.
*
* @author fangjian0423, MieAh
*/
public class MyLoadBalancerConfiguration {

@ -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 {
}

@ -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();
}
}

@ -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;

@ -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();
}

@ -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";
}
}

@ -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<String> index() {
return new ResponseEntity<>("index error", HttpStatus.INTERNAL_SERVER_ERROR);
}
@GetMapping("/test")
public ResponseEntity<String> 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<String, String> metadata = nacosDiscoveryProperties.getMetadata();
return "provider zone " + metadata.get("zone");
}
}

@ -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);
}
}
}
}

@ -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 {

@ -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 <a href="mailto:fangjian0423@gmail.com">Jim</a>
@ -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<String> allServices() {
return reactiveDiscoveryClient.getInstances("service-provider")
.map(serviceInstance -> serviceInstance.getHost() + ":"
+ serviceInstance.getPort());
}
@GetMapping("/service-call/{name}")
public Mono<String> serviceCall(@PathVariable("name") String name) {
return webClientBuilder.build().get()
.uri("http://service-provider/echo/" + name).retrieve()
.bodyToMono(String.class);
}
}
}

@ -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<String> allServices() {
return reactiveDiscoveryClient.getInstances("service-provider")
.map(serviceInstance -> serviceInstance.getHost() + ":"
+ serviceInstance.getPort());
}
@GetMapping("/service-call/{name}")
public Mono<String> serviceCall(@PathVariable("name") String name) {
return webClientBuilder.build().get().uri("http://service-provider/echo/" + name)
.retrieve().bodyToMono(String.class);
}
}

@ -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();
}
}

@ -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);
}
}

@ -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);
}
}
}

Loading…
Cancel
Save