Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

rsocket routing metadata using RSocket-Java for Spring Rsocket Server

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

Flux<Payload> s = connection.flatMapMany(requester -> requester.requestStream(DefaultPayload.create("Some Message")))

Server is using @MessageMapping("/route")

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Interaction type

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

Let's assume it is having signature:

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

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

RSocket client

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

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

Request-stream request

Data and metadata are created so you can execute requestSteam using:

rsocketClient.requestStream(DefaultPayload.create(data, metadata))
    .map(Payload::getDataUtf8)
    .toIterable()
    .forEach(System.out::println);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...