本文介绍了使用 RSocket-Java for Spring Rsocket Server 的 rsocket 路由元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置路由元数据(当服务器使用 Spring Boot Rsocket 时,仅在负载中使用 RSocket-Java.

How do I setup routing metadata (in payload using just RSocket-Java when server is using Spring Boot Rsocket.

Fluxs = connection.flatMapMany(requester -> requester.requestStream(DefaultPayload.create(Some Message")))

服务器正在使用@MessageMapping("/route")

Server is using @MessageMapping("/route")

推荐答案

交互类型

SpringBoot 上使用 @MessageMapping 的 RSocket 交互类型是根据带注释的方法的签名决定的(更多信息在 spring 文档)

Interaction type

RSocket interaction type on SpringBoot using @MessageMapping is decided based on signature of annotated method (more info in spring docs)

假设它有签名:

@MessageMapping("/route")
Flux<String> getStreamOfStrings(String message) {...}

基于 spring docs 交互类型的基数表是请求流.

Based on cardinality table from spring docs interaction type is Request-Stream.

RSocket java 客户端需要为元数据指定 mime-type:

RSocket java client needs to have specified mime-type for metadata:

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

数据

数据将是简单的字符串:

Data

Data will be simple string:

ByteBuf data = ByteBufAllocator.DEFAULT.buffer().writeBytes("request msg".getBytes());

元数据

RSocket 中的路由定义为元数据扩展,需要与数据一起发送以指定路由.这是如何创建它的示例(请参阅包 io.rsocket.metadata)

Metadata

Routing in RSocket is defined as metadata extension and needs to be sent together with data to specify routing. Here is example how it can be created (see other classes in package 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);

这篇关于使用 RSocket-Java for Spring Rsocket Server 的 rsocket 路由元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 00:53