博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于 Hystrix 高并发服务限流第 4 篇 —— 基于 Feign 实现服务熔断降级处理
阅读量:2094 次
发布时间:2019-04-29

本文共 4867 字,大约阅读时间需要 16 分钟。

查看之前的博客可以点击顶部的【分类专栏】

 

我们在之前的项目基础上,新建一个模块:feign-server

因为 openfeign 默认添加了 Hystrix 的依赖,因此我们无需显示的增加 Hystrix 依赖。

pom.xml 代码如下

study-hystrix
com.study
1.0-SNAPSHOT
4.0.0
feign-server
org.springframework.cloud
spring-cloud-starter-openfeign

bootstrap.yml 配置如下

server:  port: 9092eureka:  instance:    hostname: 127.0.0.1  client:    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:8080/eureka/spring:  application:    name: feign-server# 设置 feignfeign:  hystrix:    enabled: true  client:    config:      default:        # 连接超时时间        connect-timeout: 8000        # 读取超时时间        read-timeout: 8000

 

实体类、请求、实现类跟 order-server 一致。代码结构:

 

 ProductAPI 接口,我们需要声明调用的微服务名称。

package com.study.api;import com.study.entity.ProductEntity;import com.study.fallback.ProductFallBack;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;/** * @author biandan * @description * @signature 让天下没有难写的代码 * @create 2021-05-27 下午 4:23 */@FeignClient(value = "product-server",fallback = ProductFallBack.class)public interface ProductAPI {    //根据产品ID获取信息    @GetMapping("/product/{id}")    ProductEntity getById(@PathVariable("id") Integer id);}

OrderServiceImpl

package com.study.service.impl;import com.study.api.ProductAPI;import com.study.entity.OrderEntity;import com.study.entity.ProductEntity;import com.study.service.OrderService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.Arrays;/** * @author biandan * @description * @signature 让天下没有难写的代码 * @create 2021-05-27 下午 4:50 */@Servicepublic class OrderServiceImpl implements OrderService {    @Autowired    private ProductAPI productAPI;    //根据订单号查询信息(目前写固定)    @Override    public OrderEntity getByOrderNo(String orderNo) {        OrderEntity orderEntity = new OrderEntity();        orderEntity.setOrderNo("20210619");        orderEntity.setAddress("深圳市南山区");        orderEntity.setTotalPrice(new Float("66.66"));        ProductEntity productById = productAPI.getById(1);        orderEntity.setProductList(Arrays.asList(productById));        return orderEntity;    }}

 

然后编写 fallback 对应的实现类

package com.study.fallback;import com.study.api.ProductAPI;import com.study.entity.ProductEntity;import org.springframework.stereotype.Component;/** * @author biandan * @description * @signature 让天下没有难写的代码 * @create 2021-06-20 下午 7:33 */@Componentpublic class ProductFallBack implements ProductAPI {    //托底数据    @Override    public ProductEntity getById(Integer id) {        System.out.println("**走 feign 托底接口***");        ProductEntity product = new ProductEntity();        product.setId(166);        product.setProductName("托底数据-feign产品");        product.setPrice(new Float(2.25));        return product;    }}

启动类,需要增加注解:@EnableFeignClients

启动 feign-server 、product-server 。测试:

能正常返回。然后我们把 product-server 停掉。继续测试,发现走托底数据了。

 

OK,上面的例子演示了 Feign 整合 Hystrix 实现熔断降级处理。

但是,程序并没有捕获到异常信息,实际开发中,我们需要知道是什么异常导致的降级熔断,因此需要捕获异常信息。

 

使用 FallbackFactory 实现熔断降级

1、创建 ProductFallBackFactory  并实现 FallbackFactory

package com.study.fallback;import com.study.api.ProductAPI;import com.study.entity.ProductEntity;import feign.hystrix.FallbackFactory;import org.springframework.stereotype.Component;/** * @author biandan * @description * @signature 让天下没有难写的代码 * @create 2021-06-20 下午 7:33 */@Componentpublic class ProductFallBackFactory implements FallbackFactory
{ //如果调用异常,使用熔断机制返回错误信息(目前写固定) @Override public ProductAPI create(Throwable throwable) { return new ProductAPI() { @Override public ProductEntity getById(Integer id) { System.out.println("系统调用产品微服务失败,异常信息:" + throwable); ProductEntity product = new ProductEntity(); product.setId(166); product.setProductName("托底数据-feign产品"); product.setPrice(new Float(2.25)); return product; } }; }}

其中,Throwable throwable 就是捕获到的异常信息。

 

2、ProductAPI 需要修改 FeignClient 如下,使用 fallbackFactory

@FeignClient(value = "product-server",fallbackFactory = ProductFallBackFactory.class)

 

重启 feign-server,测试(此时 product-server 是停掉的状态):

控制台

2021-06-20 20:15:11.271  INFO 11792 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty  : Flipping property: product-server.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647

系统调用产品微服务失败,异常信息:com.netflix.hystrix.exception.HystrixTimeoutException

然后启动 product-server,继续测试,结果正常。

 

OK,使用 Feign 实现服务的熔断降级讲解到这。

 

 

 

 

 

 

 

 

 

转载地址:http://ukuhf.baihongyu.com/

你可能感兴趣的文章
详解循环神经网络(Recurrent Neural Network)
查看>>
为什么要用交叉验证
查看>>
用学习曲线 learning curve 来判别过拟合问题
查看>>
用验证曲线 validation curve 选择超参数
查看>>
用 Grid Search 对 SVM 进行调参
查看>>
用 Pipeline 将训练集参数重复应用到测试集
查看>>
PCA 的数学原理和可视化效果
查看>>
机器学习中常用评估指标汇总
查看>>
什么是 ROC AUC
查看>>
Bagging 简述
查看>>
详解 Stacking 的 python 实现
查看>>
简述极大似然估计
查看>>
用线性判别分析 LDA 降维
查看>>
用 Doc2Vec 得到文档/段落/句子的向量表达
查看>>
使聊天机器人具有个性
查看>>
使聊天机器人的对话更有营养
查看>>
一个 tflearn 情感分析小例子
查看>>
attention 机制入门
查看>>
手把手用 IntelliJ IDEA 和 SBT 创建 scala 项目
查看>>
GAN 的 keras 实现
查看>>