标签:ams 端口 include 聚合监控 指标 startup mamicode Servle EAP
在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
本片文章基于第一篇工程改造
复制eureka-client工程,改名为eureka-client-hystrix-dashboard,在pom文件中引入依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud</artifactId> <groupId>com.niuben</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-client-hystrix-dashboard</artifactId> <packaging>jar</packaging> <dependencies> <!--Eureka Client依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--Web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--actuator监控监控依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--hystrix dashboard--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies> <!--Maven打包插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
编写配置文件application.yml
server: port: 8762 spring: application: name: eureka-client eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*"
在启动类添加@EnableHystrixDashboard注解, 开启HystrixDashboard
@SpringBootApplication @EnableEurekaClient // 开启Eureka客户端 @EnableHystrix // 开启断路器Hystrix @EnableHystrixDashboard // 开启HystrixDashboard public class EurekaClientHystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientHystrixDashboardApplication.class, args); } @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/actuator/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } }
修改controller层TestEurekaClient类
@RestController public class TestEurekaClient { @Value("${server.port}") String port; @GetMapping("/test") @HystrixCommand(fallbackMethod = "hiError") public String home(@RequestParam(value = "name", defaultValue = "niuben") String name) { return "hi " + name + " ,现在的端口是:" + port; } public String hiError(String name) { return "hi, "+name+", error!"; } }
依次启动eureka-server 和eureka-client-hystrix-dashboard
浏览器,打开http://localhost:8762/actuator/hystrix.stream(在请求http://localhost:8762/actuator/hystrix.stream之前,需要随便请求一个接口,例如:http://localhost:8762/test,否则会一直ping ping ping)
在打开localhost:8762/hystrix,可以看见以下界面
在节目依次输入http://localhost:8762/actuator/hystrix.stream 、2000 、test,出现以下界面
在另一个窗口请求http://localhost:8762/test?name=test
当我们有很多个服务的时候,这就需要聚合所以服务的Hystrix Dashboard的数据了
Hystrix Turbine将每个服务Hystrix Dashboard数据进行了整合
使用上面的工程
pom文件如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud</artifactId> <groupId>com.niuben</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-client-hystrix-turbine</artifactId> <packaging>jar</packaging> <dependencies> <!--Eureka Client依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--Web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--actuator监控监控依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--hystrix dashboard--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <!--hystrix turbine--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> </dependencies> <!--Maven打包插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
编写配置文件application.yml
server: port: 8764 spring: application: name: eureka-client-turbine eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*" turbine: app-config: eureka-client,eureka-clientTwo aggregator: clusterConfig: default clusterNameExpression: new String("default") combine-host: true instanceUrlSuffix: default: actuator/hystrix.stream
在启动类开启注解@EnableTurbine,开启了注册服务
@SpringBootApplication @EnableEurekaClient // 开启Eureka客户端 @EnableHystrix // 开启断路器Hystrix @EnableHystrixDashboard // 开启HystrixDashboard @EnableTurbine // 开启了注册服务。 public class EurekaClientHystrixTurbineApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientHystrixTurbineApplication.class, args); } @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/actuator/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; } }
再复制eureka-client-hystrix-dashboard工程,命名为eureka-client-hystrix-dashboard-two
依次启动eureka-server、eureka-client-hystrix-dashboard、eureka-client-hystrix-dashboard-two、eureka-client-hystrix--turbine工程
浏览器打开:http://localhost:8763/hystrix;输入监控流http://localhost:8764/turbine.stream,点击monitor stream 进入页面,请求http://localhost:8762/test?name=ee,http://localhost:8763/test?name=ee
就可以看到这个页面聚合了2个service的hystrix dashbord数据了
源码:https://gitee.com/niugit/spring-cloud
第四篇(续):熔断监控Hystrix Dashboard和熔断聚合监控Hystrix Turbine
标签:ams 端口 include 聚合监控 指标 startup mamicode Servle EAP
原文地址:https://www.cnblogs.com/niudaben/p/12447175.html