本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识。html
Netflix建立了一个名为Hystrix的库,它实现了断路器模式。主要的目的是为了解决服务雪崩效应的一个组件,是保护服务高可用的最后一道防线。git
开发环境github
注:不必定非要用上述的版本,能够根据状况进行相应的调整。须要注意的是SpringBoot2.x之后,jdk的版本必须是1.8以上!web
确认了开发环境以后,咱们再来添加相关的pom依赖。spring
<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依赖。浏览器
因为Hystrix机制是在微服务项目上进行的,而且Fegin中包含了该机制。因此这里咱们能够把以前的springcloud-feign
的项目进行简单的改造就好了。springboot
首先是服务端这块,为了进行区分,建立一个springcloud-hystrix-eureka
的项目,用于作注册中心。 代码和配置和以前的基本同样。 application.properties
配置信息:app
配置信息:负载均衡
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/
配置说明:ide
服务端这边只须要在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-eureka
、springcloud-hystrix-consumer
和springcloud-hystrix-consumer2
这三个程序,而后在浏览器界面输入: http://localhost:8002/
,便可查看注册中心的信息。
首先在浏览器输入:
控制台打印:
接受到请求参数:pancm,进行转发到其余服务
浏览器返回:
pancm,Hello World
而后再输入:
浏览器返回:
pancm,Hello World
说明程序运行正常,fegin的调用也ok。这时咱们在进行断路测试。 中止springcloud-hystrix-consumer2
这个服务,而后在到浏览器输入:http://localhost:9004/hello/pancm
进行查看信息。
控制台打印:
接受到请求参数:pancm,进行转发到其余服务
浏览器返回:
pancm, 请求另外一个服务失败!
出现以上结果说明断路器的功能已经实现了。
示例图:
Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,经过Hystrix Dashboard咱们能够在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。
开发环境
注:不必定非要用上述的版本,能够根据状况进行相应的调整。须要注意的是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-consumer
项目进行改造下,项目名称改成springcloud-hystrix-dashboard-consumer
。而后在到启动类新增以下配置。
完整的启动类配置以下:
@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; } }
完成如上的工程改造以后,咱们启动该程序。 而后在浏览器输入:
会出现如下的界面:
能够经过该界面监控使用了hystrix dashboard的项目。这里咱们依照提示在中间的输入框输入以下的地址:
会出现如下的界面:
该界面就是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
若是感受项目不错,但愿能给个star,谢谢!
夏天的夜晚,天上繁星点点,喳喳吵闹的蝉鸣声,远处河流的流淌声,此时若躺在山丘的草坪上,想必是无比的惬意吧!
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=785902&auto=0&height=66"></iframe>
原创不易,若是感受不错,但愿给个推荐!您的支持是我写做的最大动力! 版权声明: 做者:虚无境 博客园出处:http://www.cnblogs.com/xuwujing CSDN出处:http://blog.csdn.net/qazwsxpcm 我的博客出处:http://www.panchengming.com