Spring cloud gateway+apollo=bug?

这里是weihubeats ,觉得文章不错可以关注公众号小奏技术,文章首发。拒绝营销号,拒绝标题党

spring cloud 版本

  • spring cloud: 2021.0.4
  • spring cloud gateway: 3.1.4

背景

最近在配置研究网关的超时时间,有这么一个需求。

服务路由转发接口的超时时间总体是5s,但是部分特殊服务可能需要配置更长的超时时间,比如10s。

实现需求

要实现这个需求怎么处理呢?其实很简单,大致如下配置

yaml 复制代码
spring:
  cloud:
    gateway:
      httpclient:
        response-timeout: 3s
        pool:
          type: fixed
          max-connections: 200
          max-idle-time: 5000
          acquire-timeout: 10000
      routes: 
        - id: order-service
          predicates:
            - Path=/xiaozou/order-service/**
          filters:
            - StripPrefix=2
          uri: lb://order-service
        - id: pay-service
          predicates:
            - Path=/xiaozou/pay-service/**
          filters:
            - StripPrefix=1
          uri: http://localhost:9999/pay-service
          metadata:
            response-timeout: 5000

这里我们配置了两个服务路由order-servicepay-service

配置了全局的请求超时时间

spring.cloud.gateway.httpclient.response-timeout=3s

然后配置了单个服务的超时时间5000(5s)

正常看这个配置和操作是没问题

apollo配置就有问题了?

我们的配置中心使用的是apollo,所以配置都放在apollo

然后我们通过debug查看源码NettyRoutingFilter发现一个bug

在yaml配置的Integer类型变成string了

导致判断

java 复制代码
if (responseTimeoutAttr != null && responseTimeoutAttr instanceof Number)

中的responseTimeoutAttr instanceof Number失效,从而导致metadata中配置的response-timeout失效

这个是由于apollo底层实现使用了Properties作为配置存储,所以虽然在apollo解析yaml时能知道配置的类型(如Integer),但是在存储的时候都会统一转为String类型,如下图所示

所以apollo在使用会有这个bug:@ConfigurationProperties注解导致类型从Integer转为String:github.com/apolloconfi...

感兴趣的可以查看这个issues,对此apollo并没有打算修复这个问题

解决方式

最简单的方式就是我们修改源码,这里就不用重新编译spring cloud gateway源码了

我们直接在项目中新建一个org.springframework.cloud.gateway.filter

然后新建类NettyRoutingFilter

然后修改getResponseTimeout这个方法为如下

java 复制代码
private Duration getResponseTimeout(Route route) {
        Object responseTimeoutAttr = route.getMetadata().get(RESPONSE_TIMEOUT_ATTR);
        try {
            if (responseTimeoutAttr != null) {
                long routeResponseTimeout = Long.parseLong((String) responseTimeoutAttr);
                if (routeResponseTimeout >= 0) {
                    return Duration.ofMillis(routeResponseTimeout);
                }
                else {
                    return null;
                }
            }
        } catch (Exception e) {
            log.error("get responseTimeoutAttr error, so use default responseTimeout, error ", e);
        }
        return properties.getResponseTimeout();
    }

核心是删除responseTimeoutAttr instanceof Number判断,添加类型转换

这样就解决了

总结

我们在使用Apollo配置文件的时候一定要注意这个坑,就是@ConfigurationProperties注解导致类型从Integer转为String

相关推荐
考虑考虑1 小时前
Springboot3.5.x结构化日志新属性
spring boot·后端·spring
涡能增压发动积1 小时前
一起来学 Langgraph [第三节]
后端
sky_ph1 小时前
JAVA-GC浅析(二)G1(Garbage First)回收器
java·后端
涡能增压发动积1 小时前
一起来学 Langgraph [第二节]
后端
hello早上好2 小时前
Spring不同类型的ApplicationContext的创建方式
java·后端·架构
roman_日积跬步-终至千里2 小时前
【Go语言基础【20】】Go的包与工程
开发语言·后端·golang
00后程序员3 小时前
提升移动端网页调试效率:WebDebugX 与常见工具组合实践
后端
HyggeBest3 小时前
Mysql的数据存储结构
后端·架构
TobyMint3 小时前
golang 实现雪花算法
后端
G探险者4 小时前
【案例解析】一次 TIME_WAIT 导致 TPS 断崖式下降的排查与优化
后端