【前面的話】書接上文,本文的某些知識依賴我的上一篇SpringCLoud的文章:SpringCloud之Feign,如果沒有看過可以先移步去看一下。前文提到了hystrix的應用,以及hystrix的監控,當時我們在實際生產過程中往往會在多個服務中或者說網關集群中使用hystrix,這樣我們來監控的是否再去分別查看當時的每個應用的話,效率就會顯得很低下呢,這里我們就要用的上文提到的集群監控了。
壹、Turbine的簡介
看單個的Hystrix Dashboard的數據並沒有什么多大的價值,要想看這個系統的Hystrix Dashboard數據就需要用到Hystrix Turbine。Hystrix Turbine將每個服務Hystrix Dashboard數據進行了整合。Hystrix Turbine的使用非常簡單,只需要引入相應的依賴和加上注解和配置就可以了。
簡而言之:Turbine就是聚合監控多個Hystrix Dashboard的數據。
貳、准備工作
新建一個feign子工程lovin-cloud-turbine,用於后面的操作。下面是主要的pom依賴:
<parent>
<artifactId>lovincloud</artifactId>
<groupId>com.eelve.lovincloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lovin-cloud-turbine</artifactId>
<packaging>jar</packaging>
<name>lovincloudturbine</name>
<version>0.0.1</version>
<description>turbine監控</description>
<dependencies>
<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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-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-turbine-amqp</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 這里為了安全,我這里還是添加spring-boot-starter-security
server:
port: 8808 # 服務端口號
spring:
application:
name: lovincloudturbine # 服務名稱
security:
basic:
enabled: true
user:
name: lovin
password: ${REGISTRY_SERVER_PASSWORD:lovin}
eureka:
client:
serviceUrl:
defaultZone: http://lovin:lovin@localhost:8881/eureka/ # 注冊到的eureka服務地址
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: lovin
user.password: lovin
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
turbine:
aggregator:
clusterConfig: default # 指定聚合哪些集群,多個使用","分割,默認為default。可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問
appConfig: lovinfeignclient,lovinribbonclient ### 配置Eureka中的serviceId列表,表明監控哪些服務
clusterNameExpression: new String("default")
# 1. clusterNameExpression指定集群名稱,默認表達式appName;此時:turbine.aggregator.clusterConfig需要配置想要監控的應用名稱
# 2. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig可以不寫,因為默認就是default
# 3. 當clusterNameExpression: metadata['cluster']時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置,同時turbine.aggregator.clusterConfig: ABC
- 配置spring-boot-starter-security,這里為了方便我這里放開所有請求
package com.eelve.lovin.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* @ClassName SecurityConfig
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/16 14:13
* @Version 1.0
**/
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
- 在主類上添加@EnableTurbine,當然也需要注冊到注冊中心:
package com.eelve.lovin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream;
/**
* @ClassName LovinCloudTurbineApplication
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/25 17:17
* @Version 1.0
*
**/
@SpringBootApplication
@EnableDiscoveryClient
@EnableTurbine
public class LovinCloudTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(LovinCloudTurbineApplication.class,args);
}
}
- 改造lovin-feign-client,使之變成集群,添加第二份配置文件
叄、啟動測試
- 依次啟動eureka的服務端和兩個客戶端,以及lovin-feign-client、lovin-ribbon-client和新建的lovin-cloud-turbine
我們可以看到服務已經全部啟動成功 - 然后訪問幾次http://localhost:8806/getHello和http://localhost:8805/hello使之產生熔斷器數據,然后訪問http://localhost:8806/hystrix按照提示選擇第一個集群監控
肆、消息隊列來做到異步監控
turbine服務端修改
- 修改pom依賴
<dependencies>
<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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-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-turbine-amqp</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
可以看到這里主要引入了spring-cloud-starter-turbine-amqp依賴,它實際上就是包裝了spring-cloud-starter-turbine-stream和pring-cloud-starter-stream-rabbit。
- 添加連接rabbitmq配置
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
- 在應用主類中使用@EnableTurbineStream注解來啟用Turbine Stream的配置
package com.eelve.lovin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream;
/**
* @ClassName LovinCloudTurbineApplication
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/25 17:17
* @Version 1.0
*
**/
@SpringBootApplication
@EnableDiscoveryClient
@EnableTurbineStream
public class LovinCloudTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(LovinCloudTurbineApplication.class,args);
}
}
對服務消費者進行修改
- 添加spring-cloud-netflix-hystrix-amqp依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-amqp</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
- 添加連接rabbitmq配置
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
然后重啟服務之后,就可以再次看到監控詳情
伍、監控數據流圖
- 我們可以看到我們調用的服務不再是像再上一篇文章中的直接訪問對應的服務,而是通過feign的Ribbon的負載均衡的去調用的,而且這里說明一點,Ribbon的默認機制是輪詢。
- 直接使用Turbine監控
- 使用RabbitMQ異步監控
- 其中后者更能做到和業務解耦
陸、Turbine詳解
- 我們可以在監控信息的左上部分找到兩個重要的圖形信息:一個實心圓和一條曲線。
- 實心圓:共有兩種含義。它通過顏色的變化代表了實例的健康程度,如下圖所示,它的健康度從綠色、黃色、橙色、紅色遞減。該實心圓除了顏色的變化之外,它的大小也會根據實例的請求流量發生變化,流量越大該實心圓就越大。所以通過該實心圓的展示,我們就可以在大量的實例中快速的發現故障實例和高壓力實例。
- 曲線:用來記錄2分鍾內流量的相對變化,我們可以通過它來觀察到流量的上升和下降趨勢。