基于Spring WebFlux / Reactive Mongo的应用程序将打开与mongo db的多个连接

时间:2019-12-10 13:19:04

标签: mongodb spring-boot spring-webflux project-reactor

我有一个简单的Web应用程序,带有spring boot 2.x.x,用于响应式api的Spring WebFlux和响应式Mongo存储库。 在应用程序中,我有一个与get API关联的@Tailable查询,可以无限地观察保存在DB中的更改。我有一个发布API,可以将数据推送到数据库中。 但是该应用程序会随机打开与mongo的多个连接。有时这些连接会被重用。

日志

Opened connection [connectionId{localValue:4, serverValue:342}] to localhost:27017
Opened connection [connectionId{localValue:5, serverValue:344}] to localhost:27017

以此类推。 所以我添加了一个配置类来限制每个主机的连接

  @Configuration
public class MongoConfig {

    @Value("${spring.data.mongodb.maxConnectionIdleTime}")
    private int  maxConnectionIdleTime;
    @Value("${spring.data.mongodb.connectionsPerHost}")
    private int connectionsPerHost;
    @Value("${spring.data.mongodb.minConnectionsPerHost}")
    private int minConnectionsPerHost;
    @Value("${spring.data.mongodb.socketTimeout}")
    private int socketTimeout;


    @Bean
    public MongoClientOptions mongoOptions() {
        return MongoClientOptions.builder()
                .maxConnectionIdleTime(this.maxConnectionIdleTime)
                .connectionsPerHost(this.connectionsPerHost)
                .minConnectionsPerHost(this.minConnectionsPerHost)
                .socketTimeout(this.socketTimeout)
                .build();

    }
}


现在,当我运行Get api(一个无限期地观察数据库的应用程序)时,该应用程序仍会打开与DB的多个连接,但由于空闲超时而关闭其中的几个连接。

日志:

Opened connection [connectionId{localValue:6, serverValue:345}] to localhost:27017
Closed connection [connectionId{localValue:6, serverValue:345}] to localhost:27017 because it is past its maximum allowed idle time.
Opened connection [connectionId{localValue:7, serverValue:348}] to localhost:27017
Closed connection [connectionId{localValue:7, serverValue:348}] to localhost:27017 because it is past its maximum allowed idle time.
Opened connection [connectionId{localValue:8, serverValue:351}] to localhost:27017
Closed connection [connectionId{localValue:8, serverValue:351}] to localhost:27017 because it is past its maximum allowed idle time.
Opened connection [connectionId{localValue:9, serverValue:354}] to localhost:27017

编辑

根据进一步的观察,如果不使用该连接,它将打开一个连接,然后在60秒后关闭它。例如,在下面的日志中,使用本地ID为5和6的连接通过api跟踪mongo事件,而其余连接只是空闲连接,该连接在一分钟后关闭。 编辑将mongo配置更改为->

 server:
    port: 8081
spring:
    data:
        mongodb:
            database: LocalMongo
            uri: mongodb://localhost:27017/LocalMongo?maxpoolsize=5
            maxConnectionIdleTime: 15000
            connectionsPerHost: 40
            minConnectionsPerHost: 1
            socketTimeout: 60000

可以吗?

