0

我有一个 Spring Boot 应用程序并尝试使用 ReactiveMongoTemplate 连接到 Mongo 数据库集群。

我可以使用以下代码连接到 Mongo DB:

application.properties 内容:

spring.data:mongodb:host: localhost
spring.data:mongodb.port: 13.45.67.278
spring.data:mongodb.database: logs_db 

在我的 ReactiveMongoConfig 内容是这样的:

public class ReactiveMongoConfig {
    
    @Autowired
    private Config config;
    @Autowired
    private MongoClient mongoClient;
    
    @Bean
    public ReactiveMongoTemplate reactiveMongoTemplate() {
        return new ReactiveMongoTemplate(mongoClient, config.getMongoDb());
    }
}

有了这个,我就可以连接到数据库,然后执行自动装配 ReactiveMpongoTemplate 的任何操作,如下所示

public class UserLogDaoImpl implements UserLogDao {
    
    static final String DOCUMENT_NAME = "UserActivityLogs";

    @Autowired
    private ReactiveMongoTemplate reactiveMongoTemplate;

    @Override
    public void storeLogs(List<UserActivityLog> userActivityLogs) { 
        try {
            log.info("Inside storeLogs dao method");
            reactiveMongoTemplate.insert(userActivityLogs, DOCUMENT_NAME + "_" +       LocalDate.now().getYear() + "_" + LocalDate.now().getMonthValue()).subscribe();
            
        } catch (DataAccessException ex) {
            log.error("Error while adding logs for {}", userActivityLogs);
        } 
    }

}

我想连接到 MongoCluster 而不仅仅是一个实例。为此,我正在做的事情如下:

application.properties 内容:

spring.data.mongodb.uri: mongodb://abc:xyz@15.207.198.133:27017,15.207.198.133:27018/admin

我也尝试过以下方法:

spring.data.mongodb.uri:mongodb://abc:xyz@15.207.198.133:27017,15.207.198.133:27018/admin?rs0

rs0 是我的 MongoDB 副本名称

我的 ReactiveMongoConfig 类如下所示:

@Configuration
public class ReactiveMongoConfig {

    @Autowired
    private Config config;

    @Bean
    public ReactiveMongoTemplate reactiveMongoTemplate() {
        
        MongoClient mongoClient = MongoClients.create(createMongoClientSettings());
        return new ReactiveMongoTemplate(mongoClient, "admin");
    }

    private MongoClientSettings createMongoClientSettings() {
        ConnectionString connectionString = new ConnectionString(config.getMongoUri());

        MongoClientSettings mongoClientSettings = MongoClientSettings.builder().readConcern(ReadConcern.DEFAULT)
                .writeConcern(WriteConcern.MAJORITY).readPreference(ReadPreference.secondary())
                .applyConnectionString(connectionString).build();
        return mongoClientSettings;
    }

}

当我在上面尝试时,我在应用程序日志中低于,它显示它首先连接但然后立即说

连接到服务器时监视器线程中的异常

[30m2021-05-20 21:19:21,087[0;39m [34mINFO [0;39m [[34mmain[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Cluster created with settings {hosts=[15.207.198.133:27017, 15.207.198.133:27018], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
[30m2021-05-20 21:19:21,087[0;39m [34mINFO [0;39m [[34mmain[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Adding discovered server 15.207.198.133:27017 to client view of cluster
[30m2021-05-20 21:19:21,127[0;39m [34mINFO [0;39m [[34mmain[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Adding discovered server 15.207.198.133:27018 to client view of cluster
[30m2021-05-20 21:19:21,271[0;39m [34mINFO [0;39m [[34mcluster-rtt-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27018[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Opened connection [connectionId{localValue:1, serverValue:77}] to 15.207.198.133:27018
[30m2021-05-20 21:19:21,271[0;39m [34mINFO [0;39m [[34mcluster-rtt-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Opened connection [connectionId{localValue:3, serverValue:4432}] to 15.207.198.133:27017
[30m2021-05-20 21:19:21,271[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27018[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Opened connection [connectionId{localValue:2, serverValue:78}] to 15.207.198.133:27018
[30m2021-05-20 21:19:21,271[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Opened connection [connectionId{localValue:4, serverValue:4433}] to 15.207.198.133:27017
[30m2021-05-20 21:19:21,274[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Monitor thread successfully connected to server with description ServerDescription{address=15.207.198.133:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=49970343, setName='rs0', canonicalAddress=mongo1:27017, hosts=[mongo1:27017], passives=[mongo2:27017], arbiters=[mongo3:27017], primary='mongo1:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000001, setVersion=3, topologyVersion=null, lastWriteDate=Thu May 20 21:19:15 IST 2021, lastUpdateTimeNanos=217465506716392}
[30m2021-05-20 21:19:21,274[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27018[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Monitor thread successfully connected to server with description ServerDescription{address=15.207.198.133:27018, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=49842146, setName='rs0', canonicalAddress=mongo2:27017, hosts=[mongo1:27017], passives=[mongo2:27017], arbiters=[mongo3:27017], primary='mongo1:27017', tagSet=TagSet{[]}, electionId=null, setVersion=3, topologyVersion=null, lastWriteDate=Thu May 20 21:19:15 IST 2021, lastUpdateTimeNanos=217465506716350}
[30m2021-05-20 21:19:21,275[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Discovered cluster type of REPLICA_SET
[30m2021-05-20 21:19:21,275[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Adding discovered server mongo1:27017 to client view of cluster
[30m2021-05-20 21:19:21,276[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Adding discovered server mongo2:27017 to client view of cluster
[30m2021-05-20 21:19:21,277[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Adding discovered server mongo3:27017 to client view of cluster
[30m2021-05-20 21:19:21,278[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Server 15.207.198.133:27017 is no longer a member of the replica set.  Removing from client view of cluster.
[30m2021-05-20 21:19:21,282[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-15.207.198.133:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Server 15.207.198.133:27018 is no longer a member of the replica set.  Removing from client view of cluster.
[30m2021-05-20 21:19:21,282[0;39m [34mINFO [0;39m [[34mcluster-ClusterId{value='60a68501f3849a3e4f33908d', description='null'}-mongo3:27017[0;39m] [33mcom.mongodb.diagnostics.logging.SLF4JLogger[0;39m: Exception in monitor thread while connecting to server mongo3:27017
com.mongodb.MongoSocketException: mongo3: nodename nor servname provided, or not known
    at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
    at com.mongodb.internal.connection.AsynchronousSocketChannelStream.openAsync(AsynchronousSocketChannelStream.java:60)
    at com.mongodb.internal.connection.AsynchronousChannelStream.open(AsynchronousChannelStream.java:124)
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143)
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188)
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144)
    at java.base/java.lang.Thread.run(Thread.java:834)

这有点烦人。

我的 Mongo 数据库集群在带有 3 个容器的 AWS EC2 实例上运行,每个容器都运行仲裁器、主节点和副本节点。

4

0 回答 0