【问题标题】:How do I change my java code to use replset? with Spring Data and MongoDB如何更改我的 java 代码以使用 replset?使用 Spring Data 和 MongoDB
【发布时间】:2013-06-28 12:45:04
【问题描述】:

我想知道如何更改我的 java 代码以支持使用 spring-data 和 MongoDB 的 replset。

我有 3 个 MongoDB 服务器正在运行.. 示例:

./mongod --dbpath=/home/jsmith/tmp/db1 --replSet=spring --port=27017
./mongod --dbpath=/home/jsmith/tmp/db2 --replSet=spring --port=27027
./mongod --dbpath=/home/jsmith/tmp/db3 --replSet=spring --port=27037

如果我执行 rs.status() 我可以看到,如果 27017 上的 db 出现故障,那么其他数据库之一将成为主要数据库,因此我知道 mongoDB 工作正常,但在我的 java 代码中,如果我尝试运行它,我会得到以下错误:

Exception in thread "main" org.springframework.dao.DataAccessResourceFailureException: can't call something : /127.0.0.1:27017/demo

它只在端口 27017 上查看

这是我的 mongodbconfig:

@Configuration
@EnableMongoRepositories
@ComponentScan(basePackageClasses = {MongoDBApp.class})
@PropertySource("classpath:application.properties")
public class MongoConfiguration extends AbstractMongoConfiguration {


    @Override
    protected String getDatabaseName() {
        return "demo";
    }



    @Override
    public Mongo mongo() throws Exception {
                return new Mongo(new ArrayList<ServerAddress>() {{ add(new ServerAddress("127.0.0.1", 27017)); add(new ServerAddress("127.0.0.1", 27027)); add(new ServerAddress("127.0.0.1", 27037)); }});

    }

    @Override
    protected String getMappingBasePackage() {
        return "com.xxxx.mongodb.example.domain";
    }

}

如何更改它以支持 replset?但如果它的读数和其中一台服务器出现故障,我会收到一个错误.. 无论如何要重新连接?

【问题讨论】:

  • 尝试在 mongo() 方法上使用此配置:return new Mongo(new ArrayList&lt;ServerAddress&gt;() {{ add(new ServerAddress("127.0.0.1", 27017)); add(new ServerAddress("127.0.0.1", 27027)); add(new ServerAddress("127.0.0.1", 27037)); }});
  • 不好。没用
  • Wierd.. 我设置了一个副本集并使用了上面的代码,并且非常适合我。您是否正确初始化了副本集? rs.initiate( configuration )
  • 问题是,如果主节点出现故障,我希望它重新连接一些方法

标签: java spring mongodb spring-data


【解决方案1】:

URI 方法应该可以工作,或者有更清晰的方法来使用服务器列表初始化副本集:

final List<ServerAddress> seeds = Arrays.asList(new ServerAddress("127.0.0.1", 27017),
                                                new ServerAddress("127.0.0.1", 27027),
                                                new ServerAddress("127.0.0.1", 27037));
final Mongo mongo = new Mongo(seeds);

【讨论】:

    【解决方案2】:

    这就是我的做法:

        String mongoURI="mongodb://myUsrName:pass@mongoServer-001.company.com:27017,mongoServer-002.company.com:27017,mongoServer-003.company.com:27017/myDBname?waitqueuemultiple=1500&amp;w=1&amp;maxpoolsize=40&amp;safe=true";
        MongoURI uri = new MongoURI(mongoURI);
        Mongo mongo = new Mongo(uri);
    

    我在 URI 中指定了 3 个服务器(以及最大池大小等额外参数)。 第三台服务器(mongoServer-003)是仲裁者,它不存储任何信息。当当前主服务器出现故障时,仲裁器有助于选择主服务器。看看this article

    使用此配置,即使主服务器出现故障,应用也可以继续工作。

    【讨论】:

      【解决方案3】:

      您也可以使用 CustomEditorConfigurer 和实现 PropertyEditorRegistrar 的类。 所以你的配置类需要这个:

          @Bean
          public static CustomEditorConfigurer customEditorConfigurer(){
              CustomEditorConfigurer configurer = new CustomEditorConfigurer();
              configurer.setPropertyEditorRegistrars(
                      new PropertyEditorRegistrar[]{new ServerAddressPropertyEditorRegistrar()});
              return configurer;
          }
      
          @Override
          protected String getDatabaseName() {
              return authenticationDb;
          }
      
          @Override
          @Bean
          public MongoClient mongoClient() {
              MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress(host, port)), mongoCredentials(), mongoClientOptions());
              return mongoClient;
          }
      
      
      
      
      public final class ServerAddressPropertyEditorRegistrar implements PropertyEditorRegistrar {
          @Override
          public void registerCustomEditors(PropertyEditorRegistry registry) {
              registry.registerCustomEditor(ServerAddress[].class, new ServerAddressPropertyEditor());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        • 2017-02-02
        • 1970-01-01
        • 1970-01-01
        • 2020-10-14
        • 1970-01-01
        相关资源
        最近更新 更多