微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Spring Boot 中使用带有 WebFlux 的 Resilience4j 断路器

如何解决如何在 Spring Boot 中使用带有 WebFlux 的 Resilience4j 断路器

我有服务 A 调用下游服务 B。

服务 A 代码

@RestController
@RequestMapping(value = "",produces = MediaType.APPLICATION_JSON_VALUE)
public class GreetingController {

    private final GreetingService greetingService;

    public GreetingController(GreetingService greetingService){
        this.greetingService = greetingService;
    }

    @GetMapping(value = "/greetings")
    public Mono<String> getGreetings() {
        return greetingService.callServiceB();
    }
}

@Component
@requiredArgsConstructor
public class GreetingService {
    
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("greetingService");
    Callable<Mono<String>> callable = CircuitBreaker.decorateCallable(circuitBreaker,this::clientCall);
    Future<Mono<String>> future = Executors.newSingleThreadExecutor().submit(callable);

    public Mono<String> callServiceB() {
        try {
            return future.get();
        } catch (CircuitBreakerOpenException | InterruptedException | ExecutionException ex){
            return Mono.just("Service is down!");
        }
    }


    private final String url = "/v1/holidaysgreetings";
    
    private Mono<String> clientCall(){
        WebClient client = WebClient.builder().baseUrl("http://localhost:8080").build();
        
        return client
                .get()
                .uri(url)
                .retrieve()
                .bodyToMono(String.class);
}

当我关闭下游服务 B(在 localhost:8080 上运行)并点击 /greetings 类中的 GreetingsController 端点以查看我的断路器是否正常工作时,我得到了这个令人讨厌的错误

2021-06-28 21:27:31.431 ERROR 10285 --- [nio-8081-exec-7] o.a.c.c.C.[.[.[.[dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [/v1/holidaysgreetings] 
threw exception [Request processing Failed; nested exception is org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: localhost/127.0.0.1:8080; 
nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:8080] with root cause

java.net.ConnectException: Connection refused

有人知道我为什么会收到这个吗?我在这里缺少什么?我是否正确实施了断路器?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。