公众号: java乐园
在微服务架构中如何去监控服务的状态是否正常,非常重要。为例保证微服务的可用性,防止程序断路器模型。运维人员需要一个友好的程序来监控微服务的状态, Hystrix Dashboard作为断路器监控的一个重要组件,提供了数据监控及非常友好的图形化界面,方便运维人员对服务进行监控;,通过界面反馈的信息可以快速发现系统中存在的问题。另外Hystrix Dashboard是一个独立的服务结点,不需要配置任何的注册中心。
1、 新建项目sc-hystrix-dashboard,对用的pom.xml文件如下
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
spring-cloud
sc-hystrix-dashboard
0.0.1-SNAPSHOT
jar
sc-hystrix-dashboard
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.RELEASE
pom
import
UTF-8
1.8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.boot
spring-boot-starter-actuator
说明:spring cloud 2.x之后spring-cloud-starter-hystrix-dashboard和spring-cloud-starter-hystrix都标志为过期,具体推荐使用两个依赖请看下面的图片
2、 新建spring boot启动类DashboardApplication.java
package sc.hystrix.dashboard;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class DashboardApplication {
public static void main(String[] args) {
SpringApplication.run(DashboardApplication.class, args);
}
}
只需添加注解EnableHystrixDashboard,启动Hystrix Dashboard即可
3、 配置Hystrix Dashboard对应的Servlet
package sc.hystrix.dashboard;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
@Configuration
public class DashboardServletConfig {
@Bean
public ServletRegistrationBean getDashboardServlet(){
HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean bean = new ServletRegistrationBean(servlet);
bean.setLoadOnStartup(1);
bean.addUrlMappings("/hystrix.stream");
bean.setName("hystrix");
return bean;
}
}
4、 新建配置文件bootstrap.yml
spring:
application:
name: sc-hystrix-dashboard
server:
port: 2001
5、 启动并验证是否启动成功
方法一:查看日志配置的端口2001,在日志中标志启动成功
方法二:从上图标志的1中可以知道可以方法地址:http://127.0.0.1:2001/hystrix
6、 分析一下下图中的英文
大概的意思是:如果查看默认集群使用第一个url,查看指定集群使用第二个url,单个应用监控使用最后一个
7、 在url中输入http://127.0.0.1:2001/hystrix.stream
然后点击Monitor Stream按钮
或者访问http://localhost:2001/hystrix.stream也会不断的显示ping