Heroku 环境导致 Java/Nashorn/ReactJS 应用程序崩溃
Heroku environment causing Java/Nashorn/ReactJS application to crash
Github 回购 https://github.com/damorton/dropwizardheroku-webgateway
建立./gradlew stage
Heroku 应用程序在构建和 webpack 阶段后使用 heroku local
在本地运行良好,但在 Heroku 环境中崩溃,因为应用程序无法读取构建的 webpack 阶段生成的 bundle.js
文件。
到目前为止完成
- 使用
heroku run bash
登录到 Heroku 应用程序并检查是否正在创建 bundle.js
文件,确实是。
- 已更新
system.properties
以使用与我的本地环境类似的 Java 版本 java.runtime.version=1.8.0_131
Heroku 日志
Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2017-05-05T10:55:43.963570+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx350m -Xss512k -Dfile.encoding=UTF-8
2017-05-05T10:55:45.617664+00:00 app[web.1]: INFO [2017-05-05 10:55:45,613] org.eclipse.jetty.util.log: Logging initialized @1649ms
2017-05-05T10:55:45.696063+00:00 app[web.1]: INFO [2017-05-05 10:55:45,695] io.dropwizard.server.SimpleServerFactory: Registering jersey handler with root path prefix: /
2017-05-05T10:55:45.696110+00:00 app[web.1]: INFO [2017-05-05 10:55:45,695] io.dropwizard.server.SimpleServerFactory: Registering admin handler with root path prefix: /admin
2017-05-05T10:55:45.696230+00:00 app[web.1]: INFO [2017-05-05 10:55:45,696] io.dropwizard.assets.AssetsBundle: Registering AssetBundle with name: assets for path /assets/*
2017-05-05T10:55:59.241010+00:00 app[web.1]: InputString in is null with path assets/js/bundle.js
2017-05-05T10:55:59.243502+00:00 app[web.1]: Exception in thread "main" java.lang.NullPointerException
2017-05-05T10:55:59.243739+00:00 app[web.1]: at java.io.Reader.<init>(Reader.java:78)
2017-05-05T10:55:59.243867+00:00 app[web.1]: at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
2017-05-05T10:55:59.243967+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.api.NashornController.read(NashornController.java:46)
2017-05-05T10:55:59.244023+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.api.NashornController.<init>(NashornController.java:27)
2017-05-05T10:55:59.244096+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.Main.run(Main.java:43)
2017-05-05T10:55:59.244149+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.Main.run(Main.java:23)
2017-05-05T10:55:59.244212+00:00 app[web.1]: at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43)
2017-05-05T10:55:59.244255+00:00 app[web.1]: at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
2017-05-05T10:55:59.244306+00:00 app[web.1]: at io.dropwizard.cli.Cli.run(Cli.java:75)
2017-05-05T10:55:59.244391+00:00 app[web.1]: at io.dropwizard.Application.run(Application.java:79)
2017-05-05T10:55:59.244436+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.Main.main(Main.java:26)
根据日志,应用程序正在抛出 NullPointerException
这是由 NashornController.java
中的 Nashorn 引擎评估从 bundle.js
文件的 InputStream 创建的 Reader
引起的。
我想不通的是为什么它在本地工作正常,但在 Heroku 环境中崩溃。如果我不创建 bundle.js
文件并评估添加到捆绑包中的 Java 脚本源代码,则一切正常。
问题
如果不使用 Spring Boot 或 Ratpack,Heroku 将 运行 ./gradlew stage
,这意味着开发人员需要在 stage
任务 运行 由平台。 stage
依赖的任务不是 运行 任何预定义的顺序。这导致 webpack
任务在 installDist
之后成为 运行,在 bundle.js
存在之前构建 Jar。
解决方案
定义任务的顺序 运行。
task stage(dependsOn: ['installDist', 'webpack', 'npmInstall', 'clean'])
npmInstall.mustRunAfter clean
webpack.mustRunAfter npmInstall
installDist.mustRunAfter webpack
Github 回购 https://github.com/damorton/dropwizardheroku-webgateway
建立./gradlew stage
Heroku 应用程序在构建和 webpack 阶段后使用 heroku local
在本地运行良好,但在 Heroku 环境中崩溃,因为应用程序无法读取构建的 webpack 阶段生成的 bundle.js
文件。
到目前为止完成
- 使用
heroku run bash
登录到 Heroku 应用程序并检查是否正在创建bundle.js
文件,确实是。 - 已更新
system.properties
以使用与我的本地环境类似的 Java 版本java.runtime.version=1.8.0_131
Heroku 日志
Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2017-05-05T10:55:43.963570+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx350m -Xss512k -Dfile.encoding=UTF-8
2017-05-05T10:55:45.617664+00:00 app[web.1]: INFO [2017-05-05 10:55:45,613] org.eclipse.jetty.util.log: Logging initialized @1649ms
2017-05-05T10:55:45.696063+00:00 app[web.1]: INFO [2017-05-05 10:55:45,695] io.dropwizard.server.SimpleServerFactory: Registering jersey handler with root path prefix: /
2017-05-05T10:55:45.696110+00:00 app[web.1]: INFO [2017-05-05 10:55:45,695] io.dropwizard.server.SimpleServerFactory: Registering admin handler with root path prefix: /admin
2017-05-05T10:55:45.696230+00:00 app[web.1]: INFO [2017-05-05 10:55:45,696] io.dropwizard.assets.AssetsBundle: Registering AssetBundle with name: assets for path /assets/*
2017-05-05T10:55:59.241010+00:00 app[web.1]: InputString in is null with path assets/js/bundle.js
2017-05-05T10:55:59.243502+00:00 app[web.1]: Exception in thread "main" java.lang.NullPointerException
2017-05-05T10:55:59.243739+00:00 app[web.1]: at java.io.Reader.<init>(Reader.java:78)
2017-05-05T10:55:59.243867+00:00 app[web.1]: at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
2017-05-05T10:55:59.243967+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.api.NashornController.read(NashornController.java:46)
2017-05-05T10:55:59.244023+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.api.NashornController.<init>(NashornController.java:27)
2017-05-05T10:55:59.244096+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.Main.run(Main.java:43)
2017-05-05T10:55:59.244149+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.Main.run(Main.java:23)
2017-05-05T10:55:59.244212+00:00 app[web.1]: at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43)
2017-05-05T10:55:59.244255+00:00 app[web.1]: at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
2017-05-05T10:55:59.244306+00:00 app[web.1]: at io.dropwizard.cli.Cli.run(Cli.java:75)
2017-05-05T10:55:59.244391+00:00 app[web.1]: at io.dropwizard.Application.run(Application.java:79)
2017-05-05T10:55:59.244436+00:00 app[web.1]: at com.bitbosh.dropwizardheroku.webgateway.Main.main(Main.java:26)
根据日志,应用程序正在抛出 NullPointerException
这是由 NashornController.java
中的 Nashorn 引擎评估从 bundle.js
文件的 InputStream 创建的 Reader
引起的。
我想不通的是为什么它在本地工作正常,但在 Heroku 环境中崩溃。如果我不创建 bundle.js
文件并评估添加到捆绑包中的 Java 脚本源代码,则一切正常。
问题
如果不使用 Spring Boot 或 Ratpack,Heroku 将 运行 ./gradlew stage
,这意味着开发人员需要在 stage
任务 运行 由平台。 stage
依赖的任务不是 运行 任何预定义的顺序。这导致 webpack
任务在 installDist
之后成为 运行,在 bundle.js
存在之前构建 Jar。
解决方案
定义任务的顺序 运行。
task stage(dependsOn: ['installDist', 'webpack', 'npmInstall', 'clean'])
npmInstall.mustRunAfter clean
webpack.mustRunAfter npmInstall
installDist.mustRunAfter webpack