gpt4 book ai didi

java - MongoSocketOpenException - 连接到 MongoDB 时出现问题

转载 作者:行者123 更新时间:2023-12-01 20:17:47 25 4
gpt4 key购买 nike

这是我第一次尝试使用 MongoDB,所以如果你们有任何建议,我希望得到任何提示,但特别是,我在建立连接时遇到了困难...加载 jar 时会发生此错误。

这是我的堆栈跟踪:

[00:12:43 INFO]: Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) [GangWars-1.0-SNAPSHOT-shaded.jar:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.8.0_91]
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[?:1.8.0_91]
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[?:1.8.0_91]
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[?:1.8.0_91]
at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_91]
at java.net.PlainSocketImpl.connect(Unknown Source) ~[?:1.8.0_91]
at java.net.SocksSocketImpl.connect(Unknown Source) ~[?:1.8.0_91]
at java.net.Socket.connect(Unknown Source) ~[?:1.8.0_91]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[GangWars-1.0-SNAPSHOT-shaded.jar:?]
... 3 more

这是我的代码(jar 运行时立即发生):

private void loadGangs() {
MongoDB.getIDs().forEach(id -> GangUtils.addGang(new Gang(id)));
}

我的 MongoDB 类:

public final class MongoDB {

private static final String name = "data";
private static final MongoClient mongoClient = new MongoClient();

public static String getName() {
return name;
}

public static MongoDatabase getDatabase() {
return mongoClient.getDatabase(name);
}

public enum CollectionEnum {
GANG, DRUGS, COPS
}

public static MongoCollection<Document> getCollection(CollectionEnum collection) {
return getDatabase().getCollection(collection.name().toLowerCase());
}

public static List<Integer> getIDs() {
List<Document> gangs = getCollection(CollectionEnum.GANG).find().into(new ArrayList<>());
List<Integer> ids = new ArrayList<>();

for (Document gang : gangs) {
Object object = gang.get("_id");

if (!(object instanceof Document)) continue;

List<Document> idTags = (List<Document>) object;

for (Document id : idTags) {
ids.add(id.getInteger("_id"));
}

}

return ids;
}

public static int getNextID() {
int id = 0;
for (int i = 0; i >= getIDs().size(); i++) {
if (getIDs().get(i) != i) {
id = i;
break;
}
id++;
}
return id;
}

}

我做错了什么? (我尝试在这里进行研究,但我找不到任何与 Java 相关的内容来帮助我使用 MongoDB)

最佳答案

我知道这篇文章有 9 个月的时间,但这可以帮助其他人。我遇到了同样的问题:问题是我不幸地将 test 作为 de.flapdoodle.embed.mongo 依赖项的 scope ,只需让它是默认(编译)通过注释范围解决了问题。

<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<!-- <scope>test</scope> -->
</dependency>

compile 是 maven 默认作用域。构建、测试和运行项目需要具有 compile 范围的依赖项。构建和运行项目不需要具有 test 范围的依赖项,而是编译和运行单元测试。因此,以 test 作为作用域的依赖项不会附加到构建过程的 jar/war 结果中,并且当我们运行项目时,所包含的类和其他资源不可用.
see here for more information about maven scopes

关于java - MongoSocketOpenException - 连接到 MongoDB 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45487272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com