zoukankan      html  css  js  c++  java
  • SpringCloud中的断路器(Hystrix)和断路器监控(Dashboard)

    转自:https://www.jb51.net/article/163291.htm

    前言

    本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识。

    SpringCloud Hystrix

    Hystrix 介绍

    Netflix创建了一个名为Hystrix的库,它实现了断路器模式。主要的目的是为了解决服务雪崩效应的一个组件,是保护服务高可用的最后一道防线。

    开发准备

    开发环境

    •JDK:1.8
    •SpringBoot:2.1.1.RELEASE
    •SpringCloud:Finchley

    注:不一定非要用上述的版本,可以根据情况进行相应的调整。需要注意的是SpringBoot2.x以后,jdk的版本必须是1.8以上!

    确认了开发环境之后,我们再来添加相关的pom依赖。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <dependencies>
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
     </dependency>
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
     </dependency>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
    </dependencies>

    注: 实际上这里是不需要Hystrix依赖的,Fegin已经添加了Hystrix依赖。

    SpringCloud Hystrix 示例

    由于Hystrix机制是在微服务项目上进行的,并且Fegin中包含了该机制。所以这里我们可以把之前的springcloud-feign的项目进行简单的改造就行了。

    服务端

    首先是服务端这块,为了进行区分,创建一个springcloud-hystrix-eureka的项目,用于做注册中心。 代码和配置和之前的基本一样。

    application.properties配置信息:

    配置信息:

    1
    2
    3
    4
    5
    spring.application.name=springcloud-hystrix-eureka-server
    server.port=8002
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    eureka.client.serviceUrl.defaultZone=http://localhost:8002/eureka/

    配置说明:

    •spring.application.name: 这个是指定服务名称。
    •server.port:服务指定的端口。
    •eureka.client.register-with-eureka:表示是否将自己注册到Eureka Server,默认是true。
    •eureka.client.fetch-registry:表示是否从Eureka Server获取注册信息,默认为true。
    •eureka.client.serviceUrl.defaultZone: 这个是设置与Eureka Server交互的地址,客户端的查询服务和注册服务都需要依赖这个地址。

    服务端这边只需要在SpringBoot启动类添加@EnableEurekaServer注解就可以了,该注解表示此服务是一个服务注册中心服务。

    代码示例:

    1
    2
    3
    4
    5
    6
    7
    8
    @EnableEurekaServer
    @SpringBootApplication
    public class HystrixEurekaApplication {
     public static void main(String[] args) {
      SpringApplication.run(HystrixEurekaApplication.class, args);
      System.out.println("hystrix注册中心服务启动...");
     }
    }

    客户端

    这里我们把之前的springcloud-fegin-consumer项目稍微改造下,项目名改为springcloud-hystrix-consumer。然后在application.properties配置文件新增如下配置, feign.hystrix.enabled配置表示是否启用熔断机制。

        feign.hystrix.enabled=true

    增加了配置之后,我们在来把之前的fegin进行定义转发服务的@FeignClient注解进行添加一个回调方法fallback。代码改造后的实现如下:

    1
    2
    3
    4
    5
    @FeignClient(name= "springcloud-hystrix-consumer2",fallback = HelloRemoteHystrix.class)
    public interface HelloRemote {
     @RequestMapping(value = "/hello")
     public String hello(@RequestParam(value = "name") String name);
    }

    最后新增一个回调类,用于处理断路的情况。这里我们就简单的处理下,返回错误信息即可!

    代码示例:

    1
    2
    3
    4
    5
    6
    7
    8
    @Component
    public class HelloRemoteHystrix implements HelloRemote{
     
     @Override
     public String hello(@RequestParam(value = "name") String name) {
      return name+", 请求另一个服务失败!";
     }
    }

    其余的代码就不展示了,和之前的springcloud-fegin-consumer项目中的一致,然后在把之前的springcloud-feign-consumer2进行简单的改造下,项目名称改为springcloud-hystrix-consumer2。然后更改下配置的端口。

    功能测试

    完成如上的工程开发之后,我们依次启动服务端和客户端的springcloud-hystrix-eureka、springcloud-hystrix-consumer和springcloud-hystrix-consumer2这三个程序,然后在浏览器界面输入:http://localhost:8002/,即可查看注册中心的信息。

    首先在浏览器输入:

    http://localhost:9004/hello/pancm

    控制台打印:

    接受到请求参数:pancm,进行转发到其他服务

    浏览器返回:

    pancm,Hello World

    然后再输入:

    http://localhost:9005/hello?name=pancm

    浏览器返回:

    pancm,Hello World

    说明程序运行正常,fegin的调用也ok。这时我们在进行断路测试。

     停止springcloud-hystrix-consumer2这个服务,然后在到浏览器输入:http://localhost:9004/hello/pancm 进行查看信息。

    控制台打印:

    接受到请求参数:pancm,进行转发到其他服务

    浏览器返回:

    pancm, 请求另一个服务失败!

    出现以上结果说明断路器的功能已经实现了。

    示例图:

    SpringCloud Hystrix-Dashboard

    Hystrix-Dashboard 介绍

    Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。

    开发准备

    开发环境

    •JDK:1.8
    •SpringBoot:2.1.1.RELEASE
    •SpringCloud:Finchley

    注:不一定非要用上述的版本,可以根据情况进行相应的调整。需要注意的是SpringBoot2.x以后,jdk的版本必须是1.8以上!

    确认了开发环境之后,我们再来添加相关的pom依赖。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <dependencies>
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
     </dependency>
      
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
     </dependency>
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
     </dependency>
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
      
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
    </dependencies>

    注: spring-boot-starter-actuator这个必须需要要,该jar可以得到SpringBoot项目的各种信息,关于该jar包的使用,可以在springboot-actuator这个项目中进行查看。

    SpringCloud Hystrix-Dashboard 示例

    这里我们把上面的springcloud-hystrix-consumer项目进行改造下,项目名称改为springcloud-hystrix-dashboard-consumer。然后在到启动类新增如下配置。

    •EnableCircuitBreaker:表示启用hystrix功能。
    •EnableHystrixDashboard:启用 HystrixDashboard 断路器看板 相关配置。

    完整的启动类配置如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableHystrixDashboard
    @EnableCircuitBreaker
    @EnableFeignClients
    public class HystrixDashboardApplication {
     public static void main(String[] args) {
      SpringApplication.run(HystrixDashboardApplication.class, args);
       System.out.println("hystrix dashboard 服务启动...");
     }
    }

    然后在到application.properties配置文件中新增如下配置:

        management.endpoints.web.exposure.include=hystrix.stream
        management.endpoints.web.base-path=/

    该配置的意思是指定hystrixDashboard的访问路径,SpringBoot2.x以上必须指定,不然是无法进行访问的,访问会出现 Unable to connect to Command Metric Stream 错误。

    如果不想使用配置的话,也可以使用代码进行实现。

     实现的代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @Component
     public class HystrixServlet extends Servlet{
      @Bean
      public ServletRegistrationBean getServlet() {
       HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
       ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
       registrationBean.setLoadOnStartup(1);
       registrationBean.addUrlMappings("/hystrix.stream");
       registrationBean.setName("HystrixMetricsStreamServlet");
       return registrationBean;
      }
     }

    功能测试

    完成如上的工程改造之后,我们启动该程序。

     然后在浏览器输入:

    http://localhost:9010/hystrix

    会出现以下的界面:

    可以通过该界面监控使用了hystrix dashboard的项目。这里我们依照提示在中间的输入框输入如下的地址:

    http://localhost:9010/hystrix.stream

    会出现以下的界面:

    该界面就是Hystrix Dashboard监控的界面了,通过这个界面我们可以很详细的看到程序的信息。关于这些信息中说明可以用网上找到的一张来加以说明。

    注: 如果界面一直提示loading,那么是因为没有进行请求访问,只需在到浏览器上输入:http://localhost:9010/hello/pancm,然后刷新该界面就可以进行查看了。

    项目地址

    基于SpringBoot2.x、SpringCloud的Finchley版本开发的地址:https://github.com/xuwujing/springcloud-study

    基于SpringBoot1.x、SpringCloud 的Dalston版本开发的地址: https://github.com/xuwujing/springcloud-study-old

    总结

    以上所述是小编给大家介绍的SpringCloud中的断路器(Hystrix)和断路器监控(Dashboard),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
    如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

  • 相关阅读:
    原生JS实现new方法、new一个对象发生的四部、new里面常用的优先级
    svg image标签降级技术
    ReflectionToStringBuilder使用
    记一次未解决的异常:java.lang.NoClassDefFoundError: net/sf/json/JSONObject
    eclipse安装Run-Jetty-Run插件,修改实时生效
    jdbcTemplate:包含占位符的SQL无法打印参数信息
    jdbcTemplate异常:like模糊查询报错(Parameter index out of range (1 > number of parameters)
    Spring整合MyBatis
    springmvc整合slf4j、log4j记录文本日志
    Java环境配置
  • 原文地址:https://www.cnblogs.com/sharpest/p/13712723.html
Copyright © 2011-2022 走看看