refactor example

pull/875/head
seanyu 3 years ago
parent 254f91e191
commit cbcead40f4

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

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

@ -29,6 +29,6 @@ public class ProviderBFallback implements ProviderB {
@Override @Override
public String info() { 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.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; 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. * Circuit breaker example caller application.
@ -41,6 +43,16 @@ public class ServiceA {
@Bean @Bean
@LoadBalanced @LoadBalanced
public RestTemplate restTemplate() { 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; package com.tencent.cloud.polaris.circuitbreaker.example;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired; 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.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
/** /**
* Circuit breaker example caller controller. * Circuit breaker example caller controller.
@ -39,6 +44,15 @@ public class ServiceAController {
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
@Autowired
private ReactiveCircuitBreakerFactory reactiveCircuitBreakerFactory;
@Autowired
private CircuitBreakerFactory circuitBreakerFactory;
@Autowired
private WebClient.Builder webClientBuilder;
/** /**
* Get info of Service B by Feign. * Get info of Service B by Feign.
* @return info of Service B * @return info of Service B
@ -48,9 +62,34 @@ public class ServiceAController {
return polarisServiceB.info(); return polarisServiceB.info();
} }
@GetMapping("/getBServiceInfoByRestTemplate1")
public String getBServiceInfoByRestTemplate1() {
return restTemplate.getForObject(ProviderBFallbackConstant.API, String.class);
}
@GetMapping("/getBServiceInfoByRestTemplate") @GetMapping("/getBServiceInfoByRestTemplate")
public String 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); String.class);
return entity.getBody(); return entity.getBody();
} }
} }

@ -5,7 +5,7 @@ spring:
name: polaris-circuitbreaker-example-a name: polaris-circuitbreaker-example-a
cloud: cloud:
polaris: polaris:
address: grpc://183.47.111.80:8091 address: grpc://127.0.0.1:8091
namespace: default namespace: default
enabled: true enabled: true
circuitbreaker: circuitbreaker:
@ -36,3 +36,8 @@ feign:
serivceB: serivceB:
url: http://localhost:48081 url: http://localhost:48081
logging:
level:
root: info
com.tencent.cloud: debug

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

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

Loading…
Cancel
Save