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、启动成功

相关推荐
成富32 分钟前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
鹿屿二向箔2 小时前
基于SSM(Spring + Spring MVC + MyBatis)框架的汽车租赁共享平台系统
spring·mvc·mybatis
豪宇刘2 小时前
SpringBoot+Shiro权限管理
java·spring boot·spring
想进大厂的小王2 小时前
项目架构介绍以及Spring cloud、redis、mq 等组件的基本认识
redis·分布式·后端·spring cloud·微服务·架构
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS医院管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源·intellij-idea
一只爱打拳的程序猿3 小时前
【Spring】更加简单的将对象存入Spring中并使用
java·后端·spring
杨荧3 小时前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
牛角上的男孩4 小时前
Istio Gateway发布服务
云原生·gateway·istio
ajsbxi6 小时前
苍穹外卖学习记录
java·笔记·后端·学习·nginx·spring·servlet