背景:基于springcloud(五)进行
添加熔断器仪表盘监控
一、ribbon消费者
1. 在spring-cloud-ribbon工程中 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
2. 在 RibbonApplication 中增加 @EnableHystrixDashboard
注解
package com.zsx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
3. 添加hystrix.stream的servlet配置到
ServletRegistrationBean
package com.zsx.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HystrixServletDefinitions {
@Bean(name = "hystrixRegistrationBean")
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new HystrixMetricsStreamServlet(), "/hystrix.stream");
registration.setName("hystrixServlet");
registration.setLoadOnStartup(1);
return registration;
}
}
4. 依次启动注册中心,服务提供者、Ribbon消费者
5.查看请求http://localhost:8084/hystrix
6. 添加到仪表盘
7. 查看请求http://localhost:8084/ribbon/hello(多次刷新)
8. 查看请求http://localhost:8084/hystrix.stream
8. 查看仪表盘监控状态
二、Feign消费者
1. 在spring-cloud-feign工程中 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
2. 在 FeignApplication 中增加 @EnableHystrixDashboard
注解
package com.zsx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
3. 添加依赖(此处hystrix.stream
用另外的方法实现)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4. 暴露hystrix.stream,
配置文件添加如下内容
management:
endpoints:
web:
exposure:
include: hystrix.stream
base-path: /
5. 启动注册中心,服务提供者、Feign消费者
6. 查看请求http://localhost:8085/hystrix
7. 添加到仪表盘
8. 查看请求http://localhost:8085/ribbon/hello(多次刷新)
9. 查看请求http://localhost:8085/hystrix.stream
9. 查看仪表盘监控状态