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网页。