java Spring MongoDatabaseFactory -如何设置mongo超时

mfpqipee  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(261)

bounty将在6小时后过期。回答此问题可获得+50的声誉奖励。MetaCoder正在寻找来自声誉良好来源的答案

我正在做一个项目,我的spring服务需要连接到多个Mongo DB。
为了实现连接到多个mongo数据库的能力,我创建了一个MongoDatabaseFactory配置类来表示每个连接。然而,我得到了随机的mongo SSL超时错误。通常,当我连接到一个mongo数据库时,我会这样创建客户端:

@Value("${spring.data.mongodb.host}")
    private String connectionString;

    @Bean
    public MongoClient mongoClient() {
        CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
        CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
        return MongoClients.create(MongoClientSettings.builder()
                .applyConnectionString(new ConnectionString(connectionString))
                .uuidRepresentation(UuidRepresentation.STANDARD)
                .codecRegistry(codecRegistry)
                .build());
    }

    @Bean
    public MongoClientSettings mongoClientSettings() {
        final MongoClientSettings clientSettings = MongoClientSettings.builder()
                .retryWrites(true)
                .applyToConnectionPoolSettings((ConnectionPoolSettings.Builder builder) -> {
                    builder.maxSize(300) //connections count
                            .minSize(100)
                            .maxConnectionLifeTime(300, TimeUnit.MILLISECONDS)
                            .maintenanceFrequency(5000, TimeUnit.MILLISECONDS)
                            .maxConnectionIdleTime(200, TimeUnit.MILLISECONDS)
                            .maxWaitTime(150, TimeUnit.MILLISECONDS);
                })
                .applyToSocketSettings(builder -> {
                    builder.connectTimeout(2000, TimeUnit.MILLISECONDS)
                            .readTimeout(5500, TimeUnit.MILLISECONDS);
                })

                .applicationName("TestApplication")
                .retryWrites(true)
                .build();

        return clientSettings;
    }

    @Bean
    public MongoCustomConversions mongoCustomConversions() {
        return new MongoCustomConversions(Arrays.asList(
                new BigDecimalDecimal128Converter(),
                new Decimal128BigDecimalConverter()
        ));

    }

    @WritingConverter
    private static class BigDecimalDecimal128Converter implements Converter<BigDecimal, Decimal128> {

        @Override
        public Decimal128 convert(@NonNull BigDecimal source) {
            return new Decimal128(source);
        }
    }

    @ReadingConverter
    private static class Decimal128BigDecimalConverter implements Converter<Decimal128, BigDecimal> {

        @Override
        public BigDecimal convert(@NonNull Decimal128 source) {
            return source.bigDecimalValue();
        }

    }

当连接到多个mongo数据库时,我是这样做的:

@Configuration
public class UserProfileInformationMongoConfig {
    @Autowired
    private Environment env;

    @Bean
    public MongoDatabaseFactory userProfileInformationFactory() {
        return new SimpleMongoClientDatabaseFactory(new ConnectionString(env.getProperty("spring.data.mongo.userprofileinformationdb.uri")));
    }
    @Bean
    public MongoTemplate userProfileInformationMongoTemplate() {
        return new MongoTemplate(userProfileInformationFactory());
    }
}

因此,我的问题还是在使用MongoDatabaseFactory时,如何像第一个代码示例那样设置mongo客户端选项。
多谢

ws51t4hk

ws51t4hk1#

maintenanceFrequency配置外,其余可以在连接字符串中提供,如mongodb://db1.example.net:27017,db2.example.net:2500/?replicaSet=test&maxPoolSize=300&minPoolSize=100&maxLifeTimeMS=300&maxIdleTimeMS=200&waitQueueTimeoutMS=150&connectTimeoutMS=2000&socketTimeoutMS=5500

相关问题