Dashboard又称为仪表盘,是用来监控项目的执行状况的,本文旨在Dashboard的使用
分别为单体监控、集群监控、与消息代理结合。
代码请戳个人githubhtml
新建一个SpringBoot项目起名为HystrixDashboardjava
pom文件:git
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cnblogs.hellxz</groupId> <artifactId>hystrix-dashboard</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
在com.cnblogs.hellxz包下建立DashBoardApp主类,开启@SpringBootApplication
注解和@EnableHystrixDashboard
注解github
package com.cnblogs.hellxz; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; /** * @Author : Hellxz * @Description: 仪表盘 启动主类 * @Date : 2018/5/4 17:55 */ @SpringBootApplication @EnableHystrixDashboard public class DashBoardApp { public static void main(String[] args) { SpringApplication.run(DashBoardApp.class, args); } }
在resources
包下建立application.yml
,只需指定应用名和端口号2001web
spring: application: name: hystrix-dashboard server: port: 2001
启动项目,在地址栏上输入http://localhost:2001/hystrix
spring
DashBoard不用注册到注册中心的,只须要被监控 服务作一些配置便可express
RibbonConsumHystrix
为例)单体应用是说咱们一个服务只有一个实例,Dashboard经过这个实例暴露的监控信息,将这些信息展现出来,如图apache
在被监控的服务pom.xml中添加windows
<!--暴露各类指标--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--hystrix熔断器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>RELEASE</version> </dependency>
在被监控的应用的主类上添加@EnableCircuitBreaker
注解浏览器
若是以前已经加了
@SpringCloudApplication
注解那么就不用加这个注解了,由于@SpringCloudApplication
已经包含@EnableCircuitBreaker
分别启动注册中心、服务提供者、RibbonConsumHystrix,以前开启的仪表盘项目不要关
访问议表盘页面 http://localhost:2001/hystrix
在页面中输入http://localhost:8088/hystrix.stream
, 点击Monitor Stream
,如图
参数说明:
- Delay:该参数用来控制服务器上轮询监控信息的延迟时间,默认2000ms,能够经过配置该属性来下降 客户端的网络和Cpu消耗
- Title:该参数对应了上图Hystrix Stream以后的内容,默认会使用具体监控实例的Url,配置以展现合适的标题
此时使用Postman屡次访问RibbonConsumHystrix
项目中的接口,
此时查看刚才的页面,如图(我这里调用了两个接口)
在监控信息中,咱们看到了不少数字,这些有颜色的文字对应右上方的对应颜色的参数
这里面有两个相对重要的信息:实心圆和一条拆线
在上一节咱们对Hystrix仪表盘进行了快速入门,使用的是单个实例监控的/hystrix.stream节点
这一节咱们使用/turbine.stream
对集群进行监控,这里咱们须要引入Turbine,经过它来聚集监控信息,并将信息提供给Hystrix Dashboard来集中展现
新建一个标准Spring Boot项目,取名为Turbine
pom以下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cnblogs.hellxz</groupId> <artifactId>Turbine</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent> <dependencies> <!--turbine--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>RELEASE</version> </dependency> <!--暴露各类指标--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--hystrix熔断器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
新建一个主类,开启@EnableTurbine
注解,这里使用@SpringCloudApplication
注解能够少写几个注解,好比@EnableDiscoveryClient
,turbine是须要注册到注册中心的
package com.cnblogs.hellxz; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.netflix.turbine.EnableTurbine; @EnableTurbine @SpringCloudApplication public class TurbineApp { public static void main(String[] args) { SpringApplication.run(TurbineApp.class, args); } }
在resources包中建立application.yml
server: port: 8989 spring: application: name: turbine eureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka turbine: app-config: ribbon-hystrix #指定了被收集的app名称 combine-host-port: true #同一主机多个服务使用hostname+port进行区分,此项默认为false,即同一主机多服务会合并成一个服务展现 cluster-name-expression: new String("default") #指定集群名称,书中直接使用“default“,这里已经不能用了,会报错
一、分别启动 注册中心、服务提供者
二、maven 打包RibbonConsumHystrix,指定一个端口是8089启动,一个用默认启动,注意,RibbonCustomHystrix项目中我有其余用于测试的main方法,打包会失败,因此先注释掉UserCommand的main方法
,打包以后执行代码
java -jar RibbonConsumHystrix-1.0-SNAPSHOT.jar --server.port=8089
三、启动RibbonConsumHystrix项目
四、接着启动Turbine、仪表盘
五、在地址栏输入并访问 http://localhost:2001/hystrix
六、在Hystrix Dashboard下方的地址栏输入http://localhost:8989/turbine.stream
,点击Monitor Stream
七、使用postman对两个端口8088和8089分别进行请求
这里说明一下,若是是没有对这两个端口发送请求, http://localhost:8989/turbine.stream 这里会一直在loading,
若是只请求一个端口,而另外一个没有被请求,那么会只显示Hosts:1
Spring Cloud 在封装Turbine的同时也封装了基于消息代理的收集实现。因此咱们能够经过将全部须要收集的监控消息都输出到消息代理中,而后Turbine 服务再从消息代理中异步获取这些监控信息,最后将信息交给Hystrix Dashboard中作展现。如图
使用这个架构以前,咱们须要先安装好RabbitMQ,这里先说一下本文的简单配置,使RabbitMQ可用,其实不只可使用RabbitMQ,只要实现了AMQP的消息代理,理论上均可以结合Turbine使用
准备:
首先,安装Erlang(OTP_20.3.exe),一路next安装下来便可
而后打开路径C:\Windows\System32\config\systemprofile
,将.erlang.cookie
复制到C:\Users\你的用户名
下
如上操做是为了避坑,详情参考个人上一篇文章RabbitMQ问题解决:TCP connection succeeded but Erlang distribution failed
安装RabbitMQ Server,全用默认会更方便,一路next安装下来 到close,此时默认是直接注册到系统服务的
安装完成菜单栏会有这样的显示
查看服务,服务已经正常启动
推荐使用 来进行操做,安装在默认的位置的话,打开这个直接就在
C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.5\sbin
,不然使用cd命令进入安装路径下的RabbitMQ Server\rabbitmq_server-3.7.5\sbin
,还有个缘由用这个终端是它能够自动提权,减小失败出现的机率
输入rabbitmqctl status
,显示如图就表明正常,能够继续操做,出现TCP connection succeeded but Erlang distribution failed,点击查看个人解决办法
还没完,接着输入rabbitmq-plugins enable rabbitmq_management
,来开启 web管理插件
打开浏览器,地址栏输入http://localhost:15672/
帐号密码 默认都是guest ,对于使用其余帐号登陆请参考其余文章,这里仅为此实验可用
这样RabbitMQ Server基本上咱们就已经配好了,开始正菜
新建标准Spring Boot项目TurbineAmqp
,pom文件内容以下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cnblogs.hellxz</groupId> <artifactId>TurbineAmqp</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- turbine集群监控 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> <version>1.4.4.RELEASE</version> </dependency> <!-- spring cloud的RabbitMQ的实现,实际上包装了spring-cloud-stater-turbine-stream和spring-cloud-starter-stream-rabbitmq --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine-amqp</artifactId> </dependency> <!-- 暴露各类指标 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--Hystrix的依赖,不加会报ClassNotFound异常--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!--加入此依赖,避免Caused by: java.lang.ClassNotFoundException: com.netflix.turbine.aggregator.InstanceKey--> <dependency> <groupId>com.netflix.turbine</groupId> <artifactId>turbine-core</artifactId> <version>2.0.0-DP.2</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
新建主类TurbineAmqpApp
package com.cnblogs.hellxz; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream; /** * <b>类名</b>: TurbineAmqpApp * <p><b>描 述</b>: Turbine基于RabbitMQ消息代理的主类</p> * * <p><b>建立日期</b>2018/5/28 16:29</p> * @author HELLXZ 张 * @version 1.0 * @since jdk 1.8 */ @SpringCloudApplication @EnableTurbineStream public class TurbineAmqpApp { public static void main(String[] args) { SpringApplication.run(TurbineAmqpApp.class, args); } }
这里相较其余文章中用
@SpringBootApplication
/@EnableDiscoveryClient
/@EnableCircuitBreaker
再加上@EnableTurbineStream
做用相同,咱们能够发现@SpringCloudApplication
注解封装了这几个注解,这个问题之后就不说了
在resources
包下建立application.yml
,以下:
server: port: 8989 spring: application: name: turbine-amqp management: port: 8990 eureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka
项目 在这里就搭建完成了开始测试
分别按顺序启动 注册中心、服务提供者、RibbonConsumHystrix、TurbineAmqp、Dashboard
地址栏访问:http://localhost:2001/hystrix
在Hystrix Dashboard字样下的输入框,输入http://localhost:8989/turbine.stream
,点击Monitor
postman 屡次访问带Hystrix的接口,会出现如图
在http://localhost:15672/#/
页面中也会有一些指标显示
虽然看起来和直接使用Turbine来聚合监控信息,没什么区别,其实只是这种整合消息代理的方式是异步的。
经过本文的学习,咱们能够知道Dashboard的做用和用法,各个参数的意义,单体监控/hystrix.stream
,集群可用Turbine的/turbine.stream
,还有关于Turbine与消息代理做整合的使用方法。
有了Dashboard这种神器,咱们就能够即时地了解服务中Hystrix熔断器的工做状况和健康状况,从而对服务做针对性调整。
说点题外话,总有朋友问我:这就完了?
是啊,的确写完了,可是有个问题就是,我写这些文章中出现的问题与解决的办法是什么,看了文章你会有所了解
可是
这些都是单单看个乐呵所不能体会的,看起来简单的东西不必定就能作得好,因此在这里推荐你们动手操做,你能够建立两个workspace,一个放本身的代码,另外一个放个人代码,对比着学习,我想这样你会有更多的收获。
最后码字不易,若是本文对你有所帮助,还请你们点个推荐,评论一下:)
####本文引用
《Spring Cloud 微服务实战》 翟永超
java.lang.NoClassDefFoundError: com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspec
ClassNotFound exception when using Spring Cloud Starter Turbine AMQP
####本文为实践笔记,如需转载,请注明出处:https://www.cnblogs.com/hellxz/p/9100224.html