MongoDB HostName/URI 配置

MongoDB HostName/URI Configuration

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

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

我现在正尝试通过 Java Mongo客户端连接到数据库:

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 云的内部 IP 吗?)

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

回顾一下关于我的问题的每个问题:

  1. 我应该在 Java Mongo客户端 URI 中包含哪些 IP?
  2. 我应该将成员主机名重新配置为外部 IP 吗?如果有,是哪些?
  3. 可选地,解释 my-project-name-mongo-server-1:27017 是否是本地主机名会很有帮助。
  4. 既然你知道我的应用程序设计,是否有更好的方法来联网我的外部连接?

将主机名更新为 non-internal IP 解决了规范地址问题。我仍然不完全确定什么最适合 URI,但据我所知,包含副本集的所有服务器是最安全的。

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

所以使用spring.data.mongodb.uri提供所有配置如下:

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