1. 在springcloud 体系中,可以用 hystrix-dashboard 实时监控服务的运行状态。上一文记录了单实例的监控,现在实现集群监控。
2. 新建工程 hystrix-turbine 作为集群监控的实现服务。
2.1 file - new - module
2.2 spring Initializr - module SDK 选择自己的 JDK ,其余的可以不用填写,next。
2.3 填写工程相关信息:包名、工程名等,next。
2.4 此步只是帮助自动生成依赖,可不选,直接 next。
2.5 工程名,代码存放位置等,finish 。
2.6 生成工程的结构如下:
2.7 pom.xml 重用 hystrix-dashboard 的依赖,另新增少量依赖:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>hystrix-turbine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hystrix-turbine</name>
<description>监控面板-集群 </description>
<parent>
<groupId>com</groupId>
<artifactId>hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
2.8 启动类:
package com.hystrixturbine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
//监控面板-集群
@EnableTurbine
@SpringBootApplication
public class HystrixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbineApplication.class, args);
}
}
2.9 配置文件 application.properties:
# 注册中心 - 端口: 1234、工程名: eureka (见 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/
# 端口
server.port= 8889
# 工程名
spring.application.name= hystrix-turbine
# 被监控服务名称
turbine.app-config= ribbon,feign
# 集群名称为“default”。
# 应用服务很多时,可用多个 hystrix-turbine 来监控不同的聚合集群,集群名可区分这些不同的聚合集群,
# 此集群名亦可在 hystrix-dashboard 中用来定位不同的聚合集群:
# 即:“http://turbine-hostname:port/turbine.stream?cluster=[clusterName]” 中对应 “clusterName”便可。
turbine.cluster-name-expression= "default"
# 同一主机上的服务通过主机名与端口号的组合来进行区分,默认以 host 区分服务,
# 这样,本地调试时,本机上的不同服务则会聚合成一个服务来统计。
turbine.combine-host-port= true
3. 类同已有的 ribbon 工程,调整已有工程 feign 工程。
3.1 增加依赖 spring-cloud-starter-netflix-hystrix,此时 feign工程完整 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.feign</groupId>
<artifactId>service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-feign</name>
<description>服务消费 feign 方式</description>
<parent>
<groupId>com.base</groupId>
<artifactId>base-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- 开启 hystrix 监控 -->
<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-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
3.2 配置文件中加上 hystrix 相关配置,完整配置为:
# 注册中心 - 端口: 1234、工程名: eureka (见 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/
# 端口
server.port= 8702
# 工程名
spring.application.name= feign
# 开启断熔器: ( Feign 自带断路器,但默认为不开启: false)
feign.hystrix.enabled=true
# 也可配置为'*' ,纳入 hystrix 服务监控
management.endpoints.web.exposure.include= hystrix.stream
3.3 启动类确认注解 :@EnableHystrix,同时加上 hystrixMetricsStreamServlet 方法,完整启动类为:
package com.feign.servicefeign;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
/**
* 基于接口的注解,可插拔,可使用 Feign注解 和 JAX-RS注解
* Feign 默认集成 Ribbon,并和 Eureka 结合,默认实现负载均衡。
*/
@EnableFeignClients
// 标明自已为服务
@EnableEurekaClient
// 开启断路器: Hystrix
@EnableHystrix
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/hystrix.stream");
return registration;
}
}
4. 依次启动工程: 注册中心 eureka ( 端口 1234 ) 、 服务应用 ribbon ( 端口 8701 ) 、服务应用 feign ( 端口 8702)、
监控集群服务 hystrix-turbine ( 端口 8889)、监控仪表盘hystrix-dashboard ( 端口 8888) 。
其中,服务应用 ribbon、feign 都调用第三方服务应用:seeParam ( 端口 8801) 。
4.1 seeParam 工程未启动,由于有熔断机制,访问 feign 工程效果为:
4.2 同样,seeParam 工程未启动,由于有熔断机制,访问 ribbon 工程效果为:
4.3 访问 http://localhost:8888/hystrix.stream ,可看到和单实例监控界面入口一样的面板界面。
此时,在 hystrix-dashboard 中使用集群监控的 URL 查看监控信息,在页面第一个输入框中输入 hystrix-turbine 工程的访问路径:http://localhost:8889/turbine.stream
4.4 进入实时监控显示页面可看到 ,刚才已经多次刷新过请求的 feign、ribbon 工程的运行状况统计信息:
红色 100% 表示 请求 seeParam 工程失败。球体为红色也表明服务健康状态为最差。
4.5 启动 seeParam 工程后,请求其接口:
再多次刷新 feign、ribbon 的请求后,分别请求到了 seeParam 的接口:
此时,再查看 hysteix-dashboard 中的服务统计信息:
失败请求率为 0.0%,球体也变为绿色,表示服务运行正常。
至此,服务监控的集群模式也实现了。