简体   繁体   English

使用Heroku,如何在部署应用程序时自动启动未绑定端口的Java应用程序?

[英]With Heroku, how can I automatically start a Java app that doesn't bind to a port when the app is deployed?

I have a Java application (packaged as a JAR) that interacts with a chat program (Slack) via websockets. 我有一个Java应用程序(打包为JAR),可通过websocket与聊天程序(Slack)进行交互。 As far as I understand it, my app doesn't need to bind to a specific port in order to work - it's just connecting to Slack's Real Time Messaging API. 据我了解,我的应用程序无需绑定到特定端口即可工作-只需连接到Slack的Real Time Messaging API。 It's not acting as a web application or a web server. 它不充当Web应用程序或Web服务器。 It's not listening on any ports for incoming requests because it doesn't need to. 它不需要监听任何端口上的传入请求,因为它不需要监听。 I want someone to be able to click on the "Deploy to Heroku" button for my application and I want the Java application to run as soon as the app is deployed, without a user having to manually turn on the process that starts the Java app. 我希望某人能够为我的应用程序单击“部署到Heroku”按钮,并且希望Java应用程序在部署该应用程序后立即运行,而无需用户手动打开启动Java应用程序的过程。

I've tried using "web" as the process type that starts my Java app (java -jar ...), but, as you can probably guess, since my app uses websockets, the app stops running after 60 seconds because Heroku detects that it failed to bind to a port. 我尝试使用“ web”作为启动我的Java应用程序的进程类型(java -jar ...),但是,您可能猜到了,因为我的应用程序使用了websockets,所以该应用程序在60秒后停止运行,因为Heroku检测到它无法绑定到端口。

I've tried using a different process name like "bot" as the process type, but, after a period of time, I get a "No web process running" error because no web process is running. 我尝试使用其他类型的进程名称(例如“ bot”)作为进程类型,但是一段时间后,由于没有Web进程在运行,因此出现“ No web process running”错误。 Also, in that situation, a user has to manually start the "bot" process using the Heroku website or a command line tool. 同样,在这种情况下,用户必须使用Heroku网站或命令行工具手动启动“机器人”过程。

Is there a way I can set it up so it doesn't complain that I haven't bound to aa port and that there isn't a web process, but that it also starts automatically when someone deploys it? 有没有一种我可以设置的方法,所以它不会抱怨我没有绑定到端口并且没有Web进程,但是它在有人部署时也会自动启动?

EDIT: I've discovered that I can get rid of the "No web process running" error by telling it to not expect any web dynos. 编辑:我发现我可以通过告诉它不要期望任何Web动态来摆脱“没有运行Web进程”错误。 I can even use an undocumented app.json property to achieve this: 我什至可以使用未记录的app.json属性来实现此目的:

"formation": [
    { "process": "web",    "quantity": 0},
    { "process": "bot", "quantity": 1}
 ]

However, they still have to manually start the bot process. 但是,他们仍然必须手动启动Bot进程。 Also I'd prefer not using an undocumented property. 我也不想使用未记录的属性。 If I don't use it, they have to use the command line to modify the app after it is deployed. 如果我不使用它,则在部署应用程序后,他们必须使用命令行来修改该应用程序。 Still haven't found a way to get it to start automatically. 仍未找到使它自动启动的方法。

This is a bit of a hack, but you could use the "web" proc type and start a simple web server alongside your Java process. 这有点麻烦,但是您可以使用“ web” proc类型并在Java进程旁边启动一个简单的Web服务器。 For example: 例如:

web: sh start.sh

And then add a start.sh script to your project like this: 然后将start.sh脚本添加到您的项目中,如下所示:

ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => ENV["PORT"], :DocumentRoot => Dir.pwd).start' &

java -jar myapp.jar

Note the "&" after the ruby command 在ruby命令后注意“&”

In your Procfile, just name the process type something other than "web". 在您的Procfile中,只需将进程类型命名为“ web”以外的其他名称。 For example: 例如:

worker: java -jar myapp.jar

Then scale it up accordingly: 然后相应地扩大规模:

heroku ps:scale worker=1

Only "web" dynos are required to bind to a port. 仅需要“网络”测功机才能绑定到端口。

You can also scale it up from the dashboard. 您也可以从仪表板放大它。

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

相关问题 Gradle Java Web应用程序无法在Heroku上启动 - Gradle java web app doesn't start on heroku Heroku java 应用程序崩溃:Web 进程未能绑定到 $PORT - Heroku java app crashes: Web process failed to bind to $PORT 使用Java Web start部署应用程序时,应用程序为全屏显示(不需要) - When app deployed with Java Web start, App is full screen ( not desired ) 如何在PC启动时自动启动Java应用程序? - How to make a Java app automatically start when PC starts? 停止时自动启动Java应用程序 - Automatically start java app when it stops 如何在heroku中部署的Java应用程序应用程序中添加新的遗物插件 - How may I add new relic addon in a java application app deployed in heroku 如何为我在Google App Engine上部署的应用程序的Chrome插件绑定插件令牌 - How can I bind plugin token with my chrome plugin for the application deployed on Google App Engine MySql在已部署的Java Web应用程序中不起作用 - MySql doesn't work from deployed java web app Heroku。 Java。 找不到资源。 我部署的应用程序在资源文件夹中找不到文件 - Heroku. Java. Unable to find resource. My deployed app can't find file in resource folder 如何将Java Web应用程序部署到heroku? - How can I deploy a java web app to heroku?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM