[GO] Java --Deploy Spring Boot project to GAE

Constitution

This time, create the following configurations respectively

--Java 8 standard environment --Java 8 flexible environment

Premise

--Java SE 8 SDK and Java SE 11 SDK must be installed. --Maven installation is complete --GCP can be used (free trial is OK) --Google Cloud SDK settings are complete

Please refer to it if you like because it is summarized separately: relaxed: GCP-How to get started with a free trial GCP --Install Google Cloud SDK

Preparation of development environment

First, prepare the development environment. The supported development environment is IntelliJ IDEA, Eclipse (STS), and the build tools are Apache Maven and Gradle.

Install App Engine components

By including the AppEngine component, the local execution environment and deploy commands will be installed.

gcloud components install app-engine-java

Install the IDE plugin

IntelliJ IDEA and Eclipse (STS) have different installation methods, so refer to here. Please.

This time I will use STS, so I installed it from [Help]> [Eclipse Marketplace].

スクリーンショット 2019-11-20 13.22.59.png

You can now create an App Engine Project from the IDE. (Not used this time)

Creating a Spring Boot project

Create the original Spring Boot project. It's just a Hello World project, so use whatever you like.

New> Spring Starter Project

Create Hello World from.

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HajibootGcpApplication {

	@GetMapping("/")
	String home() {
		return "Hello world!";
	}

	public static void main(String[] args) {
		SpringApplication.run(HajibootGcpApplication.class, args);
	}

}

Start it locally and check that it works fine.

Java 8 standard environment

Refer to the document here. Create a dedicated GCP project in advance.

Make the following modifications to the Spring Boot project.

--Change to App Engine Project --Changed packaging method to war --Addition of appengine-maven-plugin --Check operation locally --Project settings and App Engine app initialization --Deploy

Change to App Engine Project

Right-click on the project and select Convert to App Engine Standard Project. Then the following files will be generated. (I wonder if I should create this file with other IDEs ...?)

/src/main/webapp/WEB-INF/appengine-web.xml


<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

  <threadsafe>true</threadsafe>
  <sessions-enabled>false</sessions-enabled>
  <runtime>java8</runtime>

  <system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
  </system-properties>

</appengine-web-app>

Changed packaging method to war

Add the following to pom.xml and change the packaging method to war.

pom.xml


<packaging>war</packaging>

The implementation of the main class is also changed as follows.

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
//Extends SpringBootServletInitializer
public class HajibootGcpGaeSe8Application extends SpringBootServletInitializer {

	@GetMapping("/")
	String home() {
		return "Hello world!";
	}

	//Override the configure method
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(HajibootGcpGaeSe8Application.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(HajibootGcpGaeSe8Application.class, args);
	}

}

Add appengine-maven-plugin

Add the following plugin to pom.xml. Specify PROJECT_ID and VERSION respectively.

pom.xml


<plugin>
	<groupId>com.google.cloud.tools</groupId>
	<artifactId>appengine-maven-plugin</artifactId>
	<version>2.2.0</version>
	<configuration>
		<deploy.projectId>${PROJECT_ID}</deploy.projectId>
		<deploy.version>${VERSION}</deploy.version>
	</configuration>
</plugin>

Confirmed operation locally

Start it with the following command and access http: // localhost: 8080 to check the operation.

mvn spring-boot:run

Project settings and App Engine app initialization

Use the following commands to set up the project and initialize the App Engine app.

##Project settings
gcloud config set project YOUR_PROJECT_ID

##App Engine App Initialization
gcloud app create --project=YOUR_PROJECT_ID

Deploy

Execute the deployment with the following command.

mvn package appengine:deploy

Operation check

Launch your browser and access http://YOUR_PROJECT_ID.appspot.com, or launch your browser with the following command.

gcloud app browse

You can also check it on the GCP screen.

スクリーンショット 2019-11-27 15.18.09.png

Java 8 flexible environment

Refer to the document here. Create a dedicated GCP project in advance.

Make the following modifications to the Spring Boot project.

--Create app.yaml --Addition of appengine-maven-plugin --Check operation locally --Project settings and App Engine app initialization --Deploy

Create app.yaml

Please refer to here for the settings of app.yaml. This time, set the same contents as the sample.

/src/main/appengine/app.yaml


runtime: java
env: flex

handlers:
- url: /.*
  script: this field is required, but ignored

Add appengine-maven-plugin

Add the following plugin to pom.xml. Specify PROJECT_ID and VERSION respectively.

pom.xml


<plugin>
	<groupId>com.google.cloud.tools</groupId>
	<artifactId>appengine-maven-plugin</artifactId>
	<version>2.2.0</version>
	<configuration>
		<deploy.projectId>${PROJECT_ID}</deploy.projectId>
		<deploy.version>${VERSION}</deploy.version>
	</configuration>
</plugin>

Confirmed operation locally

Start it with the following command and access http: // localhost: 8080 to check the operation.

mvn spring-boot:run

Project settings and App Engine app initialization

Use the following commands to set up the project and initialize the App Engine app.

##Project settings
gcloud config set project YOUR_PROJECT_ID

##App Engine App Initialization
gcloud app create --project=YOUR_PROJECT_ID

Deploy

Execute the deployment with the following command.

mvn package appengine:deploy

Operation check

Launch your browser and access http://YOUR_PROJECT_ID.appspot.com, or launch your browser with the following command.

gcloud app browse

You can also check it on the GCP screen.

スクリーンショット 2019-11-27 14.21.53.png

** Don't forget to delete the project if you have a free trial: wink: **

reference

Create a WAR in Spring Boot and deploy to another tomcat

Recommended Posts

Java --Deploy Spring Boot project to GAE
Deploy django project to heroku
How to deploy a Streamlit application to GCP (GAE)
Hello World with Google App Engine (Java 8) + Spring Boot + Gradle
Hello World with Google App Engine (Java 11) + Spring Boot + Gradle
To deploy Java application on VPS (Apache / Tomcat installation / linkage)