0

我有一个部署在 Kubernetes 中的 spring boot 应用程序,需要连接到集群外部托管的 mongodb 服务器。根据https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services中指定的 Kubernetes ,我为我的 mongodb 服务器创建了 Kubernetes 服务和端点。现在我的 Spring Boot 应用程序需要连接到这个端点。我试过以下

-- 添加 uri 到 application.properties

spring.data.mongodb.uri=mongodb://mongo

-- 从 spring 显式定义 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>
4

1 回答 1

0

经过一番搜索,我能够如下连接,

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);
}

这是有效的,但不知道这是否是最好的方法。

于 2020-02-11T07:29:56.843 回答