我已经搜索了,this article帮助了我。
如何在这里添加多个主机?
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/genresCaused by: java.lang.IllegalArgumentException: state should be: databaseName does not contain '/'
at com.mongodb.MongoNamespace.checkDatabaseNameValidity(MongoNamespace.java:62) ~[mongodb-driver-core-4.0.5.jar!/:na]
at com.mongodb.ConnectionString.<init>(ConnectionString.java:371) ~[mongodb-driver-core-4.0.5.jar!/:na]
at com.myplex.contentstore_v2.config.MongoContentConfig.mongo(MongoContentConfig.java:39) ~[classes!/:na]
at com.myplex.contentstore_v2.config.MongoContentConfig$$EnhancerBySpringCGLIB$$5039498.CGLIB$mongo$2(<generated>) ~[classes!/:na]
at com.myplex.contentstore_v2.config.MongoContentConfig$$EnhancerBySpringCGLIB$$5039498$$FastClassBySpringCGLIB$$434739cb.invoke(<generated>) ~[classes!/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
at com.myplex.contentstore_v2.config.MongoContentConfig$$EnhancerBySpringCGLIB$$5039498.mongo(<generated>) ~[classes!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
... 79 common frames omitted发布于 2020-09-23 17:35:00
可以添加在uri中分隔的群集逗号的服务器。
uri: mongodb://username:password@server1:port,server2:port/database发布于 2021-01-08 13:32:18
其他选项,您可以尝试加载不同的数据源(Mongo集群)的应用程序配置设置。
实现onReady @EventListener,并在启动SpringBoot应用程序时加载所有DB集群。
在onReady内部,按照下面的配置创建所有数据源,并存储在地图中
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);
}过去,我在许多与多个DB连接的SpringBoot应用程序中成功地用Postgres和SpringBoot DB实现了这一点。
发布于 2021-01-12 13:41:05
可能是第一次添加更多细节
官方文件:https://mongodb.github.io/mongo-java-driver/3.7/driver/tutorials/connect-to-mongodb/
错:
- 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我还在分析它是如何挑选复制集的。但它对我有效。
https://stackoverflow.com/questions/64033218
复制相似问题