Thanks to Spring Cloud Stream and Spring Initializr it is easier than ever to develop event-driven microservices. Even with partner-maintained binders like Solace PubSub+! If you’re not familiar with Spring Cloud Stream or Spring Initializr, this post will provide some quick background info as well as a tutorial so you can try it yourself.

The Spring Cloud Stream Framework

There are a number of Spring projects for building applications of all types. When developing scalable event-driven microservices, Spring Cloud Stream is often the framework of choice as it provides a flexible programming model and allows developers to create event-driven microservices without having to learn messaging APIs. Instead of learning messaging APIs, Spring Cloud Stream just requires developers to learn basic message-driven concepts such as publish-subscribe and consumer groups. The framework leverages cloud stream binders, which exist for several message brokers including Solace PubSub+, Apache Kafka, and RabbitMQ.

PubSub+ and Spring Initializr

The Solace PubSub+ Spring Cloud Stream binder is automatically added to Spring Initializr projects when you choose both the Solace PubSub+ and Cloud Stream dependencies. For those who are not familiar with start.spring.io, it allows developers to generate Spring Boot projects pre-populated with the dependencies they need to get started quickly.

There are a couple scenarios that could play out when generating a project with Solace PubSub+ as a dependency using Spring Initializr:

  1. Your dependencies include Solace PubSub+, but do not require a binder.
  2. Your dependencies include Solace PubSub+ AND Cloud Stream, which states that it requires a binder.

In scenario 1, Spring Initializr adds the solace-spring-boot-starter to your project. This starter allows you to use Spring Boot with either the Solace JMS API or the Solace Java API (JCSMP).

In scenario 2, Spring Initializr realizes you’re trying to create a Spring Cloud Stream microservice and automatically includes the Solace Spring Cloud Stream Binder Starter in your project.

Using Solace with Spring enables microservices created using the Spring ecosystem to communicate with both Spring and non-Spring applications across multi-cloud, hybrid-cloud, and no-cloud environments.

5-Minute Tutorial (seriously, only 5 minutes)

Do you already have a JDK, Maven, and Docker installed? If so, it will only take you 5 minutes (video evidence here!). So, why not give it a try for yourself!

If for some reason you can’t use Docker, check out this codelab for how to do it using PubSub+ Cloud.

Let’s Begin…

  1. Deploy a Solace PubSub+ Event Broker via Docker
docker run -d -p 8080:8080 -p 55555:55555 -p:8008:8008 -p:1883:1883 -p:8000:8000 -p:5672:5672 -p:9000:9000 -p:2222:2222 --shm-size=2g --env username_admin_globalaccesslevel=admin --env username_admin_password=admin --name=solace solace/solace-pubsub-standard
  1. Go to start.spring.io and add the Solace PubSub+ and Cloud Stream dependencies. Yes, that link added them for you 😜.
  2. Click Generate
  3. Unzip the downloaded zip file and import into your IDE as a Maven project
  4. Open DemoApplication.java and add the following Spring bean
@Bean
public Function<String, String> uppercase(){
return v -> {
System.out.println("Uppercasing: " + v);
return v.toUpperCase();
};
}
  1. Save and Run*
  2. Navigate to the Try-Me! menu item in your local PubSub+ Manager which deployed with the Docker container in Step 1. Your username/password are admin/admin by default.
  3. On the Subscriber side, click Connect, type uppercase-out-0 in the Subscribe to a topic to receive direct messages box and click Subscribe.
  4. On the Publisher side click Connect, type uppercase-in-0 as the topic to publish to and click the Publish button.
  5. Boom! You’re done! Was that simple enough?

*You may be thinking, “What! No connection info needed?!” That is correct. By default, the Solace Binder knows how to automatically connect to the local docker container!

Behind the Scenes

Okay, so you followed the steps above, but what exactly happened? Let me explain:

  • You deployed a Spring Boot microservice which contained a java.util.function.Function bean in your code and both the cloud stream and a cloud stream binder as dependencies. This told Spring Boot that you wanted to create an event-driven microservice using Spring Cloud Stream.
  • After Spring Boot realized you wanted a Cloud Stream microservice it knew it needed to create cloud stream bindings to exchange events with the available broker. So, based on your function name of uppercase, it created the default input binding of uppercase-in-0. The Solace Cloud Stream binder then subscribed to the uppercase-in-0 topic on the Solace PubSub+ Event Broker.
  • When you published an event in the Try-Me! tool, you published an event to the uppercase-in-0 topic on the Solace Broker that contained a payload of “Hello world!”
  • Your microservice received this event via the binding previously set-up, identified the payload as a String type, and called your uppercase bean for processing which converted the payload of “Hello world!” to “HELLO WORLD!” and returned a String.
  • Spring Cloud Stream included that returned String as the payload in an outbound message to the Solace Binder which in turn published it to the uppercase-out-0 topic on the Solace Broker.
  • The Try-Me! tool was then able to receive the processed message and display it to the screen.

What should you try next?

  • Code for the open source Solace PubSub+ Spring Cloud Stream Binder can be found in the github repository below
Spring Cloud Stream Binder for Solace PubSub+by SolaceProductsAn implementation of Spring's Cloud Stream Binder for integrating with Solace PubSub+ message brokers.Open on GitHub

 

Marc DiPasquale

Marc is a Developer Advocate with extensive engineering experience in the public and private sector across multiple domains including healthcare, aviation and weather imagery processing. He has been using event-driven techniques and methodologies throughout his career and is excited by its elevation to the mainstream. Marc works with prospective and existing clients to enable the development of modern and reactive applications.

Join Our Developer Community

Join the Solace Developer Community to discuss and share PubSub+ API hints, new features, useful integrations, demos, and sample code!

JOIN THE DISCUSSION

Event Portal for Kafka is now GA. Try it today!