MongoDB主机名/ URI配置

时间:2017-01-27 11:01:00

标签: java mongodb uri google-cloud-platform mongo-java

注意这看起来很长,但提供了上下文并在底部列出了我的主要问题。我研究了所有部分并包含了参考文献。

我使用了Google Cloud Launcher to create a Mongo database。这创建了两个Mongo服务器(主服务器和辅助服务器)的副本集以及三个独立VM上的仲裁服务器。我没有更改任何VM配置(打开防火墙除外)。我已经将SSH连接到两个服务器中,并且已验证Mongo正在按预期进行复制。我将外部IP从x.x.x.x更改为FIRST.EXTERNAL.IP和SECOND.EXTERNAL.IP,以使帖子更具可读性(和安全性)。

我现在正试图通过Java MongoClient连接数据库:

MongoClientOptions.Builder mongoClientOptions = MongoClientOptions.builder().connectionsPerHost(40)
  .writeConcern(WriteConcern.ACKNOWLEDGED).readPreference(ReadPreference.secondaryPreferred())
  .readConcern(ReadConcern.LOCAL).socketTimeout(60000).threadsAllowedToBlockForConnectionMultiplier(1500);
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://FIRST.EXTERNAL.IP:27017,SECOND.EXTERNAL.IP:27017", mongoClientOptions);
mongoClient = new MongoClient(mongoClientURI);

我想验证我应该在URI中包含主外部IP和辅助外部IP 。我没有包含仲裁者,因为MongoClient直接与仲裁者交互没有意义。

以下显示了尝试find()时的Java异常。 Insert()创建相同的错误,但WritePreferenceServerSelector而不是ReadPref ..

除外
1449 [main] INFO org.mongodb.driver.cluster - Cluster created with settings {hosts=[SECOND.EXTERNAL.IP:27017, FIRST.EXTERNAL.IP:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=60000}
1449 [main] INFO org.mongodb.driver.cluster - Adding discovered server SECOND.EXTERNAL.IP:27017 to client view of cluster
1558 [main] INFO org.mongodb.driver.cluster - Adding discovered server FIRST.EXTERNAL.IP:27017 to client view of cluster
1579 [main] INFO org.mongodb.driver.cluster - No server chosen by ReadPreferenceServerSelector{readPreference=ReadPreference{name=secondaryPreferred}} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=SECOND.EXTERNAL.IP:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=FIRST.EXTERNAL.IP:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
1743 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:2, serverValue:12}] to FIRST.EXTERNAL.IP:27017
1766 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-SECOND.EXTERNAL.IP:27017] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:9}] to SECOND.EXTERNAL.IP:27017
1790 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=FIRST.EXTERNAL.IP:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 1]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=41160155, setName='rs0', canonicalAddress=my-project-name-mongo-server-1:27017, hosts=[my-project-name-mongo-server-1:27017, my-project-name-mongo-server-2:27017], passives=[], arbiters=[my-project-name-mongo-arbiter-1:27017], primary='my-project-name-mongo-server-1:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000003, setVersion=3, lastWriteDate=Fri Jan 27 02:30:26 PST 2017, lastUpdateTimeNanos=165366848909779}
1791 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Discovered cluster type of REPLICA_SET
1791 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Adding discovered server my-project-name-mongo-server-1:27017 to client view of cluster
1793 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Adding discovered server my-project-name-mongo-server-2:27017 to client view of cluster
1794 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Adding discovered server my-project-name-mongo-arbiter-1:27017 to client view of cluster
1796 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Server SECOND.EXTERNAL.IP:27017 is no longer a member of the replica set.  Removing from client view of cluster.
1797 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Server FIRST.EXTERNAL.IP:27017 is no longer a member of the replica set.  Removing from client view of cluster.
1798 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-FIRST.EXTERNAL.IP:27017] INFO org.mongodb.driver.cluster - Canonical address my-project-name-mongo-server-1:27017 does not match server address.  Removing FIRST.EXTERNAL.IP:27017 from client view of cluster
1799 [cluster-ClusterId{value='588b214418786c23f4fcf845', description='null'}-my-project-name-mongo-server-1:27017] INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server my-project-name-mongo-server-1:27017
com.mongodb.MongoSocketException: my-project-name-mongo-server-1: unknown error
   at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188)
   at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)
   at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
   at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
   at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
   at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.UnknownHostException: my-project-name-mongo-server-1: unknown error
//(then it just repeats the socket exception through connection retries I believe)

我目前的理论是,最初由Google Cloud Launcher设置的主机名配置需要使用外部IP进行更新。这意味着将my-project-name-mongo-server-1更改为FIRST.EXTERNAL.IP,将第二个服务器更改为Mongo Documentation。如果这是正确的,我应该留下仲裁者主机名吗? (旁注 - my-project-name-mongo-server-1:27017如何解决?它是Google Cloud的内部IP吗?)

据我所知,这将解决我的问题和异常Canonical address my-project-name-mongo-server-1:27017 does not match server address

回顾一下我对我的问题的每一个问题:

  1. 我应该在Java MongoClient URI中包含哪些IP?
  2. 我应该将成员主机名重新配置为外部IP吗?如果是这样,哪些?
  3. 或者,它可以帮助解释my-project-name-mongo-server-1:27017是否是本地主机名。
  4. 既然您了解我的应用程序设计,那么是否有更好的方法来联网我的外部连接?

2 个答案:

答案 0 :(得分:1)

将主机名更新为非内部IP修复了规范地址问题。我仍然不完全确定什么是最适合URI的,但从我能说的最安全的方式来包括副本集的所有服务器。

答案 1 :(得分:0)

Mongo 3.0 Java驱动程序不支持属性spring.data.mongodb.host和spring.data.mongodb.port。

因此,请使用spring.data.mongodb.uri提供以下所有配置:

spring.data.mongodb.uri=mongodb://username:password@mongodbIp:portnumber