将带有JPA的Spring Boot应用程序部署到Heroku

时间:2018-03-18 18:44:57

标签: heroku amazon-rds

我有一个Spring Boot 1.5.1应用程序,我使用heroku-maven-plugin进行部署:

<configuration>
    <appName>${heroku.appName}</appName>
    <includeTarget>false</includeTarget>
    <includes>
        <include>${basedir}/${heroku.fullArtifactName}</include>
    </includes>
    <processTypes>
        <web>java $JAVA_OPTS -Dserver.port=$PORT -jar ${heroku.fullArtifactName}</web>
    </processTypes>
</configuration>

我可以看到Heroku从日志填充到端口:

2018-03-18T18:16:25.966746+00:00 heroku[web.1]: Starting process with command `java $JAVA_OPTS -Dserver.port=42658 -jar target/be-0.0.1-SNAPSHOT.jar

初始化应用程序最后会出现如下所示的错误:

2018-03-18 18:18:26.046 DEBUG 4 --- [           main] o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://app/db?autoReconnect=true&useSSL=false]
2018-03-18 18:18:26.048 DEBUG 4 --- [           main] o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from org.springframework.boot.loader.LaunchedURLClassLoader@72ea2f77
2018-03-18T18:19:32.196190+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch

事情在我的本地环境中运行(通过Intellij IDEA运行),对比日志:

2018-03-18 20:31:10.284 DEBUG --- o.s.jdbc.datasource.DataSourceUtils      : Fetching JDBC Connection from DataSource
2018-03-18 20:31:10.289 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:10.290 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:11.900 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:11.900 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:13.257 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:13.257 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:14.633 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:14.634 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:15.991 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:15.991 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:17.344 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:17.345 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:18.692 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:18.693 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:20.068 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:20.069 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:21.388 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:21.388 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:22.751 DEBUG --- o.a.tomcat.jdbc.pool.PooledConnection    : Instantiating driver using class: com.mysql.jdbc.Driver [url=jdbc:mysql://url/db?autoReconnect=true&useSSL=false]
2018-03-18 20:31:22.752 DEBUG --- o.a.tomcat.jdbc.pool.ClassLoaderUtil     : Attempting to load class[com.mysql.jdbc.Driver] from sun.misc.Launcher$AppClassLoader@14dad5dc
2018-03-18 20:31:24.113 DEBUG --- o.s.jdbc.datasource.DataSourceUtils      : Returning JDBC Connection to DataSource
2018-03-18 20:31:24.168  INFO --- j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-03-18 20:31:24.179 DEBUG --- o.hibernate.jpa.internal.util.LogHelper  : PersistenceUnitInfo [

在本地获取JDBC连接以及加载驱动程序类的尝试需要花费一些时间,但连接工作正常。

配置在本地和Heroku中似乎并没有什么不同,我的调试工作也没有产生太大的影响。也许有人知道我应该在哪里引起我的注意?

编辑: 我使用Amazon RDS作为数据库,我认为这可能是一个访问问题(不知道为什么我可以在本地访问数据库,但...)。如果是这种情况,将很快检查。

1 个答案:

答案 0 :(得分:0)

你有一个端口问题,你没有绑定到Heroku期望你的端口,想要获取你的Procfile中的$PORT值,并将它配置为你的-Dserver.port

web:

中添加此Procfile
-Dserver.port=$PORT