首页 > 解决方案 > RSocket Net客户端请求流路由元数据到spring boot @MessageMapping路由

问题描述

类似于为 Spring Rsocket Server 使用 RSocket-Java 的 rsocket 路由元数据,但对于 RSocket Net Client,我们使用 spring boot @MessageMapping 用于端口 7000 上的 websocket 端点路由,这取决于路由返回 webfluxes。例如:

@MessageMapping(value = "helloWorld")
public Flux<String> getFluxSocket() {
    log.traceEntry();
    log.info("In hello world");
    
    return Flux.just("{\"Hello\":\"World\"}");
}

当 Spring Boot 服务器在本地运行时,要获得此通量,您可以使用rsc 客户端

java -jar rsc.jar --debug --request --route helloWorld ws://localhost:7000

或者对于流

java -jar rsc.jar --debug --stream --route myStream ws://localhost:7000

要在 C# Net 中以编程方式执行此操作,它在此处表示 RSocket Net 尚不支持请求路由,但可以使用元数据有效负载。有没有人有这样的网络?

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

谢谢

标签: rsocketrsocket-java

解决方案


在 .NET 库正式支持路由/复合元数据之前,您可以实现路由元数据。如果您不需要发送除路由元数据以外的任何元数据,则无需创建复合元数据。仅发送路由元数据非常简单。

从规范中可以看出,只需将路由名称的长度添加到第一个字节即可。 https://github.com/rsocket/rsocket/blob/master/Extensions/Routing.md

我对 .NET 一无所知,所以我将向您展示如何用 Java 和 JavaScript 来实现它。供参考。

https://github.com/making/demo-rsocket/blob/master/vanilla-rsocket-client/src/main/java/com/example/vanillarsocketclient/VanillaRsocketClientApplication.java

static ByteBuffer routingMetadata(String tag) {
    final byte[] bytes = tag.getBytes(StandardCharsets.UTF_8);
    final ByteBuffer buffer = ByteBuffer.allocate(1 + bytes.length);
    buffer.put((byte) bytes.length);
    buffer.put(bytes);
    buffer.flip();
    return buffer;
}

https://github.com/making/demo-rsocket-jsug/blob/master/frontend/vanilla/src/index.js

const routingMetadata = (route) => {
    return String.fromCharCode(route.length) + route;
};

推荐阅读