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示例。
更新:SETUP和METADATA_PUSH可以由@ConnectMapping
. 而 Spring Security RSocket 可以保护SETUP和REQUEST。
作为参考示例,让我们参考客户端到服务器集成测试,特别是类ServerController
:spring-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@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目前看来,注释不支持它@MessageMapping
。
归档时间: |
|
查看次数: |
647 次 |
最近记录: |