Spring Boot中如何映射RSocket的所有交互模型

Han*_*tsy 6 java spring spring-boot rsocket

RSocket 提供了 4 种交互模型。

  • 火与忘记
  • 请求和响应
  • 请求流
  • 请求通道
  • (元数据推送)

Spring(和 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());
    }
Run Code Online (Sandbox Code Playgroud)

在客户端,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));
    }
Run Code Online (Sandbox Code Playgroud)

但是怎么用 requestChannel Spring 方式中的metadataPush模型(使用消息传递基础设施)?

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

更新SETUPMETADATA_PUSH可以由@ConnectMapping. 而 Spring Security RSocket 可以保护SETUPREQUEST

Ser*_*nov 3

参考示例

\n\n

作为参考示例,让我们参考客户端到服务器集成测试,特别是类ServerControllerspring-framework/RSocketClientToServerIntegrationTests.java (第 200 行),位于 6d7bf8050fe710c5253e6032233021d5e025e1d5 \xc2\xb7 spring-projects/spring -framework \xc2\xb7 GitHub

\n\n

发行说明中提到了此提交:

\n\n
\n

<\xe2\x80\xa6>

\n\n \n\n

<\xe2\x80\xa6>

\n\n

\xe2\x80\x94 Spring Framework 5.2.0.M1 现已推出

\n
\n\n

渠道互动模型

\n\n

参考示例对应的代码部分:

\n\n
@MessageMapping("echo-channel")\nFlux<String> echoChannel(Flux<String> payloads) {\n    return payloads.delayElements(Duration.ofMillis(10)).map(payload -> payload + " async");\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

元数据推送

\n\n

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

\n