2019-12-10 21:45:32.862[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[           main][0;39m [36mc.p.test.Application     [0;39m [2m:[0;39m Started Application in 3.489 seconds (JVM running for 4.87)
[2m2019-12-10 21:46:31.977[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[imer-1-thread-1][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Closed connection [connectionId{localValue:3, serverValue:899}] to localhost:27017 because it is past its maximum allowed idle time.
[2m2019-12-10 21:46:31.983[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[imer-1-thread-1][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Opened connection [connectionId{localValue:4, serverValue:903}] to localhost:27017
[2m2019-12-10 21:47:28.917[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[ntLoopGroup-2-2][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Opened connection [connectionId{localValue:5, serverValue:905}] to localhost:27017
[2m2019-12-10 21:47:31.122[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[ntLoopGroup-2-3][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Opened connection [connectionId{localValue:6, serverValue:906}] to localhost:27017
[2m2019-12-10 21:47:31.974[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[imer-1-thread-1][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Closed connection [connectionId{localValue:4, serverValue:903}] to localhost:27017 because it is past its maximum allowed idle time.
[2m2019-12-10 21:47:31.981[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[imer-1-thread-1][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Opened connection [connectionId{localValue:7, serverValue:907}] to localhost:27017
[2m2019-12-10 21:48:31.974[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[imer-1-thread-1][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Closed connection [connectionId{localValue:7, serverValue:907}] to localhost:27017 because it is past its maximum allowed idle time.
[2m2019-12-10 21:48:31.983[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[imer-1-thread-1][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Opened connection [connectionId{localValue:8, serverValue:910}] to localhost:27017
[2m2019-12-10 21:49:31.975[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[imer-1-thread-1][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Closed connection [connectionId{localValue:8, serverValue:910}] to localhost:27017 because it is past its maximum allowed idle time.
[2m2019-12-10 21:49:31.981[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[imer-1-thread-1][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Opened connection [connectionId{localValue:9, serverValue:913}] to localhost:27017
[2m2019-12-10 21:50:31.974[0;39m [32m INFO[0;39m [35m20092[0;39m [2m---[0;39m [2m[imer-1-thread-1][0;39m [36morg.mongodb.driver.connection           [0;39m [2m:[0;39m Closed connection [connectionId{localValue:9, serverValue:913}] to localhost:27017 because it is past its maximum allowed idle time.
[2m

这附近是否有最佳实践?

进一步的编辑 如评论中所建议,我使用了uri: mongodb://localhost:27017/LocalMongo?maxpoolsize=5 将最大池大小设置为5。mongo确保最大5个打开的连接符合预期,并且一旦达到空闲超时,连接就会过期。 但是,每次连接终止时,都会打开一个新的连接。基本上,似乎可以确保打开至少5个连接。我在这里想念一个概念吗?

日志:

Opened connection [connectionId{localValue:2, serverValue:2843}] to localhost:27017
Opened connection [connectionId{localValue:3, serverValue:2844}] to localhost:27017
: Opened connection [connectionId{localValue:4, serverValue:2846}] to localhost:27017
2019-12-13 13:00:17.316  INFO 10748 --- [ntLoopGroup-2-3] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:5, serverValue:2848}] to localhost:27017
2019-12-13 13:00:20.436  INFO 10748 --- [ntLoopGroup-2-4] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:6, serverValue:2849}] to localhost:27017
2019-12-13 13:00:39.124  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:3, serverValue:2844}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:00:39.125  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:7, serverValue:2851}] to localhost:27017
2019-12-13 13:01:39.124  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:7, serverValue:2851}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:01:39.125  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:8, serverValue:2854}] to localhost:27017
2019-12-13 13:02:39.123  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:8, serverValue:2854}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:02:39.123  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:9, serverValue:2856}] to localhost:27017
2019-12-13 13:03:39.124  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:9, serverValue:2856}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:03:39.125  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:10, serverValue:2859}] to localhost:27017
2019-12-13 13:04:39.123  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:10, serverValue:2859}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:04:39.125  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:11, serverValue:2862}] to localhost:27017
2019-12-13 13:05:39.122  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:11, serverValue:2862}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:05:39.125  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:12, serverValue:2865}] to localhost:27017
2019-12-13 13:06:39.123  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:12, serverValue:2865}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:06:39.126  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:13, serverValue:2868}] to localhost:27017
2019-12-13 13:07:39.123  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:13, serverValue:2868}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:07:39.124  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:14, serverValue:2870}] to localhost:27017
2019-12-13 13:08:39.124  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:14, serverValue:2870}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:08:39.128  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:15, serverValue:2873}] to localhost:27017
2019-12-13 13:09:39.124  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:15, serverValue:2873}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:09:39.125  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:16, serverValue:2876}] to localhost:27017
2019-12-13 13:10:39.123  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:16, serverValue:2876}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:10:39.124  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:17, serverValue:2879}] to localhost:27017
2019-12-13 13:11:39.122  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:17, serverValue:2879}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:11:39.129  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:18, serverValue:2882}] to localhost:27017
2019-12-13 13:12:39.123  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:18, serverValue:2882}] to localhost:27017 because it is past its maximum allowed idle time.
2019-12-13 13:12:39.124  INFO 10748 --- [imer-1-thread-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:19, serverValue:2885}] to localhost:27017

请告知我是否需要其他信息。我将在这里编辑此空间。

1 个答案:

答案 0 :(得分:0)

这是由设置maxConnectionIdleTime=1引起的,您也可以在日志中看到此设置:...because it is past its maximum allowed idle time.