Streamline Your Messaging: Cross-Platform Integration Made Easy with Spring Cloud Stream

This document is a beginner-friendly guide that demonstrates how to use the Spring Cloud Stream framework to develop applications that can communicate across different messaging platforms in a decoupled manner. Whether you’re a seasoned developer or new to the field, this guide will walk you through the essential steps and concepts.

cover

Download the Project Template

Introduction to Binders

Binders are a key concept in Spring Cloud Stream. They act as a bridge between your application and the messaging platform (like Kafka, GCP Pub/Sub, etc.). To start, you need to add the Binder dependency corresponding to the messaging platform you plan to use. Let’s look at how to do this for three popular platforms.

GCP Pub/Sub Binder

To use Google Cloud Pub/Sub, add the following dependency:

1
implementation 'com.google.cloud:spring-cloud-gcp-pubsub-stream-binder'

Kafka Binder

For Kafka, include this dependency:

1
implementation 'org.springframework.cloud:spring-cloud-stream-binder-kafka'

Nats Binder

And for Nats, use:

1
implementation 'io.nats:nats-spring-cloud-stream-binder:0.5.6'

Once you’ve added the appropriate Binder, you’re ready to dive into the coding part of sending and receiving messages.

Coding for Message Sending

Spring Cloud Stream simplifies the coding process. You only need to specify where to send the message (the “Binding Name”), and the framework handles the rest.

Example: Sending Messages to GCP Pub/Sub

Here’s how you can send a message to a GCP Pub/Sub topic:

1
2
3
4
5
6
7
8
9
private final StreamBridge streamBridge;

private final String bindingName = "order-created-out";

@GetMapping("/publish")
public void publish(@RequestParam("msg") String msg) {
MsgDTO msgDTO = new MsgDTO(counter.incrementAndGet() + ":" + msg);
streamBridge.send(bindingName, msgDTO);
}

In this code, you declare the bindingName and use the streamBridge.send() method to send your message. The actual topic (ORDERS.CREATED) and the binder (pubsub1) are defined in a configuration file, not in the code, making it easier to change the messaging platform without altering the code.

Configuring the Binder and Destination

Your application.yml file should have configurations like this for GCP Pub/Sub:

1
2
3
4
5
6
7
8
9
10
11
spring:
cloud:
stream:
bindings:
order-created-out:
destination: ORDERS.CREATED
content-type: application/json
binder: pubsub1
binders:
pubsub1:
type: pubsub

For Kafka, you would change the binder settings accordingly, specifying details like the broker’s address.

Receiving Messages

To receive messages, you need to set up a Bean that implements the Consumer interface. Spring Cloud Stream then automatically links this Bean with the specified Binding Name, allowing your application to receive messages.

Example: Receiving Messages from GCP Pub/Sub

Here’s a basic setup for receiving messages:

1
2
3
4
5
6
7
8
9
10
@Configuration
public class ConsumerConfiguration {

@Bean
public Consumer<Message<MsgDTO>> orderCreated() {
return message -> {
log.info("message={}", message.getPayload().getMsg());
};
}
}

This orderCreated method is automatically linked to a Binding Name (like orderCreated-in-0), and it listens for messages from the specified topic and binder as defined in your configuration file.

Additional Notes

  1. You can easily switch between different messaging platforms by changing the active profile in application.yml.
  2. For GCP, this setup involves creating a specific topic (ORDERS.CREATED) and a subscription.
  3. Kafka does not support wildcard topics like ORDERS.*, so adjust your settings accordingly.
  4. For information on how to name Function subscriptions, refer to Functional binding names.

This guide covers the basics of setting up a cross-platform messaging system using Spring Cloud Stream. Remember, the framework is designed to simplify your interaction with different messaging platforms, allowing you to focus more on the logic of your application rather than the intricacies of the messaging infrastructure.