Skip to content

Commit fe4dade

Browse files
authored
Add Cloud Run Events Samples (#3256)
Fixes #issue > It's a good idea to open an issue first for discussion. - [x] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/SAMPLE_FORMAT.md) - [x] `pom.xml` parent set to latest `shared-configuration` - [x] Appropriate changes to README are included in PR - [x] API's need to be enabled to test (tell us) - [x] Environment Variables need to be set (ask us to set them) - [x] **Tests** pass: `mvn clean verify` **required** - [x] **Lint** passes: `mvn -P lint checkstyle:check` **required** - [ ] **Static Analysis**: `mvn -P lint clean compile pmd:cpd-check spotbugs:check` **advisory only** - [x] Please **merge** this PR for me once it is approved.
1 parent 3635a1a commit fe4dade

File tree

13 files changed

+845
-0
lines changed

13 files changed

+845
-0
lines changed

run/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
1818
|[Manual Logging](logging-manual/) | Structured logging for Stackdriver | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_log] |
1919
|[Local Troubleshooting](hello-broken/) | Broken services for local troubleshooting tutorial | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_broken] |
2020
|[Cloud SQL (MySQL)][mysql] | Use MySQL with Cloud Run | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_sql] |
21+
|[Events - Pub/Sub](events-pubsub/) | Events for Cloud Run with Pub/Sub | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_events_pubsub] |
22+
|[Events - GCS](events-gcs/) | Events for Cloud Run with GCS | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_events_gcs] |
23+
|[Authentication](authentication/) | Make an authenticated request by retrieving a JSON Web Tokens (JWT) | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_auth] |
2124

2225
For more Cloud Run samples beyond Java, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
2326

@@ -134,6 +137,9 @@ Learn more about [testing your container image locally.][testing]
134137
[run_button_image]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/image-processing
135138
[run_button_log]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/logging-manual
136139
[run_button_pubsub]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/pubsub
140+
[run_button_events_gcs]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/events-gcs
141+
[run_button_events_pubsub]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/events-pubsub
142+
[run_button_auth]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/authentication
137143
[run_button_sys_package]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/system-package
138144
[push-pull]: https://cloud.google.com/container-registry/docs/pushing-and-pulling
139145
[jib]: https://github.com/GoogleContainerTools/jib

run/events-pubsub/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Events for Cloud Run - Pub/Sub tutorial
2+
3+
This sample shows how to create a service that processes Pub/Sub messages.
4+
5+
For more details on how to work with this sample read the [Google Cloud Run Java Samples README](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/run).
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://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/events-pubsub
11+
12+
13+
## Dependencies
14+
15+
* **Spring Boot**: Web server framework.
16+
* **Jib**: Container build tool.
17+
* **Junit + SpringBootTest**: [development] Test running framework.
18+
* **MockMVC**: [development] Integration testing support framework.
19+
20+
## Quickstart
21+
22+
Create a Cloud Pub/Sub topic:
23+
24+
```sh
25+
gcloud pubsub topics create my-topic
26+
```
27+
28+
Use the [Jib Maven Plugin](https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin) to build and push your container image:
29+
30+
```sh
31+
mvn jib:build -Dimage gcr.io/$(gcloud config get-value project)/cloudrun-events-pubsub
32+
```
33+
34+
Deploy your Cloud Run service:
35+
36+
```sh
37+
gcloud run deploy cloudrun-events-pubsub \
38+
--image gcr.io/$(gcloud config get-value project)/cloudrun-events-pubsub
39+
```
40+
41+
Create a Cloud Pub/Sub trigger:
42+
43+
```sh
44+
gcloud alpha events triggers create pubsub-trigger \
45+
--target-service cloudrun-events-pubsub \
46+
--type com.google.cloud.pubsub.topic.publish \
47+
--parameters topic=my-topic
48+
```
49+
50+
## Test
51+
52+
Test your Cloud Run service by publishing a message to the topic:
53+
54+
```sh
55+
gcloud pubsub topics publish my-topic --message="Hello there"
56+
```
57+
58+
You may observe the Run service receiving an event in Cloud Logging.

