1 Preface

This article will briefly introduce the integration of Spring Cloud Stream with Google Cloud Pub/Sub.

2 Starting Pub/Sub via Emulator

Since it is relatively troublesome to use the actual GCP Pub/Sub, this article runs through the emulator.

For the installation of Google Cloud SDK, please refer to: Installing Google Cloud SDK for Mac

Install the necessary components.

1
2
gcloud components install beta
gcloud components install pubsub-emulator

To start the simulator.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ gcloud beta emulators pubsub start --project=pkslow-prj --host-port=0.0.0.0:8511
Executing: /google-cloud-sdk/platform/pubsub-emulator/bin/cloud-pubsub-emulator --host=0.0.0.0 --port=8511
[pubsub] This is the Google Pub/Sub fake.
[pubsub] Implementation may be incomplete or differ from the real system.
[pubsub] May 11, 2021 10:27:31 PM com.google.cloud.pubsub.testing.v1.Main main
[pubsub] INFO: IAM integration is disabled. IAM policy methods and ACL checks are not supported
[pubsub] SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
[pubsub] SLF4J: Defaulting to no-operation (NOP) logger implementation
[pubsub] SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[pubsub] May 11, 2021 10:27:32 PM com.google.cloud.pubsub.testing.v1.Main main
[pubsub] INFO: Server started, listening on 8511

The project name and port are set at startup.

3 Integration

Introducing dependencies.

1
2
3
4
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-pubsub-stream-binder</artifactId>
</dependency>

Implementation of simple message sending and receiving processing.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.pkslow.cloud.stream.binder.pubsub;

@SpringBootApplication
public class StreamBinderPubsub {
    private static final Logger log = LoggerFactory.getLogger(StreamBinderPubsub.class);
    public static void main(String[] args) {
        SpringApplication.run(StreamBinderPubsub.class, args);
    }

    @Bean
    public Supplier<String> pkslowSource() {
        return () -> {
            String message = "www.pkslow.com";
            log.info("Sending value: " + message);
            return message;
        };
    }

    @Bean
    public Consumer<String> pkslowSink() {
        return message -> {
            log.info("Received message " + message);
        };
    }
}

Configure Pub/Sub properties with Cloud Stream properties.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
spring:
  cloud:
    stream:
      function:
        definition: pkslowSource;pkslowSink
      bindings:
        pkslowSource-out-0:
         destination: pkslow-topic
        pkslowSink-in-0:
          destination: pkslow-topic
      poller:
        fixed-delay: 500
    gcp:
      pubsub:
        emulator-host: localhost:8511
        project-id: pkslow-prj

In the case of the actual GCP Pub/Sub, specify the key file as follows.

1
2
3
4
5
spring:
  cloud:
    gcp:
      credentials:
        location: file:/xxx.json

The run log is as follows.

4 Summary

The code can be found at: https://github.com/LarryDpk/pkslow-samples

Reference https://www.pkslow.com/archives/spring-cloud-stream-binder-pubsub