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.
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.
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:
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.
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.
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
@Bean public Function<String, String> uppercase(){ return v -> { System.out.println("Uppercasing: " + v); return v.toUpperCase(); }; }
uppercase-out-0
in the Subscribe to a topic to receive direct messages box and click Subscribe.uppercase-in-0
as the topic to publish to and click the Publish button.*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!
Okay, so you followed the steps above, but what exactly happened? Let me explain:
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.uppercase-in-0
. The Solace Cloud Stream binder then subscribed to the uppercase-in-0
topic on the Solace PubSub+ Event Broker.uppercase-in-0
topic on the Solace Broker that contained a payload of “Hello world!”uppercase-out-0
topic on the Solace Broker.These are the steps that the codelab will walk you through: