SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

栏目: Java · 发布时间: 6年前

内容简介:本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识。Netflix创建了一个名为Hystrix的库,它实现了断路器模式。主要的目的是为了解决服务雪崩效应的一个组件,是保护服务高可用的最后一道防线。确认了开发环境之后,我们再来添加相关的pom依赖。

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

SpringCloud Hystrix

Hystrix 介绍

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

开发准备

开发环境

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

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

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

<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 配置信息:

配置信息:

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 注解就可以了,该注解表示此服务是一个服务注册中心服务。

代码示例:

@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 。代码改造后的实现如下:

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

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

代码示例:

@Component
public class HelloRemoteHystrix implements HelloRemote{

    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return name+", 请求另一个服务失败!";
    }
}

其余的代码就不展示了,和之前的 springcloud-fegin-consumer 项目中的一致,详细的可以在这篇博文 SpringCloud学习系列之二 —– 服务消费者(Feign)和负载均衡(Ribbon) 进行查看。

然后在把之前的 springcloud-feign-consumer2 进行简单的改造下,项目名称改为 springcloud-hystrix-consumer2 。然后更改下配置的端口。

功能测试

完成如上的工程开发之后,我们依次启动服务端和客户端的 springcloud-hystrix-eurekaspringcloud-hystrix-consumerspringcloud-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)

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依赖。

<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 断路器看板 相关配置。

完整的启动类配置如下:

@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 错误。

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

实现的代码如下:

@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

会出现以下的界面:

SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

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

http://localhost:9010/hystrix.stream

会出现以下的界面:

SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard) 该界面就是Hystrix Dashboard监控的界面了,通过这个界面我们可以很详细的看到程序的信息。关于这些信息中说明可以用网上找到的一张来加以说明。

SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

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

其他

springcloud系列博客:

项目地址

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

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

如果感觉项目不错,希望能给个star,谢谢!


以上所述就是小编给大家介绍的《SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Automate This

Automate This

Christopher Steiner / Portfolio / 2013-8-9 / USD 25.95

"The rousing story of the last gasp of human agency and how today's best and brightest minds are endeavoring to put an end to it." It used to be that to diagnose an illness, interpret legal docume......一起来看看 《Automate This》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具