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

相关推荐
程序猿进阶18 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭9 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
李小白6610 小时前
Spring MVC(上)
java·spring·mvc
Lojarro12 小时前
【Spring】Spring框架之-AOP
java·mysql·spring
zjw_rp13 小时前
Spring-AOP
java·后端·spring·spring-aop
撒呼呼16 小时前
# 起步专用 - 哔哩哔哩全模块超还原设计!(内含接口文档、数据库设计)
数据库·spring boot·spring·mvc·springboot
维李设论17 小时前
Node.js的Web服务在Nacos中的实践
前端·spring cloud·微服务·eureka·nacos·node.js·express
天使day17 小时前
SpringMVC
java·spring·java-ee
壹佰大多18 小时前
【spring-cloud-gateway总结】
java·spring·gateway