pom.xml添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
在配置文件中,增加spring.cloud.gateway.default-filters:
default-filters:
- name: Hystrixargs:name: fallbackcmdfallbackUri: forward:/fallbackcontroller
一定要注意是spring.cloud.gateway.default-filters这个配置节。
如上的配置,将会使用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秒
hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 30000
示例代码:
springcloudconsul_test/springtest_gateway at master · wanghongqi/springcloudconsul_test · GitHub