Developing reactive microservice using Springboot 2

Reactive Spring is based on the project reactor for building non-blocking applications using the spring platform and spring cloud frameworks. Three important interfaces available in Reactor are Publisher, Subscriber, and Processor.

Publisher – source of the data

Subscriber – One which receives data asynchronously

Processor – nothing but a publisher which is also a subscriber (most of the time we won’t need this)

Flux – represents multiple sequences of a result 0…N (many items) which suggests Flux is a standard publisher

Mono – As the name suggests, represents single sequence of a result either an empty result or one result 0…1 which suggests Mono will emit at most one item

Starting from Spring 5 and Spring Boot 2 reactive core is completely supported and provides an excellent long-awaited alternative to Akka Streams. If you are coming from a spring background and uses it extensively there is no reason why you shouldn’t try this. However, it is great for people who begin with Spring platform as well.

This post is hugely inspired by Josh Long’s webinar on Reactive streams which you can find here. We are going to use Kotlin for our demo and sample project.

To begin with visit start.spring.io or use the spring initializer in STS or Intellij. Select the Springboot version 2.0.0 snapshot and above and the language as Kotlin. Once you have done that, select the reactive dependencies “Reactive-Web” and “Reactive Mongo” and import the same to your favorite IDE. Please refer the below the screenshot if you have any doubts.

We are using gradle in our example and the dependencies in my build.gradle file looks like this.

In the above code, the first two functions are self-explanatory and the third one will generate events. Flux.genreate publishes the data created the subscriber in which we will be using a Synchronous Sink which will emit data one-by-one and its callback method next() can be called at most only once per callback invocation. delayElements() will delay the signal for the specified time un its.

Function events is going to produce an array of Text Event Stream which is an implementation of SSE (Server Sent Events) specification. SSE events are unidirectional unlike websockets and sent over the traditional HTTP. If you do not have MongoDB installed you can use a docker version and run it using

As the reactive Springboot service ready and now need a consumer for it. Let’s go ahead and create a Web client which will listen to these events. Follow the same steps above from the start.spring.io and create a client service.

We can use the Spring Rest template and invoke our service but instead of doing that we are goin to use Webclient, which provides a non-blocking way of performing HTTP requests originally part of the Spring Web Flux project but included in the Spring 5 and uses Netty.
Using the Application runner, we will be invoking the service and subscribe to it.

About Me

I am a technologist interested in Java, Kotlin, Javascript, Scala and Go programming languages and an open source enthusiast. I currently work at Ascend Money as Software Architect with focus area as Spring Cloud stack. I like containers, scalability and developing high-performance applications.