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

相关推荐
.生产的驴2 分钟前
SpringCloud OpenFeign用户转发在请求头中添加用户信息 微服务内部调用
spring boot·后端·spring·spring cloud·微服务·架构
吴半杯2 小时前
gateway漏洞(CVE-2022-22947)
docker·kubernetes·gateway
码蜂窝编程官方3 小时前
【含开题报告+文档+PPT+源码】基于SpringBoot+Vue的虎鲸旅游攻略网的设计与实现
java·vue.js·spring boot·后端·spring·旅游
AuroraI'ncoding3 小时前
时间请求参数、响应
java·后端·spring
计算机毕设指导66 小时前
基于 SpringBoot 的作业管理系统【附源码】
java·vue.js·spring boot·后端·mysql·spring·intellij-idea
ExiFengs7 小时前
实际项目Java1.8流处理, Optional常见用法
java·开发语言·spring
wenyue11217 小时前
Revolutionize Your Kubernetes Experience with Easegress: Kubernetes Gateway API
容器·kubernetes·gateway
瓜牛_gn7 小时前
依赖注入注解
java·后端·spring
木子_lishk8 小时前
gateway 支持同时使用 https 和 http访问
https·gateway