与常规mongo一起配置反应式mongodb集群

时间:2019-03-04 08:53:29

标签: java mongodb spring-data-mongodb spring-webflux

我有使用MongoDB的Spring WebFlux应用程序。 即使听起来很奇怪,我也必须同时配置常规的Mongo存储库和ReactiveMongo存储库。

在上一个春季应用程序中,我们的Mongo定义方式是:

@EnableMongoRepositories
@SpringBootApplication(exclude = {MongoAutoConfiguration.class})
@EnableEurekaClient
public class MyMsApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(MyMsApplication.class, args);
    }
}

@Configuration
public class MongoConfigurations {

    @Value("${spring.data.mongodb.database}")
    private String defaultDatabase;

    @Value("${mongodb.cluster}")
    private String mongoCluster;

    @Value("${mongodb.port}")
    private int mongoPort;

    @Value("${mongodb.connectTimeoutMS}")
    private int mongoTimeout;

    @Value("${mongodb.auth.database}")
    private String mongoAuthDB;

    @Value("${mongodb.auth.username}")
    private String mongoUser;

    @Value("${mongodb.auth.password}")
    private String mongoPass;

    private MongoClientOptions.Builder optionsBuilder;
    private MongoClientOptions options;
    private List<ServerAddress> seeds;
    private MongoCredential cred;
    private ConnectionString connectionString;
    private MongoClient mongoClient;

    @PostConstruct
    public void init() {
        optionsBuilder = MongoClientOptions.builder();
        optionsBuilder.connectTimeout(mongoTimeout);
        options = optionsBuilder.build();
        cred = MongoCredential.createCredential(mongoUser, mongoAuthDB, mongoPass.toCharArray());
        seeds = new ArrayList<ServerAddress>();
        for (String ip : mongoCluster.split(",")) {
            seeds.add(new ServerAddress(ip, mongoPort));
        }
    }

    @Bean
    public MongoDbFactory mongoDbFactory() throws UnknownHostException {
        return new SimpleMongoDbFactory(getMongoClient(),defaultDatabase);
    }

    @Bean
    public MongoClient getMongoClient() throws UnknownHostException {
        this.mongoClient = new MongoClient(seeds, cred, options);
        return this.mongoClient;
    }

    @Primary
    @Bean
    public MongoTemplate mongoTemplate() throws UnknownHostException {
        return new MongoTemplate(mongoDbFactory());
    }

    @PreDestroy
    public void close() {
        if (this.mongoClient != null) {
            this.mongoClient.close();
        }
    }
}
  1. 但是我没有找到配置ReactMongo集群的任何工作方法
  2. 我担心设置属性
    spring.main.allow-bean-definition-overriding=true

我在这里找到了一些解决方案,但所有解决方案都会导致我异常(我将尝试重现它们并添加错误消息

我尝试过的建议解决方案之一是:

@EnableMongoRepositories
@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoReactiveAutoConfiguration.class})
@EnableEurekaClient
public class MyMsApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(MyMsApplication.class, args);
    }
}

@Configuration
public class MongoConfigurations {

    @Value("${spring.data.mongodb.database}")
    private String defaultDatabase;

    @Value("${mongodb.cluster}")
    private String mongoCluster;

    @Value("${mongodb.port}")
    private int mongoPort;

    @Value("${mongodb.connectTimeoutMS}")
    private int mongoTimeout;

    @Value("${mongodb.auth.database}")
    private String mongoAuthDB;

    @Value("${mongodb.auth.username}")
    private String mongoUser;

    @Value("${mongodb.auth.password}")
    private String mongoPass;

    MongoClientOptions.Builder optionsBuilder;
    MongoClientOptions options;
    List<ServerAddress> seeds;
    MongoCredential cred;
    ConnectionString connectionString;

    @PostConstruct
    public void init() {
        optionsBuilder = MongoClientOptions.builder();
        optionsBuilder.connectTimeout(mongoTimeout);
        options = optionsBuilder.build();
        cred = MongoCredential.createCredential(mongoUser, mongoAuthDB, mongoPass.toCharArray());
        seeds = new ArrayList<ServerAddress>();
        for (String ip : mongoCluster.split(",")) {
            seeds.add(new ServerAddress(ip, mongoPort));
        }
    }

    @Configuration
    private class ReactiveMongoConfiguration extends AbstractReactiveMongoConfiguration {

        private com.mongodb.reactivestreams.client.MongoClient mongo;

        public ReactiveMongoConfiguration() {
        }

        @PreDestroy
        public void close() {
            if (this.mongo != null) {
                this.mongo.close();
            }
        }

        @Bean
        @Override
        public com.mongodb.reactivestreams.client.MongoClient reactiveMongoClient() {
            this.mongo = MongoClients.create(mongoClientSettings());
            return this.mongo;
        }

        private MongoClientSettings mongoClientSettings() {
            return MongoClientSettings.builder()
                    .applyToClusterSettings(clusterSettingsBuilder())
                    .applyToSslSettings(sslSettingsBuilder())
                    .credential(cred)
                    .build()
        }

        private Block<ClusterSettings.Builder> clusterSettingsBuilder() {
            return (cluster) -> cluster.hosts(seeds).build();
        }

        private Block<SslSettings.Builder> sslSettingsBuilder() {
            return (ssl) -> ssl.enabled(false).build();
        }

        @Override
        protected String getDatabaseName() {
            return defaultDatabase;
        }
    }

    @Configuration
    private class ImperativeMongoConfiguration {

        private MongoClient mongoClient;

        public ImperativeMongoConfiguration() {
        }

        @Bean
        public MongoDbFactory mongoDbFactory() throws UnknownHostException {
            return new SimpleMongoDbFactory(getMongoClient(),defaultDatabase);
        }

        @Bean
        public MongoClient getMongoClient() throws UnknownHostException {
            this.mongoClient = new MongoClient(seeds, cred, options);
            return this.mongoClient;
        }

        @Primary
        @Bean
        public MongoTemplate mongoTemplate() throws UnknownHostException {
            return new MongoTemplate(mongoDbFactory());
        }

        @PreDestroy
        public void close() {
            if (this.mongoClient != null) {
                this.mongoClient.close();
            }
        }
    }
}

但它引发了一个异常:

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar:?]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:?]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) [mongodb-driver-core-3.8.2.jar:?]
    at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.PlainSocketImpl.waitForConnect(Native Method) ~[?:?]
    at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:107) ~[?:?]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[?:?]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[?:?]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[?:?]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[?:?]
    at java.net.Socket.connect(Socket.java:591) ~[?:?]
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:?]
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:?]
    ... 3 more

谢谢

0 个答案:

没有答案