You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何为Spring Boot项目配置Google Cloud Compute Engine及主机购买与部署问题

Hey there! Let's break down everything you need to know about getting your Spring Boot app up and running on Google Cloud Compute Engine (GCE) — I’ve walked through this process dozens of times, so I’ll make it as straightforward as possible for you.

Google Cloud Compute Engine: Instance Creation & Pricing Breakdown

1. Step-by-Step to Create a GCE Instance

  • First, log into the Google Cloud Console, navigate to Compute Engine > VM Instances, and hit the "Create Instance" button.
  • Fill in the basic configs (I’ll stick to beginner-friendly defaults here):
    • Name: Pick something memorable, like spring-boot-dev-server
    • Region/Zone: Choose one close to your target users (e.g., us-central1-a for low latency in North America)
    • Machine Configuration: Start with an e2-micro instance — this falls under GCP’s free tier, perfect for testing.
    • Boot Disk: Go with Ubuntu 22.04 LTS (it’s widely used, has tons of support, and is easy to work with). The default 10GB disk is more than enough for a small Spring Boot app.
    • Firewall: Don’t skip this! Check both "Allow HTTP traffic" and "Allow HTTPS traffic" so external users can reach your app.
  • Click "Create" and wait 2-3 minutes — your instance will spin up, and you’ll see its public IP in the VM Instances list once it’s ready.

2. Free Tier vs. Paid Usage

  • Free Tier: New GCP users get a 12-month free trial, which includes 1 fully operational e2-micro instance (in specific regions) with 750 hours of monthly runtime (that’s 24/7 usage for a month). You also get free disk space and limited network bandwidth. As long as you stay within these limits, you won’t pay a dime.
  • Paid Usage: If you need more power (bigger CPU/RAM), additional instances, or exceed the free tier limits, you’ll pay per-second usage rates. The good news is you can stop instances when you’re not using them to avoid unnecessary charges — billing stops as soon as the instance is halted.
Deploying Your Spring Boot App to GCE

1. Prep Your Local Project

  • Make sure your app runs smoothly locally first. Then package it into an executable JAR:
    • For Maven: Run mvn clean package in your project root — the JAR will end up in the target/ folder.
    • For Gradle: Run ./gradlew bootJar — check the build/libs/ folder for the JAR.

2. Connect to Your GCE Instance

  • The easiest way is to use the built-in SSH tool in the GCP Console: Just click the "SSH" button next to your instance, and a browser-based terminal will pop up.
  • If you prefer using your local terminal, run ssh your-username@your-instance-public-ip (replace with your GCP username and the instance’s public IP from the console).

3. Set Up the Environment on GCE

  • Install Java (Spring Boot needs a JRE to run):
    sudo apt update && sudo apt install openjdk-17-jre -y
    
    (Use openjdk-8-jre or openjdk-11-jre if you’re still on Spring Boot 2.x — it’s compatible either way.)
  • Verify the installation with java -version — you should see the Java version printed out.

4. Upload Your Spring Boot JAR

  • Option 1: Use scp to transfer the JAR from your local machine to GCE:
    scp path/to/your-app.jar your-username@your-instance-public-ip:/home/your-username/
    
  • Option 2: If your code is on GitHub, you can clone the repo directly on GCE and build the JAR there — just install Maven/Gradle first with sudo apt install maven -y or sudo apt install gradle -y.

5. Run the App

  • Test it first with a direct run:
    java -jar your-app.jar
    
    Wait for the Spring Boot startup logs — if you see "Started Application in X seconds", you’re good to go.
  • To keep the app running even after you close the SSH terminal, use nohup (it runs the process in the background):
    nohup java -jar your-app.jar &
    
    Startup logs will be saved to nohup.out if you need to debug later.
  • Check if it’s accessible: Open your browser and go to http://your-instance-public-ip:8080 (8080 is Spring Boot’s default port). If your app loads, you’ve successfully deployed!

Reverse Proxy with Nginx (No More Port Numbers!)

  • Install Nginx:
    sudo apt install nginx -y
    
  • Edit the default Nginx config:
    sudo nano /etc/nginx/sites-available/default
    
  • Replace the location / block with this:
    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    
  • Save and exit (Ctrl+O, Enter, then Ctrl+X), then restart Nginx:
    sudo systemctl restart nginx
    
    Now you can access your app at http://your-instance-public-ip without adding :8080.

Auto-Start on Instance Reboot

  • Create a systemd service file to ensure your app starts automatically if the instance restarts:
    sudo nano /etc/systemd/system/spring-boot-app.service
    
  • Paste this content (replace your-username and the JAR path with your details):
    [Unit]
    Description=My Spring Boot Application
    After=network.target
    
    [Service]
    User=your-username
    ExecStart=/usr/bin/java -jar /home/your-username/your-app.jar
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
  • Enable and start the service:
    sudo systemctl daemon-reload
    sudo systemctl enable spring-boot-app.service
    sudo systemctl start spring-boot-app.service
    
    Now your app will start up automatically whenever the instance boots.

内容的提问来源于stack exchange,提问作者Vijay Nathan

火山引擎 最新活动