RSocket
RSocket 是用于字节流传输的二进制协议。它通过单个连接的异步消息传递来启用对称交互模型。
RSocket is a binary protocol for use on byte stream transports. It enables symmetric interaction models through async message passing over a single connection.
Spring Framework 的 spring-messaging
模块为 RSocket 请求者和响应者提供了支持,包括客户端和服务器端。有关更多详细信息,包括 RSocket 协议的概述,请参阅 Spring Framework 参考的 {url-spring-framework-docs}/rsocket.html#rsocket-spring[RSocket 部分]。
The spring-messaging
module of the Spring Framework provides support for RSocket requesters and responders, both on the client and on the server side.
See the {url-spring-framework-docs}/rsocket.html#rsocket-spring[RSocket section] of the Spring Framework reference for more details, including an overview of the RSocket protocol.
RSocket Strategies Auto-configuration
Spring Boot 自动配置了一个 RSocketStrategies
bean,它提供了对 RSocket 有效负载进行编码和解码所需的所有基础设施。默认情况下,自动配置将尝试按以下顺序配置(顺序):
Spring Boot auto-configures an RSocketStrategies
bean that provides all the required infrastructure for encoding and decoding RSocket payloads.
By default, the auto-configuration will try to configure the following (in order):
-
CBOR codecs with Jackson
-
JSON codecs with Jackson
spring-boot-starter-rsocket
starter 提供了这两个依赖关系。请参阅 Jackson support section 以了解有关自定义可能性的更多信息。
The spring-boot-starter-rsocket
starter provides both dependencies.
See the Jackson support section to know more about customization possibilities.
开发人员可以通过创建实现 RSocketStrategiesCustomizer
接口的 bean 来自定义 RSocketStrategies
组件。请注意它们的 @Order
很重要,因为它决定了编解码器的顺序。
Developers can customize the RSocketStrategies
component by creating beans that implement the RSocketStrategiesCustomizer
interface.
Note that their @Order
is important, as it determines the order of codecs.
RSocket server Auto-configuration
Spring Boot 提供 RSocket 服务器自动配置。必需的依赖关系由 spring-boot-starter-rsocket
提供。
Spring Boot provides RSocket server auto-configuration.
The required dependencies are provided by the spring-boot-starter-rsocket
.
Spring Boot 允许通过 WebFlux 服务器或建立独立的 RSocket 服务器在 WebSocket 上公开 RSocket。这取决于应用程序的类型及其配置。
Spring Boot allows exposing RSocket over WebSocket from a WebFlux server, or standing up an independent RSocket server. This depends on the type of application and its configuration.
对于 WebFlux 应用程序(类型为 WebApplicationType.REACTIVE
),仅当以下属性匹配时,RSocket 服务器才会插入到 Web 服务器中:
For WebFlux application (that is of type WebApplicationType.REACTIVE
), the RSocket server will be plugged into the Web Server only if the following properties match:
spring: rsocket: server: mapping-path: "/rsocket" transport: "websocket"
将 RSocket 插件插入 web 服务器时仅支持 Reactor Netty,因为 RSocket 本身使用该库构建。
Plugging RSocket into a web server is only supported with Reactor Netty, as RSocket itself is built with that library.
或者,将 RSocket TCP 或 websocket 服务器启动为独立的嵌入式服务器。除了依赖项要求之外,唯一必需的配置是为该服务器定义端口:
Alternatively, an RSocket TCP or websocket server is started as an independent, embedded server. Besides the dependency requirements, the only required configuration is to define a port for that server:
spring: rsocket: server: port: 9898
Spring Messaging RSocket support
Spring Boot 将为 RSocket 自动配置 Spring Messaging 基础设施。
Spring Boot will auto-configure the Spring Messaging infrastructure for RSocket.
这意味着 Spring Boot 将创建一个 RSocketMessageHandler
bean,该 bean 将处理对应用程序的 RSocket 请求。
This means that Spring Boot will create a RSocketMessageHandler
bean that will handle RSocket requests to your application.
Calling RSocket Services with RSocketRequester
一旦在服务器和客户端之间建立了 RSocket
通道,任何一方都可以向另一方发送或接收请求。
Once the RSocket
channel is established between server and client, any party can send or receive requests to the other.
作为一台服务器,您可以向 RSocket @Controller
任何处理程序方法中的 RSocketRequester
实例注入。作为客户端,您需要首先配置并建立 RSocket 连接。Spring Boot 为此类情况自动配置了一个 RSocketRequester.Builder
,其中包含预期的编解码器并应用任何 RSocketConnectorConfigurer
bean。
As a server, you can get injected with an RSocketRequester
instance on any handler method of an RSocket @Controller
.
As a client, you need to configure and establish an RSocket connection first.
Spring Boot auto-configures an RSocketRequester.Builder
for such cases with the expected codecs and applies any RSocketConnectorConfigurer
bean.
RSocketRequester.Builder
实例是一个原型 bean,这意味着每个注入点将为您提供一个新实例。此操作是有意进行的,因为此生成器是有状态的,您不应使用同一实例创建具有不同设置的请求者。
The RSocketRequester.Builder
instance is a prototype bean, meaning each injection point will provide you with a new instance .
This is done on purpose since this builder is stateful and you should not create requesters with different setups using the same instance.
以下代码显示了一个典型的示例:
The following code shows a typical example: