问题
gateway整合hystrix,测试熔断功能。
注意事项
- gateway整合hystrix是否生效和启动类上的注解
@EnableHystrix
没有什么关系 - gateway整合hystrix生效的关键是在于是否配置了hystrix的过滤器,配置了全局或路由局部的都可以,我这里配置的全局
spring:
gateway:
discovery:
locator:
# 开启负载均衡对网关的路由转发的支持
enabled: true
enabled: true
default-filters:
# 熔断过滤器
- name: Hystrix
args:
name: fallbackcmd
fallbackuri: forward:/fallexecute
- hystrix的依赖,不能去掉,否则会报异常,是因为gateway中route路由组件需hystrix
<!-- 此依赖不能去掉 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
缺失hystrix依赖的报错信息:
Caused by: reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name Hystrix
Caused by: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name Hystrix
at org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator.loadGatewayFilters(RouteDefinitionRouteLocator.java:187) ~[spring-cloud-gateway-server-2.2.9.RELEASE.jar:2.2.9.RELEASE]
at org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator.getFilters(RouteDefinitionRouteLocator.java:228) ~[spring-cloud-gateway-server-2.2.9.RELEASE.jar:2.2.9.RELEASE]
at org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator.convertToRoute(RouteDefinitionRouteLocator.java:170) ~[spring-cloud-gateway-server-2.2.9.RELEASE.jar:2.2.9.RELEASE]
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:100) ~[reactor-core-3.3.17.RELEASE.jar:3.3.17.RELEASE]
at reactor.core.publisher.FluxFlatMap$FlatMapMain.tryEmitScalar(FluxFlatMap.java:481) ~[reactor-core-3.3.17.RELEASE.jar:3.3.17.RELEASE]
......
gateway整合hystrix的两种模式(线程池/信号量)细节问题
配置
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
# 开启负载均衡对网关的路由转发的支持
enabled: true
enabled: true
default-filters:
# 熔断过滤器
- name: Hystrix
args:
name: fallbackcmd
fallbackuri: forward:/fallexecute
# 负载均衡路由规则 开启路由后的服务swagger才能显示
routes:
- id: test-service
uri: lb://test-service
predicates:
- Path=/gateway/test/**
filters:
- StripPrefix=2