gateway整合Hystrix详细教程

2022-07-01 12:37:18

gateway整合Hystrix详细教程

一、首先maven依赖导入

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

二、设置yml文件,在routes组里面加上这个

- id: product_consumer
          uri: lb://service-consumer
          predicates:
          - Path=/Cus_echo/** #拦截该路径转发到URI
          - After=2019-12-25T14:33:47.789+08:00
          - Method=GET
          filters:
          - AddRequestHeader=X-Request-Foo, Bar
          - AddRequestParameter=foo, bar
          - AddResponseHeader=X-Response-Foo, Bar
          - name: Hystrix 
            args:
              name: fallbackcmd 
              fallbackUri: forward:/fallback  #返回路径

接着第二步在yml中设置Hystrix的全局超时信息

#这里的fallbackcmd就是第二步的那个名字
hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 3000

三、最后一步,在gateway的controller里面设置/fallback路径

@RestController
    public class DefaultHystrixController{
        @RequestMapping(value = "/fallback",method = RequestMethod.GET)
        public String fallback(){
            System.out.println("fallback****************Gateway");
            return "welcome to fallback";
        }
    }

为了看见效果,你可以在访问 lb://service-consumer 的时候,故意在方法里面设置Thread,slepp(5000),超过3秒返回结果,就会触发Hystrix的。请求路径将被转发到 /fallback

  • 作者:Messi哈
  • 原文链接:https://blog.csdn.net/qq_43184345/article/details/111226005
    更新时间:2022-07-01 12:37:18