Spring Cloud【Finchley】-09Feign使用Hystrix

举报
小工匠 发表于 2021/09/10 23:19:08 2021/09/10
【摘要】 文章目录 版本说明新建子moduleapplication.yml中开启Hystrix修改Feign接口测试代码 版本说明 先说下使用的spring cloud和spring boo...


在这里插入图片描述

版本说明

先说下使用的spring cloud和spring boot的版本
在这里插入图片描述

Disable HystrixCommands For FeignClients By Default
https://github.com/spring-cloud/spring-cloud-netflix/issues/1277


新建子module

父工程microservice-spring-cloud右键新建Maven Module 命名为:micorservice-consumer-movie-feign-hystrix ,为了简单我们把micorservice-consumer-movie-feign的内容copy到该子模块,修改下application.yml中的spring.application.name即可。


application.yml中开启Hystrix

server:
  port: 7901

spring: 
  application:
    name: micorservice-consumer-movie-feign-hystrix  
#eureka
eureka: 
  client:
    service-url:
      defaultZone: http://artisan:artisan123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

# Disable HystrixCommands For FeignClients By Default
# https://github.com/spring-cloud/spring-cloud-netflix/issues/1277    
feign:
  hystrix:
    enabled: true

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

如果是application.property ,请设置 feign.hystrix.enabled=true


修改Feign接口

使用fallback属性指定回退类

在这里插入图片描述

回退类 也需要实现上面的接口,同时需要标注@Component让其成为受spring管理的bean
在这里插入图片描述


测试

  1. 启动microservice-discovery-eureka,注册中心
  2. 启动micorservice-provider-user,服务提供者
  3. 启动micorservice-consumer-movie-feign-hystrix,服务消费者开启了Hystrix

访问http://localhost:8761/ 确认下服务已经注册成功。

在这里插入图片描述

访问 http://localhost:7901/movie/1

{"id":1,"username":"artisan1","name":"小工匠一","age":10,"balance":100.00}

  
 
  • 1

功能正常,OK。

现在停掉micorservice-provider-user

在这里插入图片描述

访问 http://localhost:7901/movie/1 ,进入了回退方法

{"id":1,"username":"默认用户","name":null,"age":null,"balance":null}

  
 
  • 1

再次启动 micorservice-provider-user
在这里插入图片描述

再次访问 http://localhost:7901/movie/1

{"id":1,"username":"artisan1","name":"小工匠一","age":10,"balance":100.00}

  
 
  • 1

功能正常,OK。


代码

https://github.com/yangshangwei/SpringCloudMaster/tree/master/micorservice-consumer-movie-fegin-hystrix

文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。

原文链接:artisan.blog.csdn.net/article/details/85042142

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。