Waldemar Panas
Posted on January 20, 2020
In this article I will present you how to use RSocket with Spring Boot.
What is RSocket?
RSocket is a bi-directional message-driven communication protocol. It features an advanced communication flow beyond standard request/response. You can response with a single message, a stream of messages or don’t response at all. A server can also start a communication on an established channel with a client.
RSocket allows you to communicate using following transport protocols:
- TCP
- WebSocket
- Argon
- HTTP/2 Stream
If you want to know more, you can watch Ben Hale’s presentation from Spring I/0 2019.
RSocket TCP Server
RSocket server is the easiest part of the whole project. All you need to do is to go to Spring Initializr and select Spring Boot, at least version 2.2.x. Then, select RSocket as a dependency.
To configure RSocket server with TCP protocol, add following lines to application.properties
. This configuration will open RSocket at port 7000.
spring.rsocket.server.transport=tcp
spring.rsocket.server.port=7000
Last step required is to add some controllers and you are ready with the server. You can try something like that:
@Controller
class ServerController {
@MessageMapping("queue")
fun helloQueue(incomingMessage: Message): Mono<Message>
= Mono.just(Message("Hello ${incomingMessage.message}"))
}
data class Message(val message: String)
RSocket TCP Client
Client application is very similar. Go to Spring Initializr and bootstrap it with RSocket
and Spring Reactive Web
.
To communicate with RSocket over a TCP port 7000
, you need to create a RSocketRequester
. For the TCP protocol it can look like this.
@Configuration
class ClientConfiguration {
@Bean
internal fun rSocketRequester(rSocketStrategies: RSocketStrategies): RSocketRequester
= RSocketRequester.builder()
.rsocketStrategies(rSocketStrategies)
.connect(TcpClientTransport.create(7000))
.block()!!
}
Your client application can be a standard reactive web application that uses HTTP. To test communication with configured server add a following endpoint.
@RestController
class ClientController(private val rSocketRequester: RSocketRequester) {
@GetMapping("/hello/{name}")
fun hello(@PathVariable("name") name: String): Mono<String> = rSocketRequester.route("queue")
.data(Message(name))
.retrieveMono(Message::class.java)
.map { it.message }
}
data class Message(val message: String)
RSocket WebSocket server
WebSocket requires you to add Spring Reactive Web
dependency to server application. RSocket port will be the same as Web application port. Instead configuring port, you need to define mapping for RSocket endpoint. In this example, you will use /rsocket
path. Configure following properties in application.properties
file.
spring.rsocket.server.transport=websocket
spring.rsocket.server.mapping-path=/rsocket
Controllers that you created for TCP server don’t need to change.
RSocket WebSocket client
Your server might be already started at port 8080
. Change client port to another in application.properties
, i.e. 8081
.
server.port=8081
Configuration for client with WebSocket transport protocol is like previous one. All you need to do is provide ClientTransport
.
@Configuration
class ClientConfiguration {
@Bean
internal fun rSocketRequester(rSocketStrategies: RSocketStrategies): RSocketRequester {
val uri = URI.create("ws://localhost:8080/rsocket")
return RSocketRequester.builder()
.rsocketStrategies(rSocketStrategies)
.connect(WebsocketClientTransport.create(uri))
.block()!!
}
}
Controllers that you created for TCP client don’t need to change.
Summary
Spring Boot made it simple to create application that use RSocket. I hope that those examples show you how to do it. For more information about RSocket in Spring Boot visit official reference guide.
Posted on January 20, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.