1、加入断路由,由于feign中本身就有断路由,可以不加入依赖。
直接配置,
在属性文件中开启Feign的hystrix:
feign: hystrix: enabled: true
java中例:
@FeignClient(value="base",fallback=HelloService.HelloServiceBack.class) public interface HelloService { @GetMapping("/portal") String sayHi() ; @Component//必须加,否者找不到实例,官方文档未标注 static class HelloServiceBack implements HelloService{ @Override public String sayHi() { return "sayHi is error"; } } }
2、加入监控器Dashboard
Hystrix的指标流需依赖
<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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId> </dependency>
最新版的starter总是缺少关键依赖。需自己加入,官方文档没有。
启动类加入注解:
@EnableHystrix @EnableHystrixDashboard @EnableCircuitBreaker
完了后,打开页面:
会发现logo看不到,jquery也找不到,如果看不到,请自行配置静态资源路径。大概就是需要指定static,resource位置。
然后监控点加入http://localhost:8764/hystrix.stream
点击,发现无法连接,这是boot2.0的问题,需手动配置一个bean
@Bean public ServletRegistrationBean<HystrixMetricsStreamServlet> getHysServlet(){ HystrixMetricsStreamServlet hysServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean<HystrixMetricsStreamServlet> rb = new ServletRegistrationBean<HystrixMetricsStreamServlet>(hysServlet, "/hystrix.stream"); rb.setLoadOnStartup(1); rb.setName("HystrixMetricsStreamServlet"); return rb; }
然后就一切正常了,就会监控到配置了断路由的接口调用。
新版本坑比较多,需要自己摸索。