赞
踩
熔断降级是通过对服务之间的依赖关系进行管理来实现的。当一个服务调用另一个服务时,如果被调用的服务出现故障或响应时间过长,调用方可以使用熔断器来中断对该服务的调用,从而避免整个系统的瘫痪。
案例使用的熔断依赖包为springCloud自带的,Hystrix已经不维护了,推荐使用SpringCloud自带依赖,以下为springboot及springCloud的版本生命,及熔断的依赖包
-
- <!-- SpringBoot的依赖配置-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>2.6.11</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
-
- <!-- springCloud版本 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>2021.0.1</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
-
- <!-- springCloud版本对应的alibabaCloud版本 -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>2021.0.4.0</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
-
-
- <!-- 熔断降级的依赖包 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
- </dependency>

增加自定义熔断配置类
- package com.zhangzz.gateway.config;
-
- import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
- import io.github.resilience4j.timelimiter.TimeLimiterConfig;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.circuitbreaker.resilience4j.ReactiveResilience4JCircuitBreakerFactory;
- import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder;
- import org.springframework.cloud.client.circuitbreaker.Customizer;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import java.time.Duration;
-
- /**
- * 自定义熔断配置 设置默认超时时间为30s
- *
- * @author 大蒜是只猫
- * @since 2023/6/24
- */
- @Configuration
- public class Resilience4JConfig {
-
- @Value("${resilience.timeout:30}")
- private int timeOut ;
-
- @Bean
- public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
- TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
- .timeoutDuration(Duration.ofSeconds(timeOut))
- .build();
- return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
- .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
- .timeLimiterConfig(timeLimiterConfig)
- .build());
- }
- }

自定义熔断后的响应路径
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import reactor.core.publisher.Mono;
-
- @RestController
- public class FallbackHandler {
-
- @RequestMapping("/error")
- public Mono<String> error() {
- return Mono.just("Service Unreachable");
- }
- }
因为我的路由信息采用动态路由,全部由前端http请求添加的,springCloud之Gateway动态路由_昵称无限重复的博客-CSDN博客可参考我的上一篇。以下为动态路由的实现参数:
- {
- "id":"test1",
- "name":"routeTest1",
- "description":"路由测试1",
- "order":1,
- "predicateDefinitions":[
- {
- "name":"Path",
- "args":{
- "_genkey_0":"/b3/**",
- "_genkey_1":"/b4/**"
- }
- }
- ],
- "filters":[
- {
- "name":"StripPrefix",
- "args":{
- "_genkey_0":"1"
- }
- },
- {
- "name":"CircuitBreaker", //固定名称
- "args":{
- "name":"defaultBreaker", //map的key值,名称任意,但全局不可重复
- "fallbackUri":"forward:/error" //进入熔断后的响应路径
- }
- }
- ],
- "url":"lb://content"
-
- }

启动服务后,输出地址。http://localhost/b3/get,可在/get方法中,使用线程等待,使其超过30秒,就可以看见熔断错误页面的响应。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。