Skip to content

Commit 55971b1

Browse files
authored
Move springboot app from getting started (GoogleCloudPlatform#1810)
1 parent bccc238 commit 55971b1

File tree

6 files changed

+317
-0
lines changed

6 files changed

+317
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Spring Boot based Hello World app
2+
3+
This sample shows how to run a [Spring Boot][spring-boot] application on [Google
4+
Cloud Platform][cloud-java]. It uses the [Google App Engine flexible
5+
environment][App Engine-flexible].
6+
7+
[App Engine-flexible]: https://cloud.google.com/appengine/docs/flexible/
8+
[cloud-java]: https://cloud.google.com/java/
9+
[spring-boot]: http://projects.spring.io/spring-boot/
10+
11+
12+
## Before you begin
13+
14+
This sample assumes you have [Java 8][java8] installed.
15+
16+
[java8]: http://www.oracle.com/technetwork/java/javase/downloads/
17+
18+
### Download Maven
19+
20+
These samples use the [Apache Maven][maven] build system. Before getting
21+
started, be sure to [download][maven-download] and [install][maven-install] it.
22+
When you use Maven as described here, it will automatically download the needed
23+
client libraries.
24+
25+
[maven]: https://maven.apache.org
26+
[maven-download]: https://maven.apache.org/download.cgi
27+
[maven-install]: https://maven.apache.org/install.html
28+
29+
### Create a Project in the Google Cloud Platform Console
30+
31+
If you haven't already created a project, create one now. Projects enable you to
32+
manage all Google Cloud Platform resources for your app, including deployment,
33+
access control, billing, and services.
34+
35+
1. Open the [Cloud Platform Console][cloud-console].
36+
1. In the drop-down menu at the top, select **Create a project**.
37+
1. Give your project a name.
38+
1. Make a note of the project ID, which might be different from the project
39+
name. The project ID is used in commands and in configurations.
40+
41+
[cloud-console]: https://console.cloud.google.com/
42+
43+
### Enable billing for your project.
44+
45+
If you haven't already enabled billing for your project, [enable
46+
billing][enable-billing] now. Enabling billing allows the application to
47+
consume billable resources such as running instances and storing data.
48+
49+
[enable-billing]: https://console.cloud.google.com/project/_/settings
50+
51+
### Install the Google Cloud SDK.
52+
53+
If you haven't already installed the Google Cloud SDK, [install and initialize
54+
the Google Cloud SDK][cloud-sdk] now. The SDK contains tools and libraries that
55+
enable you to create and manage resources on Google Cloud Platform.
56+
57+
[cloud-sdk]: https://cloud.google.com/sdk/
58+
59+
### Install the Google App Engine SDK for Java
60+
61+
62+
```
63+
gcloud components update app-engine-java
64+
gcloud components update
65+
```
66+
67+
### Configure the `app.yaml` descriptor
68+
69+
The [`app.yaml`][app-yaml] descriptor is used to describe URL
70+
dispatch and resource requirements. This example sets
71+
[`manual_scaling`][manual-scaling] to 1 to minimize possible costs.
72+
These settings should be revisited for production use.
73+
74+
[app-yaml]: https://cloud.google.com/appengine/docs/flexible/java/configuring-your-app-with-app-yaml
75+
[manual-scaling]: https://cloud.google.com/appengine/docs/flexible/java/configuring-your-app-with-app-yaml#manual-scaling
76+
77+
## Run the application locally
78+
79+
1. Set the correct Cloud SDK project via `gcloud config set project
80+
YOUR_PROJECT` to the ID of your application.
81+
1. Run `mvn spring-boot:run`
82+
1. Visit http://localhost:8080
83+
84+
85+
## Deploy to App Engine flexible environment
86+
87+
1. `mvn appengine:deploy`
88+
1. Visit `http://YOUR_PROJECT.appspot.com`.
89+
90+
Note that deployment to the App Engine flexible environment requires the new
91+
[`com.google.cloud.tools:appengine-maven-plugin` plugin][new-maven-plugin].
92+
93+
[new-maven-plugin]: https://cloud.google.com/appengine/docs/flexible/java/using-maven
94+
95+
Java is a registered trademark of Oracle Corporation and/or its affiliates.
96+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<!--
2+
~ Copyright (c) 2013 Google Inc.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License"); you
5+
~ may not use this file except in compliance with the License. You may
6+
~ obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
~ implied. See the License for the specific language governing
14+
~ permissions and limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
19+
<groupId>com.example.flex.gettingstarted</groupId>
20+
<artifactId>helloworld-springboot</artifactId>
21+
<version>0.0.1-SNAPSHOT</version>
22+
<packaging>jar</packaging>
23+
24+
<name>helloworld-springboot</name>
25+
<description>Demo project for Spring Boot</description>
26+
27+
<!--
28+
The parent pom defines common style checks and testing strategies for our samples.
29+
Removing or replacing it should not affect the execution of the samples in anyway.
30+
-->
31+
<parent>
32+
<groupId>com.google.cloud.samples</groupId>
33+
<artifactId>shared-configuration</artifactId>
34+
<version>1.0.11</version>
35+
</parent>
36+
37+
<properties>
38+
<java.version>1.8</java.version>
39+
<maven.compiler.source>${java.version}</maven.compiler.source> <!-- REQUIRED -->
40+
<maven.compiler.target>${java.version}</maven.compiler.target> <!-- REQUIRED -->
41+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
42+
<appengine.maven.plugin>1.3.2</appengine.maven.plugin>
43+
</properties>
44+
45+
<dependencies>
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-web</artifactId>
49+
<version>2.2.2.RELEASE</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-test</artifactId>
55+
<version>2.2.2.RELEASE</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-maven-plugin</artifactId>
65+
<version>1.5.7.RELEASE</version>
66+
<executions>
67+
<execution>
68+
<goals>
69+
<goal>repackage</goal>
70+
</goals>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
<!-- START plugin -->
75+
<plugin>
76+
<groupId>com.google.cloud.tools</groupId>
77+
<artifactId>appengine-maven-plugin</artifactId>
78+
<version>${appengine.maven.plugin}</version>
79+
</plugin>
80+
<!-- END plugin -->
81+
</plugins>
82+
</build>
83+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [START_EXCLUDE]
2+
# Copyright 2015 Google Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# [END_EXCLUDE]
16+
#
17+
# This sample has manual_scaling set to 1.
18+
#
19+
runtime: java
20+
env: flex
21+
22+
runtime_config: # Optional
23+
jdk: openjdk8
24+
25+
handlers:
26+
- url: /.*
27+
script: this field is required, but ignored
28+
29+
manual_scaling:
30+
instances: 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2015 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.java.gettingstarted;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RestController;
23+
24+
@SpringBootApplication
25+
@RestController
26+
public class HelloworldApplication {
27+
@RequestMapping("/")
28+
public String home() {
29+
return "Hello World!";
30+
}
31+
32+
/**
33+
* (Optional) App Engine health check endpoint mapping.
34+
* @see <a href="https://cloud.google.com/appengine/docs/flexible/java/how-instances-are-managed#health_checking"></a>
35+
* If your app does not handle health checks, a HTTP 404 response is interpreted
36+
* as a successful reply.
37+
*/
38+
@RequestMapping("/_ah/health")
39+
public String healthy() {
40+
// Message body required though ignored
41+
return "Still surviving.";
42+
}
43+
44+
public static void main(String[] args) {
45+
SpringApplication.run(HelloworldApplication.class, args);
46+
}
47+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2015 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2015 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.java.gettingstarted.it;
18+
19+
import static org.junit.Assert.assertTrue;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.test.context.SpringBootTest;
25+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
26+
import org.springframework.boot.test.web.client.TestRestTemplate;
27+
import org.springframework.http.ResponseEntity;
28+
import org.springframework.test.context.junit4.SpringRunner;
29+
30+
@RunWith(SpringRunner.class)
31+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
32+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
33+
public class HelloWorldApplicationIT {
34+
35+
@Autowired
36+
private TestRestTemplate template;
37+
38+
@Test
39+
public void contextLoads() throws Exception {
40+
}
41+
42+
@Test
43+
public void testRequest() throws Exception {
44+
ResponseEntity<String> responseEntity = template.getForEntity("/_ah/health", String.class);
45+
assertTrue(responseEntity.toString().contains("surviving"));
46+
}
47+
}

0 commit comments

Comments
 (0)