Mongo DB Canonical地址与服务器地址不匹配

时间:2019-04-18 07:32:00

标签: mongodb spring-boot spring-mvc java-8 spring-data-mongodb

我正在将我的spring boot应用程序与mongo db连接起来。

代码在数据库具有单个节点且未在集群模式下运行的本地环境下运行良好。

但是,当数据库在具有集群模式,单个节点和1个副本集的登台环境中运行时,sp​​ring应用程序将显示以下错误:

INFO  |  |  | 2019-04-18 12:43:25 | [cluster-ClusterId{value='5cb823951a734cea7f7eebe1', description='null'}-10.202.41.254:27017] com.mongodb.diagnostics.logging.SLF4JLogger - Canonical address educationelasticsearch-digital-v1-41-254:27017 does not match server address.  Removing 10.20.41.254:27017 from client view of cluster
INFO  |  |  | 2019-04-18 12:43:25 | [cluster-ClusterId{value='5cb823951a734cea7f7eebe1', description='null'}-educationelasticsearch-digital-v1-41-254:27017] com.mongodb.diagnostics.logging.SLF4JLogger - Exception in monitor thread while connecting to server educationelasticsearch-digital-v1-41-254:27017
com.mongodb.MongoSocketException: educationelasticsearch-digital-v1-41-254: nodename nor servname provided, or not known
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188) ~[mongodb-driver-core-3.8.2.jar:?]
    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:?]
    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:748) [?:1.8.0_171]
Caused by: java.net.UnknownHostException: educationelasticsearch-digital-v1-41-254: nodename nor servname provided, or not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[?:1.8.0_171]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[?:1.8.0_171]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[?:1.8.0_171]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1276) ~[?:1.8.0_171]
    at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[?:1.8.0_171]
    at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[?:1.8.0_171]
    at java.net.InetAddress.getByName(InetAddress.java:1076) ~[?:1.8.0_171]
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186) ~[mongodb-driver-core-3.8.2.jar:?]
    ... 5 more

在这两种情况下,应用程序属性都相同,只是登台服务器的IP位置更改为localhost:

spring.elasticsearch.education.hostname=localhost   // staging server IP
spring.elasticsearch.education.scheme=http
spring.elasticsearch.education.port=9200


###### Mongo properties ########
spring.data.mongodb.host=localhost  //staging server ip
spring.data.mongodb.port=27017
spring.data.mongodb.database=ddddd
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=root
spring.data.mongodb.password=root
spring.data.mongodb.repositories.enabled=true
logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG

我检查了this question,但此方法分辨率不高。

请在这里提出问题吗?

1 个答案:

答案 0 :(得分:2)

看这里https://stackoverflow.com/a/34373673/5712629

此版本的mongodb驱动程序可能不支持spring.data.mongodb.host和spring.data.mongodb.port。

您可以尝试:

spring.data.mongodb.uri=mongodb://root:root@localhost:27017

相反。