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方式使用requestChannelmetadataPush模型(使用消息基础设施)?

示例代码在 Github更新:添加了requestChannel示例。

Update: SETUPMETADATA_PUSH 可以通过 @ConnectMapping。 Spring 安全 RSocket 可以保护 SETUPREQUEST.

参考范例

作为参考示例,让我们参考客户端到服务器的集成测试,特别是 ServerController class:spring-framework/RSocketClientToServerIntegrationTests.java (line 200) at 6d7bf8050fe710c5253e6032233021d5e025e1d5 · spring-projects/spring-framework · GitHub.

此提交已在发行说明中提及:

<…>

<…>

Spring Framework 5.2.0.M1 available now.

渠道互动模型

参考示例对应代码部分:

@MessageMapping("echo-channel")
Flux<String> echoChannel(Flux<String> payloads) {
    return payloads.delayElements(Duration.ofMillis(10)).map(payload -> payload + " async");
}

元数据推送

目前看来,@MessageMapping 注释不支持它。