当前位置: 首页 > news >正文

【OpenFeign】OpenFeign结合Hystrix和Sentinel实现熔断降级

OpenFeign可以与Hystrix和Sentinel结合使用,实现降级和熔断。

OpenFeign与Hystrix结合使用

使用OpenFeign需要引入OpenFeign的依赖:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

spring-cloud-starter-openfeign引入的依赖如下:

[INFO] +- org.springframework.cloud:spring-cloud-starter-openfeign:jar:2.2.6.RELEASE:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-openfeign-core:jar:2.2.6.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-aop:jar:2.3.2.RELEASE:compile
[INFO] |  |  |  \- org.aspectj:aspectjweaver:jar:1.9.6:compile
[INFO] |  |  \- io.github.openfeign.form:feign-form-spring:jar:3.8.0:compile
[INFO] |  |     +- io.github.openfeign.form:feign-form:jar:3.8.0:compile
[INFO] |  |     \- commons-fileupload:commons-fileupload:jar:1.4:compile
[INFO] |  +- io.github.openfeign:feign-core:jar:10.10.1:compile
[INFO] |  +- io.github.openfeign:feign-slf4j:jar:10.10.1:compile
[INFO] |  \- io.github.openfeign:feign-hystrix:jar:10.10.1:compile
[INFO] |     +- com.netflix.archaius:archaius-core:jar:0.7.6:compile
[INFO] |     \- com.netflix.hystrix:hystrix-core:jar:1.5.18:compile
[INFO] |        \- org.hdrhistogram:HdrHistogram:jar:2.1.9:compile

默认已经自动引入了hystrix的依赖,不再需要单独再引入hystrix了。

使用fallback实现降级方法

降级方法的类需要实现FeignClient的接口,同时这个类需要注入到Spring容器中:

package com.morris.user.client;import com.morris.user.entity.Order;
import com.morris.user.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.util.Collections;
import java.util.List;
import java.util.Map;@Service
@Slf4j
public class OrderFeignService implements OrderClient {@Overridepublic List<Order> findOrderByUserId(Long userId) {log.error("findOrderByUserIdFall error {}", userId);return Collections.emptyList();}
}

@FeignClient注解中指定fallback属性:

@FeignClient(value = "order-service", path = "/order", fallback = OrderFeignService.class)
public interface OrderClient {
... ...

在配置文件中开启hystrix:

feign:hystrix:enabled: true

使用fallbackFactory实现降级方法

使用fallback实现降级方法无法获取到异常信息,而使用fallbackFactory实现降级方法可以获取到异常信息。

降级方法的工厂类需要实现FallbackFactory的接口,同时这个类需要注入到Spring容器中:

package com.morris.user.client;import com.morris.user.entity.Order;
import com.morris.user.entity.User;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.util.Collections;
import java.util.List;
import java.util.Map;@Slf4j
@Service
public class OrderFeignFactory implements FallbackFactory {@Overridepublic Object create(Throwable throwable) {log.error("OrderFeignFactory ", throwable);return new OrderClient() {@Overridepublic List<Order> findOrderByUserId(Long userId) {return Collections.emptyList();}}}
}

@FeignClient注解中指定fallback属性:

@FeignClient(value = "order-service", path = "/order", fallbackFactory = OrderFeignFactory.class)
public interface OrderClient {
... ...

在配置文件中开启hystrix:

feign:hystrix:enabled: true

Hystrix熔断的使用

Hystrix熔断的功能是默认开启的,commandKey为类名#方法名(参数类型),例如上面的方法对应的commandKey为OrderClient#findOrderByUserId(Long)

可以在配置文件中根据commandKey对熔断的一些参数进行设置:

hystrix:command:OrderClient#findOrderByUserId(Long):circuitBreaker:enabled: falserequestVolumeThreshold: 2execution:timeout:enabled: trueisolation:thread:#设置请求超时时间,默认1秒,超过指定的时间后,触发服务熔断timeoutInMilliseconds: 10000

OpenFeign与Sentinel结合使用

引入Sentinel的依赖:

<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

并开启Sentinel的熔断降级功能:

spring:sentinel:transport:dashboard: 127.0.0.1:8080feign:sentinel:enabled: true

降级的使用还是跟前面的一样,使用fallback和fallbackFactory实现。

Sentinel熔断的使用

虽然熔断的功能开启了,但是需要配置熔断规则,没有配置规则是不会触发熔断的。

配置如下降级规则测试熔断:

[{"count": 0.5,"grade": 1,"limitApp": "default","minRequestAmount": 5,"resource": "GET:http://order-service/order/findOrderByUserId","slowRatioThreshold": 1,"statIntervalMs": 5000,"timeWindow": 5000}
]

自定义全局异常

OpenFeign可以配置一个全局异常,来对请求过程中的其他异常进行包装,这样在fallbackFactory中获取到的是自定义的全局异常,而不是原始的异常。

package com.morris.user.config;import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import feign.form.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;import java.io.IOException;
import java.io.Reader;
import java.text.MessageFormat;@Configuration
@Slf4j
public class FeignErrorDecoder implements ErrorDecoder {@Overridepublic Exception decode(String methodKey, Response response) {Reader reader = null;try {reader = response.body().asReader(CharsetUtil.UTF_8);String errMsg = Util.toString(reader);log.error("FeignErrorDecoder {}", errMsg);return new RuntimeException(errMsg);} catch (IOException e) {return new RuntimeException(MessageFormat.format("自定义Feign错误信息出错:{0}", e.getMessage()));} finally {if (null != reader) {try {reader.close();} catch (IOException e) {e.printStackTrace();}}}}
}
http://www.lryc.cn/news/151042.html

相关文章:

  • 软件工程(十) 需求工程之需求开发与管理
  • C++网狐服务器引入开源日志库spdlog
  • 【C++】—— c++11之智能指针
  • html5——前端笔记
  • 如何在 Vue TypeScript 项目使用 emits 事件
  • 文件操作 黑马教程(04)
  • Jmeter(二十七):BeanShell PostProcessor跨线程全局变量使用
  • 手写表格OCR识别并与大模型ChatGPT交互?
  • 使用 v-for 指令和数组来实现在 Uni-app 中动态增减表单项并渲染多个数据
  • xml
  • Java中的动态代理(JDK Proxy VS CGLib)
  • Redis 7 第七讲 哨兵模式(sentinal)
  • Python入门教程 - 判断语句(二)
  • LeetCode-55-跳跃游戏-贪心
  • 【USRP】调制解调系列4:BPSK、QPSK、8PSK、OQPSK、Pi/4DQPSK,基于labview的实现
  • 深入探讨梯度下降:优化机器学习的关键步骤(一)
  • layui框架学习(40:数据表格_主要事件)
  • kotlin实现猜数游戏
  • 51单片机项目(8)——基于51单片机的DS1302时钟系统
  • 高频策略:做市商与逆向选择
  • Valgrind内存诊断工具的使用笔记
  • docker安装Nacos
  • 【Linux】线程安全-死锁
  • pdf转换成图片免费软件用哪个?pdf转换成图片就用它
  • 【LeetCode】《LeetCode 101》第十二章:字符串
  • Android去掉视频声音
  • java-thread-affinity线程绑核
  • Springboot - 5.test集成
  • 弯道超车必做好题集锦三(C语言编程题)
  • JavaScript基础语法03——JS注释、结束符