被监控端
1: 在微服务上pom.xml 引入依赖
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>
<version>1.5.18</version>
</dependency>
2: 在application.java 中 引入bean
package com.itlaoqi.springcloud.memberserviceopenfeign;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import feign.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableFeignClients
public class MemberServiceOpenfeignApplication {
// 被引入的bean
@Bean
public ServletRegistrationBean hystrixServlet(){
HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
registrationBean.setLoadOnStartup(1);
return registrationBean;
}
public static void main(String[] args) {
SpringApplication.run(MemberServiceOpenfeignApplication.class, args);
}
}
2: 创建监控工程 dashboard 监控
1: pom.xml
<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-dashboard</artifactId>
</dependency>
2: application.yml文件
server:
port: 9100
spring:
application:
name: hystrix-dashboard
eureka:
client:
service-url:
defaultZone:
http://localhost:8761/eureka
3: 在java 类上开启
package com.itlaoqi.springcloud.hystrix.dashboard.dashboardservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
public class DashboardServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DashboardServiceApplication.class, args);
}
}
4: 开始演示
在 浏览器 输入 http://localhost:9100/hystrix
注意:springboot2.2.x, hystrix-dashboard监控页面一直显示loading的问题 (https://blog.csdn.net/weixin_43493520/article/details/106646860?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.nonecase)
我的其他微服务 使用的是 2.3.x版本 。但是2.3.X 版本的 dashboard 前端代码有问题。所以该用2.1.x 版本的dashboard 。使用正常