Hystrix Service monitoring
SpringCloud Family bucket
Learning goals
Join Xiaobian Java Learning and communication Jun Yang (785794074) Code :67
Actuator
In addition to implementing service fault tolerance ,Hystrix It also provides near real-time monitoring function , The service execution results and operation indicators , Number of requests, number of successes, etc. These statuses pass Actuator
To collect , And then visit /actuator/hystrix.stream
You can see real-time monitoring data .
Add dependency
Add... In the project that needs to start data monitoring actuator
rely on .
<!-- spring boot actuator rely on -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy code
The configuration file
Open... In the configuration file hystrix.stream
Endpoint . If you want all endpoints to be exposed , Configure to '*'
.
# Metrics monitoring and health checks
management:
endpoints:
web:
exposure:
include: hystrix.stream
Copy code
Start class
package com.xxxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
// Open the fuse 2 choose 1,@EnableHystrix Encapsulates the @EnableCircuitBreaker
// @EnableHystrix
@EnableCircuitBreaker
@SpringBootApplication
public class OrderServiceRestApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderServiceRestApplication.class, args);
}
}
Copy code
visit
At this time, no Hystrix The data of . Next, request a method that will definitely make an error to generate the service after degradation processing , give the result as follows :
For this pure JSON It is very inconvenient for us to visually observe the running state of the service . We can use Hystrix Monitoring center to view .
The monitoring center
The so-called monitoring center is Hystrix Provide a set of visualization system Hystrix-Dashboard
, You can see the running status of services in the current environment very friendly .Hystrix-Dashboard
It's about Hystrix
Tools for real-time monitoring , adopt Hystrix-Dashboard
We can see each Hystrix Command
Request response time for , Request success rate and other data .
Add dependency
Add... In the project that needs to start data monitoring dashboard
rely on .
<!-- spring boot actuator rely on -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- spring cloud netflix hystrix rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- spring cloud netflix hystrix dashboard rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
Copy code
Start class
Add... In the project startup class that needs to start data monitoring @EnableHystrixDashboard
annotation .
package com.xxxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
// Open the fuse 2 choose 1,@EnableHystrix Encapsulates the @EnableCircuitBreaker
// @EnableHystrix
@EnableCircuitBreaker
// Open data monitoring annotation
@EnableHystrixDashboard
@SpringBootApplication
public class OrderServiceRestApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderServiceRestApplication.class, args);
}
}
Copy code
visit
View the data
Enter the information that can return monitoring data URL:
Diagram of monitoring center
Aggregate monitoring
Turbine It is a tool for aggregation server to send event flow data ,dashboard Only a single node can be monitored , The actual production environment is a cluster , So you can use Turbine To monitor cluster services .
Create project
stay hystrix-demo
Created under the parent project hystrix-turbine
engineering .
Add dependency
Project introduction hystrix
、dashboard
、turbine
Three dependencies .
<?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.xxxx</groupId>
<artifactId>hystrix-turbine</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- To inherit a father depends on -->
<parent>
<groupId>com.xxxx</groupId>
<artifactId>hystrix-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!-- Project dependence -->
<dependencies>
<!-- spring-cloud netflix hystrix rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- spring cloud netflix hystrix dashboard rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- spring cloud netflix turbine rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
</dependencies>
</project>
Copy code
The configuration file
application.yml
server:
port: 8181 # port
spring:
application:
name: hystrix-turbine # apply name
# To configure Eureka Server Registry Center
eureka:
instance:
prefer-ip-address: true # Whether to use ip Address registration
instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
client:
service-url: # Set service registry address
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
# Aggregate monitoring
turbine:
# List of services to monitor , Multiple separated by commas
app-config: order-service-rest,order-service-feign
# Specify cluster name
cluster-name-expression: "'default'"
Copy code
Start class
The startup class needs to be opened @EnableHystrix
、@EnableHystrixDashboard
、@EnableTurbine
Three annotations .
package com.xxxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
// Open the fuse 2 choose 1,@EnableHystrix Encapsulates the @EnableCircuitBreaker
// @EnableHystrix
@EnableCircuitBreaker
// Open data monitoring annotation
@EnableHystrixDashboard
// Enable aggregation monitoring annotation
@EnableTurbine
@SpringBootApplication
public class HystrixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbineApplication.class, args);
}
}
Copy code
visit
order-service-rest
and order-service-feign
All are equipped with monitoring centers , The final environment is as follows :
Access the multi node service status data as follows :
order-service-rest
and order-service-feign
The running states of the two services are as follows :
thus Hystrix The service monitoring knowledge points were explained . Thank you very much for your serious study , Come on, young man ~ Join Xiaobian Java Learning and communication Jun Yang (785794074) Code :67