Getting Started with RSocket using Spring Boot 2.2

Mihkel Selgal
Nov 7 · 2 min read

RSocket is an application protocol providing Reactive Streams semantics — it functions, for example, as an alternative to HTTP.

The main functionalities RSocket provides are:

  • request/response: stream of 1
  • request/stream: finite stream of many
  • fire-and-forget: no response
  • event subscription: infinite stream of many)

In the following tutorial I try to create RSocket consumer/prodducer in single applicattion based on final implementation provided in Spring Boot 2.2.

Getting started

To get started head over to start.spring.io and let’s create a new Spring Boot project with following starters.

  • Spring Reactive Web for our REST endpoints
  • RSocket to get RSocket functionality
  • Lombok is optional but it will take much of the verbosity out of Java coding so I recommend using it

The dependencies section for your build.gradle dependencies section should look something similar to this:

dependencies { 
implementation 'org.springframework.boot:spring-boot-starter-rsocket'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
// OPTIONAL: only if you want to use Lombok in your project
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}

Now that we have done with setup we can start coding — finally!

Configuring the consumer RSocketRequester

RSocket Spring Boot autoconfiguration will create server setup as long you provide spring.rsocket.server.port in the application.yml file. As shown below:

spring:
rsocket:
server:
port: 7000
address: localhost

For consumer we need to create small none blocking configuration using the RSocketRequester.Builder

That’s it! You are now ready to code your RSocket producer/consumer application.

RSocket prodcuer

We will build Hello World application of Spring Boot and REST world called the greeting application. First we need to create request and response models.

Request:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class GreetingRequest {
String name;
}

Response:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class GreetingResponse {
String greeting;
}

Now we can creae the producer

RSocket consumer

To consume the messages we will create a simple REST controller to send the name and display the response from RSocket producer.

That is all you need to create RSocket consumer/producer you now have REST application running on port 8080 and RSocket server running on port 7000 in the same application.

Wrap up

All the code in code can be found in single Github Gist here: https://gist.github.com/mihkels/57c1b3f155c27f6d5d752d101b132cf1

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade