资讯专栏INFORMATION COLUMN

12、Feign整合断路器Hystrix

lei___ / 386人阅读

摘要:公众号乐园上编说了整合断路器,这篇来看看如何整合断路器,整合断路器也是相对比较简单的。默认已经自带断路器,所以不需要像整合断路器那样需要在的启动类添加注解。但是自带断路器并没有打开,需要做些额外的配置。

公众号: java乐园

上编说了《RestTemplate+Ribbon整合断路器Hystrix》,这篇来看看如何Feign整合断路器Hystrix,Feign整合断路器Hystrix也是相对比较简单的。Feign默认已经自带断路器Hystrix,所以不需要像RestTemplate+Ribbon整合断路器Hystrix那样需要在SpringBoot的启动类添加注解。但是Feign自带断路器并没有打开,需要做些额外的配置。

</>复制代码

  1. feign:
  2. hystrix:
  3. enabled: true

1、 新建项目sc-eureka-client-consumer-feign-hystrix,对应的pom.xml文件如下

</>复制代码

  1. 4.0.0
  2. spring-cloud
  3. sc-eureka-client-consumer-feign
  4. 0.0.1-SNAPSHOT
  5. jar
  6. sc-eureka-client-consumer-feign
  7. http://maven.apache.org
  8. org.springframework.boot
  9. spring-boot-starter-parent
  10. 2.0.4.RELEASE
  11. org.springframework.cloud
  12. spring-cloud-dependencies
  13. Finchley.RELEASE
  14. pom
  15. import
  16. UTF-8
  17. 1.8
  18. 1.8
  19. org.springframework.cloud
  20. spring-cloud-starter-netflix-eureka-client
  21. org.springframework.boot
  22. spring-boot-starter-web
  23. org.springframework.cloud
  24. spring-cloud-starter-openfeign

备注:从继续关系可以看出spring-cloud-starter-openfeign已经集成断路器Hystrix

2、 新建springboot启动类

</>复制代码

  1. package sc.consumer;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @SpringBootApplication
  7. @EnableEurekaClient
  8. @EnableFeignClients
  9. public class ConsumerFeignApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(ConsumerFeignApplication.class, args);
  12. }
  13. }

3、 新建配置文件bootstrap.yml和application.yml
bootstrap.yml

</>复制代码

  1. server:
  2. port: 5800
  3. application.yml
  4. spring:
  5. application:
  6. name: sc-eureka-client-consumer-feign-hystrix
  7. eureka:
  8. client:
  9. registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true
  10. fetchRegistry: true #是否从Eureka中获取注册信息,默认为true
  11. serviceUrl:
  12. defaultZone: http://localhost:5001/eureka/
  13. feign:
  14. hystrix:
  15. enabled: true

说明:在application.yml配置文件添加了开启断路器Hystrix的配置项

4、 新建服务消费者类UserService.java

</>复制代码

  1. package sc.consumer.service;
  2. import java.util.Map;
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.web.bind.annotation.DeleteMapping;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.PutMapping;
  9. import org.springframework.web.bind.annotation.RequestBody;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import sc.consumer.model.User;
  12. import sc.consumer.service.hystrix.UserServiceHystrix;
  13. @FeignClient(value="sc-eureka-client-provider", fallback=UserServiceHystrix.class)
  14. public interface UserService {
  15. @GetMapping("/user/getUser/{id}")
  16. Map getUser(@PathVariable(value ="id") Long id);
  17. @RequestMapping("/user/listUser")
  18. Map listUser();
  19. @PostMapping("/user/addUser")
  20. Map addUser(@RequestBody User user);
  21. @PutMapping("/user/updateUser")
  22. Map updateUser(@RequestBody User user);
  23. @DeleteMapping("/user/deleteUser/{id}")
  24. Map deleteUser(@PathVariable(value ="id") Long id);
  25. }

可以看到在该类的FeignClient注解上添加了一个属性fallback

5、 新建断路器处理类UserServiceHystrix.java

