简体   繁体   English

无法使用Heroku上的Java驱动程序与MongoDB连接

[英]Unable to connect with MongoDB using Java Driver on Heroku

I am building a web application with Java and MongoDB on Heroku. 我正在Heroku上使用Java和MongoDB构建Web应用程序。 I learnt this project from here . 我从这里学到了这个项目。 I have been kept trying for two days. 我一直努力尝试了两天。

I use 3.2.2 mongo-java-driver and 3.5.1 maven-compiler-plugin 我使用3.2.2 mongo-java-driver3.5.1 maven-compiler-plugin

This is in my Main class: 这是在我的Main班上:

public static void main(String[] args) throws MongoException, UnknownHostException, Exception {
    MongoClientURI uri = new MongoClientURI(System.getenv("MONGOHQ_URL"));
    MongoClient mongoClient = new MongoClient(uri);

    String dbname = uri.getDatabase();

    //mongoClient.setWriteConcern(WriteConcern.JOURNALED);
    DB db = mongoClient.getDB(dbname);

    staticFileLocation("/public");
    new TodoResource(new TodoService(db));

    MongoCredential credential = MongoCredential.createCredential(uri.getUsername(),dbname,uri.getPassword());
    MongoClientOptions mongoClientOptions = MongoClientOptions.builder().build();
}

And I got this error: 我得到了这个错误:

1:38:42 AM web.1 | Exception in thread "main"
1:38:42 AM web.1 | java.lang.NullPointerException
1:38:42 AM web.1 | at com.mongodb.MongoClientURI.<init>   (MongoClientURI.java:174…
1:38:42 AM web.1 | at com.mongodb.MongoClientURI.<init> (MongoClientURI.java:159…
1:38:42 AM web.1 | at Main.main(Main.java:32)

It's NullPointerException, so I tried to replace MONGOHQ_URL by mongodb://user:passwd@host1:port1,host2:port2/dbname , and I also tried to use mLab MongoDB on Heroku, but the same error: 它是NullPointerException,因此我尝试用mongodb://user:passwd@host1:port1,host2:port2/dbname替换MONGOHQ_URL ,并且我还尝试在Heroku上使用mLab MongoDB ,但存在相同的错误:

11:17:05 AM web.1 |  SLF4J: Class path contains multiple SLF4J bindings.
11:17:05 AM web.1 |  SLF4J: Found binding in [jar:file:/Users/zhugejunwei/full-sta…
11:17:05 AM web.1 |  SLF4J: Found binding in [jar:file:/Users/zhugejunwei/full-sta…
11:17:05 AM web.1 |  SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings …
11:17:05 AM web.1 |  SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerF…
11:17:05 AM web.1 |  Exception in thread "main" java.lang.NullPointerException
11:17:05 AM web.1 |     at com.mongodb.ConnectionString.<init>(ConnectionString.java…
11:17:05 AM web.1 |     at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:176…
11:17:05 AM web.1 |     at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:158…
11:17:05 AM web.1 |     at Main.main(Main.java:28)

From the original version to my current version, I really tried a lot, and I just want to make it runnable on Heroku to learn this project in another way. 原始版本到当前版本,我确实做了很多尝试,并且我只想使其在Heroku上可运行以通过另一种方式学习此项目。

When I learnt another tutorial, the code below is ok, but I don't know why it doesn't work in this project. 当我学习了另一个教程时,下面的代码还可以,但是我不知道为什么它在该项目中不起作用。

MongoURI mongoURI = new MongoURI(System.getenv("MONGOHQ_URL"));
//get connected
DB db = mongoURI.connectDB();
mongoURI.getPassword());
MongoCredential credential = MongoCredential.createCredential(mongoURI.getUsername(), mongoURI.getDatabase(), mongoURI.getPassword());
MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));

I also opened an issue on github and on jira 我还在githubjira上打开了一个问题

Please help me with this issue. 请帮我解决这个问题。 Thanks in advance. 提前致谢。

I tried for more than 40 hours, and finally get it worked. 我尝试了40多个小时,终于成功了。

I changed a lot. 我改变了很多。 Here are something I think is important: 我认为这很重要:

First, it seems I cannot use DB db = mongoURI.connectDB(); 首先,看来我不能使用DB db = mongoURI.connectDB(); , maybe because of the 3.2.2 java-driver version or because of the MongoDB version on Heroku. ,可能是因为3.2.2 java驱动程序版本,还是因为Heroku上的MongoDB版本 When I use " MongoDatabase " instead of " DB ", some errors disappeared, but still cannot run the project. 当我使用“ MongoDatabase ”代替“ DB ”时, 一些错误消失了,但仍然无法运行项目。 Because I have to changed a lot in my project wherever used "DB", and this is very difficult to change from DB to MongoDatabase . 因为无论在哪里使用“ DB”,我都必须在项目中进行很多更改,而从DB更改为MongoDatabase

I realized that I can use DB in 2.7.2 , so I changed all the syntaxes related to the mongo-java-driver version. 我意识到可以在2.7.2中使用DB ,因此我更改了与mongo-java-driver版本有关的所有语法。 But again, errors disappeared, I cannot run the project. 但是同样,错误消失了,我无法运行该项目。 However, at this time, it seems most errors disappeared. 但是,这时似乎大多数错误都消失了。 One more error showed that another process is running on the same port. 另一个错误表明另一个进程正在同一端口上运行。 I killed that process. 我杀死了那个过程。 Now all errors gone. 现在所有错误都消失了。 When I run it, I cannot open it on Heroku, but on port 4567. This is weird, because the default port for Heroku is 5000, and 4567 is the default port for Jetty, which is a embedded server for Java application. 当我运行它时,无法在Heroku上打开它,而无法在端口4567上打开它。这很奇怪,因为Heroku的默认端口为5000,而4567是Jetty的默认端口,Jetty是Java应用程序的嵌入式服务器。 I added this piece of code, and it finally worked. 我添加了这段代码,终于奏效了。

port(Integer.valueOf(System.getenv("PORT")));

Thanks to my professor and thanks to all the people that helped me. 感谢我的教授,也感谢所有帮助我的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM