0

有条件地我想通过设置来关闭/打开断路器开关spring.cloud.circuitbreaker.resilience4j.enabled=false。我的逻辑应该不受断路器逻辑的影响。

spring.cloud.circuitbreaker.resilience4j.enabled=true我尝试使用下面的演示示例来扩展我的要求,我试图根据inapplication.property和case中true的断路器标志绑定目标方法上的断路器调用。false可能有一种更简单的方法来实现这一点,如果有任何其他解决方案而不是我尝试的解决方案,请帮助我。

示例: spring cloud circuit-breaker-resilency4j 示例

尝试调用快乐路径 - 没有异常时工作正常[响应在 3 秒内出现,因为时间限制器在 bean 创建中设置为 3 秒]

应用程序属性:

spring.cloud.circuitbreaker.resilience4j.enabled=true
spring.cloud.config.enabled=false
spring.cloud.config.import-check.enabled=false
spring.main.allow-bean-definition-overriding=true

控制器:

@GetMapping("/delay/{seconds}")
public Map delay(@PathVariable int seconds) {
   return mockService.delay(seconds);
}

模拟服务:

@ApplyCircuitBreaker
public Map delay(int seconds) {
    return rest.getForObject("https://httpbin.org/delay/" + seconds, Map.class);
}

配置类:

@Configuration
@ConditionalOnProperty(name = { "spring.cloud.circuitbreaker.resilience4j.enabled"}, matchIfMissing = true)
public class ResiliencyConfig {

    @Bean
    public Customizer<Resilience4JCircuitBreakerFactory> defaultCustomizer() {
        return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
            .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(3)).build())
            .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
            .build());
    }
}

ApplyCircuitBreaker - 自定义注释仅适用于所需方法:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ApplyCircuitBreaker {
}

AOP:CircuitBreakerAroundAspect:

@Aspect
@Component
@ConditionalOnProperty(name = { "spring.cloud.circuitbreaker.resilience4j.enabled",
    "spring.cloud.circuitbreaker.resilience4j.reactive.enabled" }, matchIfMissing = true)
public class CircuitBreakerAroundAspect {

    @Autowired
    CircuitBreakerFactory circuitBreakerFactory;

    @Around("@annotation(com.ravibeli.circuitbreaker.aspects.ApplyCircuitBreaker)")
    public Object aroundAdvice(final ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("Arguments passed to method are: {}", Arrays.toString(joinPoint.getArgs()));
        AtomicReference<Map<String, String>> fallback = new AtomicReference<>();
        Object proceed = circuitBreakerFactory.create(joinPoint.getSignature().toString())
            .run(() -> {
                try {
                    log.info("Inside CircuitBreaker logic in Aspect");
                    return joinPoint.proceed();
                } catch (Throwable t) {
                    log.error(t.getMessage());
                }
                return null;
            }, Throwable::getMessage);
        log.info("Result from method is: {}", proceed);
        return proceed;
    }
}

我的要求: circuitBreakerFactory.create(joinPoint.getSignature().toString()) .run(() -> ....)在这一行,当目标方法抛出异常时,controll应该去回退机制调用。由于 joinPoint.proceed() 抛出异常,它强制处理异常 - 所以我在这里做错了,需要建议来解决这个问题以解决要求。

错误日志:

{
    "timestamp": "2021-07-10T01:33:10.558+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "trace": "java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.Map (java.lang.String and java.util.Map are in module java.base of loader 'bootstrap')\r\n\tat com.ravibeli.circuitbreaker.service.MockService$$EnhancerBySpringCGLIB$$3e293bd0.delay(<generated>)\r\n\tat com.ravibeli.circuitbreaker.controllers.DemoController.delay(DemoController.java:53)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:566)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1063)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:655)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:764)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:228)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1723)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\r\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.base/java.lang.Thread.run(Thread.java:834)\r\n",
    "message": "class java.lang.String cannot be cast to class java.util.Map (java.lang.String and java.util.Map are in module java.base of loader 'bootstrap')",
    "path": "/delay/3"
}
4

3 回答 3

2

你似乎在这里问了几个不同的问题。

标题似乎在问为什么当 spring.cloud.circuitbreaker.resilience4j.enabled=false

问题在于你的条件

@ConditionalOnProperty(name = { "spring.cloud.circuitbreaker.resilience4j.enabled",
    "spring.cloud.circuitbreaker.resilience4j.reactive.enabled" }, matchIfMissing = true)

它只是要求该属性存在,而不是检查它的设置。你也需要设置havingValue=true

也就是说,我强烈建议不要为断路器创建自己的切入点。使用 Resiliancy4j 提供的注释并在那里指定回退方法。我希望这可以解决您在回退时遇到的任何其他问题。

@Bulkhead(name = 'myService', fallbackMethod = "myFallback")
@CircuitBreaker(name = 'myService', fallbackMethod = "myFallback")
@RateLimiter(name = 'myService', fallbackMethod = "myFallback")
@TimeLimiter(name = 'myService', fallbackMethod = "myFallback")
于 2021-07-10T16:50:59.163 回答
1
  1. 要动态启用断路器,您可以使用配置文件或外部化配置(首选方法是使用配置文件,您可以在谷歌上搜索更多关于它们的信息)
  2. 就您方面的代码而言,它对我来说看起来和运行良好。链接到代码。如果您可以共享代码库的链接,这样可以进一步调查问题,那会更好。然而,这似乎是一个小问题。
于 2021-07-10T16:52:40.497 回答
0

谢谢,伙计们的评论,有一个简单的想法来解决这个问题。我通过自定义工厂实现解决了它,以使启用/禁用功能正常工作。

我的 GitHub 示例代码: spring-cloud-resiliency4j

于 2021-07-11T13:29:29.100 回答