1.首先创建eureka server,hystrix仪表盘的显示需要注册到eureka上,此处省略了,源码下载地址:https://download.csdn.net/download/qq_35449098/12514397
2.创建service-hystrix,pom.xml添加如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<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-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>
application.yml修改为:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8766
spring:
application:
name: service-lxy
ServieHystrixApplication修改如下:
@SpringBootApplication
@RestController
@EnableEurekaClient
/**开启断路器 打开仪表盘 */
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServieHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(ServieHystrixApplication.class, args);
}
//添加servlet 仪表盘插入点 http://localhost:8766/hystrix.stream
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
@RequestMapping("/test")
@HystrixCommand(fallbackMethod = "hiError")//开启断路器
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "hi:" + name;
}
public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}
3.分别启动eureka、service-hystrix即可,
浏览器访问:http://localhost:8766/test?name=test,出现如图所示:
然后访问仪表盘:http://localhost:8766/hystrix
最后,出现如上图所示,即为配置成功啦~
tip:如果出现loading 1.检查插入点是否正确 2.记得先请求一次服务http://localhost:8766/test?name=test,仪表盘才会显示
3.如果还是loading,且http://localhost:8766/hystrix.stream,有数据返回,那就是boot和cloud的版本不一致导致的错误,版本对应可参考https://blog.csdn.net/qq_35449098/article/details/106633994