DEV Community

Marc DiPasquale for Solace Developers

Posted on • Originally published at solace.com on

Getting Started with Spring Cloud Stream using Spring Initializr

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 in this youtube video! 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
    
  2. Go to start.spring.io and add the Solace PubSub+ and Cloud Stream dependencies. Yes, that link added them for you 😜.

  3. Click Generate

  4. Unzip the downloaded zip file and import into your IDE as a Maven project

  5. 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();
      };
    }
    
  6. *Save and Run

  7. 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.

  8. On the Subscriber side, click Connect, type uppercase-out-0 in the Subscribe to a topic to receive direct messages box and click Subscribe.

  9. On the Publisher side click Connect, type uppercase-in-0 as the topic to publish to and click the Publish button.

  10. 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?

The post Getting Started with Spring Cloud Stream using Spring Initializr appeared first on the Solace Blog.

Top comments (2)

Collapse
 
mermuys profile image
Trish Mermuys • Edited

I feel like this is the first time I've seen a five minute dev tutorial that is actually five minutes and there's no "draw the rest of the owl" needed. Great article @mrc0113 👍

Collapse
 
mrc0113 profile image
Marc DiPasquale

Haha. I like simple! And...if I had to draw the rest of an owl I would fail miserably ;)