天天看點

Hystrix監控、Feign整合Hystrix監控一、Hystrix監控二、Feign項目的Hystrix監控 

一、Hystrix監控

除實作容錯外,Hystrix還提供了近乎實時的監控。HystrixCommand和HystrixObservableCommand在執行時,會生成執行結果和運作名額,比如每秒執行的請求數、成功數等,這些監控資料對分析應用系統的狀态很有用。

使用Hystrix的子產品 hystrix-metrics-event-stream ,就可将這些監控的名額資訊以 text/event-stream 的格式暴露給外部系統。spring-cloud-starter-hystrix包含該子產品,在此基礎上,隻須為項目添加spring-boot-starter-actuator,就可使用 /hystrix.stream 端點擷取Hystrix的監控資訊了。

1、eureka-client-consumer-hystrix的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.springclouddemo</groupId>
    <artifactId>eureka-client-consumer-hystrix</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client-consumer-hystrix</name>
    <description>整合Hystrix</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-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.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-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、修改EurekaClientConsumerHystrixApplication類如下

package com.springclouddemo.eurekaclientconsumerhystrix;

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.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author 何昌傑
 */
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class EurekaClientConsumerHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientConsumerHystrixApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @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;
    }
}
           

3、啟動項目eureka-server、eureka-client-provider、eureka-client-consumer-hystrix

4、通路http://localhost:7206/hystrix.stream後可以看到看到頁面會不斷出現以下内容:

Hystrix監控、Feign整合Hystrix監控一、Hystrix監控二、Feign項目的Hystrix監控 

5、通路http://localhost:7206/hello/hcj後再次通路http://localhost:7206/hystrix.stream後可以看到看到頁面會不斷出現以下内容:

Hystrix監控、Feign整合Hystrix監控一、Hystrix監控二、Feign項目的Hystrix監控 

說明系統不斷地擷取實時的監控資料。Hystrix的監控名額非常全面,例如HystrixCommand的名稱、group名稱、斷路器狀态、錯誤率、錯誤數等。

二、Feign項目的Hystrix監控

1、複制項目eureka-client-consumer-feign-fallback為eureka-client-consumer-feign-fallback-stream

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>
	<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.springclouddemo</groupId>
	<artifactId>eureka-client-consumer-feign-fallback-stream</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>eureka-client-consumer-feign-fallback-stream</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-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-openfeign</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-actuator</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、修改application配置檔案

spring.application.name=eureka-client-consumer-feign-fallback-stream
server.port=7209
eureka.client.service-url.defaultZone=http://localhost:7000/eureka/
eureka.instance.prefer-ip-address=true
feign.hystrix.enabled=true
           

4、為啟動類添加@EnableCircuitBreaker注解

package com.springclouddemo.eurekaclientconsumerfeignfallbackstream;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author 何昌傑
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
public class EurekaClientConsumerFeignFallbackStreamApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClientConsumerFeignFallbackStreamApplication.class, args);
	}

}
           

這樣就可以使用/hystrix.stream端點監控Hystrix了。

附一張參數解釋圖:

Hystrix監控、Feign整合Hystrix監控一、Hystrix監控二、Feign項目的Hystrix監控 

繼續閱讀