前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >服务消费者feign与Hystrix断路器容错机制-微服务架构

服务消费者feign与Hystrix断路器容错机制-微服务架构

作者头像
低调小熊猫
发布2020-06-07 10:47:38
6050
发布2020-06-07 10:47:38
举报
文章被收录于专栏:低调小熊猫

Feign消费者与Hystrix断路器容错机制-微服务架构

注册中心:https://github.com/java-aodeng/hope/tree/master/micro-service1-eureka-server

服务提供者:https://github.com/java-aodeng/hope/tree/master/micro-service2-eureka-provider

后面都以上面的注册中心,和服务提供者为基础操作后续内容(代码已经开源,使用方式见我上一篇and上上篇文章)

创建Feign与Hystrix

1.添加依赖

代码语言:javascript
复制
<!--feign star-->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-openfeign</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <!--feign end-->        <!--添加Hystrix依赖 断路器容错保护 -->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>        </dependency>

2.配置yml文件

代码语言:javascript
复制
#配置服务消费者server:  port: 8888spring:  application:    name: eureka-feigneureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/#开启hystrix 指标feign:  hystrix:    enabled: true

3.启动入口添加注解

代码语言:javascript
复制
@SpringBootApplication@EnableEurekaClient@EnableFeignClients@EnableHystrix//开启断路器功能public class MicroService5FeignApplication {    public static void main(String[] args) {        SpringApplication.run(MicroService5FeignApplication.class, args);    }}

测试

用feign方式消费接口并添加Hystrix断路器

1.创建service层

代码语言:javascript
复制
/** * Feign消费服务接口,定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了eureka-provider服务的“/test”接口 * 注意:服务名大小写无所谓,@FeignClient 会把接口类交给spring所管理,也就是说是可以@Autowired注入的 * @program:hope * @author:aodeng * @blog:低调小熊猫(https://aodeng.cc) * @微信公众号:低调小熊猫 * @create:2019-03-14 13:58 **/@FeignClient(value = "eureka-provider",fallback = TestFeignFallback.class)//关联fallback = TestFeignFallback.class 一旦错误就会调同名称的方法public interface TestFeign {    @RequestMapping("/test")    String testFegin();    @RequestMapping("/testByParam/{from}")    String testByParam(@PathVariable(value = "from") String from);}

2.创建fallback类,出错调用

代码语言:javascript
复制
/** * @program:hope * @ClassName:TestFeignFallback * @author:aodeng * @blog:低调小熊猫(https://aodeng.cc) * @create:2019-03-16 15:33 * @Description: 错误回调类 关联fallback = TestFeignFallback.class 一旦错误就回调同名称的方法 * @Version 1.0 **/@Componentpublic class TestFeignFallback implements TestFeign{    @Override    public String testFegin() {        return "Hystrix断路器容错机制启动";    }    @Override    public String testByParam(String from) {        return "Hystrix断路器容错机制启动";    }}

3.创建controller使用

代码语言:javascript
复制
/** * @program:hope * @ClassName:TestFeignController * @author:aodeng * @blog:低调小熊猫(https://aodeng.cc) * @create:2019-03-14 13:58 * @Description: TODO * @Version 1.0 **/@RestControllerpublic class TestFeignController {    @Autowired(required = false)    private TestFeign testFeign;    @RequestMapping("/testFegin")    public String testFeign(){        return testFeign.testFegin();    }    /**     * @Description: 传参数的接口需要加接收参数的注解,service也要加接收参数的注解,服务提供者provider也要加注解,3个接口必须保持一致    * @Param: [from]    * @return: [from]    * @Author: aodeng    * @Date: 19-3-15    */     @RequestMapping("/testByParam/{from}")    public String testByParam(@PathVariable(value = "from") String from){        return testFeign.testByParam(from);    }}

结果

当我们访问/testFegin接口时,会调用服务提供者的/test接口,这个接口人为制造异常,访问出错,页面出现:Hystrix断路器容错机制启动 表示成功

这就是Hystrix回退概念:当 断路器 打开或运行错误时,执行默认的代码,也就是我们的fallback的代码

当我们的服务提供者自身问题导致接口出现故障,防止出现雪崩效应,微服务架构提供了断路器等...保护机制。

源码永久开源

https://github.com/java-aodeng/hope/tree/master/micro-service5-feign

  • 本文作者: 低调小熊猫
  • 本文链接: https://aodeng.cc/archives/feignandhystrix
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-03-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 低调小熊猫 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Feign消费者与Hystrix断路器容错机制-微服务架构
  • 创建Feign与Hystrix
  • 测试
  • 结果
  • 源码永久开源
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档