Implementação de monitoramento de fluxo do painel Springcloud
Às vezes, precisamos monitorar nossos serviços, devemos usar esse tempo para monitorar o fluxo que
concluímos com base neste artigo
https://blog.csdn.net/weixin_43520670/article/details/114333758
1. Crie um novo projeto maven como um projeto separado do painel
1. Escreva um arquivo pom
A dependência é um pouco demais, mas na verdade não usa tanto.
<?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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-consumer-hystrix-dashboard</artifactId>
<dependencies>
<!-- ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- hystrix-dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- 我们需要拿到实体类 ,所以配置api module -->
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
2. Escreva a configuração
server:
port: 9001
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
username: root
password: 520China
3. Escreva a classe de inicialização
Participe principalmente de @EnableHystrixDashboard para iniciar o monitoramento
package com.hzxy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard //开启监控
public class UserConsumerDashboard_9001 {
public static void main(String[] args) {
SpringApplication.run(UserConsumerDashboard_9001.class,args);
}
}
4. Teste
Visite http://ybg.eureka1.com:9001/hystrix no navegador, a seguinte interface aparece e a inicialização é bem-sucedida!
2. Modifique o código do provedor do disjuntor de serviço
1. Adicionar dependências
Para adicionar dependência de monitoramento do atuador
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. Adicione um feijão
Para registrar um servlet em springcloud
//增加一个Servlet
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
O código completo é o seguinte:
package com.hzxy.springcloud;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
@MapperScan("com.hzxy.springcloud.mapper")
@EnableDiscoveryClient //服务发现
@EnableEurekaClient //在服务启动后自动注册到Eureka中
@SpringBootApplication
@EnableCircuitBreaker //添加对熔断的支持
public class HystrixProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(HystrixProvider_8001.class,args);
}
//增加一个Servlet
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
}
3. Teste
Abra o centro de registro, provedor de serviços e módulo de monitoramento, e a seguir insira nosso url registrado e outros parâmetros no módulo de monitoramento e clique no botão abaixo.
Então você pode ver que
podemos visitar o url registrado com o disjuntor para ver o mudanças de dados
Nota: encontrei um problema ao fazer isso. Depois de clicar no botão, ele carregava o tempo todo. O motivo é que não acessei a url cadastrada com o disjuntor e não pude monitorar.