
Hystrix 除去断路器,还有给我们提供一种仪表盘,用来给我们查看 Feign 远程调用的情况,现在来引入一下。
一、仪表盘搭建 Hystrix-dashboard 项目创建我们重新创建一个子项目,项目名称:microservice-common-hystrix-dashboard-9000
pomorg.springframework.cloud spring-cloud-starter-netflix-hystrix-dashboard2.2.9.RELEASE
项目中只需要引入一个仪表盘的依赖
ymlserver:
port: 9000
servlet:
context-path: /
tomcat:
uri-encoding: UTF-8
hystrix:
dashboard:
proxy-stream-allow-list:
- "localhost"
yml 中需要配置我们 Hystrix 的监控访问端口,还有一个是仪表盘的代理流允许监控的列表(一定要配置这个列表),这里可以配置数组。
启动类@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashBoardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashBoardApplication.class, args);
}
}
启动类很简单,主要增加一个注解:@EnableHystrixDashboard,这个是为了能开启 Hystrix Dashboard。
修改订单模块 pomorg.springframework.boot spring-boot-starter-actuatororg.springframework.cloud spring-cloud-starter-netflix-hystrix2.2.9.RELEASE
pom 中必须保证有两个东西,一个是 actuator,还有一个是 hystrix 断路器。
启动类@Bean public ServletRegistrationBeangetServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean (streamServlet); registrationBean.setLoadonStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
启动类中我们增加这么一段 Bean,这个配置是为了服务监控而配置,与服务容错本身无关,其实这个也是 springcloud 升级后的坑,之前我们是不需要配置这个的。
二、测试单机模式弄好之后,保存,启动我们的微服务项目(Eureka Server 集群、订单模块、商品模块,以及 hystrix-dashboard)
hystrix-dashboard 的访问路径是:http://localhost:9000/hystrix
当你看到这个的时候就说明我们的服务启动了。下面有三个输入框,我们来一个一个介绍一下。
输入框1:这个框子中输入的是我们要监控的地址,地址的样子可以参考这个框下面的三句话来输入,它提供了集群模式,还有单例的模式(这里我们用单例!!!)
输入框2:Delay 中的参数表示服务器上的轮询时间间隔,一般我们不管,走它默认的
输入框3:Title 中的输入框用于设置浏览器中所监视服务的标题,这个一般我们也不管,默认即可。
在输入框 1 中输入我们要监控的服务地址(这里要注意,只能输入有 Hystrix 的服务,没有的话是没有办法监控的!!!):http://localhost:1001/hystrix.stream
点击 Monitor Stream
进来就是这个样子(里面具体的参数下面我们去说),现在通过 postman 再去调用创建订单。
当你进行请求的时候,它这个图表有变化,那就说明我们的监控已经部署好了。
三、Hystrix 仪表盘属性讲解这一讲就讲到这里,有问题可以联系我:QQ 2100363119,欢迎大家访问我的个人网站:https://www.lemon1234.com
最近网站已经做好,并且已经上线,欢迎各位留言~~