一 hystrix的服务监控调用
1.1 hystrix的服务监控调用
hystrix提供了准实时的监控调用(hystrix dashbord),Hystrix 会持续的记录所有通过hystrix发送的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。Netflix通过 hystrix-metrics-event-stream项目实现了对以上指标的监控,springcloud提供了hystrix dashbord的整合,对监控内容转化内容实现可视化界面。
1.2 访问原理
二 hystrix dashboard搭建
2.1 工程结构
2.2 配置pom
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.3 配置配置文件
2.4 启动类
2.5 测试访问
三 hystrix dashboard监控相应微服务
3.1 监控9009服务
启动加代码
package com.ljf.mscloud;
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.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
/**
* Hello world!
*
*/
@EnableDiscoveryClient
@SpringBootApplication
//@EnableHystrix
@EnableCircuitBreaker
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
System.out.println("9009服务启动完毕!!!!");
}
/**
*此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
*ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
*只要在自己的项目里配置上下面的servlet就可以了
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
System.out.println("初始化..................");
return registrationBean;
}
}
2.截图
3.2 1001服务的配置
1.修改配置文件
2.代码
hystrix:
dashboard:
proxy-stream-allow-list: localhost
3.3 启动服务测试
1.启动nacos,启动sleuth
2.启动服务
1001服务启动
9009服务启动
3.测试访问
3.1) 成功时,访问:
查看dashboard,如图:
3.2)失败时访问:
访问