基于SpringBoot整合SpringCloud微服务框架--Eureka注册中心及Feign远程调用/Ribbon负载均衡及Hystrix熔断器

一、整合步骤

    本文主要讲的是如何整合Hystrix熔断器,“基于SpringBoot整合SpringCloud微服务框架--Eureka注册中心及Feign远程调用/Ribbon负载均衡”请参考我的上一篇有关springcloud的博客:https://www.cnblogs.com/lkw-cnblogs/p/15764123.html,本文也是基于它的项目基础上作进一步的开发。

1、方式一:基于Feign整合Hystrix

(1)目录结构

 

 

 (2)步骤

 1 //使用Feign+Hystrix熔断器
 2 1、引入依赖
 3 <!-- 引入Hystric熔断器框架依赖 -->
 4         <dependency>
 5             <groupId>org.springframework.cloud</groupId>
 6             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 7         </dependency>
 8         <!-- 引入Hystric熔断器仪表盘框架依赖 -->
 9         <dependency>
10             <groupId>org.springframework.cloud</groupId>
11             <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
12         </dependency>
13 2、服务配置
14 # 开启feign支持hystric
15 feign:
16   hystrix:
17     enabled: true
18   client:
19     config:
20       default:
21         connectTimeout: 5000 # 5s就超时
22         readTimeout: 5000  # 5s就超时
23 3、创建Fallback处理类(要对哪个远程服务进行降级熔断,就实现哪个Service接口)
24 /**
25  * @author lkw
26  * 针对调用远端服务出错时进行降级处理
27  */
28 @Component
29 public class FeignClientFallBack implements HelloFeign{
30 
31     @Override
32     public String hello() {
33         String message="调用服务提供方HelloService失败,熔断hello方法";
34         System.out.println(message);
35         return message;
36     }
37 }
38 4、通过@FeignClient注解里的fallback声明对指定远程服务进行对应熔断处理
39 /**
40  * @author lkw
41  * 使用Feign对Hello服务进行远程调用
42  *
43  */
44 @FeignClient(value="service-hello",fallback = FeignClientFallBack.class)
45 public interface HelloFeign {
46 
47     @RequestMapping(value = "/hello")
48     String hello();
49 }
50 5、为了便于监控熔断情况,增加熔断仪表盘配置(本质为注册相应的Servlet)
51 /**
52  * @author lkw
53  * 配置Hystrix熔断仪表盘,便于监控服务熔断情况
54  */
55 @Configuration
56 public class HystrixDashBoardConfiguration {
57 
58     /*
59     *注册servlet
60     * servlet的名称: HystrixMetricsStreamServlet
61     * servlet的url: hystrix.stream
62     *
63      */
64     @Bean
65     public ServletRegistrationBean getServlet(){
66 
67         HystrixMetricsStreamServlet hystrixMetricsStreamServlet=new HystrixMetricsStreamServlet();
68         ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(hystrixMetricsStreamServlet);
69         servletRegistrationBean.setLoadOnStartup(1);
70         servletRegistrationBean.addUrlMappings("/hystrix.stream");
71         servletRegistrationBean.setName("HystrixMetricsStreamServlet");
72         return servletRegistrationBean;
73     }
74 }
75 6、启动项目,然后把HellService的一个实例服务关掉,然后再进行ip:端口/hello访问,通过页面显示确认是否进入了熔断方法

2、方式二:基于Ribbon整合Hystrix

(1)目录结构

 

 

 (2)步骤

 1 //使用Ribbon+Hystrix熔断器
 2 1、在pom.xml添加相关依赖
 3 <!-- 引入Hystric熔断器框架依赖 -->
 4         <dependency>
 5             <groupId>org.springframework.cloud</groupId>
 6             <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 7         </dependency>
 8         <!-- 引入Hystric熔断器仪表盘框架依赖 -->
 9         <dependency>
10             <groupId>org.springframework.cloud</groupId>
11             <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
12         </dependency>
13 2、在原有的控制器里增加对外方法getRibbonHystricHello()以及对应的熔断方法getRibbonHystrixFallBackHello
14 /**
15  * @author lkw
16  * Hello服务调用控制器
17  */
18 @RestController
19 public class HelloController {
20 
21     @Autowired
22     private RestTemplate restTemplate;
23 
24     @HystrixCommand(fallbackMethod = "getRibbonHystrixFallBackHello")
25     @GetMapping(value="/ribbonHystrixHello")
26     public String getRibbonHystricHello(){
27 
28         String urlHelloService="http://service-hello/hello";
29         String result=restTemplate.getForObject(urlHelloService,String.class);
30 
31         System.out.println("使用ribbbon结合熔断访问结果:"+result);
32         return "使用ribbbon结合熔断访问结果:"+result;
33     }
34 
35     public String getRibbonHystrixFallBackHello(){
36 
37         String message="服务提供方service-hello调用异常,熔断方法getRibbonHystrixHello";
38         System.out.println(message);
39         return message;
40     }
41 }
42 3、为了便于监控熔断情况,增加熔断仪表盘配置(本质为注册相应的Servlet)
43 /**
44  * @author lkw
45  * 配置Hystrix熔断仪表盘,便于监控服务熔断情况
46  */
47 @Configuration
48 public class HystrixDashBoardConfiguration {
49 
50     /*
51     *注册servlet
52     * servlet的名称: HystrixMetricsStreamServlet
53     * servlet的url: hystrix.stream
54     *
55      */
56     @Bean
57     public ServletRegistrationBean getServlet(){
58 
59         HystrixMetricsStreamServlet hystrixMetricsStreamServlet=new HystrixMetricsStreamServlet();
60         ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(hystrixMetricsStreamServlet);
61         servletRegistrationBean.setLoadOnStartup(1);
62         servletRegistrationBean.addUrlMappings("/hystrix.stream");
63         servletRegistrationBean.setName("HystrixMetricsStreamServlet");
64         return servletRegistrationBean;
65     }
66 }
67 4、启动项目,然后把HellService的一个实例服务关掉,然后再进行ip:端口/ribbonHystrixHello访问,通过页面显示确认是否进入了熔断方法

二、FAQ(问题集锦)

1、“Failed to bind properties under 'feign.client.config.default' to org.springframework.cloud.openfeign.FeignClientProperties$FeignClientConfiguration:”?

原因:在yml配置文件里,有个属性值和key之间没有空格导致认别失败

解决方法:key: xxx,:和xxx之间空一个格

2、当关掉一个服务提供方HelloService实例后,浏览器页面并没有显示熔断器里面的服务降级方法“调用服务提供方HelloService失败,熔断hello方法”?

原因:说明FeignClient进行远程方法调用失败后,并没有进入fallback处理

解决方法:在该注解后加入fallback声明,另外前面的value部分要写value不能写name!!!!

@FeignClient(value="service-hello",fallback = FeignClientFallBack.class)

三、成果

1、Hystrix仪表盘监控平台入口

 

 

 2、在1、输入之前配置的Hystrix的url和title后,进行登录

 

 3、远程方法调用成功时

 

 4、远程方法调用失败时

 

5、基于Feign,远程方法调用失败时,进入熔断方法,即进行服务降级

6、基于Ribbon,远程方法调用失败时,进入熔断方法,即进行服务降级

 

 

posted @ 2022-01-04 21:34  chance_for_ready  阅读(690)  评论(0)    收藏  举报