本文共 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 实现熔断降级处理。
但是,程序并没有捕获到异常信息,实际开发中,我们需要知道是什么异常导致的降级熔断,因此需要捕获异常信息。
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/