Spring Boot中如何映射RSocket的所有交互模型
How to map all interaction models of RSocket in Spring Boot
RSocket中提供了4种交互模型。
- 即刻即忘
- 请求和响应
- 请求流
- 请求频道
- (元数据推送)
Spring(and Spring Boot) 提供 RSocket 集成,可以很容易地使用现有的消息传递基础设施构建 RSocket 服务器以隐藏原始 RSocket API。
@MessageMapping("hello")
public Mono<Void> hello(Greeting p) {
log.info("received: {} at {}", p, Instant.now());
return Mono.empty();
}
@MessageMapping("greet.{name}")
public Mono<String> greet(@DestinationVariable String name, @Payload Greeting p) {
log.info("received: {}, {} at {}", name, p, Instant.now());
return Mono.just("Hello " + name + ", " + p.getMessage() + " at " + Instant.now());
}
@MessageMapping("greet-stream")
public Flux<String> greetStream(@Payload Greeting p) {
log.info("received: {} at {}", p, Instant.now());
return Flux.interval(Duration.ofSeconds(1))
.map(i -> "Hello #" + i + "," + p.getMessage() + " at " + Instant.now());
}
而在客户端,有一个RescoketRequester
提供与服务器握手
@GetMapping("hello")
Mono<Void> hello() {
return this.requester.route("hello").data(new Greeting("Welcome to Rsocket")).send();
}
@GetMapping("name/{name}")
Mono<String> greet(@PathVariable String name) {
return this.requester.route("greet." + name).data(new Greeting("Welcome to Rsocket")).retrieveMono(String.class);
}
@GetMapping(value = "stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<String> greetStream() {
return this.requester.route("greet-stream").data(new Greeting("Welcome to Rsocket"))
.retrieveFlux(String.class)
.doOnNext(msg -> log.info("received messages::" + msg));
}
但是如何以Spring方式使用requestChannel和metadataPush模型(使用消息基础设施)?
示例代码在 Github。 更新:添加了requestChannel示例。
Update: SETUP 和 METADATA_PUSH 可以通过 @ConnectMapping
。 Spring 安全 RSocket 可以保护 SETUP 和 REQUEST.
参考范例
作为参考示例,让我们参考客户端到服务器的集成测试,特别是 ServerController
class:spring-framework/RSocketClientToServerIntegrationTests.java (line 200) at 6d7bf8050fe710c5253e6032233021d5e025e1d5 · spring-projects/spring-framework · GitHub.
此提交已在发行说明中提及:
<…>
- RSocket support including response handling via annotated
@MessageMapping
methods and performing requests via RSocketRequester
.
<…>
渠道互动模型
参考示例对应代码部分:
@MessageMapping("echo-channel")
Flux<String> echoChannel(Flux<String> payloads) {
return payloads.delayElements(Duration.ofMillis(10)).map(payload -> payload + " async");
}
元数据推送
目前看来,@MessageMapping
注释不支持它。
RSocket中提供了4种交互模型。
- 即刻即忘
- 请求和响应
- 请求流
- 请求频道
- (元数据推送)
Spring(and Spring Boot) 提供 RSocket 集成,可以很容易地使用现有的消息传递基础设施构建 RSocket 服务器以隐藏原始 RSocket API。
@MessageMapping("hello")
public Mono<Void> hello(Greeting p) {
log.info("received: {} at {}", p, Instant.now());
return Mono.empty();
}
@MessageMapping("greet.{name}")
public Mono<String> greet(@DestinationVariable String name, @Payload Greeting p) {
log.info("received: {}, {} at {}", name, p, Instant.now());
return Mono.just("Hello " + name + ", " + p.getMessage() + " at " + Instant.now());
}
@MessageMapping("greet-stream")
public Flux<String> greetStream(@Payload Greeting p) {
log.info("received: {} at {}", p, Instant.now());
return Flux.interval(Duration.ofSeconds(1))
.map(i -> "Hello #" + i + "," + p.getMessage() + " at " + Instant.now());
}
而在客户端,有一个RescoketRequester
提供与服务器握手
@GetMapping("hello")
Mono<Void> hello() {
return this.requester.route("hello").data(new Greeting("Welcome to Rsocket")).send();
}
@GetMapping("name/{name}")
Mono<String> greet(@PathVariable String name) {
return this.requester.route("greet." + name).data(new Greeting("Welcome to Rsocket")).retrieveMono(String.class);
}
@GetMapping(value = "stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<String> greetStream() {
return this.requester.route("greet-stream").data(new Greeting("Welcome to Rsocket"))
.retrieveFlux(String.class)
.doOnNext(msg -> log.info("received messages::" + msg));
}
但是如何以Spring方式使用requestChannel和metadataPush模型(使用消息基础设施)?
示例代码在 Github。 更新:添加了requestChannel示例。
Update: SETUP 和 METADATA_PUSH 可以通过 @ConnectMapping
。 Spring 安全 RSocket 可以保护 SETUP 和 REQUEST.
参考范例
作为参考示例,让我们参考客户端到服务器的集成测试,特别是 ServerController
class:spring-framework/RSocketClientToServerIntegrationTests.java (line 200) at 6d7bf8050fe710c5253e6032233021d5e025e1d5 · spring-projects/spring-framework · GitHub.
此提交已在发行说明中提及:
<…>
- RSocket support including response handling via annotated
@MessageMapping
methods and performing requests viaRSocketRequester
.<…>
渠道互动模型
参考示例对应代码部分:
@MessageMapping("echo-channel")
Flux<String> echoChannel(Flux<String> payloads) {
return payloads.delayElements(Duration.ofMillis(10)).map(payload -> payload + " async");
}
元数据推送
目前看来,@MessageMapping
注释不支持它。