交互类型
SpringBoot 上使用@MessageMapping 的RSocket 交互类型是根据注解方法的签名决定的(更多信息在spring docs)
假设它有签名:
@MessageMapping("/route")
Flux<String> getStreamOfStrings(String message) {...}
基于spring docs交互类型的基数表是Request-Stream。
RSocket 客户端
RSocket java 客户端需要为元数据指定 mime-type:
RSocket rsocketClient = RSocketConnector.create()
//metadata header needs to be specified
.metadataMimeType(WellKnownMimeType.MESSAGE_RSOCKET_COMPOSITE_METADATA.getString())
// value of spring.rsocket.server.port eg 7000
.connect(TcpClientTransport.create(7000))
.block();
数据
数据将是简单的字符串:
ByteBuf data = ByteBufAllocator.DEFAULT.buffer().writeBytes("request msg".getBytes());
元数据
RSocket中的路由定义为metadata extension,需要与数据一起发送来指定路由。这是如何创建它的示例(请参阅包中的其他类 io.rsocket.metadata)
CompositeByteBuf metadata = ByteBufAllocator.DEFAULT.compositeBuffer();
RoutingMetadata routingMetadata = TaggingMetadataCodec.createRoutingMetadata(ByteBufAllocator.DEFAULT, List.of("/route"));
CompositeMetadataCodec.encodeAndAddMetadata(metadata,
ByteBufAllocator.DEFAULT,
WellKnownMimeType.MESSAGE_RSOCKET_ROUTING,
routingMetadata.getContent());
请求流请求
创建了数据和元数据,因此您可以使用以下命令执行requestSteam:
rsocketClient.requestStream(DefaultPayload.create(data, metadata))
.map(Payload::getDataUtf8)
.toIterable()
.forEach(System.out::println);