run/events-pubsub/pom.xml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2020 Google LLC
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+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
15+
<modelVersion>4.0.0</modelVersion>
16+
<groupId>com.example.cloudrun</groupId>
17+
<artifactId>events-pubsub</artifactId>
18+
<version>0.0.1-SNAPSHOT</version>
19+
<!-- The parent pom defines common style checks and testing strategies for our samples.
20+
Removing or replacing it should not affect the execution of the samples in anyway. -->
21+
<parent>
22+
<groupId>com.google.cloud.samples</groupId>
23+
<artifactId>shared-configuration</artifactId>
24+
<version>1.0.18</version>
25+
</parent>
26+
27+
<properties>
28+
<maven.compiler.target>11</maven.compiler.target>
29+
<maven.compiler.source>11</maven.compiler.source>
30+
</properties>
31+
32+
<dependencyManagement>
33+
<dependencies>
34+
<dependency>
35+
<!-- Import dependency management from Spring Boot -->
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-dependencies</artifactId>
38+
<version>2.3.1.RELEASE</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.cloud</groupId>
44+
<artifactId>spring-cloud-dependencies</artifactId>
45+
<version>Hoxton.SR6</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<dependencies>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-web</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.apache.commons</groupId>
59+
<artifactId>commons-lang3</artifactId>
60+
<version>3.10</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-test</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.json</groupId>
69+
<artifactId>json</artifactId>
70+
<version>20180130</version>
71+
<scope>test</scope>
72+
</dependency>
73+
</dependencies>
74+
75+
<build>
76+
<plugins>
77+
<plugin>
78+
<groupId>org.springframework.boot</groupId>
79+
<artifactId>spring-boot-maven-plugin</artifactId>
80+
</plugin>
81+
<!-- [START run_events_pubsub_jib] -->
82+
<plugin>
83+
<groupId>com.google.cloud.tools</groupId>
84+
<artifactId>jib-maven-plugin</artifactId>
85+
<version>2.4.0</version>
86+
<configuration>
87+
<to>
88+
<image>gcr.io/PROJECT_ID/cloudrun-events-pubsub</image>
89+
</to>
90+
</configuration>
91+
</plugin>
92+
<!-- [END run_events_pubsub_jib] -->
93+
</plugins>
94+
</build>
95+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.cloudrun;
18+
19+
// [START run_events_pubsub_server]
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
23+
@SpringBootApplication
24+
public class Application {
25+
public static void main(String[] args) {
26+
String port = System.getenv().getOrDefault("PORT", "8080");
27+
System.setProperty("server.port", port);
28+
SpringApplication.run(Application.class, args);
29+
}
30+
}
31+
// [END run_events_pubsub_server]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.cloudrun;
18+
19+
// [START run_events_pubsub_handler]
20+
import com.example.cloudrun.eventpojos.PubSubBody;
21+
import java.util.Base64;
22+
import java.util.Map;
23+
import org.apache.commons.lang3.StringUtils;
24+
import org.springframework.http.HttpStatus;
25+
import org.springframework.http.ResponseEntity;
26+
import org.springframework.web.bind.annotation.RequestBody;
27+
import org.springframework.web.bind.annotation.RequestHeader;
28+
import org.springframework.web.bind.annotation.RequestMapping;
29+
import org.springframework.web.bind.annotation.RequestMethod;
30+
import org.springframework.web.bind.annotation.RestController;
31+
32+
@RestController
33+
public class EventController {
34+
@RequestMapping(value = "/", method = RequestMethod.POST)
35+
public ResponseEntity<String> receiveMessage(
36+
@RequestBody PubSubBody body, @RequestHeader Map<String, String> headers) {
37+
// Get PubSub message from request body.
38+
PubSubBody.PubSubMessage message = body.getMessage();
39+
if (message == null) {
40+
String msg = "No Pub/Sub message received.";
41+
System.out.println(msg);
42+
return new ResponseEntity<String>(msg, HttpStatus.BAD_REQUEST);
43+
}
44+
45+
String data = message.getData();
46+
if (data == null || data.isEmpty()) {
47+
String msg = "Invalid Pub/Sub message format.";
48+
System.out.println(msg);
49+
return new ResponseEntity<String>(msg, HttpStatus.BAD_REQUEST);
50+
}
51+
52+
String name =
53+
!StringUtils.isEmpty(data) ? new String(Base64.getDecoder().decode(data)) : "World";
54+
String ceId = headers.getOrDefault("ce-id", "");
55+
String msg = String.format("Hello, %s! ID: %s", name, ceId);
56+
System.out.println(msg);
57+
return new ResponseEntity<String>(msg, HttpStatus.OK);
58+
}
59+
}
60+
// [END run_events_pubsub_handler]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.cloudrun.eventpojos;
18+
19+
import java.util.Map;
20+
21+
public class PubSubBody {
22+
private PubSubMessage message;
23+
24+
public PubSubMessage getMessage() {
25+
return message;
26+
}
27+
28+
public class PubSubMessage {
29+
private String data;
30+
private Map<String, String> attributes;
31+
private String messageId;
32+
private String publishTime;
33+
34+
public String getData() {
35+
return data;
36+
}
37+
38+
public void setData(String data) {
39+
this.data = data;
40+
}
41+
42+
public Map<String, String> getAttributes() {
43+
return attributes;
44+
}
45+
46+
public void setAttributes(Map<String, String> attributes) {
47+
this.attributes = attributes;
48+
}
49+
50+
public String getMessageId() {
51+
return messageId;
52+
}
53+
54+
public void setMessageId(String messageId) {
55+
this.messageId = messageId;
56+
}
57+
58+
public String getPublishTime() {
59+
return publishTime;
60+
}
61+
62+
public void setPublishTime(String publishTime) {
63+
this.publishTime = publishTime;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)