在Spring Cloud 中构建一个Hystrix Dashboard非常容易只需要4步:
1.创建一个Spring Boot工程,命名为hystrix-dashboard。
2.编辑pom.xml文件,如:
<?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.example</groupId><artifactId>hystrix-dashboard</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>hystrix-dashboard</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.SR1</spring-cloud.version></properties><dependencies><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.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
3.在主类上添加@EnableHystrixDashboard注解启用Hystrix Dashboard功能
4.修改application.properties,如:
spring.application.name=hystrix-dashboard
server.port=2001
启动后访问http://localhost:2001/hystrix
这是Hystrix Dashboard的监控首页,该页面没有具体的监控信息。从页面上可以看出它支持三种监控方式:
1.默认的集群监控:http://turbine-hostname:port/turbine.stream
2.指定的集群监控:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
3.单体应用的监控:http://hystrix-app:port/actuator/hystrix.stream
前两者都是对集群的监控,需要整合Turbine实现。
而开启单点应用的集群监控只需要两步:
1.在服务消费者中新增如下配置文件:
<?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.example</groupId><artifactId>eureka-ribbon</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka-ribbon</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><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-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><!--仪表盘--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</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-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.在其主类上添加@EnableCircuitBreaker注解开启断路器功能(服务消费者)
3.SpringBoot2.0需要配置如下一个Bean:
@Configuration
public class HystrixConfig {@Beanpublic ServletRegistrationBean getServlet() {HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;}
}
然后开启服务消费者,服务提供者,服务注册中心:
Ok配置成功。
在http://localhost:2001/hystrix 下填写地址
首页上的参数:
Delay:该参数用来控制服务器上轮询监控信息的延迟时间,默认为2000ms,可以通过配置该属性降低客户端的网络和cpu消耗。
Title:该参数对应了上图Hystrix Stream之后的内容,默认为具体监控实例的URL,可以通过配置该信息展示合适标题。
从监控信息可以看到两个重要的图形信息:一个实心圆和一个曲线。
实心圆:有两种含义:通过颜色变化代表了实例的健康程度,从绿色,黄色,橙色,红色递减。除此之外,它的大小也会根据请求流量的变化而变化,流量越大实心圆就越大。
曲线:用来记录两分钟内流量的相对变化,可以用它来观察流量的上升和下降趋势。
Turbine集群监控
1.新建一个SpringBoot项目
2.修改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.example</groupId><artifactId>turbine</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>turbine</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.SR1</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-turbine</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
3.在主类上使用@EnableTurbine和@EnableTurbine注解:
@EnableDiscoveryClient
@EnableTurbine
@SpringBootApplication
public class TurbineApplication {public static void main(String[] args) {SpringApplication.run(TurbineApplication.class, args);}
}
4.修改application.properties
spring.application.name=turbineserver.port=8989
management.server.port=8990eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/turbine.app-config=ribbon-consumer
turbine.cluster-name-expression="default"
turbine.combine-host-port=true
turbine.instanceUrlSuffix.default = /hystrix.stream
然后启动服务注册中心,服务提供者,服务消费者,仪表盘等服务。
在上面的配置文件中,turbine.app-config参数指定了需要收集监控信息的服务名,turbine.cluster-name-expression指定了集群默认名为default,当服务多的时候可以启动多个Tubine服务来构建不同的聚合集群,该参数可以用来区分这些不同的聚合集群。同时该参数值可以在Hystrix仪表盘中用cluster参数来指定。turbine.combine-host-port参数设为true,可以让同一主机上的服务通过主机名与端口号的组合进行区分。默认情况下会以host来区分不同的服务,这会使本地调试时,本机上的不同服务聚合成一个服务来统计。
这里我启动了两个服务消费者进行测试:
OK~~~~~~~~~~~~~~~~
参考《Spring Cloud 微服务实战》