diff --git a/lab-59/lab-59-resilience4j-demo01/pom.xml b/lab-59/lab-59-resilience4j-demo01/pom.xml new file mode 100644 index 00000000..09f31b34 --- /dev/null +++ b/lab-59/lab-59-resilience4j-demo01/pom.xml @@ -0,0 +1,60 @@ + + + + lab-59 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + lab-59-resilience4j-demo01 + + + 1.8 + 1.8 + 2.2.4.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring.boot.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + io.github.resilience4j + resilience4j-spring-boot2 + 1.4.0 + + + + + org.aspectj + aspectjrt + 1.9.5 + + + org.aspectj + aspectjweaver + 1.9.5 + + + + + diff --git a/lab-59/lab-59-resilience4j-demo01/src/main/java/cn/iocoder/springboot/lab59/resillience4jdemo/DemoApplication.java b/lab-59/lab-59-resilience4j-demo01/src/main/java/cn/iocoder/springboot/lab59/resillience4jdemo/DemoApplication.java new file mode 100644 index 00000000..9f482e9d --- /dev/null +++ b/lab-59/lab-59-resilience4j-demo01/src/main/java/cn/iocoder/springboot/lab59/resillience4jdemo/DemoApplication.java @@ -0,0 +1,20 @@ +package cn.iocoder.springboot.lab59.resillience4jdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +@SpringBootApplication +public class DemoApplication { + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + +} diff --git a/lab-59/lab-59-resilience4j-demo01/src/main/java/cn/iocoder/springboot/lab59/resillience4jdemo/controller/DemoController.java b/lab-59/lab-59-resilience4j-demo01/src/main/java/cn/iocoder/springboot/lab59/resillience4jdemo/controller/DemoController.java new file mode 100644 index 00000000..37882518 --- /dev/null +++ b/lab-59/lab-59-resilience4j-demo01/src/main/java/cn/iocoder/springboot/lab59/resillience4jdemo/controller/DemoController.java @@ -0,0 +1,34 @@ +package cn.iocoder.springboot.lab59.resillience4jdemo.controller; + +import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +@RequestMapping("/demo") +public class DemoController { + + private Logger logger = LoggerFactory.getLogger(getClass()); + + @Autowired + private RestTemplate restTemplate; + + @GetMapping("/get_user") + @CircuitBreaker(name = "backendA", fallbackMethod = "getUserFallback") + public String getUser(@RequestParam("id") Integer id) { + logger.info("[getUser][准备调用 user-service 获取用户({})详情]", id); + return restTemplate.getForEntity("http://127.0.0.1:18080/user/get?id=" + id, String.class).getBody(); + } + + public String getUserFallback(Integer id, Throwable throwable) { + logger.info("[getUserFallback][id({}) exception({})]", id, throwable.getClass().getSimpleName()); + return "mock:User:" + id; + } + +} diff --git a/lab-59/lab-59-resilience4j-demo01/src/main/resources/application.yml b/lab-59/lab-59-resilience4j-demo01/src/main/resources/application.yml new file mode 100644 index 00000000..2204a060 --- /dev/null +++ b/lab-59/lab-59-resilience4j-demo01/src/main/resources/application.yml @@ -0,0 +1,11 @@ +resilience4j: + # Resilience4j 的断路器配置项,对应 CircuitBreakerProperties 属性类 + circuitbreaker: + instances: + backendA: + ring-buffer-size-in-closed-state: 5 # 熔断器关闭状态的缓冲区大小,不会限制线程的并发量,在熔断器发生状态转换前所有请求都会调用后端服务。 + ring-buffer-size-in-half-open-state: 5 # 熔断器半开状态的缓冲区大小,会限制线程的并发量。例如,缓冲区为 10 则每次只会允许 10 个请求调用后端服务。 + wait-duration-in-open-state : 5000 # 熔断器从打开状态转变为半开状态等待的时间,单位:毫秒 + failure-rate-threshold: 50 # 熔断器关闭状态和半开状态使用的同一个失败率阈值,单位:百分比 + + register-health-indicator: true # 是否注册到健康监测 diff --git a/lab-59/pom.xml b/lab-59/pom.xml index e9d36038..b05e21fa 100644 --- a/lab-59/pom.xml +++ b/lab-59/pom.xml @@ -14,6 +14,7 @@ lab-59-user-service + lab-59-resilience4j-demo01