FeignClient中使用熔断机制hystrix
一, Feign是自带断路器的
feign.hystrix.enabled=true 是否开启熔断器
hystrix.command.default.execution.timeout.enabled=true 是否开启超时熔断, 如果为false, 则熔断机制只在服务不可用时开启
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=300 设置超时熔断时间
- @FeignClient(value = "YunCai-SearchServer", fallback=ServiceHystrix.class)
- public interface SchedualServiceHi {
- @RequestMapping(value = "/luke/itemsearch",method = RequestMethod.GET)
- ResponseMap sayHiFromClientOne(@RequestParam(value = "start") Integer start,
- @RequestParam(value = "rows") Integer rows,
- @RequestParam(value = "tenantId") Integer tenantId,
- @RequestParam(value = "status") Integer status,
- @RequestParam(value = "key") String key);
- }
- @Component
- public class ServiceHystrix implements SchedualServiceHi {
- @Override
- public ResponseMap sayHiFromClientOne(Integer start, Integer rows, Integer tenantId, Integer status, String key) {
- SearchResult result = new SearchResult(0, new ArrayList<>(Arrays.asList(1L,2L,3L)));
- return new ResponseMap(false, "111", 506, result);
- }
- }
二, Hystrix 仪表盘
首选在pom.xml引入spring-cloud-starter-hystrix-dashboard的起步依赖:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
- </dependency>
在主程序启动类中加入@EnableHystrixDashboard注解,开启hystrixDashboard:
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableHystrix
- @EnableHystrixDashboard
- public class ServiceRibbonApplication {
- public static void main(String[] args) {
- SpringApplication.run(ServiceRibbonApplication.class, args);
- }
- @Bean
- @LoadBalanced
- RestTemplate restTemplate() {
- return new RestTemplate();
- }
- }
打开浏览器:访问http://localhost:8764/hystrix,界面如下:
点击monitor stream,进入下一个界面,访问:http://localhost:8764/hi?name=forezp
此时会出现监控界面: