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.
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><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-rsocket</artifactId>
</dependency></code>Specify the RSocket server port.
<code>spring.rsocket.server.port=8848</code>Define the request‑response handling channel using
@MessageMappingto set the route.
<code>@Controller
public class GreetingsController {
@MessageMapping("request-response")
Mono<String> 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><dependency>
<groupId>org.springframework.retrosocket</groupId>
<artifactId>spring-retrosocket</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency></code>Add the Spring Maven repositories.
<code><repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories></code>3. Basic Usage
Enable RSocket client support in your Java code with
@EnableRSocketClientand define an
RSocketRequesterbean.
<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<GreetingResponse> requestResponse(Mono<String> name);
}
</code>Test the client.
<code>@SpringBootTest
class DemoApplicationTests {
@Autowired
private GreetingClient greetingClient;
@Test
void testGreetingClient() {
Mono<String> 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 .
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.