Run a Kotlin Spring Boot app on App Engine standard environment
Contributed by Google employees.
App Engine standard environment deploys your apps to the same infrastructure that powers Google's products. In this tutorial you'll see how to deploy your Kotlin and Spring Boot application to App Engine standard environment.
You will create a new Spring Boot application, and then you will learn how to:
- Deploy your application
- Update your application
While the tutorial uses Kotlin 1.2 and Spring Boot 2 M7, other releases of Kotlin and Spring Boot should work without any modifications (other than version numbers in Maven files). This tutorial does assume you're familiar with Spring Boot and creating web applications. For simplicity the tutorial responds with JSON to a specific HTTP request, but can be built-on to connect to other Google services and/or databases.
Before you begin
Before running this tutorial, you must set up a Google Cloud Platform project, and you need to have the Google Cloud SDK installed.
Create a project that will host your Spring Boot application. You can also reuse an existing project.
Use the Cloud Console to create a new Google Cloud project. Remember the project ID; you will need it later. Later commands in this tutorial will use
[PROJECT_ID]
as a substitution, so you might consider setting thePROJECT_ID
environment variable in your shell.Enable billing for your project.
Install the Cloud SDK. Make sure you initialize the SDK and set the default project to the new project you created.
Version 175.0.0 or later of the SDK is required.
Install JDK 8 or higher if you do not already have it.
Install Maven.
Creating a new app and running it locally
In this section, you will create a new Spring Boot app and make sure it runs. If you already have an app to deploy, you can use it instead.
Alternatively, you can download the sample application.
Use start.spring.io to generate a Spring Boot application.
- Select Kotlin as the language and Maven as the build system.
- Using the Dependency selector, type GCP Support and select it.
- In the same Dependency selector, type Web and select first result.
- Click "Switch to the full version" at the bottom to expand the advanced options. Under Packaging select War in the dropdown.
Click the Generate Project button to download the generated Spring Boot application and save it to a local folder.
Open the downloaded application folder in your favourite IDE or editor an create a new source file
MessageController.kt
in the directorysrc/main/kotlin
with the following contents:package com.example.demo import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController data class Message(val text: String, val priority: String) @RestController class MessageController { @RequestMapping("/message") fun message(): Message { return Message("Hello from Google Cloud", "High") } }
The package should match that of the
groupId
andartifactId
specified inpom.xml
.Edit the file named
DemoApplication.kt
insrc/main/kotlin
and replace its contents with the contents of the following exampleDemoApplication.kt
file.Run the application from the command line using Maven:
mvn spring-boot:run
Open the browser and make sure you get a valid JSON response when accessing http://localhost:8080/message. The result should be:
{ "text": "Hello from Google Cloud", "priority": "High" }
Deploy your application
To deploy your application, you will use an App Engine plugin for Maven which simplifies some of the process. The plugin is also available for Gradle.
Create a file called
appengine-web.xml
in a new foldersrc/main/webapp/WEB-INF
with the contents of the following exampleappengine-web.xml
file.Add this plugin entry to the
plugins
section of yourpom.xml
file to configure the Google Cloud Maven plugin.Run the following command to deploy your app:
mvn appengine:deploy
If this is the first time you have deployed to App Engine in this project,
gcloud
will prompt you for a region. You may choose the default of "us-central", or select a region close to your location.Deployment will also take a few minutes to requisition and configure the needed resources, especially the first time you deploy.
Note: If the command fails with
Google Cloud SDK could not be found
, make sure the environment variableGOOGLE_CLOUD_SDK_HOME
is set to the root directory of where you installed the Cloud SDK.Once the deploy command has completed, you can run the following to see your app running in production on App Engine in the browser:
gcloud app browse
This application does not respond to the root endpoint. Once the browser is open with the correct URL, you need to append
/message
to it.
Update your application
Make a simple change and redeploy.
Change the text returned from the
message()
function to"Hello from App Engine"
Run the deployment command again:
mvn appengine:deploy
View your changes live by running:
gcloud app browse
Remember to add
/message
to the URL.
Clean up
After you've finished this tutorial, you can clean up the resources you created on Google Cloud so you won't be billed for them in the future. To clean up the resources, you can delete the project or stop the App Engine service.
Deleting the project
The easiest way to eliminate billing is to delete the project you created for
the tutorial. To do so using gcloud
, run:
gcloud projects delete [PROJECT_ID]
where [PROJECT_ID]
is your Google Cloud project ID.
Warning: Deleting a project has the following consequences:
If you used an existing project, you'll also delete any other work you've done in the project. You can't reuse the project ID of a deleted project. If you created a custom project ID that you plan to use in the future, you should delete the resources inside the project instead. This ensures that URLs that use the project ID, such as an appspot.com URL, remain available.
Stopping App Engine services
To disable an App Engine service:
- In the Cloud Console, go to the App Engine Versions page.
- Make sure your project is selected. If necessary, pull down the project selection dropdown at the top, and choose the project you created for this tutorial.
- If you deployed to a service other than "default", you can navigate to the Services page, select the service you want to remove, and click Delete.
- If you have multiple versions running, you can navigate to the Versions page, select your version and click Split Traffic to migrate all traffic away from the version you want to stop. Once your version is serving zero traffic, select it and click Stop or Delete.
- If you want to stop App Engine entirely, you can navigate to the Settings page and click Disable Application.
Next steps
See the App Engine documentation for more information on App Engine features including scaling, health checks, and infrastructure customization.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.