RSocket for Spring Boot Microservices

Sajeer Babu
3 min readSep 21, 2019

Let’s make your microservices be friends to each other.

I have described how we can let our microservices talk to each other using GRPC on my last story.

Now, let me tell you another efficient way, which is using RSocket.

RSocket is a binary protocol for use on byte stream transports such as TCP, WebSockets, and Aeron.

It enables the following symmetric interaction models via async message passing over a single connection:

request/response (stream of 1)

request/stream (finite stream of many)

fire-and-forget (no response)

channel (bi-directional streams)

It supports session resumption, to allow resuming long-lived streams across different transport connections. This is particularly useful for mobile⬄server communication when network connections drop, switch, and reconnect frequently.
— It’s a pick from
RSocket’s Website

I found RSocket is much faster than GRPC, Trust me I got a pong in 10ms once when GRPC was between 20ms to 25ms.

So, Let’s code.

At first you have to create two spring boot projects.

  1. GRPC Server App and
  2. GRPC Client App

I’m depending Spring Initializr for this as it is much easier.

Server App

Add below dependencies to your pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-rsocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

Also these repositories.

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
The complete pom.xml

Add the server port number and the RSocket port number inside application.properties

server.port=8081
spring.rsocket.server.port=7000

Now create a new controller for RSocket requests.

The client will communicate with your server app using message mapping keywords.

Here, if the incoming keyword is ping, then the request will hit the ping method and will return a pong response.

That’s it for server app.

Client App

Add below dependencies to your pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-rsocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

Also these repositories.

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
The complete pom.xml

We need to create a configuration class for configuring RSocket and RSocketRequester.

With this configuration, it will communicate with the port 7000, we can use dynamic ports for multiple microservices.

Now let’s make a client side endpoint to ask ping to server app.

rSocketRequester.route("ping").data(new String()).retrieveMono(String.class)

The string “ping” is the message mapping keyword, the server will determine this keyword and will respond accordingly.

--

--