首页 > 解决方案 > 从 MongoDB 获取数据时程序卡住

问题描述

我正在尝试开发一个服务器/客户端程序,该服务器基于 Netty。当服务器从netty通道获取数据时,我会从mongo查询数据。但是,当打开 MongoDB 的连接时,通道会卡住。我的代码很简单:

Query query = new Query(Criteria.where("account_id").is(accountId).and("account_type").is(accountType));
return seqKeyConvert.toProtoBuf(mongoTemplate.findOne(query, SeqKeyMongoEntity.class, collectionName));

服务器的日志将是这样的:

[nioEventLoopGroup-3-1] org.mongodb.driver.connection            - Opened connection[connectionId{localValue:7, serverValue:7530577}] to 100.75.53.176:5521
[nioEventLoopGroup-3-6] org.mongodb.driver.connection            - Opened connection [connectionId{localValue:10, serverValue:7530585}] to 100.75.53.176:5521
[nioEventLoopGroup-3-8] org.mongodb.driver.connection            - Opened connection [connectionId{localValue:8, serverValue:7530584}] to 100.75.53.176:5521
[nioEventLoopGroup-3-7] org.mongodb.driver.connection            - Opened connection [connectionId{localValue:9, serverValue:7530586}] to 100.75.53.176:5521

MongoDB 配置如下:

spring:
  data:
    mongodb:
      manager:
        address: mongodb://100.75.53.176:5521,100.75.53.177:5521
        min-connections-per-host: 2
        max-connections-per-host: 100
        threads-allowed-to-block-for-connection-multiplier: 10
        server-selection-timeout: 30000
        max-wait-time: 60000
        max-connection-idel-time: 28800000
        max-connection-life-time: 0
        connect-timeout: 30000
        socket-timeout: 0
        socket-keep-alive: false
        ssl-enabled: false
        ssl-invalid-host-name-allowed: false
        always-use-m-beans: false
        heartbeat-socket-timeout: 20000
        heartbeat-connect-timeout: 20000
        min-heartbeat-frequency: 500
        heartbeat-frequency: 10000
        local-threshold: 15

通道将卡在此处并且不会抛出任何错误。我试图测试 MongoDB 是否正常,以及我是否设置了太少的 MongoDB 连接数限制但没有任何发现。有什么建议吗?谢谢你。

标签: mongodbnetty

解决方案


InitialzationBean用来设置一些使用 MongoDB 的变量。事实证明,如果我将 InitialzationBean 替换为CommandLineRunner. 暂时,如果发生某种竞争条件,我不会。


推荐阅读