SpringBoot连接MongoDB集群问题RFC 2732

 

这是我springboot连接 MongoDB 配置

spring:
  data:
    mongodb:
      uri: mongodb://192.168.1.143:9085/test,mongodb://192.168.1.144:9085/test,mongodb://192.168.1.145:9085/test

 

连接MongoDB 集群是出现

java.lang.IllegalArgumentException: The connection string contains an invalid host 'mongodb://39.97.113.214:9085'. Reserved characters such as ':' must be escaped according RFC 2396. Any IPv6 address literal must be enclosed in '[' and ']' according to RFC 2732.

 

看到RFC 2732 有点懵逼,这是啥玩意,百度一下,发现这是“Internet 官方协议标准”(STD 1),给出的解释是在文字中IPv6 地址定义的文本表示与 URL 不直接兼容。因为两者使用了 “:”和“.” 作为分隔符。他们要求要在URL中使用文字IPv6地址,文字地址应包含在“[”和“]”字符中,详细解释请看1999年12月发布的URL中的文字IPv6地址的格式备忘录

 

配置文件修改如下

spring:
  data:
    mongodb:
      uri: mongodb://[192.168.1.143:9085]/test,[192.168.1.144:9085]/test,[192.168.1.145:9085]/test

MongoDB源码也给出了示例的:

地址:https://github.com/mongodb/mongo-java-driver/blob/master/driver-core/src/main/com/mongodb/ConnectionString.java

**
 * <p>Represents a <a href="http://www.mongodb.org/display/DOCS/Connections">Connection String</a>. The Connection String describes the
 * hosts to be used and options.</p>
 *
 * <p>The format of the Connection String is:</p>
 * <pre>
 *   mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]
 * </pre>
 * <ul>
 * <li>{@code mongodb://} is a required prefix to identify that this is a string in the standard connection format.</li>
 * <li>{@code username:password@} are optional.  If given, the driver will attempt to login to a database after
 * connecting to a database server.  For some authentication mechanisms, only the username is specified and the password is not,
 * in which case the ":" after the username is left off as well</li>
 * <li>{@code host1} is the only required part of the connection string. It identifies a server address to connect to.
 * Support for Unix domain sockets was added in 3.7. Note: The path must be urlencoded eg: {@code mongodb://%2Ftmp%2Fmongodb-27017.sock}
 * and the {@code jnr.unixsocket} library installed.
 * </li>
 * <li>{@code :portX} is optional and defaults to :27017 if not provided.</li>
 * <li>{@code /database} is the name of the database to login to and thus is only relevant if the
 * {@code username:password@} syntax is used. If not specified the "admin" database will be used by default.</li>
 * <li>{@code ?options} are connection options. Note that if {@code database} is absent there is still a {@code /}
 * required between the last host and the {@code ?} introducing the options. Options are name=value pairs and the pairs
 * are separated by "&amp;". For backwards compatibility, ";" is accepted as a separator in addition to "&amp;",
 * but should be considered as deprecated.</li>
 * </ul>
 * <p>An alternative format, using the mongodb+srv protocol, is:
 * <pre>
 *   mongodb+srv://[username:password@]host[/[database][?options]]
 * </pre>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot应用程序中配置MongoDB集群,需要执行以下步骤: 1. 添加MongoDB驱动程序依赖项:在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> ``` 2. 配置MongoDB集群连接:在application.properties或application.yml文件中添加以下属性: ``` spring.data.mongodb.uri=mongodb://host1:port1,host2:port2,host3:port3/databaseName?replicaSet=yourReplicaSetName ``` 其中,host1、host2、host3是MongoDB集群中的主机名,port1、port2、port3是MongoDB端口号,databaseName是要连接的数据库名称,yourReplicaSetName是MongoDB集群的副本名称。 3. 创建MongoDB集群配置类:创建一个MongoDB集群配置类,以便将MongoDB集群连接设置传递给MongoDB客户端。例如: ``` @Configuration public class MongoConfig { @Value("${spring.data.mongodb.uri}") private String mongoUri; @Bean public MongoClient mongoClient() { return MongoClients.create(mongoUri); } @Bean public MongoTemplate mongoTemplate() throws Exception { return new MongoTemplate(mongoClient(), "databaseName"); } } ``` 在这个配置类中,我们注入了MongoDB集群连接URI,并使用它来创建MongoDB客户端和MongoDB模板。 4. 测试MongoDB集群连接:可以编写一个简单的测试类来测试MongoDB集群连接是否正常工作。例如: ``` @SpringBootTest public class MongoClusterTest { @Autowired private MongoTemplate mongoTemplate; @Test public void testMongoTemplate() { mongoTemplate.createCollection("testCollection"); List<String> collections = mongoTemplate.getCollectionNames(); System.out.println(collections); mongoTemplate.dropCollection("testCollection"); } } ``` 这个测试类将创建一个名为“testCollection”的集合,并在控制台上输出所有集合名称。最后,它将删除“testCollection”集合。 以上就是在Spring Boot应用程序中配置MongoDB集群的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值