Backend Development 6 min read

Build Reactive RSocket Services with Spring Retrosocket: A Step‑by‑Step Guide

This article explains the background of RSocket and Dubbo 3.0, introduces the spring‑retrosocket project, and provides a detailed, code‑rich tutorial on creating an RSocket server and client in Spring Boot, including dependency setup, configuration, annotations, and testing examples.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Build Reactive RSocket Services with Spring Retrosocket: A Step‑by‑Step Guide

Background

Recently the hottest topic in the Chinese tech community is the release of Apache Dubbo 3.0. The most important feature of Dubbo 3 is the adoption of HTTP/2 as the underlying transport protocol and protobuf as the serialization protocol, a combination also used by gRPC. RSocket was not chosen as a complementary solution for reactive programming.

RSocket is a new, language‑agnostic, seventh‑layer application network protocol. It is a bidirectional, multiplexed, message‑based binary protocol with reactive‑stream back‑pressure. Besides the traditional Request/Response model of HTTP, RSocket also supports Fire‑And‑Forget (no response), Stream (one‑way stream), and Channel (bidirectional stream).

The fundamentals of RSocket can be studied in the article "RSocket | The Perfect Alternative to REST".

This article focuses on using the newly incubated Spring project spring‑retrosocket .

spring‑retrosocket provides an annotation‑driven RSocket client, shielding developers from the complexity of the rsocket‑java SDK.

Create RSocket Server

Create a Spring Boot project and add the required dependency.

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-rsocket&lt;/artifactId&gt;
&lt;/dependency&gt;</code>

Specify the RSocket server port.

<code>spring.rsocket.server.port=8848</code>

Define the request‑response handling channel using

@MessageMapping

to set the route.

<code>@Controller
public class GreetingsController {
  @MessageMapping("request-response")
  Mono&lt;String&gt; reqResponse(@Payload String payload) {
    log.info("Received RR request: {}", payload);
    return Mono.just("Hello, " + payload);
  }
}
</code>

2. Create Client with spring‑retrosocket

Generate a new project with Spring Initializr.

Dependency

Version

spring‑retrosocket

0.0.1‑SNAPSHOT

Spring Boot

2.5.2

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.retrosocket&lt;/groupId&gt;
  &lt;artifactId&gt;spring-retrosocket&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;</code>

Add the Spring Maven repositories.

<code>&lt;repositories&gt;
  &lt;repository&gt;
    &lt;id&gt;spring-milestones&lt;/id&gt;
    &lt;name&gt;Spring Milestones&lt;/name&gt;
    &lt;url&gt;https://repo.spring.io/milestone&lt;/url&gt;
  &lt;/repository&gt;
  &lt;repository&gt;
    &lt;id&gt;spring-snapshots&lt;/id&gt;
    &lt;name&gt;Spring Snapshots&lt;/name&gt;
    &lt;url&gt;https://repo.spring.io/snapshot&lt;/url&gt;
    &lt;snapshots&gt;
      &lt;enabled&gt;true&lt;/enabled&gt;
    &lt;/snapshots&gt;
  &lt;/repository&gt;
&lt;/repositories&gt;</code>

3. Basic Usage

Enable RSocket client support in your Java code with

@EnableRSocketClient

and define an

RSocketRequester

bean.

<code>@SpringBootApplication
@EnableRSocketClients
class RSocketClientApplication {
  @Bean
  RSocketRequester requester(RSocketRequester.Builder builder) {
    return builder.connectTcp("localhost", 8888).block();
  }
}
</code>

Define an RSocket client interface similar to FeignClient .

<code>@RSocketClient
interface GreetingClient {
  @MessageMapping("request-response")
  Mono&lt;GreetingResponse&gt; requestResponse(Mono&lt;String&gt; name);
}
</code>

Test the client.

<code>@SpringBootTest
class DemoApplicationTests {
  @Autowired
  private GreetingClient greetingClient;

  @Test
  void testGreetingClient() {
    Mono&lt;String&gt; stringMono = greetingClient.requestResponse(Mono.just("lengleng"));
    System.out.println(stringMono.block());
  }
}
</code>

spring‑retrosocket source code is available at https://github.com/spring-projects-experimental/spring-retrosocket .

backendJavaSpring BootReactiveRSocketspring-retrosocket
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.