Mongodb SpringBoot multipleRepositories<&燃气轮机;多数据库集群

Mongodb SpringBoot multipleRepositories<&燃气轮机;多数据库集群,mongodb,spring-boot,multiple-instances,Mongodb,Spring Boot,Multiple Instances,我搜索了Spring multi mongo配置并帮助了我 作为扩展,我需要帮助配置具有不同IP的集群mongodb。这是我当地的样品 如何在此处添加多个主机 mongodb: content: uri: mongodb://localhost:27017/contents genre: uri: mongodb://localhost:27017/genres 更新的配置:不工作 //也尝试过类似于你的答案 mongodb: content: ur

我搜索了Spring multi mongo配置并帮助了我

  • 作为扩展,我需要帮助配置具有不同IP的集群mongodb。这是我当地的样品
  • 如何在此处添加多个主机

        mongodb:
      content:
        uri: mongodb://localhost:27017/contents
      genre:
        uri: mongodb://localhost:27017/genres
    
    更新的配置:不工作 //也尝试过类似于你的答案

    mongodb:
      content:
        uri: mongodb://localhost:27017/contents,mongodb://un:pw@host1:27017/contents,mongodb://host2:27017/contents,mongodb://host3:27017/contents
      genre:
        uri: mongodb://localhost:27017/genres,mongodb://un:pw@host1:27017/genres,mongodb://host2:27017/genres,mongodb://host3:27017/genres
    

    原因:java.lang.IllegalArgumentException:状态应为:databaseName不包含“/”
    在com.mongodb.MongoNamespace.checkDatabaseNameValidity(MongoNamespace.java:62)~[mongodb-driver-core-4.0.5.jar!/:na]
    在com.mongodb.ConnectionString.(ConnectionString.java:371)~[mongodb-driver-core-4.0.5.jar!/:na]
    在com.myplex.contentstore_v2.config.MongoContentConfig.mongo(MongoContentConfig.java:39)~[classes!/:na]
    在com.myplex.contentstore_v2.config.MongoContentConfig$$EnhancerBySpringCGLIB$$5039498.CGLIB$mongo$2()~[classes!/:na]
    在com.myplex.contentstore_v2.config.MongoContentConfig$$EnhancerBySpringCGLIB$$5039498$$FastClassBySpringCGLIB$$434739cb.invoke()~[classes!/:na]
    在org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)~[spring-core-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
    在org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
    在com.myplex.contentstore_v2.config.MongoContentConfig$$EnhancerBySpringCGLIB$$5039498.mongo()~[classes!/:na]
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)~[na:1.8.0_161]
    在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)~[na:1.8.0_161]
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[na:1.8.0¡]
    在java.lang.reflect.Method.invoke(Method.java:498)~[na:1.8.0_161]
    在org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:154)~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
    ... 省略79个公共框架
    
    您可以添加在uri中以逗号分隔的集群服务器

    uri: mongodb://username:password@server1:port,server2:port/database
    

    您可以添加在uri中以逗号分隔的集群服务器

    uri: mongodb://username:password@server1:port,server2:port/database
    

    您还可以尝试根据应用程序配置设置加载不同的数据源(Mongo DB集群)

    实现onReady@EventListener并在启动SpringBoot应用程序时加载所有DB集群

    在onReady中,根据下面的配置创建所有数据源,并存储在Map中

    HikariConfig hikariConfig = new HikariConfig();
            hikariConfig.setJdbcUrl(url);
            hikariConfig.addDataSourceProperty("url", url);
            hikariConfig.addDataSourceProperty("user", username);
            hikariConfig.addDataSourceProperty("password", password);
            com.zaxxer.hikari.HikariDataSource hikariDataSource = new com.zaxxer.hikari.HikariDataSource(hikariConfig);
    
    // Now lets store the hikariDataSource to a Map (dataSourcesMap)
    try (Connection c = hikariDataSource.getConnection()) {
         dataSourcesMap.put(uniqueDBId, hikariDataSource);
    }
    

    我曾经在许多连接多个数据库的SpringBoot应用程序中成功地使用Postgres和MySQL数据库实现了这一点。

    其他选项您可以尝试根据应用程序配置设置加载不同的数据源(Mongo DB集群)

    实现onReady@EventListener并在启动SpringBoot应用程序时加载所有DB集群

    在onReady中,根据下面的配置创建所有数据源,并存储在Map中

    HikariConfig hikariConfig = new HikariConfig();
            hikariConfig.setJdbcUrl(url);
            hikariConfig.addDataSourceProperty("url", url);
            hikariConfig.addDataSourceProperty("user", username);
            hikariConfig.addDataSourceProperty("password", password);
            com.zaxxer.hikari.HikariDataSource hikariDataSource = new com.zaxxer.hikari.HikariDataSource(hikariConfig);
    
    // Now lets store the hikariDataSource to a Map (dataSourcesMap)
    try (Connection c = hikariDataSource.getConnection()) {
         dataSourcesMap.put(uniqueDBId, hikariDataSource);
    }
    

    过去,我在许多连接多个数据库的SpringBoot应用程序中成功地使用Postgres和MySQL数据库实现了这一点。

    可能是第一次添加更多细节

    官方文件:

    错:

     -     uri: mongodb://localhost:27017/contents,mongodb://un:pw@host1:27017/contents,mongodb://host2:27017/contents,mongodb://host3:27017/contents
    
    对:

     -     uri: mongodb://un:pw@primaryhost:27017/contents
    

    我还在分析它是如何挑选复制集的。但这对我来说很管用。

    也许是第一次添加更多细节

    官方文件:

    错:

     -     uri: mongodb://localhost:27017/contents,mongodb://un:pw@host1:27017/contents,mongodb://host2:27017/contents,mongodb://host3:27017/contents
    
    对:

     -     uri: mongodb://un:pw@primaryhost:27017/contents
    

    我还在分析它是如何挑选复制集的。但它对我有效。

    我尝试过这种配置风格,但不起作用。问题的最新细节。请注意,我尝试了与答案类似的uri,并添加了mongodb://前缀。两者都给出了相同的例外。我尝试过这种配置样式,但不起作用。问题的最新细节。请注意,我尝试了与答案类似的uri,并添加了mongodb://前缀。两者都有相同的例外