RSocket in Spring Boot 2.2

Author: Shazin Sadakath


 

We have talked about Reactive Programming numerous times here at Techtalks.lk in the following here, here and here since 2017. And we have talked about Reactive Programming Frameworks such as Spring WebFlux in posts here and here. Keeping up with the tradition, today we are going to look at the newest paradigm in Reactive Programming, Reactive Socket - RSocket in short.

RSocket is a bi-directional, multiplexed, message-based, binary protocol based on Reactive Streams back pressure. Also it is transport agnostic meaning it can use TCP, UDP, WebSocket, HTTP/2 to communicate. Let's look at this in detail.

RSocket provides following methods of communication as of writing this article;

RSocket vs HTTP

RSocket Efficient and Responsive HTTP Slowly Improving
Single, shared long-lived connection New connection per request (HTTP 1.0)
Multiplexes messages Pipelines messages (HTTP 1.1)
Communicates back pressure Does not communicate back pressure
Either party can initiate requests (flexible requester/responder roles) Only client can initiate requests (fixed client/server roles)
Supports canceling/resuming streams Does not support canceling/resuming streams

Multiplexed

Reactive Streams Back Pressure

Bi-Directional

Message Driven Binary Protocol

Demonstration

To demonstrate these cababilities in practical terms, we have written a Taxi Hailing API with two Spring Boot 2.2 Microservices which communicate using RSocket.

The producer microservice runs a RSocket server and listens for RSocket client messages to process them and respond. While the consumer microservice runs a standard Web Server (Tomcat/Jetty etc.) and a RSocket clients to connect to producer. The consumer also exposes a REST API which enables standard outside connections over HTTP.

There are two types of requests, one is standard request response and the other is Server Sent Events streaming.

Following shows the request response via the consumer REST API

taxis/Nano/Colombo-7/Colombo-3

{"latitude":61.39493,"longitude":23.30044,"driverName":"Driver 1","message":"A Nano is on the way to go from Colombo-7 to Colombo-3 @ 2019-09-11T08:53:14.656584400Z"}

 

And following shows the streaming SSEs.

/taxis/sse/Nano/Colombo-7/Colombo-3

data:{"latitude":2.9834009716548326,"longitude":11.789483737363046,"driverName":"Driver 0","message":"A Nano is on the way to go from Colombo-7 to Colombo-3 @ 2019-09-11T08:54:37.209294600Z"}

data:{"latitude":6.047541948429936,"longitude":11.009578387402208,"driverName":"Driver 1","message":"A Nano is on the way to go from Colombo-7 to Colombo-3 @ 2019-09-11T08:54:38.209020600Z"}

data:{"latitude":4.656551692518654,"longitude":8.693834735837799,"driverName":"Driver 2","message":"A Nano is on the way to go from Colombo-7 to Colombo-3 @ 2019-09-11T08:54:39.210028100Z"}

data:{"latitude":3.7343685236915807,"longitude":11.282951670396018,"driverName":"Driver 3","message":"A Nano is on the way to go from Colombo-7 to Colombo-3 @ 2019-09-11T08:54:40.209030400Z"}

data:{"latitude":2.0276712745328465,"longitude":7.427149202964668,"driverName":"Driver 4","message":"A Nano is on the way to go from Colombo-7 to Colombo-3 @ 2019-09-11T08:54:41.208422700Z"}

data:{"latitude":9.31958758416886,"longitude":17.163145161972913,"driverName":"Driver 5","message":"A Nano is on the way to go from Colombo-7 to Colombo-3 @ 2019-09-11T08:54:42.208576600Z"}

data:{"latitude":10.903687677488112,"longitude":10.29561403575638,"driverName":"Driver 6","message":"A Nano is on the way to go from Colombo-7 to Colombo-3 @ 2019-09-11T08:54:43.210079800Z"}

data:{"latitude":10.466338653039967,"longitude":3.2158017641712124,"driverName":"Driver 7","message":"A Nano is on the way to go from Colombo-7 to Colombo-3 @ 2019-09-11T08:54:44.210238900Z"}

.....

In case of error in this invalid vehicle type (not nano or mini) error message can also be sent as response transparently.

taxis/Van/Colombo-7/Colombo-3

{"latitude":null,"longitude":null,"driverName":null,"message":"Only Taxi types Nano and Mini supported"}

Source code can be found at https://github.com/shazin/rsocket-spring-boot

Conclusion

This is a promising technology as it improves latency, performance drastically while reducing bandwidth usage, CPU cycles etc. We can expect many more applications of this technology in the future as this matures.

References



Tags: RSocket SpringBoot 2 Reactive Socket REST TCP UDP
Views: 223
Register for more exciting articles

Comments

Please login or register to post a comment.


There are currently no comments.