Skip to content

Commit 4bb1a7c

Browse files
author
YunaiV
committed
resilience4j + CircuitBreaker
1 parent 7f23fa2 commit 4bb1a7c

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-59</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-59-resilience4j-demo01</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-parent</artifactId>
25+
<version>${spring.boot.version}</version>
26+
<type>pom</type>
27+
<scope>import</scope>
28+
</dependency>
29+
</dependencies>
30+
</dependencyManagement>
31+
32+
<dependencies>
33+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
39+
<!-- 引入 Resilience4j Starter 相关依赖,并实现对其的自动配置 -->
40+
<dependency>
41+
<groupId>io.github.resilience4j</groupId>
42+
<artifactId>resilience4j-spring-boot2</artifactId>
43+
<version>1.4.0</version>
44+
</dependency>
45+
46+
<!-- 引入 Aspectj 依赖,支持 AOP 相关注解、表达式等等 -->
47+
<dependency>
48+
<groupId>org.aspectj</groupId>
49+
<artifactId>aspectjrt</artifactId>
50+
<version>1.9.5</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.aspectj</groupId>
54+
<artifactId>aspectjweaver</artifactId>
55+
<version>1.9.5</version>
56+
</dependency>
57+
58+
</dependencies>
59+
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.iocoder.springboot.lab59.resillience4jdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
@SpringBootApplication
9+
public class DemoApplication {
10+
11+
@Bean
12+
public RestTemplate restTemplate() {
13+
return new RestTemplate();
14+
}
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(DemoApplication.class, args);
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.iocoder.springboot.lab59.resillience4jdemo.controller;
2+
3+
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
import org.springframework.web.client.RestTemplate;
12+
13+
@RestController
14+
@RequestMapping("/demo")
15+
public class DemoController {
16+
17+
private Logger logger = LoggerFactory.getLogger(getClass());
18+
19+
@Autowired
20+
private RestTemplate restTemplate;
21+
22+
@GetMapping("/get_user")
23+
@CircuitBreaker(name = "backendA", fallbackMethod = "getUserFallback")
24+
public String getUser(@RequestParam("id") Integer id) {
25+
logger.info("[getUser][准备调用 user-service 获取用户({})详情]", id);
26+
return restTemplate.getForEntity("http://127.0.0.1:18080/user/get?id=" + id, String.class).getBody();
27+
}
28+
29+
public String getUserFallback(Integer id, Throwable throwable) {
30+
logger.info("[getUserFallback][id({}) exception({})]", id, throwable.getClass().getSimpleName());
31+
return "mock:User:" + id;
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
resilience4j:
2+
# Resilience4j 的断路器配置项,对应 CircuitBreakerProperties 属性类
3+
circuitbreaker:
4+
instances:
5+
backendA:
6+
ring-buffer-size-in-closed-state: 5 # 熔断器关闭状态的缓冲区大小,不会限制线程的并发量,在熔断器发生状态转换前所有请求都会调用后端服务。
7+
ring-buffer-size-in-half-open-state: 5 # 熔断器半开状态的缓冲区大小,会限制线程的并发量。例如,缓冲区为 10 则每次只会允许 10 个请求调用后端服务。
8+
wait-duration-in-open-state : 5000 # 熔断器从打开状态转变为半开状态等待的时间,单位:毫秒
9+
failure-rate-threshold: 50 # 熔断器关闭状态和半开状态使用的同一个失败率阈值,单位:百分比
10+
11+
register-health-indicator: true # 是否注册到健康监测

lab-59/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
<modules>
1616
<module>lab-59-user-service</module>
17+
<module>lab-59-resilience4j-demo01</module>
1718
</modules>
1819

1920
</project>

0 commit comments

Comments
 (0)