上一篇写了hystrix熔断器。
其实还不算完,因为还需要监控服务的运行情况,所以这一章写服务监控hystrix-dashboard
一、和ribbon配合使用
1.1 在ribbon-client项目的pom文件中添加依赖
<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>
1.2 在启动类上添加注解@EnableHystrixDashboard
package com.example.ribbonclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class RibbonClientApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonClientApplication.class, args);
}
}
1.3 运行测试
访问localhost:8700/hystrix,
依次在页面中输入localhost:8700/hystrix.stream、2000、index
然后点击Monitor Stream 按钮
如果是使用的spring2.0以上的版本,则会出现以下情况
因为acturtor默认只暴露了health和info端点,我们打开hystrix.stream端点即可
在pom文件中添加以下配置后重启ribbon-clientf服务并刷新页面
#打开hystix.stream端点(默认为打开,可以不写)
#management.endpoint.hystrix.stream.enabled=true
#对外暴露以下端点
management.endpoints.web.exposure.include=hystrix.stream,health,info
这时候页面处于Loading状态
这时候,另起一个标签页,访问http://localhost:8700/index 多次刷新后返回hystrix.stream标签页
这个时候服务信息已经出来了。
不知道什么意思?
打开浏览器的翻译功能(神器)
二、和feign配合使用
2.1 在pom文件中添加相同的配置,并同样添加配置文件
2.2 在启动类上添加注解@EnableCircuitBreaker
package com.example.feignclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class FeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
}
}
2.3 启动测试
不过,这样也还不算完,因为一个微服务中不会仅仅只存在一个对多服务的负载均衡。我总不能挨个去配置吧,那样多麻烦,springcloud中也已经解决了(turbine组件)。