Spring Boot 断路器回退方法不起作用

bqucvtff  于 2022-12-23  发布在  Spring
关注(0)|答案(1)|浏览(140)

我有下面的代码在计费服务微服务:

@RestController
@RequestMapping("/billing")
public class WebController {

    @Autowired
    private BillingService service;
    
    @GetMapping("/hi")
    @CircuitBreaker(name="BillingServiceCapture", fallbackMethod = "hiFallback")
    public String hi() {
        return "Hello Khushboo!";
    }
    
    public String hiFallback() {
        return "Hello Khushboo FallBack!";
    }

应用程序.属性文件:

server.port=9191
spring.h2.console.enable=true
spring.application.name=billing-service
eureka.client.serviceurl.defaultzone=http://localhost:8761/eureka   
eureka.instance.hostname=localhost

management.health.circuitbreakers.enabled=true
#actuator settings
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

resilience4j.circuitbreaker.instances.BillingServiceCapture.registerHealthIndicator=true
resilience4j.circuitbreaker.instances.BillingServiceCapture.eventConsumerBufferSize=10
resilience4j.circuitbreaker.instances.BillingServiceCapture.failureRateThreshold=20
resilience4j.circuitbreaker.instances.BillingServiceCapture.minimumNumberOfCalls=5
resilience4j.circuitbreaker.instances.BillingServiceCapture.automaticTransitionFromOpenToHalfOpenEnabled=true
resilience4j.circuitbreaker.instances.BillingServiceCapture.waitDurationInOpenState=5s
resilience4j.circuitbreaker.instances.BillingServiceCapture.permittedNumberOfCallsInHalfOpenState=3 
resilience4j.circuitbreaker.instances.BillingServiceCapture.slidingWindowSize=10
resilience4j.circuitbreaker.instances.BillingServiceCapture.slidingWindowType=COUNT_BASED

但是,如果我发送获取请求:localhost:8765/billing/您好我收到您好Khushboo的消息。
但是,当我停止BillingService微服务并再次发送相同的请求时,断路器方法不会被调用。
此外,在访问执行器健康状态时,我没有在状态日志中看到我应该看到的断路器信息。
我甚至在OrderService中添加了CircuitBreaker代码,它实际上调用了BillingService:

@CircuitBreaker(name="BillingServiceCapture", fallbackMethod = "getAllBillingDetails")
    public TransactionResponse saveOrder(TransactionRequest request) {
        
        Order order=request.getOrder();
        Billing billing=request.getBilling();
        
        billing.setOrderId(order.getId());
        billing.setAmount(order.getPrice());

        Order ordermade=orderRepo.save(order);
        
        Billing billingresponse=billingproxy.getBillingDone(billing);
        
        TransactionResponse response=null;
        
        String responseStr= billingresponse.getPaymentStatus().equals("success")?"Payment processing successful":"Payment failed";
        response=new TransactionResponse(order, billingresponse.getTransactionId(),billingresponse.getAmount(),responseStr);
        
        return response;
    }
    
    public Billing getAllBillingDetails(Billing bill,Exception e) {
        return new Billing(1000,"pass",101,102,1000);
    }

当我调用http://localhost:8765/order/bookorder时-这会抛出一个500内部服务器异常,但不会调用CircuitBreaker。错误为:

[503] during [GET] to [http://billing-service/billing/preparebill] [BillingProxy#getBillingDone(Billing)]: [Load balancer does not contain an instance for the service billing-service]

请注意,出于测试目的,我没有启动BillingService,因此OrderService Feign无法调用该示例。
任何见解都将不胜感激。
谢谢。

vsdwdz23

vsdwdz231#

回退方法应传递Exception参数:

public String hiFallback(Exception e) {
    return "Hello Khushboo FallBack!";
}

相关问题