This time, create the following configurations respectively
--Java 8 standard environment --Java 8 flexible environment
--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
First, prepare the development environment. The supported development environment is IntelliJ IDEA, Eclipse (STS), and the build tools are Apache Maven and Gradle.
By including the AppEngine component, the local execution environment and deploy commands will be installed.
gcloud components install app-engine-java
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].
You can now create an App Engine Project from the IDE. (Not used this time)
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.
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
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>
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 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>
Start it with the following command and access http: // localhost: 8080
to check the operation.
mvn spring-boot:run
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
Execute the deployment with the following command.
mvn package appengine:deploy
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.
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
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 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>
Start it with the following command and access http: // localhost: 8080
to check the operation.
mvn spring-boot:run
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
Execute the deployment with the following command.
mvn package appengine:deploy
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.
** Don't forget to delete the project if you have a free trial: wink: **
Create a WAR in Spring Boot and deploy to another tomcat
Recommended Posts