6

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());
    }

而在客户端,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));
    }

但是怎么用requestChannel 以 Spring 方式的metadataPush模型(使用消息传递基础架构)?

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

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

4

1 回答 1

3

参考示例

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

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

<…>

<…>

Spring Framework 5.2.0.M1 现已推出

渠道互动模型

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

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

元数据推送

@MessageMapping目前,注释似乎不支持它。

于 2019-08-16T05:38:00.523 回答