我在 openshift 上安装了一个 MongoDB 墨盒,我在从 java 代码连接到它时遇到了麻烦。IP 地址、端口和凭据取自 openshift 的 RockMongo 卡式盒。以下方法调用:
public Document insert(String audio, String username) {
Document document = new Document();
document.put("username", username);
document.put("audio", audio);
document.put("timestamp", new Date());
collection.insertOne(document);
return document;
}
和这个 mongo 客户端配置:
private static MongoClient build() throws UnknownHostException {
if (mongoClient == null) {
mongoClient = new MongoClient(
new MongoClientURI( "mongodb://admin:password@X.X.X.X:27017/dbName"));
}
return mongoClient;
}
public static MongoCollection<Document> getCollection(String collectionName) {
try {
build();
} catch (UnknownHostException e) {
}
MongoDatabase db = mongoClient.getDatabase(dbName);
MongoCollection<Document> collection = db.getCollection(collectionName);
return collection;
}
导致 INFO:PrimaryServerSelector 从集群描述 ClusterDescription 中选择没有服务器,并且异常:在等待与 PrimaryServerSelector 匹配的服务器时在 30000 毫秒后超时。
编辑:我也无法通过 mongo 终端应用程序在 openshift 上连接 mongoDB 服务:“异常:连接失败”,所以我认为这是 openshift 配置问题。端口转发和服务本身启动