在进行Hystrix仪表盘、Turbine集群监控、与消息代理结合的例子中,首先要保证已经有eureka-server(注册中心)、hello-service(服务提供者)、ribbon-consume(服务消费者)。没有的请参考Spring Cloud分类中的文章进行搭建。
-----------------------------------------------------Hystrix仪表盘-----------------------------------------------
Spring Cloud整合了Hystrix的仪表盘组件Hystrix Dashboard,主要用来实时监控Hystrix的各项指标信息。通过Hystrix Dashboard反馈的实时信息,可以帮助我们快速发现系统中存在的问题。
这里我们来构建一个Hystrix Dashboard来对ribbon-consumer进行实施监控。
一、创建一个Spring Boot项目,命名为hystrix-dashboard。
二、编写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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>dashboard</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dashboard</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.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-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-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>
三、为应用主类上添加@EnableHystrixDashboard,启用Hystrix Dashboard功能。
@EnableHystrixDashboard
@SpringBootApplication
public class DashboardApplication {
public static void main(String[] args) {
SpringApplication.run(DashboardApplication.class, args);
}
}
四、修改application.properties配置文件:
spring.application.name=hystrix-dashboard server.port=2001
到这里我们就可以启动hystrix-dashboard应用,通过http://localhost:2001/hystrix进行访问(这里使用谷歌浏览器,不然后期会在监控时一直处于loading状态)。可以看到如下的页面:
这是Hystrix Dashboard的监控首页,我们可以看到Hystrix Dashboard共支持三种不同的监控方式:
- 默认的集群监控:通过URL http://turbine-hostname:port/turbine.stream开启,实现默认集群的监控。
- 指定的集群监控:通过URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控。
- 单体应用的监控:通过URL http://hystrix-app:port/hystrix.stream开启,实现对具体某个服务实例的监控。
前两者都是对集群的监控,需要整合Turbine才能实现,现在我们实现对单个服务实例的监控:进入以下步骤。
四、在ribbon-consume服务消费者的pom.xml中添加spring-boot-starter-actuator模块以开启监控相关的端点,并确保已经引入了断路由的依赖spring-cloud-starter-netflix-hystrix:
<dependencies> ... <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> .... </dependencies>
五、查看服务实例的主类,确保已经使用了@EnableCircuitBreaker注解,开启断路由的功能。
/**
* @author HDN
* @date 2019/6/16 14:29
*/
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumeApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumeApplication.class, args);
}
}
六、分别启动eureka-server、hello-service、ribbon-consume:
在Hystrix Dashboard的首页输入http://localhost:9000/hystrix.stream进行对ribbon-consumer实例进行监,单机Monitor Stream:
很可惜,出现了以下问题Unable to connect to Command Metric Stream.
查看hystrix-dashboard控制台,发现也报错:
解决办法:我们在ribbon-consume的实例主类中添加以下内容:
/** * @author HDN * @date 2019/6/16 14:29 */ @EnableCircuitBreaker @EnableDiscoveryClient @SpringBootApplication public class RibbonConsumeApplication { @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonConsumeApplication.class, args); } @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主页上访问http://localhost:9000/hystrix.stream,此时处于loading:
此时,hystrix-dashboard的控制台是这样子的:
我们访问http://localhost:9000/getUser/1和http://localhost:9000/getUser/2,再来看Hystrix 的页面:
注意事项:只有我们在对ribbon-consumer进行访问的时候hystrix dashboard才会进行监控,否则一直处于loading状态。
-----------------------------------------------------Turbine集群监控-----------------------------------------------
我们在单个实例监控的基础上做一些扩展,通过引入Turbine来聚合ribbon-consumer服务的监控信息,并输出给Hystrix Dashboard来进行展示。
一、创建一个Spring Boot工程,命名为turbine。
二、编写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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>turbine</artifactId> <version>0.0.1-SNAPSHOT</version> <name>turbine</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <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-netflix-turbine</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>
三、创建应用主类TurbineApplication,并使用@EnableTurbine注解开启Turbine、Turbine也是一个服务,需要注册到Eureka注册中心,要使用@EnableDiscoveryClient。
@EnableTurbine
@EnableDiscoveryClient
@SpringBootApplication
public class TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
}
四、在application.properties加入Eureka和Turbine的相关配置:
spring.application.name=turbine server.port=8989 eureka.client.service-url.defaultZone=http://localhost:1111/eureka/ turbine.app-config=ribbon-consumer turbine.cluster-name-expression="default" turbine.combine-host-port=true
其中,turbine.app-config指定了需要收集监控信息的服务名;turbine.cluster-name-expression该参数用来区分不同的聚合集群;turbine.combine-host-port参数设置为true,可以让同意主机上的服务通过主机名与端口号的组合来进行区分。
五、分别启动euraka-server、hello-service、ribbon-consume(8086、8086)、turbine和hystrix-dashboard,访问Hystrix Dashboard主页,并开启http://localhost:8989/turbine.stream的监控:
当访问了http://localhost:8086/getUser/1和http://localhost:8087/getUser/2,查看Hystrix的页面一直处于loading状态:
解决办法:我们在turbine的application.properties配置文件中添加:
spring.application.name=turbine server.port=8989 management.server.port=8990 eureka.client.service-url.defaultZone=http://localhost:1111/eureka/ turbine.instanceUrlSuffix=/hystrix.stream turbine.app-config=ribbon-consumer turbine.cluster-name-expression="default" turbine.combine-host-port=true
重启turbine应用,再次进行访问即可成功:
由上图中的host为2我们可以知道ribbon-consume启动了两个实例,这里只展示了一个监控图,是由于这两个实例是同一个服务,而对于集群来说我们关注的是服务集群的高可用性,所以Turbine会将相同服务作为整体来看待,并汇总成一个监控图。