如何在 Google Cloud Compute Engine 中部署 Spring Boot 应用程序?

How to deploy Spring Boot application in Google Cloud Compute Engine?

我是 Google Cloud platform 的新手。我想在 Compute Engine 中部署我的 Spring Boot 项目,因为在 App Engine 中部署比在 Compute Engine 中部署成本更高。 YouTube/Websites 中有很多 videos/articles 可用于在 App Engine 中部署,但我没有找到任何有关在 Compute Engine 中部署 Spring 启动应用程序的教程。

这是 How to deploy Spring Boot app in Google Cloud Compute Engine with embedded tomcat? 上写的非常好的博客。我只是简单介绍一下。

If you want to use an embedded tomcat server then PM2 is the best tool for managing the deployment in Compute Engine. PM2 is a process manager for the JavaScript runtime Node.js. Actually without PM2 also you can deploy Spring Boot app directly by executing command mvn spring--boot:run but the problem is when you exit the terminal then your server will also go down. Here you can use excute the command in background by using setsid mvn spring-boot:run. This will execute your Spring Boot app in background but when you need to restart the server then you will the error like Web server failed to start. Port 8080 was already in use

因此您需要通过查找 processId 来终止现有的 运行 应用程序。这有点让人头疼。因此,我也会推荐你使用 PM2。要安装 PM2,您可以在 ubuntu.

中使用这些命令
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install nodejs
sudo npm install -g pm2

如果你想测试你的 Spring 引导应用程序,你可以从你的 Github 存储库克隆或拉取项目,或者你可以克隆 spring-boot -test 从这里开始的项目。

git clone https://github.com/altafjava/spring-boot-test.git
cd spring-boot-test/

您必须创建一个 .sh 文件,PM2 将使用该文件重新启动服务器。在您的 .sh 文件中,您需要编写用于执行 spring 引导项目的命令,它不过是 mvn spring-boot:run

echo "mvn spring-boot:run">server.sh
chmod +x server.sh

最后,使用命令pm2 restart server重启服务器。您甚至可以使用 pm2 logs.

检查运行时日志