问题描述
我正在制作一个 springboot 应用程序,其中我的微服务将事件发送到队列。 如果队列关闭,我想实现断路器。基本上,如果 10 个请求中有 5 个失败,我想打开电路并将错误返回给前端而不处理新请求。
@Autowired
CircuitBreakerFactory circuitBreakerFactory;
@PatchMapping(value = MY_URL,consumes = {MediaType.APPLICATION_JSON },produces = MediaType.APPLICATION_JSON)
public ResponseEntity<MyResponse> executePatch(@UserPrincipal UserContext userContext,@RequestBody List<MyAction> myActions) {
//some code
publishEvent(myEvent);
//some code
}
private void publishEvent(MyEvent myEvent){
CircuitBreaker circuitBreaker = circuitBreakerFactory.create("myCircuitBreaker");
circuitBreaker.run(() -> myEventPublisher.publish(applicationProperties.getQueue(),myEvent),throwable -> logalertEvent(myEvent));
}
private void logalertEvent(MyEvent myEvent){
LOGGER.error("Event Could not be published: {}",myEvent);
}
依赖于 pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
application.properties 中的配置
spring.cloud.circuitbreaker.resilience4j.enabled=false
我收到以下编译错误:
Multiple markers at this line
- The method run(supplier<T>,Function<Throwable,T>) in the type CircuitBreaker is not applicable for the arguments (() -> {},(<no type>
throwable) -> {})
- Cannot return a void result
- Cannot return a void result
我知道我在这里不提供供应商,但 run() 只接受供应商。 我如何将断路器包装在这里,因为我的 myEventPublisher.publish() 返回无效并且不是供应商。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)