我在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的配置问题。启动端口转发和服务本身
发布于 2015-04-22 17:21:36
我猜你没有正确配置集群(日志中的消息告诉了这个问题),我不确定OpenShift Cratridge是如何工作的,但我建议你检查一下它是否正确启动了mondo-db。通过ssh客户端进行检查,并运行mongo-db命令检查其状态以及是否已启动。看一下这个问题:Java MongoClient cannot connect to primary,我想它给了你一些关于如何检查哪里有问题的想法。
https://stackoverflow.com/questions/29792597
复制相似问题