文章目录
前言
哈喽,我是会写bug的要饭的。本次是在gateway中集成hystrix,就是针对不同的接口设定不同的超时时间。时间参数是从数据库抓取
一、hystrix是什么?
Hystrix 就是一个可以通过使用 延时策略 和 故障容错逻辑 帮助您管理控制这些分布式服务之间交互的一个库. Hystrix 通过 服务隔离,停止故障服务级联调用并提供应急计划来改善系统的弹性.。让整个系统拥有自我保护能力的组件。
二、基础使用
我本来想写基础使用的,但感觉有点懒,特别懒,还要调试一下,算了,网上一大把,自己搜吧
三、gateway集成hystrix
正文
3.1pom中引入hystrix的jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
对,就这其他的都是springboot、springcloud、gateway、redis、、、自己添加喽。
3.2 引入hystrix组件
先上代码
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts;
import java.math.BigDecimal;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixObservableCommand;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import reactor.core.publisher.Mono;
import rx.Observable;
import rx.RxReactiveStre