微服务Springcloud——Gateway(二) 集成hystrix(自定义接口熔断时间)

本文介绍了如何在Springcloud Gateway中集成Hystrix,通过配置实现不同接口的定制化超时时间。内容包括在pom中引入Hystrix依赖,配置Hystrix,以及详细讲解了如何在yml文件中设置接口超时,实现服务的熔断和降级处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

哈喽,我是会写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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值