在使用replicaset连接嵌入式mongodb以执行junit测试用例时,我遇到了以下问题。我得到以下例外。
[mongod output] 2021-03-06T23:28:13.202+0530 I CONTROL [initandlisten]**WARNING: This server is bound to localhost.
[mongod output] 2021-03-06T23:28:13.202+0530 I CONTROL [initandlisten]** Remote systems will be unable to connect to this server.
[mongod output] 2021-03-06T23:28:13.202+0530 I CONTROL [initandlisten]** Start the server with --bind_ip <address> to specify which IP
[mongod output] 2021-03-06T23:28:13.202+0530 I CONTROL [initandlisten]** addresses it should serve responses from, or with --bind_ip_all to
[mongod output] 2021-03-06T23:28:13.202+0530 I CONTROL [initandlisten]** bind to all interfaces. If this behavior is desired, start the
[mongod output] 2021-03-06T23:28:13.202+0530 I CONTROL [initandlisten]** server with --bind_ip 127.0.0.1 to disable this warning.
[mongod output] 2021-03-06T23:28:13.202+0530 I CONTROL [initandlisten]
[mongod output] 2021-03-06T23:28:13.206+0530 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: 05d4a8b0-4709-4a0d-91fa-0de499c942a0
[mongod output] 2021-03-06T23:28:13.856+0530 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/Users/mishrd5/AppData/Local/Temp/embedmongo-db-9c4148c7-97f4-498d-bf55-a03c88a38830/diagnostic.data'
[mongod output] 2021-03-06T23:28:13.857+0530 I STORAGE [initandlisten] createCollection: local.replset.oplogTruncateAfterPoint with generated UUID: 3fd3b9bc-3f44-4975-ba81-8b995268f49d
[mongod output] 2021-03-06T23:28:13.879+0530 I STORAGE [initandlisten] createCollection: local.replset.minvalid with generated UUID: a1e095da-9f04-4cda-bd3c-cda0297f4453
[mongod output] 2021-03-06T23:28:13.910+0530 I REPL [initandlisten] Did not find local voted for document at startup.
[mongod output] 2021-03-06T23:28:13.910+0530 I REPL [initandlisten] Did not find local Rollback ID document at startup. Creating one.
[mongod output] 2021-03-06T23:28:13.910+0530 I STORAGE [initandlisten] createCollection: local.system.rollback.id with generated UUID: c82cb441-efd9-4605-abc5-c2a18bbb06c1
[mongod output] 2021-03-06T23:28:13.938+0530 I REPL [initandlisten] Initialized the rollback ID to 1
[mongod output] 2021-03-06T23:28:13.938+0530 I REPL [initandlisten] Did not find local replica set configuration document at startup; NoMatchingDocument: Did not find replica set configuration document in local.system.replset
[mongod output] 2021-03-06T23:28:13.939+0530 I CONTROL [LogicalSessionCacheRefresh] Sessions collection is not set up; waiting until next sessions refresh interval: Replication has not yet been configured
[mongod output] 2021-03-06T23:28:13.939+0530 I CONTROL [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: Replication has not yet been configured
[mongod output] 2021-03-06T23:28:13.939+0530 I NETWORK [initandlisten] waiting for connections on port 27023
[mongod output] 2021-03-06 23:28:13.939 INFO 18616 --- [ main] d.f.embed.mongo.MongodExecutable : start de.flapdoodle.embed.mongo.config.MongodConfigBuilder$ImmutableMongodConfig@6870c3c2
2021-03-06 23:28:14.023 INFO 18616 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[host:27021], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', requiredReplicaSetName='rs0'}
2021-03-06 23:28:14.024 INFO 18616 --- [ main] org.mongodb.driver.cluster : Adding discovered server host:27021 to client view of cluster
--------------------------------------
MongoClient : com.mongodb.client.internal.MongoClientImpl@53f0d09c
--------------------------------------
2021-03-06 23:28:14.092 INFO 18616 --- [ main] org.mongodb.driver.cluster : No server chosen by com.mongodb.client.internal.MongoClientDelegate$1@5eabff6b from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=host:27021, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
2021-03-06 23:28:28.297 INFO 18616 --- [ll'}-host:27021] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server host:27021
com.mongodb.MongoSocketException: host
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-4.0.5.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-4.0.5.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_261]
Caused by: java.net.UnknownHostException: host
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_261]
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_261]
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_261]
at java.net.InetAddress.getAllByName0(InetAddress.java:1277) ~[na:1.8.0_261]
at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_261]
at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_261]
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongodb-driver-core-4.0.5.jar:na]
... 5 common frames omitted
我提供以下代码。
@Profile("test")
@ActiveProfiles("test")
@TestConfiguration
public class TestMongoDBConfig implements InitializingBean, DisposableBean {
/**The executable. */
private MongodExecutable executable;
private MongoTemplate mongoTemplate;
private MongodProcess node1Mongod;
private MongodProcess node2Mongod;
private MongodExecutable node1MongodExe;
private MongodExecutable node2MongodExe;
private int node1Port = 27021;
private int node2Port = 27023;
private static final String CONNECTION_STRING = "mongodb://%s:%d";
private int port = 27021;
@Override
public void afterPropertiesSet() throws Exception {
// IFeatureAwareVersion version = de.flapdoodle.embed.mongo.distribution.Versions
// .withFeatures(new GenericVersion("4.0.0"), Version.Main.PRODUCTION.getFeatures());
// IMongoCmdOptions cmdOptions = new MongoCmdOptionsBuilder().useNoPrealloc(false).useSmallFiles(false)
// .master(false).verbose(false).useNoJournal(false).syncDelay(0).build();
// port = Network.getFreeServerPort();
// System.out.println("-------------------------");
// System.out.println("Test Mongo PORT : "+port);
// System.out.println("-------------------------");
// IMongodConfig mongodConfig = new MongodConfigBuilder().version(version)
// .net(new Net(port, Network.localhostIsIPv6()))
//// .replication(new Storage(null, "testRepSet", 5000))
// .replication(new Storage(null, "rs0", 5000))
// .configServer(true)
//// .withLaunchArgument("--replSet", "rs0")
//// .configServer(false)
// .cmdOptions(cmdOptions).build();
// MongodStarter starter = MongodStarter.getDefaultInstance();
// executable = starter.prepare(mongodConfig);
// executable.start();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MongodStarter runtime = MongodStarter.getDefaultInstance();
node1MongodExe = runtime.prepare(new MongodConfigBuilder().version(Version.Main.V4_0)
.withLaunchArgument("--replSet", "rs0")
.cmdOptions(new MongoCmdOptionsBuilder().useNoJournal(false).build())
.net(new Net(node1Port, Network.localhostIsIPv6())).build());
node1Mongod = node1MongodExe.start();
node2MongodExe = runtime.prepare(new MongodConfigBuilder().version(Version.Main.V4_0)
.withLaunchArgument("--replSet", "rs0")
.cmdOptions(new MongoCmdOptionsBuilder().useNoJournal(false).build())
.net(new Net(node2Port, Network.localhostIsIPv6())).build());
node2Mongod = node2MongodExe.start();
}
/**
* Mongo client.
*
* @return the mongo client
*/
// @Primary
@Bean(name = "test1")
public MongoClient mongoClient() {
MongoClient mongoClient = MongoClients.create(cs);
System.out.println("--------------------------------------");
System.out.println("MongoClient : "+mongoClient);
System.out.println("--------------------------------------");
MongoDatabase adminDatabase = mongoClient.getDatabase("admin");
Document config = new Document("_id", "rs0");
BasicDBList members = new BasicDBList();
members.add(new Document("_id", 0)
.append("host", "localhost:" + node1Port));
members.add(new Document("_id", 1)
.append("host", "localhost:" + node2Port));
config.put("members", members);
adminDatabase.runCommand(new Document("replSetInitiate", config));
return mongoClient;
}
@Override
public void destroy() throws Exception {
executable.stop();
}
}
请忽略注解的代码。我也参考了以下链接,但没有成功。
https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/257
在springboot中用flapdoodle嵌入mongodb测试@transactional
使用mongodb版本4和副本配置Flapdoole embedded mongo
请帮帮我。
暂无答案!
目前还没有任何答案,快来回答吧!