FeignClient中使用熔断机制hystrix
阅读原文时间:2021年04月18日阅读:9

一, Feign是自带断路器的

feign.hystrix.enabled=true  是否开启熔断器

hystrix.command.default.execution.timeout.enabled=true  是否开启超时熔断, 如果为false, 则熔断机制只在服务不可用时开启

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=300   设置超时熔断时间

[java]  view plain  copy

  1. @FeignClient(value = "YunCai-SearchServer", fallback=ServiceHystrix.class)

  2. public interface SchedualServiceHi {

  3. @RequestMapping(value = "/luke/itemsearch",method = RequestMethod.GET)

  4. ResponseMap sayHiFromClientOne(@RequestParam(value = "start") Integer start,

  5. @RequestParam(value = "rows") Integer rows,

  6. @RequestParam(value = "tenantId") Integer tenantId,

  7. @RequestParam(value = "status") Integer status,

  8. @RequestParam(value = "key") String key);

  9. }

[java]  view plain  copy

  1. @Component

  2. public class ServiceHystrix implements SchedualServiceHi {

  3. @Override

  4. public ResponseMap sayHiFromClientOne(Integer start, Integer rows, Integer tenantId, Integer status, String key) {

  5. SearchResult result = new SearchResult(0, new ArrayList<>(Arrays.asList(1L,2L,3L)));

  6. return new ResponseMap(false, "111", 506, result);

  7. }

  8. }

二, Hystrix 仪表盘

首选在pom.xml引入spring-cloud-starter-hystrix-dashboard的起步依赖:

[java]  view plain  copy

  1. org.springframework.boot

  2. spring-boot-starter-actuator

  3. org.springframework.cloud

  4. spring-cloud-starter-hystrix-dashboard

在主程序启动类中加入@EnableHystrixDashboard注解,开启hystrixDashboard:

[java]  view plain  copy

  1. @SpringBootApplication

  2. @EnableDiscoveryClient

  3. @EnableHystrix

  4. @EnableHystrixDashboard

  5. public class ServiceRibbonApplication {

  6. public static void main(String[] args) {

  7. SpringApplication.run(ServiceRibbonApplication.class, args);

  8. }

  9. @Bean

  10. @LoadBalanced

  11. RestTemplate restTemplate() {

  12. return new RestTemplate();

  13. }

  14. }

打开浏览器:访问http://localhost:8764/hystrix,界面如下:

点击monitor stream,进入下一个界面,访问:http://localhost:8764/hi?name=forezp

此时会出现监控界面: