Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改java代码以使用replset?使用Spring数据和MongoDB_Java_Spring_Mongodb_Spring Data - Fatal编程技术网

如何更改java代码以使用replset?使用Spring数据和MongoDB

如何更改java代码以使用replset?使用Spring数据和MongoDB,java,spring,mongodb,spring-data,Java,Spring,Mongodb,Spring Data,我想知道如何更改java代码以支持带有spring数据和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 --rep

我想知道如何更改java代码以支持带有spring数据和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上的数据库出现故障,那么其他数据库中的一个将成为主数据库,因此我知道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";
    }

}
@配置
@使能性假设
@ComponentScan(basePackageClasses={MongoDBApp.class})
@PropertySource(“类路径:application.properties”)
公共类MongoConfiguration扩展了AbstractMongoConfiguration{
@凌驾
受保护的字符串getDatabaseName(){
返回“演示”;
}
@凌驾
public Mongo Mongo()引发异常{
返回newmongo(newarraylist(){add(newserveraddress(“127.0.0.1”,27017));add(newserveraddress(“127.0.0.1”,27027));add(newserveraddress(“127.0.0.1”,27037));});
}
@凌驾
受保护的字符串getMappingBasePackage(){
返回“com.xxxx.mongodb.example.domain”;
}
}
如何更改它以支持replset?但是如果它的读数和一个服务器发生故障,我会得到一个错误。。是否要重新连接?

我就是这样做的:

    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)是仲裁器,它不存储任何信息。当当前主服务器关闭时,仲裁器帮助选择主服务器。看一看

使用此配置,即使主服务器停机,应用程序也可以继续工作。

我就是这样做的:

    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)是仲裁器,它不存储任何信息。当当前主服务器关闭时,仲裁器帮助选择主服务器。看一看


使用此配置,即使主服务器关闭,应用程序也可以继续工作。

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);
final List seeds=Arrays.asList(新服务器地址(“127.0.0.1”,27017),
新服务器地址(“127.0.0.1”,27027),
新服务器地址(“127.0.0.1”,27037));
最终蒙戈蒙戈=新蒙戈(种子);

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);
final List seeds=Arrays.asList(新服务器地址(“127.0.0.1”,27017),
新服务器地址(“127.0.0.1”,27027),
新服务器地址(“127.0.0.1”,27037));
最终蒙戈蒙戈=新蒙戈(种子);

您也可以通过CustomEditorConfigure和实现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());
    }
}

您还可以使用CustomEditorConfigure和实现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());
    }
}

尝试在mongo()方法上使用此配置:
returnnewmongo(newarraylist(){{add(newserveraddress(“127.0.0.1”,27017));add(newserveraddress(“127.0.0.1”,27027));add(newserveraddress(“127.0.0.1”,27037));}不好。没有工作。。我设置了一个副本集,并使用了上面的代码,对我来说效果非常好。您是否正确初始化了副本集
rs.initiate(configuration)
问题是,如果主节点出现故障,一切都崩溃了,我希望它重新连接一些尝试在mongo()方法上使用此配置:
returnnewmongo(new ArrayList(){add(new ServerAddress(“127.0.0.1”,27017));add(new ServerAddress(“127.0.0.1”,27027));add(new ServerAddress(“127.0.0.1”,27037)); }});不好。没有工作。。我设置了一个副本集,并使用了上面的代码,对我来说效果非常好。您是否正确初始化了副本集
rs.initiate(configuration)
问题是,如果主节点发生故障,所有东西都会崩溃,我希望它重新连接