Skip to content

Commit d6d47ec

Browse files
authored
Add Cloud Run directory and ReadMes (GoogleCloudPlatform#1525)
* Move directories * Add Cloud Run ReadMes * Update readme * Update region tags * Update readmes * Update docker command and dockerfile * Add next steps to readme * rename files * Add link with more info
1 parent 7a1668f commit d6d47ec

9 files changed

Lines changed: 193 additions & 46 deletions

File tree

appengine-java11/cloudrun-pubsub/README.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

run/README.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Google Cloud Run Java Samples
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=blog/README.md
7+
8+
This directory contains samples for [Google Cloud Run](https://cloud.run). [Cloud Run][run_docs] runs stateless [containers](https://cloud.google.com/containers/) on a fully managed environment or in your own GKE cluster.
9+
10+
## Samples
11+
12+
| Sample | Description | Deploy |
13+
| ------------------------------- | ------------------------ | ------------- |
14+
|[Hello World][helloworld]&nbsp;&#10149; | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_helloworld] |
15+
|[Cloud Pub/Sub][pubsub] | Handling Pub/Sub push messages | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_pubsub] |
16+
17+
For more Cloud Run samples beyond Java, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
18+
19+
## Setup
20+
21+
1. [Set up for Cloud Run development](https://cloud.google.com/run/docs/setup)
22+
23+
2. Clone this repository:
24+
25+
```
26+
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
27+
```
28+
29+
Note: Some samples in the list above are hosted in other repositories. They are noted with the symbol "&#10149;".
30+
31+
32+
## How to run a sample locally
33+
34+
1. [Install docker locally](https://docs.docker.com/install/)
35+
36+
2. [Build the sample container](https://cloud.google.com/run/docs/building/containers#building_locally_and_pushing_using_docker):
37+
38+
```
39+
export SAMPLE=<SAMPLE_NAME>
40+
cd $SAMPLE
41+
docker build --tag $SAMPLE .
42+
```
43+
44+
3. [Run containers locally](https://cloud.google.com/run/docs/testing/local)
45+
46+
With the built container:
47+
48+
```
49+
PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE
50+
```
51+
52+
Overriding the built container with local code:
53+
54+
```
55+
PORT=8080 && docker run --rm \
56+
-p 8080:${PORT} -e PORT=${PORT} \
57+
-v $PWD:/app $SAMPLE
58+
```
59+
60+
Injecting your service account key for access to GCP services:
61+
62+
```
63+
# Set the name of the service account key within the container
64+
export SA_KEY_NAME=my-key-name-123
65+
66+
PORT=8080 && docker run --rm \
67+
-p 8080:${PORT} \
68+
-e PORT=${PORT} \
69+
-e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \
70+
-v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \
71+
-v $PWD:/app $SAMPLE
72+
```
73+
74+
* Use the --volume (-v) flag to inject the credential file into the container
75+
(assumes you have already set your `GOOGLE_APPLICATION_CREDENTIALS`
76+
environment variable on your machine)
77+
78+
* Use the --environment (-e) flag to set the `GOOGLE_APPLICATION_CREDENTIALS`
79+
variable inside the container
80+
81+
Learn more about [testing your container image locally.][testing]
82+
83+
## Deploying
84+
85+
1. Set your GCP Project ID as an environment variable:
86+
```
87+
export GOOGLE_CLOUD_PROJECT=<PROJECT_ID>
88+
```
89+
90+
2. Choose to push your image to Google Container Registry or submit a build to
91+
Cloud Build:
92+
* Push the docker build to Google Container Registry:
93+
```
94+
docker tag $SAMPLE gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
95+
docker push gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
96+
```
97+
Learn more about [pushing and pulling images][push-pull].
98+
99+
* Submit a build using Google Cloud Build:
100+
```
101+
gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
102+
```
103+
104+
3. Deploy to Cloud Run:
105+
```
106+
gcloud beta run deploy $SAMPLE \
107+
--set-env-vars GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT} \
108+
--image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
109+
```
110+
111+
## Next Steps
112+
* See [building containers][run_build] and [deploying container images][run_deploy]
113+
for more information.
114+
115+
* [Dockerize a Spring Boot app][jib-tutorial] without a Dockerfile using [Jib][jib].
116+
117+
* To improve [Tomcat startup time][startup], add
118+
`-Djava.security.egd=file:/dev/./urandom` to the Dockerfile's `CMD`
119+
instructions. This is acceptable for many applications. However, if your
120+
application does it's own key generation, such as for SSL, and you require
121+
greater security, you may prefer to not set `java.security.egd`.
122+
123+
124+
[run_docs]: https://cloud.google.com/run/docs/
125+
[run_build]: https://cloud.google.com/run/docs/building/containers
126+
[run_deploy]: https://cloud.google.com/run/docs/deploying
127+
[helloworld]: https://github.com/knative/docs/tree/master/docs/serving/samples/hello-world/helloworld-java
128+
[pubsub]: pubsub/
129+
[run_button_helloworld]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/knative/docs&cloudshell_working_dir=docs/serving/samples/hello-world/helloworld-java
130+
[run_button_pubsub]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&cloudshell_working_dir=run/cloudrun-pubsub
131+
[push-pull]: https://cloud.google.com/container-registry/docs/pushing-and-pulling
132+
[jib]: https://github.com/GoogleContainerTools/jib
133+
[jib-tutorial]: https://github.com/GoogleContainerTools/jib/tree/master/examples/spring-boot
134+
[startup]: https://cwiki.apache.org/confluence/display/TOMCAT/HowTo+FasterStartUp
135+
[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services

appengine-java11/cloudrun-pubsub/Dockerfile renamed to run/pubsub/Dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Copyright 2019 Google LLC. All rights reserved.
2+
# Use of this source code is governed by the Apache 2.0
3+
# license that can be found in the LICENSE file.
4+
5+
# [START run_pubsub_dockerfile]
6+
17
# Use the official maven/Java 11 image to create a build artifact.
28
# https://hub.docker.com/_/maven
39
FROM maven:3.6.1-jdk-11 as builder
@@ -17,7 +23,9 @@ RUN mvn package -DskipTests
1723
FROM adoptopenjdk/openjdk11:x86_64-alpine-jdk-11.0.3_7-slim
1824

1925
# Copy the jar to the production image from the builder stage.
20-
COPY --from=builder /app/target/cloudrun-pubsub-*.jar /cloudrun-pubsub.jar
26+
COPY --from=builder /app/target/pubsub-*.jar /pubsub.jar
2127

2228
# Run the web service on container startup.
23-
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-Dserver.port=${PORT}","-jar","/cloudrun-pubsub.jar"]
29+
CMD ["java","-Dserver.port=${PORT}","-jar","/pubsub.jar"]
30+
31+
# [END run_pubsub_dockerfile]

run/pubsub/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Cloud Run Pub/Sub Tutorial Sample
2+
3+
This sample shows how to create a service that processes Pub/Sub messages.
4+
5+
Use it with the [Cloud Pub/Sub with Cloud Run tutorial](http://cloud.google.com/run/docs/tutorials/pubsub).
6+
7+
[![Run in Google Cloud][run_img]][run_link]
8+
9+
[run_img]: https://storage.googleapis.com/cloudrun/button.svg
10+
[run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&cloudshell_working_dir=run/cloudrun-pubsub
11+
12+
## Build
13+
14+
```
15+
docker build --tag pubsub-tutorial:java .
16+
```
17+
18+
## Run
19+
20+
```
21+
PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} pubsub-tutorial:java
22+
```
23+
24+
## Test
25+
26+
```
27+
mvn clean verify
28+
```
29+
30+
## Deploy
31+
32+
```
33+
gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/pubsub-tutorial
34+
gcloud beta run deploy pubsub-tutorial --image gcr.io/${GOOGLE_CLOUD_PROJECT}/pubsub-tutorial
35+
```
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1515
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1616
<modelVersion>4.0.0</modelVersion>
17-
<groupId>com.example.appengine</groupId>
18-
<artifactId>cloudrun-pubsub</artifactId>
17+
<groupId>com.example.cloudrun</groupId>
18+
<artifactId>pubsub</artifactId>
1919
<version>0.0.1-SNAPSHOT</version>
2020

2121
<!--

appengine-java11/cloudrun-pubsub/src/main/java/com/example/appengine/cloudrunpubsub/Body.java renamed to run/pubsub/src/main/java/com/example/cloudrun/Body.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.appengine.cloudrunpubsub;
17+
package com.example.cloudrun;
1818

1919
// Body.Message is the payload of a Pub/Sub event. Please refer to the docs for
2020
// additional information regarding Pub/Sub events.

appengine-java11/cloudrun-pubsub/src/main/java/com/example/appengine/cloudrunpubsub/CloudrunPubsubApplication.java renamed to run/pubsub/src/main/java/com/example/cloudrun/PubSubApplication.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
// [START run_pubsub_server_setup]
18-
19-
package com.example.appengine.cloudrunpubsub;
17+
package com.example.cloudrun;
2018

19+
// [START run_pubsub_server]
2120
import org.springframework.boot.SpringApplication;
2221
import org.springframework.boot.autoconfigure.SpringBootApplication;
2322

2423
@SpringBootApplication
25-
public class CloudrunPubsubApplication {
24+
public class PubSubApplication {
2625
public static void main(String[] args) {
27-
SpringApplication.run(CloudrunPubsubApplication.class, args);
26+
SpringApplication.run(PubSubApplication.class, args);
2827
}
2928
}
30-
// [END run_pubsub_server_setup]
29+
// [END run_pubsub_server]

appengine-java11/cloudrun-pubsub/src/main/java/com/example/appengine/cloudrunpubsub/PubsubController.java renamed to run/pubsub/src/main/java/com/example/cloudrun/PubSubController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.appengine.cloudrunpubsub;
17+
package com.example.cloudrun;
1818

19+
// [START run_pubsub_handler]
1920
import java.util.Base64;
2021
import org.apache.commons.lang3.StringUtils;
2122
import org.springframework.http.HttpStatus;
@@ -25,10 +26,9 @@
2526
import org.springframework.web.bind.annotation.RequestMethod;
2627
import org.springframework.web.bind.annotation.RestController;
2728

28-
// [START run_pubsub_handler]
2929
// PubsubController consumes a Pub/Sub message.
3030
@RestController
31-
public class PubsubController {
31+
public class PubSubController {
3232
@RequestMapping(value = "/", method = RequestMethod.POST)
3333
public ResponseEntity receiveMessage(@RequestBody Body body) {
3434
// Get PubSub message from request body.

appengine-java11/cloudrun-pubsub/src/test/java/com/example/appengine/cloudrunpubsub/PubsubControllerTests.java renamed to run/pubsub/src/test/java/com/example/cloudrun/PubSubControllerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.appengine.cloudrunpubsub;
17+
package com.example.cloudrun;
1818

1919
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
2020
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -31,7 +31,7 @@
3131
@RunWith(SpringRunner.class)
3232
@SpringBootTest
3333
@AutoConfigureMockMvc
34-
public class PubsubControllerTests {
34+
public class PubSubControllerTests {
3535

3636
@Autowired private MockMvc mockMvc;
3737

0 commit comments

Comments
 (0)