简体   繁体   English

如何在gradle Springboot REST应用程序中将heroku连接到mongodb?

[英]How to connect heroku to mongodb in gradle Springboot REST app?

I followed this guide to set up a gradle Springboot service with mongoDB, then followed the deployment instructions for heroku. 我按照本指南使用mongoDB设置了gradle Springboot服务,然后按照heroku的部署说明进行操作。 I also added the mongoLab addOn. 我还添加了mongoLab addOn。 It works perfectly on localhost, but when database parts are reached on heroku, the responses stall indefinitely. 它在localhost上可以完美运行,但是在heroku上访问数据库部件时,响应将无限期停止。 When I remove the mongoDB lines and re-deploy, the responses work perfectly. 当我删除mongoDB行并重新部署时,响应工作正常。 How can I deploy this app to heroku with mongoDB working? 如何在mongoDB工作的情况下将此应用程序部署到heroku?

EDIT: this is the error from heroku logs 编辑:这是来自Heroku日志的错误

org.mongodb.driver.cluster : No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]}. Waiting for 30000 ms before timing out

I had to add a mongoLab configuration. 我必须添加mongoLab配置。

@Configuration
public class MongoLabConfiguration {

  public @Bean MongoDbFactory mongoDbFactory() throws MongoException, UnknownHostException {
    return new SimpleMongoDbFactory(new MongoClientURI(System.getenv("MONGODB_URI")));
  }
  public @Bean
    MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
  }
}

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

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