Spring Boot 多存储库 <> 多数据库集群
SpringBoot multipleRepositories <> multiple DBs cluster
我搜索了 Spring 多 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
Caused 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$39498.CGLIB$mongo(<generated>) ~[classes!/:na]
at com.myplex.contentstore_v2.config.MongoContentConfig$$EnhancerBySpringCGLIB$39498$$FastClassBySpringCGLIB$4739cb.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$39498.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
您可以添加在 uri 中以逗号分隔的集群服务器
uri: mongodb://username:password@server1:port,server2:port/database
您可以根据应用配置设置尝试加载不同数据源(Mongo 数据库集群)的其他选项。
执行onReady @EventListener 并在启动SpringBoot 应用程序时加载所有数据库集群。
在 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 数据库成功实现了这一点。
可能是初学者添加更多详细信息
官方文档: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
我还在分析它是如何选择副本集的。但这对我有用。
我搜索了 Spring 多 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
Caused 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$39498.CGLIB$mongo(<generated>) ~[classes!/:na]
at com.myplex.contentstore_v2.config.MongoContentConfig$$EnhancerBySpringCGLIB$39498$$FastClassBySpringCGLIB$4739cb.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$39498.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
您可以添加在 uri 中以逗号分隔的集群服务器
uri: mongodb://username:password@server1:port,server2:port/database
您可以根据应用配置设置尝试加载不同数据源(Mongo 数据库集群)的其他选项。
执行onReady @EventListener 并在启动SpringBoot 应用程序时加载所有数据库集群。
在 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 数据库成功实现了这一点。
可能是初学者添加更多详细信息
官方文档: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
我还在分析它是如何选择副本集的。但这对我有用。