目录
一、问题
二、原因
1、修改配置
2、添加@Lazy注解在client上面
3、启动成功
一、问题
当在gateway的全局过滤器GlobalFilter中注入OpenFeign接口的时候会一直卡在路由中,但是不会进一步,导致启动未成功也未报错失败
2024-01-18 22:06:59.299 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [After]
2024-01-18 22:06:59.300 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Before]
2024-01-18 22:06:59.300 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Between]
2024-01-18 22:06:59.300 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Cookie]
2024-01-18 22:06:59.300 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Header]
2024-01-18 22:06:59.300 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Host]
2024-01-18 22:06:59.300 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Method]
2024-01-18 22:06:59.300 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Path]
2024-01-18 22:06:59.300 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Query]
2024-01-18 22:06:59.300 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [ReadBody]
2024-01-18 22:06:59.301 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [RemoteAddr]
2024-01-18 22:06:59.301 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [XForwardedRemoteAddr]
2024-01-18 22:06:59.301 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Weight]
2024-01-18 22:06:59.301 INFO 19000 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [CloudFoundryRouteService]
二、原因
在gateway网关中不能使用openfeign同步调用
三、解决方法
1、修改配置
由于配置了spring.cloud.gateway.discovery.locator.enabled = true导致,改为false或者去掉该配置,因为默认为false,但是该方案会导致动态路由不能加载,影响微服务调用。gateway:discovery:locator:enabled: false
2、添加@Lazy注解在client上面
在注入的AuthClient上面添加@Lazy注解
@Lazy@Autowiredprivate AuthClient authClient;
全部代码:
@Component
@Slf4j
public class GatewayGlobalFilter implements GlobalFilter, Ordered {@Autowiredprivate AuthProperties authProperties;@Lazy@Autowiredprivate AuthClient authClient;@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {//获取请求头的requestList<String> excludePaths = authProperties.getExcludePaths();log.info(excludePaths.toString());//判断是否需要拦截ServerHttpRequest request = exchange.getRequest();RequestPath path = request.getPath();//如果不需要拦截则放行if(excludePaths.contains(path.toString())){return chain.filter(exchange);}//获取tokenString authorization = request.getHeaders().getFirst("authorization");String token= authorization.replace("Bearer ","");// 验证tokenString userId = authClient.getUserId(token);if(userId==null){//设置响应码为401ServerHttpResponse response = exchange.getResponse();response.setStatusCode(HttpStatus.UNAUTHORIZED);return response.setComplete();}//放行return chain.filter(exchange);}@Overridepublic int getOrder() {return 0;}
}