我遵循此guide与mongoDB一起设置了gradle Springboot服务,然后遵循deployment instructions来实现heroku。我还添加了mongoLab addOn。它在localhost上可以完美运行,但是在heroku上访问数据库部件时,响应将无限期停止。当我删除mongoDB行并重新部署时,响应工作正常。如何在mongoDB工作的情况下将此应用程序部署到heroku?

编辑:这是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

最佳答案

我必须添加一个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());
  }
}

09-17 07:50