Skip to content

Instantly share code, notes, and snippets.

@wenqiglantz
Created February 3, 2022 03:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wenqiglantz/7764326972065174aaf1436cd895d4b1 to your computer and use it in GitHub Desktop.
Save wenqiglantz/7764326972065174aaf1436cd895d4b1 to your computer and use it in GitHub Desktop.
@RestController
@Slf4j
@RequiredArgsConstructor
public class CustomerClientController {
private final WebClient webClient;
private final ReactiveCircuitBreakerFactory reactiveCircuitBreakerFactory;
@PostMapping("/customers")
public Mono<CustomerVO> createCustomer(CustomerVO customerVO){
return webClient.post()
.uri("/customers")
//.header("Authorization", "Bearer MY_SECRET_TOKEN")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(Mono.just(customerVO), CustomerVO.class)
.retrieve()
.bodyToMono(CustomerVO.class)
.timeout(Duration.ofMillis(10_000))
.transform(it -> {
ReactiveCircuitBreaker rcb = reactiveCircuitBreakerFactory.create("customer-service");
return rcb.run(it, throwable -> Mono.just(CustomerVO.builder().build()));
});
}
@GetMapping("/customers/{customerId}")
public Mono<CustomerVO> getCustomer(@PathVariable String customerId) {
return webClient
.get().uri("/customers/" + customerId)
.retrieve()
.bodyToMono(CustomerVO.class)
.transform(it -> {
ReactiveCircuitBreaker rcb = reactiveCircuitBreakerFactory.create("customer-service");
return rcb.run(it, throwable -> Mono.just(CustomerVO.builder().build()));
});
}
@PutMapping("/customers/{customerId}")
public Mono<CustomerVO> updateCustomer(@PathVariable String customerId, CustomerVO customerVO){
return webClient.put()
.uri("/customers/" + customerVO.getCustomerId())
//.header("Authorization", "Bearer MY_SECRET_TOKEN")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(Mono.just(customerVO), CustomerVO.class)
.retrieve()
.bodyToMono(CustomerVO.class)
.transform(it -> {
ReactiveCircuitBreaker rcb = reactiveCircuitBreakerFactory.create("customer-service");
return rcb.run(it, throwable -> Mono.just(CustomerVO.builder().build()));
});
}
@DeleteMapping("/customers/{customerId}")
public Mono<String> deleteCustomer(@PathVariable String customerId){
return webClient.delete()
.uri("/customers/" + customerId)
.retrieve()
.bodyToMono(String.class)
.transform(it -> {
ReactiveCircuitBreaker rcb = reactiveCircuitBreakerFactory.create("customer-service");
return rcb.run(it, throwable -> Mono.just(customerId));
});
}
}
@matador333iqp
Copy link

Deleted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment