我在Heroku中部署了一个需要MongoDB数据库的Spring Boot应用程序(Gradle buildback) . 基于Spring Boot完成的MongoDB自动配置,它在开发时连接到 localhost:27017 . 模式,这是正确的 . 但是,它会在部署到Heroku之后尝试执行相同操作,如日志所示:

2018-04-03T22:19:35.444076+00:00 app[web.1]: Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vehicles' defined in URL [jar:file:/app/build/libs/scraper-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/test/en
tities/Vehicles.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vehicleRepositoryImpl' defined in URL [jar:file:/app/build/li
bs/scraper-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/test/repo/VehicleRepositoryImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to
instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting to connect. Client view of cluster
 state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]; nested exception i
s com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening
 socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]

...尽管我配置了Heroku配置 . 像这样的变种(对外部mLab实例):

enter image description here

Heroku使用默认的 Procfile 指令,正确启动应用程序:

web: java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/*.jar

他应该按official docs中的描述阅读those config. vars.,因此连接到mLab而不是localhost(Spring Boot默认行为) . 有趣的是它正确地从Heroku配置中读取Spring活动配置文件 . VAR:

2018-04-03T22:59:10.790581+00:00 app[web.1]: 2018-04-03 22:59:10.790  INFO 4 --- [           main] com.test.ExplorerApplicationKt    : The following profiles are active: production

谢谢