</>复制代码

  1. package sc.consumer.service.hystrix;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.springframework.stereotype.Component;
  7. import sc.consumer.model.User;
  8. import sc.consumer.service.UserService;
  9. @Component
  10. public class UserServiceHystrix implements UserService{
  11. @Override
  12. public Map getUser(Long id) {
  13. Map result = new HashMap();
  14. result.put("code", "000000");
  15. result.put("msg", "success");
  16. User u = new User();
  17. u.setId(-1L);
  18. u.setUserName("failBackName");
  19. result.put("body", u);
  20. return result;
  21. }
  22. @Override
  23. public Map listUser() {
  24. Map result = new HashMap();
  25. result.put("code", "000000");
  26. result.put("msg", "success");
  27. List list = new ArrayList();
  28. User u = new User();
  29. u.setId(-1L);
  30. u.setUserName("failBackName");
  31. list.add(u);
  32. result.put("body", list);
  33. return result;
  34. }
  35. @Override
  36. public Map addUser(User user) {
  37. Map result = new HashMap();
  38. result.put("code", "000000");
  39. result.put("msg", "success");
  40. result.put("body", 0);
  41. return result;
  42. }
  43. @Override
  44. public Map updateUser(User user) {
  45. Map result = new HashMap();
  46. result.put("code", "000000");
  47. result.put("msg", "success");
  48. result.put("body", 0);
  49. return result;
  50. }
  51. @Override
  52. public Map deleteUser(Long id) {
  53. Map result = new HashMap();
  54. result.put("code", "000000");
  55. result.put("msg", "success");
  56. result.put("body", 0);
  57. return result;
  58. }
  59. }

该类实现了UserService接口,并实现了该接口的所有方法。

6、 分别启动注册中心sc-eureka-server和服务提供者sc-eureka-client-provider

7、 启动sc-eureka-client-consumer-feign-hystrix,并验证是否启动成功
方式一:查看日志看看对应的端口是启动

方式二:查看注册中心是否注册成功

8、 使用postman验证断路器是否启用
上篇从服务提供者是否正常启动验证断路器是否启动,今天看看从数据库是否启动来验证断路器是否启动
(1)MySQL服务正常启动时访问:
http://127.0.0.1:5800/feign/user/listUser

(2)MySQL服务关闭时访问
http://127.0.0.1:5800/feign/user/listUser

再看下后台日志:

其他接口可以自行按照以上方式进行验证,看看断路器是否启动

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76053.html

相关文章

  • Spring Cloud 体验

    摘要:多层服务调用常见于微服务架构中较底层的服务如果出现故障,会导致连锁故障。 Spring Cloud 体验 简介 Spring Cloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、 事件总线、全局锁、决策竞选、分布式会话等等 基于Spring Boot,Spring Cloud将各公司成熟服务框架组合起来,通过Spring Boo...

    NotFound 评论0 收藏0
  • 两年了,我写了这些干货!

    摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...

    huayeluoliuhen 评论0 收藏0
  • Spring Cloud 快速入门

    摘要:服务注册中心一个服务注册中心,所有的服务都在注册中心注册,负载均衡也是通过在注册中心注册的服务来使用一定策略来实现。在客户端实现了负载均衡。 文章参考于史上最简单的 SpringCloud 教程 | 终章 Spring Cloud 是一个微服务框架,与 Spring Boot 结合,开发简单。将一个大工程项目,分成多个小 web 服务工程,可以分别独立扩展,又可以共同合作。 环境 ...

    fuyi501 评论0 收藏0
  • SpringCloud(第 016 篇)电影微服务, 定制Feign,一个Feign功能禁用而另一个

    摘要:在该配置中,加入这个方法的话,表明使用了该配置的地方,就会禁用该模块使用容灾降级的功能添加访问层添加电影微服务启动类电影微服务,定制,一个功能禁用,另一个功能启用。 SpringCloud(第 016 篇)电影微服务,定制Feign,一个Feign功能禁用Hystrix,另一个Feign功能启用Hystrix - 一、大致介绍 1、在一些场景中,部分功能需要使用断路器功能,部分功能不需...

    张宪坤 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<