我有一个部署在 Kubernetes 中的 Spring 启动应用程序,需要连接到集群外部托管的 mongodb 服务器。根据 https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services 中指定的 Kubernetes,我已经为我的 mongodb 服务器创建了 Kubernetes 服务和端点。现在我的 Spring 启动应用程序需要连接到这个端点。我试过以下
-- 将 URI 添加到应用程序属性
spring.data.mongodb.uri=mongodb://mongo
-- 从春天明确定义 MongoDB 客户端
@Configuration
@Slf4j
public class AppConfig {
@Autowired
DiscoveryClient discoveryClient;
public @Bean MongoClient mongoClient() {
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://mongo");
return new MongoClient(mongoClientURI);
}
}
但是我的应用程序不断抛出错误
com.mongodb.MongoSocketException: mongo: Try again
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211) ~[mongo-java-driver-3.11.2.jar:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75) ~[mongo-java-driver-3.11.2.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongo-java-driver-3.11.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongo-java-driver-3.11.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.11.2.jar:na]
at java.base/java.lang.Thread.run(Thread.java:835) ~[na:na]
Caused by: java.net.UnknownHostException: mongo: Try again
at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:na]
at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:929) ~[na:na]
at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1515) ~[na:na]
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:848) ~[na:na]
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364) ~[na:na]
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298) ~[na:na]
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongo-java-driver-3.11.2.jar:na]
... 5 common frames omitted
我的项目中有以下依赖项
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId>
</dependency>
经过少量搜索,我能够连接如下,
public @Bean MongoClient mongoClient() {
List<ServiceInstance> mongo = discoveryClient.getInstances("mongo");
List<ServerAddress> mongoAddressList = new ArrayList<>();
for (ServiceInstance serviceInstance : mongo) {
ServerAddress serverAddress = new ServerAddress(serviceInstance.getHost());
mongoAddressList.add(serverAddress);
}
MongoClientURI mongoClientURI = new MongoClientURI("mongodb://mongo");
return new MongoClient(mongoAddressList);
}
这是有效的,但不知道这是否是最好的方法。