简体   繁体   English

如何在 AWS EC2 上自动启动 2 个 Java jar?

[英]How to automatically start 2 Java jars on AWS EC2?

I'm learning to deploy Spring Boot apps on AWS EC2.我正在学习在 AWS EC2 上部署 Spring Boot 应用程序。 And I know how to automate app launch, when I start the EC2 instance, I don't need to manually use the command java -jar java-service.jar, I just add this command in the /etc/rc.local file and that is all.而且我知道如何自动启动应用程序,当我启动 EC2 实例时,我不需要手动使用命令 java -jar java-service.jar,我只需将此命令添加到 /etc/rc.local 文件中并就这些。 But I have 2 microservice, and I want to start both of them automatically, but if I try to add both commands in the /etc/rc.local it's not working, only the first service will start, the second service will not start.但是我有 2 个微服务,我想自动启动它们,但是如果我尝试在 /etc/rc.local 中添加这两个命令,它不起作用,只有第一个服务会启动,第二个服务不会启动。 So I have the commands added like this:所以我添加了这样的命令:

在此处输入图片说明

And after I start the EC2 instance only the first service is started:在我启动 EC2 实例后,只启动了第一个服务:

在此处输入图片说明

Thank you!谢谢!

I recommend to use Systemd.我建议使用 Systemd。 Create a Systemd unit file for every microservice, save it in /etc/systemd/system/my-app.service .为每个微服务创建一个 Systemd 单元文件,将其保存在/etc/systemd/system/my-app.service Something like that:类似的东西:

[Unit]
Description=My Java app
After=syslog.target network.target

[Service]
EnvironmentFile=/etc/sysconfig/my-app-env
WorkingDirectory=/my/app/home
ExecStart=/usr/bin/java $JAVA_OPTS -jar my-app.jar
KillMode=process
User=my-app-user
Restart=on-failure

[Install]
WantedBy=multi-user.target

Then, run:然后,运行:

systemctl daemon-reload
systemctl enable --now my-app

After that, you can use:之后,您可以使用:

systemctl status my-app
systemctl stop my-app
systemctl start my-app

I am not a unix expert, but I see the only issue in running 2 java commands from terminal is that unless the first command returns, the next command is not executed.我不是 unix 专家,但我看到从终端运行 2 个 java 命令的唯一问题是,除非第一个命令返回,否则不会执行下一个命令。 So, I think the solution would be run the 1st command in some interactive mode so that the other commands can be executed simultaneously.所以,我认为解决方案是在某种交互模式下运行第一个命令,以便其他命令可以同时执行。

There are ways in unix shell to run a command in background.在 unix shell 中有一些方法可以在后台运行命令。 I found this useful link - https://www.maketecheasier.com/run-bash-commands-background-linux/我找到了这个有用的链接 - https://www.maketecheasier.com/run-bash-commands-background-linux/

In bash terminal, a command can be made to run in background by appending it with & .在 bash 终端中,可以通过附加&使命令在后台运行。 So, I think you should be able to start both jars if you do something like -因此,我认为如果您执行以下操作,您应该能够启动两个罐子 -

java -jar /home/ec2-user/first.jar &
java -jar /home/ec2-user/second.jar

I am not sure which system you are using in starting application: For linux base system, you can use crontab to schedule the task when the server reboot.我不确定您在启动应用程序时使用的是哪个系统:对于 linux 基本系统,您可以使用 crontab 在服务器重新启动时安排任务。 Follow this steps:请按照以下步骤操作:

  1. Download crontab下载 crontab

    #apt-get install cron #apt-get 安装 cron

  2. Edit the file file to enable the task编辑文件文件以启用任务

    crontab -e crontab -e

    (Choose Vim or nano to edit the task) (选择 Vim 或 nano 来编辑任务)

  3. Add this code to your server将此代码添加到您的服务器

    @reboot /usr/bin/java -jar XXXXX.jar @reboot /usr/bin/java -jar XXXXX.jar

  4. Save your file保存您的文件

  5. Check the result检查结果

    crontab -l #systemctl status cron crontab -l #systemctl status cron

This method works in my Debian system.这种方法适用于我的 Debian 系统。 For more details, you can refer to How to automatically run program on Linux startup更多细节可以参考如何在Linux启动时自动运行程序

Another solution is to bundle your jars into Docker images.另一种解决方案是将您的 jars 捆绑到 Docker 镜像中。 This of course requires Docker runtime and adds an overhead, but it also has some benefits:这当然需要 Docker 运行时并增加开销,但它也有一些好处:

  • Complete separation of jar files.完全分离jar文件。 Easily use different java versions.轻松使用不同的 Java 版本。
  • No need to worry about differences of local and ec2 environment.无需担心本地和 ec2 环境的差异。
  • Easily scale to 3 or more jars.轻松扩展到 3 个或更多罐子。
  • Use Docker Cli to build and start containers.使用 Docker Cli 构建和启动容器。 Works great in a Devops Pipeline.在 Devops 管道中工作得很好。

You can read here to learn how to create Spring Boot Docker images.您可以在此处阅读以了解如何创建 Spring Boot Docker 映像。 After you build an image.构建镜像后。 You start it like this.:你这样开始:

docker run -p 8080:8080 springio/gs-spring-boot-docker

You can run as many docker run commands you need, one after another.您可以一个接一个地运行任意数量的 docker run 命令。

If you are running from bash, then command separator is semicolon ";"如果您从 bash 运行,则命令分隔符是分号“;” so you need to quote the value.所以你需要引用这个值。

java -jar "/home/ec2-user/first.jar;/home/ec2-user/second.jar"

coupon service优惠券服务

  1. Run the command 'java -jar /home/ec2-user/coupon-service-0.0.1-SNAPSHOT.JAR'运行命令'java -jar /home/ec2-user/coupon-service-0.0.1-SNAPSHOT.JAR'
  2. Press CTRL+Z, type bg, press Enter, type disown, press Enter.按 CTRL+Z,键入 bg,按 Enter,键入 disown,按 Enter。

product service产品服务

  1. Run the command 'java -jar /home/ec2-user/product-service-0.0.1-SNAPSHOT.JAR'运行命令'java -jar /home/ec2-user/product-service-0.0.1-SNAPSHOT.JAR'
  2. Press CTRL+Z, type bg, press Enter, type disown, press Enter.按 CTRL+Z,键入 bg,按 Enter,键入 disown,按 Enter。

NOTE : Both services should have different ports.注意:这两个服务应该有不同的端口。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM