问题描述
我正在尝试在我的 Spring Boot 应用程序中实现断路器。 我做了 2 个最低限度的微服务来测试断路器的实现。
这是我的代码。
微服务A:
application.properties
server.port=8076
resilience4j.circuitbreaker.instances.recordstore.sliding-window-size=4
resilience4j.circuitbreaker.instances.recordstore.minimum-number-of-calls=2
resilience4j.circuitbreaker.instances.recordstore.failure-rate-threshold=70
resilience4j.circuitbreaker.instances.recordstore.automatic-transition-from-open-to-half-open-enabled=true
#resilience4j.circuitbreaker.instances.recordstore.enable-exponential-backoff=true
resilience4j.circuitbreaker.instances.recordstore.permitted-number-of-calls-in-half-open-state=2
resilience4j.circuitbreaker.instances.recordstore.sliding-window-type=COUNT_BASED
resilience4j.circuitbreaker.instances.recordstore.wait-duration-in-open-state=10
resilience4j.circuitbreaker.instances.recordstore.writable-stack-trace-enabled=true
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>A</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>A</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.7.1</version>
</dependency>
<!-- <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-timelimiter</artifactId>
<version>1.7.0</version> </dependency> -->
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置
@Configuration
public class AConfig {
@Value("${resilience4j.circuitbreaker.instances.recordstore.minimum-number-of-calls}")
private int minimumNumberOfCalls;
@Value("${resilience4j.circuitbreaker.instances.recordstore.sliding-window-size}")
private int slidingWindowSize;
@Value("${resilience4j.circuitbreaker.instances.recordstore.sliding-window-type}")
private SlidingWindowType slidingWindowType;
@Value("${resilience4j.circuitbreaker.instances.recordstore.wait-duration-in-open-state}")
private int waitDurationInopenState;
@Value("${resilience4j.circuitbreaker.instances.recordstore.failure-rate-threshold}")
private int failureRateThreshold;
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public CircuitBreaker circuitBreaker() {
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.slidingWindowSize(slidingWindowSize)
.slidingWindowType(slidingWindowType)
.waitDurationInopenState(Duration.ofSeconds(waitDurationInopenState))
.failureRateThreshold(failureRateThreshold)
.writableStackTraceEnabled(true)
.enableAutomaticTransitionFromOpenToHalfopen()
.automaticTransitionFromOpenToHalfOpenEnabled(true)
.build();
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);
return circuitBreakerRegistry.circuitBreaker("recordstore",circuitBreakerConfig) ;
}
}
控制器
@RestController
public class AController {
private static final Logger LOGGER = LoggerFactory.getLogger(AController.class);
@Autowired
RestTemplate restTemplate;
@Autowired
CircuitBreaker circuitBreaker;
@GetMapping("/test/{id}")
public void test(@PathVariable Integer id) {
System.out.println(" id :::" + id);
supplier<String> supplier = () -> restTemplate.getForObject("http://localhost:8077/read",String.class);
supplier<String> decoratedsupplier = circuitBreaker.decoratesupplier(supplier);
Try<String> response = Try.ofsupplier(decoratedsupplier).recover(throwable -> this.logId(id));
LOGGER.info("Response for id::: {} ...is.... {}",id,response);
}
private String logId(Integer id) {
System.out.println("Logging id for RTB");
return "manual intervention needed";
}
}
微服务B代码如下:
@RestController
public class BController {
@GetMapping("/read")
public String read() {
System.out.println("in B");
return "Successsssssss";
}
}
这是我的日志。最初微服务 B 已启动,我发出 3 个请求,然后我关闭微服务 B 并再发出 6 个请求。
2021-07-17 17:31:33.197 INFO 9904 --- [ main] com.example.demo.AApplication : Started AApplication in 3.808 seconds (JVM running for 4.722)
2021-07-17 17:31:53.562 INFO 9904 --- [nio-8076-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring dispatcherServlet 'dispatcherServlet'
2021-07-17 17:31:53.562 INFO 9904 --- [nio-8076-exec-1] o.s.web.servlet.dispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-07-17 17:31:53.564 INFO 9904 --- [nio-8076-exec-1] o.s.web.servlet.dispatcherServlet : Completed initialization in 1 ms
id :::1
2021-07-17 17:31:53.948 INFO 9904 --- [nio-8076-exec-1] com.example.demo.AController : Response for id::: 1 ...is.... Success(Successsssssss)
id :::1
2021-07-17 17:31:57.466 INFO 9904 --- [nio-8076-exec-2] com.example.demo.AController : Response for id::: 1 ...is.... Success(Successsssssss)
id :::1
2021-07-17 17:32:11.553 INFO 9904 --- [nio-8076-exec-3] com.example.demo.AController : Response for id::: 1 ...is.... Success(Successsssssss)
id :::1
Logging id for RTB
2021-07-17 17:32:21.132 INFO 9904 --- [nio-8076-exec-4] com.example.demo.AController : Response for id::: 1 ...is.... Success(manual intervention needed)
id :::2
Logging id for RTB
2021-07-17 17:32:27.915 INFO 9904 --- [nio-8076-exec-5] com.example.demo.AController : Response for id::: 2 ...is.... Success(manual intervention needed)
id :::3
Logging id for RTB
2021-07-17 17:32:38.345 INFO 9904 --- [nio-8076-exec-6] com.example.demo.AController : Response for id::: 3 ...is.... Success(manual intervention needed)
id :::4
Logging id for RTB
2021-07-17 17:32:42.706 INFO 9904 --- [nio-8076-exec-7] com.example.demo.AController : Response for id::: 4 ...is.... Success(manual intervention needed)
id :::5
Logging id for RTB
2021-07-17 17:32:47.001 INFO 9904 --- [nio-8076-exec-8] com.example.demo.AController : Response for id::: 5 ...is.... Success(manual intervention needed)
id :::6
Logging id for RTB
2021-07-17 17:32:56.484 INFO 9904 --- [nio-8076-exec-9] com.example.demo.AController : Response for id::: 6 ...is.... Success(manual intervention needed)
我现在已经投入了 4 天,但无法理解为什么这个简单的事情不起作用。为什么我的电路打不开。我感到非常沮丧。请知道这个的人帮助我。我会非常感谢你。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)