背景
项目采用springcloud
架构,使用 feign
做接口调用,项目已经运行很长时间。
最近刚发现服务重启后的一段时间访问很慢,有很多如下的日志:
2019-07-02 18:26:06.406 INFO [http-nio-8080-exec-1] org.springframework.context.annotation.AnnotationConfigApplicationContext:583 8d40dc484a9e5e79202d8f1705501502 - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1b9834d4: startup date [Tue Jul 02 18:26:06 CST 2019]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@53b32d7
2019-07-02 18:26:06.425 INFO [http-nio-8080-exec-1] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor:155 8d40dc484a9e5e79202d8f1705501502 - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
断点发现,都是在调用 feign 接口时,才会触发 spring的 refresh,所以进一步查看为什么会触发这个机制,接着查看,发现 feign 使用的 ribbon的loadbalance 方式导致的。 然后看了下官方文档,发现确实有eager-load这个相关的配置。
解决方案
spring cloud
使用用 feign
的 loadbalance
做接口调用,默认是延迟加载eager-load=false
, 第一次请求(针对每个client的)的时候才会加载,导致第一次请求会慢,并发的时候也容易造成等待现象。可以通过设置,在初始化时直接加载,会延长一些启动时间。需要把不开启延迟加载的 serviceid 配置一下,必须配置ribbon.eager-load.clients
,要不然不生效。
设置参考如下:
ribbon.eager-load.enabled=true
ribbon.eager-load.clients=dispatcher-driver-service,driverCommonService,passenger-settlementGroup-service,passenger-syrepoGroup-service
zuul
使用 ribbon
时,也有通用的问题,对应的配置为:
zuul.ribbon.eager-load.enabled=true
参考资料
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为: 2020/05/17 03:15