【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
新增了一个微服务 , 该微服务 因为 查询耗时比较久,那么 在网关这里对应的 hystrix 怎么配置超时?
因为 默认已经配置 5s 了。 这个新增的微服务 肯定 不能少于5s 的
spring gateway 的配置
# 开启熔断器功能
feign:
hystrix:
enabled: true
# 熔断器配置
hystrix:
command:
default:
execution:
isolation:
thread:
# 全局熔断器5s超时
timeoutInMilliseconds: 5000
如果是 某个接口的话配置 hystrix 网上还是很多资料的。 但是到了网关这里, 看到有点懵逼,那么官网的 文档也是看着有点懵
这里我们使用的 配置路由规则
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
// uri以lb://开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称
// 本地环境调试说明:假设需要调试服务为cloudx-sys,1、将网关的此后缀修改为-xc(xc为姓名缩写);2、将对应的服务名修改为cloudx-sys-xc
// 用户和APP的权限验证和记录超时请求的过滤器
AuthAndTimeFilter authAndTimeFilter = new AuthAndTimeFilter(userServiceClient);
// 将微服务返回的对象,统一改写成前端需要的输出格式
ModifyResponseFilter modifyResponseFilter = new ModifyResponseFilter();
return builder.routes()
// 旧的智慧门店
.route(r -> r
.path("/api/o2o/**")
.filters(f -> f
.stripPrefix(2)
.hystrix(config -> config
.setName("o2oHystrix")
.setFallbackUri("forward:/fallback/o2o")
)
.filter(authAndTimeFilter)
.filter(modifyResponseFilter)
)
.uri("lb://" + BalancerClient.CLOUDX_O2O))
// 智慧门店-支付接口
.route(r -> r
.path("/api/pay/**")
.filters(f -> f
.stripPrefix(1)
.hystrix(config -> config
.setName("payHystrix")
.setFallbackUri("forward:/fallback/pay")
)
.filter(authAndTimeFilter)
.filter(modifyResponseFilter)
)
.uri("lb://" + BalancerClient.CLOUDX_O2O))
// 智慧门店开放接口-POST请求
.route(r -> r.method(HttpMethod.POST).and()
.readBody(String.class, readBody -> {
// 启用Gateway的ReadBodyPredicateFactory校验,将body缓存到请求中,方便后续过滤器获取post请求参数
// logger.info("请求体为:" + readBody);
return true;
}).and()
.path("/api/open/**")
.filters(f -> f
.stripPrefix(1)
.hystrix(config -> config
.setName("openPostHystrix")
.setFallbackUri("forward:/fallback/open")
)
.filter(authAndTimeFilter)
.filter(modifyResponseFilter)
)
.uri("lb://" + BalancerClient.CLOUDX_O2O).order(0))
// 智慧门店开放接口-GET请求
.route(r -> r.method(HttpMethod.GET).and()
.path("/api/open/**")
.filters(f -> f
.stripPrefix(1)
.hystrix(config -> config
.setName("openGetHystrix")
.setFallbackUri("forward:/fallback/open")
)
.filter(authAndTimeFilter)
.filter(modifyResponseFilter)
)
.uri("lb://" + BalancerClient.CLOUDX_O2O).order(1))
// 系统服务
.route(r -> r
.path("/api/sys/**")
.filters(f -> f
.stripPrefix(2)
.hystrix(config -> config
.setName("sysHystrix")
.setFallbackUri("forward:/fallback/sys")
)
.filter(authAndTimeFilter)
.filter(modifyResponseFilter)
)
.uri("lb://" + BalancerClient.CLOUDX_SYS))
// 交易服务
.route(r -> r.path("/api/trade/**")
.filters(f -> f
.stripPrefix(2)
.hystrix(config -> config
.setName("tradeHystrix")
.setFallbackUri("forward:/fallback/trade")
)
.filter(authAndTimeFilter)
.filter(modifyResponseFilter)
//.modifyResponseBody(String.class, String.class, rewriteFunction)
)
.uri("lb://" + BalancerClient.CLOUDX_TRADE))
// jira 服务
.route(r -> r.path("/api/jira/**")
.filters(f -> f
.stripPrefix(2)
.hystrix(config -> config
.setName("jiraHystrix")
.setFallbackUri("forward:/fallback/jira")
)
.filter(authAndTimeFilter)
)
.uri("lb://" + BalancerClient.CLOUDX_JIRA))
// da 服务 , hystrix 不需要配置,以防超时限制 或者为该服务额外配置超时
.route(r -> r.path("/api/da/**")
.filters(f -> f
.stripPrefix(2)
.hystrix(config -> config
.setName("daHystrix")
.setFallbackUri("forward:/fallback/da")
)
.filter(authAndTimeFilter)
)
.uri("lb://" + BalancerClient.CLOUDX_DA))
.build();
}
1. 开启了 hystrix ,是否 全局有效?
比如 :
以 这里的 路由规则 da 为例,就是 da 微服务
.route(r -> r.path("/api/da/**")
.filters(f -> f
.stripPrefix(2)
// .hystrix(config -> config
// .setName("daHystrix")
// .setFallbackUri("forward:/fallback/da")
// )
.filter(authAndTimeFilter)
)
.uri("lb://" + BalancerClient.CLOUDX_DA))
经过测试, 该路由 对应 全局的 hystrix 已经不生效了。 即 不存在 hystrix 了。
2. 配置 对应路由 的 hystrix 超时
如果 需要 不同的路由不同的 hystrix 配置比如 超时配置 怎么做?
我尝试 着 从 代码层面 上 控制, 发现 并不 可以,很难行得通。
而且 路由规则的配置 这里 查看网友的和官网的也没有在这里 进行控制 hystrix 的。
那么试着 使用 hystrix 配置了
路由代码:
.route(r -> r.path("/api/da/**")
.filters(f -> f
.stripPrefix(2)
.hystrix(config -> config
.setName("daHystrix")
.setFallbackUri("forward:/fallback/da")
)
.filter(authAndTimeFilter)
)
.uri("lb://" + BalancerClient.CLOUDX_DA))
配置文件:
# 熔断器配置
hystrix:
command:
default:
execution:
isolation:
thread:
# 全局熔断器5s超时
timeoutInMilliseconds: 5000
# da 服务 熔断超时
daHystrix:
execution:
isolation:
thread:
timeoutInMilliseconds: 62000
发现 对应 da微服务即 /api/da 路由来说 其 配置的 hystrix 超时 已经生效了,并不是全局的5s 了。
即 路由配置里面的 name 对应 hystrix 配置的 name 即可,比如 这里 daHystrix 名称
参考 https://www.jianshu.com/p/81b0059dbd98
来源:oschina
链接:https://my.oschina.net/ouminzy/blog/3152299