spring cloud 断路器监控-Hystrix Dashboard
阅读数:128 评论数:0
跳转到新版页面分类
python/Java
正文
Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图表化界面。
修改service-hi
1、在pom工程文件引入相应的依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
2、在程序的入口ServiceHiApplication类,加上@EnableHystrix注解开启断路器,这个必须的,并且需要在程序中声明断点HystrixCommand,加上@EnableHystrixDashboard注解,开启HystrixDashboard。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceHiApplication {
/**
* 访问地址 http://localhost:8762/actuator/hystrix.stream
* @param args
*/
public static void main(String[] args) {
SpringApplication.run( ServiceHiApplication.class, args );
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
@HystrixCommand(fallbackMethod = "hiError")
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "hi " + name + " ,i am from port:" + port;
}
public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}
4、application.yml
server:
port: 8762
application:
name: service-hi
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
4、运行程序,依次开启eureka-server和service-hi
5、打开http://localhost:8762/actuator/hystrix.stream,可以看到一些具体的数据。
6、打开localhost:8762/hystrix,可以看见下界面。在界面依次输入http://localhost:8762/actuator/hystrix.stream、2000、miya点击确定。
7、在另一个窗口输入http://localhost:8762/hi?name=xxx,重新刷新hystrix.stream网页。
zuul+hystrix
zuul本身就集成了Hystrix,实际上Zuul的路由转发也是用到了Ribbon+Hystrix,也就意味着我们可以通过Hystrix Dashboard监控zuul的工作情况。
方式一:开启Hystrix.stream入口
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 HystrixConfiguration {
@Bean(name = "hystrixRegistrationBean")
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new HystrixMetricsStreamServlet(), "/hystrix.stream");
registration.setName("hystrixServlet");
registration.setLoadOnStartup(1);
return registration;
}
@Bean(name = "hystrixForTurbineRegistrationBean")
public ServletRegistrationBean servletTurbineRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new HystrixMetricsStreamServlet(), "/actuator/hystrix.stream");
registration.setName("hystrixForTurbineServlet");
registration.setLoadOnStartup(1);
return registration;
}
}
配置完成后重启zuul,访问http://localhost:port/hystrix.stream或者http://localhost:port/actuator/hystrix.stram就可以看到Hystrix流信息(之所以配置两个入口,是为了满足turbine需要)。
这时候我们可以通过已有看板以链接方式查看Hystrix Dashboard,或者可以用Turbine来聚合Hystrix数据了。
方式二:引入Hystrix Dashboard
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
加入@EnableHystrixDashboard注解 ,
management:
endpoints:
web:
exposure:
include: hystrix.stream # 或者'*'
在Dashboard中通过/actuator/hystrix.stream接口监控。
重新启动服务实例后再次访问,页面变成了Loading,是因为需要调用任意hystrix接口,不然没有hystrix调用,访问hystrix.stream会一直ping, hystrix监控界面就会一直loading。