SpringBoot-mongodb集群环境配置

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lovelyesz/article/details/87921627

 spring:
  data:
    mongodb:
      host: 192.168.1.1:27017
      username: test_user
      password: pass@123
      database: tdb


这是spring-mongodb的配置,当mongodb使用了集群之后我尝试着直接修改

 host: 192.168.1.1:27017,192.168.1.2:27017,192.168.1.3:27017

启动是没问题,使用是直接报错了UnknowHost,显然这样是不行的

查看了spring autorconfig的源码

org.springframework.boot.autoconfigure.mongo.MongoProperties

  private MongoClient createEmbeddedMongoClient(MongoClientOptions options, int port) {
        if (options == null) {
            options = MongoClientOptions.builder().build();
        }
        String host = (this.host != null ? this.host : "localhost");
        return new MongoClient(Collections.singletonList(new ServerAddress(host, port)),
                Collections.<MongoCredential>emptyList(), options);
    }
 
    private MongoClient createNetworkMongoClient(MongoClientOptions options) {
        if (hasCustomAddress() || hasCustomCredentials()) {
            if (this.uri != null) {
                throw new IllegalStateException("Invalid mongo configuration, "
                        + "either uri or host/port/credentials must be specified");
            }
            if (options == null) {
                options = MongoClientOptions.builder().build();
            }
            List<MongoCredential> credentials = new ArrayList<MongoCredential>();
            if (hasCustomCredentials()) {
                String database = (this.authenticationDatabase != null
                        ? this.authenticationDatabase : getMongoClientDatabase());
                credentials.add(MongoCredential.createCredential(this.username, database,
                        this.password));
            }
            String host = (this.host != null ? this.host : "localhost");
            int port = (this.port != null ? this.port : DEFAULT_PORT);
            return new MongoClient(
                    Collections.singletonList(new ServerAddress(host, port)), credentials,
                    options);
        }
        // The options and credentials are in the URI
        return new MongoClient(new MongoClientURI(determineUri(), builder(options)));
    }


显然这个配置提供了两种配置方式createEmbeddedMongoClient和createNetworkMongoClient

createEmbeddedMongoClient中创建MongoClient的时候使用的是Collections.singletonList这不可能是集群环境的配置所以看了一下createNetworkMongoClient的代码hasCustomAddress()和hasCustomCredentials()是是否有判断username、password、host、port配置,如果有这些配置并且uri不为空就会直接报错,也就是uri配置不能与其共存,host的配置方式又不能配置集群环境那么只能使用uri来实现集群配置了。

根据配置类上的文档注释尝试着修改配置为

spring:
  data:
    mongodb:
      uri: mongodb://test_user:pass%40123@192.168.1.1:27017,192.168.1.2:27017,192.168.1.3:27017/tdb


注意:username和password中含有“:”或“@”需要进行URLEncoder编码

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值