refactor example

pull/882/head
seanyu 3 years ago
parent f2bc9ae079
commit b0e9e86f9e

@ -20,13 +20,13 @@
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency>
<dependency>
@ -40,8 +40,8 @@
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-circuitbreaker-spring-retry</artifactId>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
</dependency>
<dependency>

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.circuitbreaker.example;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.GetMapping;
/**
@ -25,7 +26,8 @@ import org.springframework.web.bind.annotation.GetMapping;
*
* @author Haotian Zhang
*/
@FeignClient(name = "polaris-circuitbreaker-example-b", fallback = ProviderBFallback.class)
@Primary
@FeignClient(name = ProviderBFallbackConstant.SERVICE_NAME, fallback = ProviderBFallback.class)
public interface ProviderB {
/**
@ -33,6 +35,6 @@ public interface ProviderB {
*
* @return info of service B
*/
@GetMapping("/example/service/b/info")
@GetMapping(ProviderBFallbackConstant.API)
String info();
}

@ -29,6 +29,6 @@ public class ProviderBFallback implements ProviderB {
@Override
public String info() {
return "trigger the refuse for service b";
return ProviderBFallbackConstant.FALLBACK_MESSAGE;
}
}

@ -0,0 +1,10 @@
package com.tencent.cloud.polaris.circuitbreaker.example;
public class ProviderBFallbackConstant {
public static final String SERVICE_NAME = "polaris-circuitbreaker-example-b";
public static final String API = "/example/service/b/info";
public static final String FALLBACK_MESSAGE = "trigger the refuse for service b";
}

@ -24,6 +24,8 @@ import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.DefaultUriBuilderFactory;
/**
* Circuit breaker example caller application.
@ -41,6 +43,16 @@ public class ServiceA {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory("http://" + ProviderBFallbackConstant.SERVICE_NAME);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(uriBuilderFactory);
return restTemplate;
}
@LoadBalanced
@Bean
WebClient.Builder webClientBuilder() {
return WebClient.builder()
.baseUrl("http://" + ProviderBFallbackConstant.SERVICE_NAME);
}
}

@ -17,12 +17,17 @@
package com.tencent.cloud.polaris.circuitbreaker.example;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
/**
* Circuit breaker example caller controller.
@ -39,6 +44,15 @@ public class ServiceAController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private ReactiveCircuitBreakerFactory reactiveCircuitBreakerFactory;
@Autowired
private CircuitBreakerFactory circuitBreakerFactory;
@Autowired
private WebClient.Builder webClientBuilder;
/**
* Get info of Service B by Feign.
* @return info of Service B
@ -48,9 +62,34 @@ public class ServiceAController {
return polarisServiceB.info();
}
@GetMapping("/getBServiceInfoByRestTemplate1")
public String getBServiceInfoByRestTemplate1() {
return restTemplate.getForObject(ProviderBFallbackConstant.API, String.class);
}
@GetMapping("/getBServiceInfoByRestTemplate")
public String getBServiceInfoByRestTemplate() {
return restTemplate.getForObject("http://polaris-circuitbreaker-example-b/example/service/b/info", String.class);
return circuitBreakerFactory
.create(ProviderBFallbackConstant.SERVICE_NAME)
.run(() ->
restTemplate.getForObject(ProviderBFallbackConstant.API, String.class),
throwable -> ProviderBFallbackConstant.FALLBACK_MESSAGE
);
}
@GetMapping("/getBServiceInfoByWebClient")
public Mono<String> getBServiceInfoByWebClient() {
return webClientBuilder
.build()
.get()
.uri(ProviderBFallbackConstant.API)
.retrieve()
.bodyToMono(String.class)
.transform(it ->
reactiveCircuitBreakerFactory
.create(ProviderBFallbackConstant.SERVICE_NAME)
.run(it, throwable -> Mono.just(ProviderBFallbackConstant.FALLBACK_MESSAGE))
);
}
/**
@ -64,4 +103,5 @@ public class ServiceAController {
String.class);
return entity.getBody();
}
}

@ -15,7 +15,7 @@ spring:
response:
enabled: false
polaris:
address: grpc://183.47.111.80:8091
address: grpc://127.0.0.1:8091
namespace: default
enabled: true
circuitbreaker:
@ -32,3 +32,11 @@ spring:
ignore-internal-server-error: true
series: server_error
statuses: gateway_timeout, bad_gateway, service_unavailable
serivceB:
url: http://localhost:48081
logging:
level:
root: info
com.tencent.cloud: debug

@ -5,7 +5,7 @@ spring:
name: polaris-circuitbreaker-example-b
cloud:
polaris:
address: grpc://183.47.111.80:8091
address: grpc://127.0.0.1:8091
namespace: default
enabled: true
stat:

@ -5,7 +5,7 @@ spring:
name: polaris-circuitbreaker-example-b
cloud:
polaris:
address: grpc://183.47.111.80:8091
address: grpc://127.0.0.1:8091
namespace: default
enabled: true
stat:

Loading…
Cancel
Save