spring cloud gateway集成hystrix全局断路器

pom.xml添加依赖

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

在配置文件中,增加spring.cloud.gateway.default-filters:

default-filters:
- name: Hystrix
  args:
    name: fallbackcmd
    fallbackUri: forward:/fallbackcontroller

如上的配置,将会使用HystrixCommand打包剩余的过滤器,并命名为fallbackcmd,我们还配置了可选的参数fallbackUri,降级逻辑被调用,请求将会被转发到URI为/fallbackcontroller的控制器处理。定义降级处理如下:

@RequestMapping(value = "/fallbackcontroller")
public Map<String, String> fallBackController() {
    Map<String, String> res = new HashMap();
    res.put("code", "-100");
    res.put("data", "service not available");
    return res;
}

此时可以设置hystrix超时时间(毫秒) ,默认只有2秒

default-filters:
- name: Hystrix
  args:
    name: fallbackcmd
    fallbackUri: forward:/fallbackcontroller

猜你喜欢

转载自blog.csdn.net/whq12789/article/details/88179419