Getting Started with Spring Cloud Stream using Spring Initializr
Tags: java, spring, tutorial, coding
Published: 2020-09-28 19:32:26

---
title: Getting Started with Spring Cloud Stream using Spring Initializr
published: true
date: 2020-09-11 15:02:04 UTC
tags: java, spring, tutorial, coding
canonical_url: https://solace.com/blog/getting-started-spring-cloud-stream-and-spring-initializr/
---

![](https://solace.com/wp-content/uploads/2019/08/spring-cloud-featured-image.png) 

Thanks to [Spring Cloud Stream](https://spring.io/projects/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](https://spring.io/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](https://start.spring.io/#!dependencies=solace,cloud-stream) 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!
{% youtube FWgkl3S1zA4 %}

If for some reason you can’t use Docker, check out [this codelab](https://codelabs.solace.dev/codelabs/spring-cloud-stream-basics/#0) 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](https://start.spring.io/#!dependencies=solace,cloud-stream) and add the _Solace PubSub+_ and _Cloud Stream_ dependencies. Yes, that link added them for you 😜.
1. Click _Generate_
1. Unzip the downloaded zip file and import into your IDE as a Maven project
1. Open _DemoApplication.java_ and add the following Spring bean ```java
   @Bean
   public Function<String, String> uppercase()
   {
      return v -> {
          System.out.println("Uppercasing: " + v);
          return v.toUpperCase();
      };
   }
```
1. **\***Save and Run 
1. 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.
1. On the Subscriber side, click _Connect_, type `uppercase-out-0` in the _Subscribe to a topic to receive direct messages_ box and click _Subscribe_.
1. On the Publisher side click _Connect_, type `uppercase-in-0` as the topic to publish to and click the _Publish_ button.
1. Boom! You’re done! Was that simple enough?

<sub><strong>*</strong>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!</sub>

<iframe src="//www.youtube.com/embed/FWgkl3S1zA4" width="560" height="314" allowfullscreen="allowfullscreen"></iframe>

## 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?

- Read about [how to use AsyncAPI to generate a Cloud Stream app](https://solace.com/blog/asyncapi-codegen-microservices-using-spring-cloud-stream/)
- Learn [how Solace works with Spring Initializr at start.spring.io](https://www.solace.dev/start-spring-io-help/)
- Check out this [codelab](https://codelabs.solace.dev/codelabs/spring-cloud-stream-basics/#0) for some more Cloud Stream Fundamentals and to extend what you did here with Function Composition:  
- Code for the open source Solace PubSub+ Spring Cloud Stream Binder can be found in the github repository.
{% github SolaceProducts/solace-spring-cloud no-readme %}
- Watch [our recent Live Coding session](https://youtu.be/3iUGQL6Zc7U?t=1155) featuring Oleg, a lead developer on the Spring Cloud Stream project.
{% youtube 3iUGQL6Zc7U %}



The post [Getting Started with Spring Cloud Stream using Spring Initializr](https://solace.com/blog/getting-started-spring-cloud-stream-and-spring-initializr/) appeared first on the [Solace Blog](https://solace.com).