SpringCloud GateWay 在全局过滤器中注入OpenFeign网关后无法启动

目录

一、问题

二、原因

1、修改配置

2、添加@Lazy注解在client上面

3、启动成功


一、问题

当在gateway的全局过滤器GlobalFilter中注入OpenFeign接口的时候会一直卡在路由中,但是不会进一步,导致启动未成功也未报错失败

java 复制代码
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、修改配置

java 复制代码
	由于配置了spring.cloud.gateway.discovery.locator.enabled = true导致,改为false或者去掉该配置,因为默认为false,但是该方案会导致动态路由不能加载,影响微服务调用。
    gateway:
      discovery:
        locator:
          enabled: false 

2、添加@Lazy注解在client上面

在注入的AuthClient上面添加@Lazy注解

java 复制代码
    @Lazy
    @Autowired
    private AuthClient authClient;

全部代码:

java 复制代码
@Component
@Slf4j
public class GatewayGlobalFilter implements GlobalFilter, Ordered {
    @Autowired
    private AuthProperties authProperties;

    @Lazy
    @Autowired
    private AuthClient authClient;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取请求头的request

        List<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);
        }
        //获取token
        String authorization = request.getHeaders()
                .getFirst("authorization");
        String token= authorization.replace("Bearer ","");

        // 验证token
        String userId = authClient.getUserId(token);
        if(userId==null){
            //设置响应码为401
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }

        //放行
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

3、启动成功

相关推荐
我命由我123451 小时前
Spring Boot 自定义日志打印(日志级别、logback-spring.xml 文件、自定义日志打印解读)
java·开发语言·jvm·spring boot·spring·java-ee·logback
杉之6 小时前
常见前端GET请求以及对应的Spring后端接收接口写法
java·前端·后端·spring·vue
canonical_entropy8 小时前
Nop入门-如何通过配置扩展服务函数的返回对象
spring·mvc·graphql
小李同学_LHY9 小时前
三.微服务架构中的精妙设计:服务注册/服务发现-Eureka
java·spring boot·spring·springcloud
非ban必选9 小时前
spring-ai-alibaba第四章阿里dashscope集成百度翻译tool
java·人工智能·spring
非ban必选9 小时前
spring-ai-alibaba第五章阿里dashscope集成mcp远程天气查询tools
java·后端·spring
爱喝醋的雷达10 小时前
Spring SpringBoot 细节总结
java·spring boot·spring
阁阁下13 小时前
springcloud configClient获取configServer信息失败导致启动configClient注入失败报错解决
后端·spring·spring cloud
whisperrr.14 小时前
【spring01】Spring 管理 Bean-IOC,基于 XML 配置 bean
xml·java·spring
天上掉下来个程小白14 小时前
HttpClient-03.入门案例-发送POST方式请求
java·spring·httpclient·苍穹外卖