zoukankan      html  css  js  c++  java
  • SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

    前言

    本篇主要介绍的是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

    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

    会出现以下的界面:

    在这里插入图片描述

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

    http://localhost:9010/hystrix.stream

    会出现以下的界面:

    该界面就是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,谢谢!

    音乐推荐

    夏天的夜晚,天上繁星点点,喳喳吵闹的蝉鸣声,远处河流的流淌声,此时若躺在山丘的草坪上,想必是无比的惬意吧!

    原创不易,如果感觉不错,希望给个推荐!您的支持是我写作的最大动力!
    版权声明:
    作者:虚无境
    博客园出处:http://www.cnblogs.com/xuwujing
    CSDN出处:http://blog.csdn.net/qazwsxpcm    
    个人博客出处:http://www.panchengming.com

  • 相关阅读:
    Linux程序的执行
    Linux图形操作与命令行
    Linux网络配置
    Zip文件中文乱码问题解决方法(MAC->Windows)
    我只是一直很努力
    Android抓包方法(三)之Win7笔记本Wifi热点+WireShark工具
    Android抓包方法(二)之Tcpdump命令+Wireshark
    Android抓包方法(一)之Fiddler代理
    Android反编译(二)之反编译XML资源文件
    Android反编译(一)之反编译JAVA源码
  • 原文地址:https://www.cnblogs.com/xuwujing/p/10446126.html
Copyright © 2011-2022 走看看