Add RSocket server support with Spring Messaging

This commit adds support for RSocket server applications.
The auto-configuration will either add RSocket support to an existing
Reactor Netty server in a WebFlux application (as a WebSocket endpoint),
or bootstrap a brand new RSocket server instance.

Spring Boot will also auto-configure the Spring Messaging infrastructure
that supports Controller beans with `@MessageMapping` annotated methods.

Fixes gh-16021
This commit is contained in:
Brian Clozel
2019-04-11 15:44:17 +02:00
parent 5e58f4a8c6
commit b33944b53f
28 changed files with 1776 additions and 6 deletions

View File

@@ -63,6 +63,10 @@ include::{generated-resources-root}/config-docs/server.adoc[]
include::{generated-resources-root}/config-docs/security.adoc[]
=== RSocket properties
include::../../../target/generated-resources/config-docs/rsocket.adoc[]
=== Actuator properties
include::{generated-resources-root}/config-docs/actuator.adoc[]

View File

@@ -3437,6 +3437,83 @@ both clients and servers.
You can learn more about the resource configuration on the client side in the
<<boot-features-webclient-runtime, WebClient Runtime section>>.
[[boot-features-rsocket]]
== RSocket
https://rsocket.io[RSocket] is a binary protocol for use on byte stream transports.
It enables symmetric interaction models via async message passing over a single connection.
Spring Framework, with the Spring Messaging module supports RSocket both on the server and
the client side. On the server side, it lets you create special `@Controller` beans
to handle incoming RSocket messages.
Methods in your controller are mapped to RSocket routes by using `@MessageMapping` annotations.
The following code shows a typical `@Controller`:
[source,java,indent=0]
----
@Controller
public class MyRSocketController {
@MessageMapping("chat.room.{name}")
public Flux<ChatMessages> enterChatRoom(@DestinationVariable String chatRoom,
Flux<ChatMessages> messages) {
// ...
}
@MessageMapping("users.{user}.info")
Mono<ChatUserInfo> getUserInfo(@DestinationVariable String user) {
// ...
}
}
----
[[boot-features-rsocket-server-auto-configuration]]
=== RSocket server Auto-configuration
Spring Boot provides auto-configuration for RSocket servers. The required dependencies
are provided by the `spring-boot-starter-rsocket`.
Spring Boot will start an RSocket server as a new embedded server in your application,
or will plug the RSocket infrastructure into an existing reactive Web server. This
depends on the type of application and its configuration.
In case of a WebFlux application (i.e. of type `WebApplicationType.REACTIVE`), the
RSocket server will be plugged into the existing Web Server only if the following
properties match:
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
----
spring.rsocket.server.mapping-path=/rsocket # a mapping path is defined
spring.rsocket.server.transport=websocket # websocket is chosen as a transport
#spring.rsocket.server.port= # no port is defined
----
The only other way to create an RSocket server is to start an independent, embedded
RSocket server. Besides the dependency requirements, the only required configuration
is to define a port for that server:
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
----
spring.rsocket.server.port=9898 # the only required configuration
spring.rsocket.server.transport=tcp # you're free to configure other properties
----
[[boot-features-rsocket-messaging]]
=== Spring Messaging RSocket support
Spring Boot will auto-configure the Spring Messaging infrastructure for RSocket.
An `RSocketStrategies` bean is created to provide encoding and decoding support
for RSocket messages. By default, Spring Boot will try to auto-configure JSON
support with Jackson for `application/json` and `"application/*+json"` media types.
Check out the <<boot-features-json-jackson,Jackson support section>> to know more
about customization possibilities.
Developers can create `RSocketStrategiesCustomizer` beans to add other strategies,
assuming there are `Encoder` and `Decoder` implementations available.
[[boot-features-security]]

View File

@@ -40,6 +40,8 @@ def generateConfigMetadataDocumentation() {
"spring.mvc", "spring.resources", "spring.webflux")
.addSection("json")
.withKeyPrefixes("spring.jackson", "spring.gson")
.addSection("rsocket")
.withKeyPrefixes("spring.rsocket")
.addSection("templating")
.withKeyPrefixes("spring.freemarker", "spring.groovy", "spring.mustache", "spring.thymeleaf")
.addOverride("spring.groovy.template.configuration", "See GroovyMarkupConfigurer")