Support for making requests via RSocketClient
Closes gh-25332
This commit is contained in:
@@ -176,80 +176,33 @@ symmetrically, to make requests from clients and to make requests from servers.
|
||||
[[rsocket-requester-client]]
|
||||
=== Client Requester
|
||||
|
||||
To obtain an `RSocketRequester` on the client side requires connecting to a server along with
|
||||
preparing and sending the initial RSocket `SETUP` frame. `RSocketRequester` provides a
|
||||
builder for that. Internally it builds on `io.rsocket.core.RSocketConnector`.
|
||||
To obtain an `RSocketRequester` on the client side is to connect to a server which involves
|
||||
sending an RSocket `SETUP` frame with connection settings. `RSocketRequester` provides a
|
||||
builder that helps to prepare an `io.rsocket.core.RSocketConnector` including connection
|
||||
settings for the `SETUP` frame.
|
||||
|
||||
This is the most basic way to connect with default settings:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
.Java
|
||||
----
|
||||
Mono<RSocketRequester> requesterMono = RSocketRequester.builder()
|
||||
.connectTcp("localhost", 7000);
|
||||
RSocketRequester requester = RSocketRequester.builder().tcp("localhost", 7000);
|
||||
|
||||
Mono<RSocketRequester> requesterMono = RSocketRequester.builder()
|
||||
.connectWebSocket(URI.create("https://example.org:8080/rsocket"));
|
||||
URI url = URI.create("https://example.org:8080/rsocket");
|
||||
RSocketRequester requester = RSocketRequester.builder().webSocket(url);
|
||||
----
|
||||
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
.Kotlin
|
||||
----
|
||||
import org.springframework.messaging.rsocket.connectTcpAndAwait
|
||||
import org.springframework.messaging.rsocket.connectWebSocketAndAwait
|
||||
val requester = RSocketRequester.builder().tcp("localhost", 7000)
|
||||
|
||||
val requester = RSocketRequester.builder()
|
||||
.connectTcpAndAwait("localhost", 7000)
|
||||
|
||||
val requester = RSocketRequester.builder()
|
||||
.connectWebSocketAndAwait(URI.create("https://example.org:8080/rsocket"))
|
||||
URI url = URI.create("https://example.org:8080/rsocket");
|
||||
val requester = RSocketRequester.builder().webSocket(url)
|
||||
----
|
||||
|
||||
The above is deferred. To actually connect and use the requester:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
.Java
|
||||
----
|
||||
// Connect asynchronously
|
||||
RSocketRequester.builder().connectTcp("localhost", 7000)
|
||||
.subscribe(requester -> {
|
||||
// ...
|
||||
});
|
||||
|
||||
// Or block
|
||||
RSocketRequester requester = RSocketRequester.builder()
|
||||
.connectTcp("localhost", 7000)
|
||||
.block(Duration.ofSeconds(5));
|
||||
----
|
||||
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
.Kotlin
|
||||
----
|
||||
// Connect asynchronously
|
||||
import org.springframework.messaging.rsocket.connectTcpAndAwait
|
||||
|
||||
class MyService {
|
||||
|
||||
private var requester: RSocketRequester? = null
|
||||
|
||||
private suspend fun requester() = requester ?:
|
||||
RSocketRequester.builder().connectTcpAndAwait("localhost", 7000).also { requester = it }
|
||||
|
||||
suspend fun doSomething() = requester().route(...)
|
||||
}
|
||||
|
||||
// Or block
|
||||
import org.springframework.messaging.rsocket.connectTcpAndAwait
|
||||
|
||||
class MyService {
|
||||
|
||||
private val requester = runBlocking {
|
||||
RSocketRequester.builder().connectTcpAndAwait("localhost", 7000)
|
||||
}
|
||||
|
||||
suspend fun doSomething() = requester.route(...)
|
||||
}
|
||||
----
|
||||
The above does not connect immediately. When requests are made, a shared connection is
|
||||
established transparently and used.
|
||||
|
||||
|
||||
[[rsocket-requester-client-setup]]
|
||||
@@ -291,16 +244,14 @@ can be registered as follows:
|
||||
.decoders(decoders -> decoders.add(new Jackson2CborDecoder()))
|
||||
.build();
|
||||
|
||||
Mono<RSocketRequester> requesterMono = RSocketRequester.builder()
|
||||
RSocketRequester requester = RSocketRequester.builder()
|
||||
.rsocketStrategies(strategies)
|
||||
.connectTcp("localhost", 7000);
|
||||
.tcp("localhost", 7000);
|
||||
----
|
||||
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
.Kotlin
|
||||
----
|
||||
import org.springframework.messaging.rsocket.connectTcpAndAwait
|
||||
|
||||
val strategies = RSocketStrategies.builder()
|
||||
.encoders { it.add(Jackson2CborEncoder()) }
|
||||
.decoders { it.add(Jackson2CborDecoder()) }
|
||||
@@ -308,7 +259,7 @@ can be registered as follows:
|
||||
|
||||
val requester = RSocketRequester.builder()
|
||||
.rsocketStrategies(strategies)
|
||||
.connectTcpAndAwait("localhost", 7000)
|
||||
.tcp("localhost", 7000)
|
||||
----
|
||||
|
||||
`RSocketStrategies` is designed for re-use. In some scenarios, e.g. client and server in
|
||||
@@ -334,9 +285,9 @@ infrastructure that's used on a server, but registered programmatically as follo
|
||||
SocketAcceptor responder =
|
||||
RSocketMessageHandler.responder(strategies, new ClientHandler()); // <2>
|
||||
|
||||
Mono<RSocketRequester> requesterMono = RSocketRequester.builder()
|
||||
RSocketRequester requester = RSocketRequester.builder()
|
||||
.rsocketConnector(connector -> connector.acceptor(responder)) // <3>
|
||||
.connectTcp("localhost", 7000);
|
||||
.tcp("localhost", 7000);
|
||||
----
|
||||
<1> Use `PathPatternRouteMatcher`, if `spring-web` is present, for efficient
|
||||
route matching.
|
||||
@@ -346,8 +297,6 @@ infrastructure that's used on a server, but registered programmatically as follo
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
.Kotlin
|
||||
----
|
||||
import org.springframework.messaging.rsocket.connectTcpAndAwait
|
||||
|
||||
val strategies = RSocketStrategies.builder()
|
||||
.routeMatcher(PathPatternRouteMatcher()) // <1>
|
||||
.build()
|
||||
@@ -357,7 +306,7 @@ infrastructure that's used on a server, but registered programmatically as follo
|
||||
|
||||
val requester = RSocketRequester.builder()
|
||||
.rsocketConnector { it.acceptor(responder) } // <3>
|
||||
.connectTcpAndAwait("localhost", 7000)
|
||||
.tcp("localhost", 7000)
|
||||
----
|
||||
<1> Use `PathPatternRouteMatcher`, if `spring-web` is present, for efficient
|
||||
route matching.
|
||||
@@ -374,23 +323,22 @@ you can still declare `RSocketMessageHandler` as a Spring bean and then apply as
|
||||
ApplicationContext context = ... ;
|
||||
RSocketMessageHandler handler = context.getBean(RSocketMessageHandler.class);
|
||||
|
||||
Mono<RSocketRequester> requesterMono = RSocketRequester.builder()
|
||||
RSocketRequester requester = RSocketRequester.builder()
|
||||
.rsocketConnector(connector -> connector.acceptor(handler.responder()))
|
||||
.connectTcp("localhost", 7000);
|
||||
.tcp("localhost", 7000);
|
||||
----
|
||||
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
.Kotlin
|
||||
----
|
||||
import org.springframework.beans.factory.getBean
|
||||
import org.springframework.messaging.rsocket.connectTcpAndAwait
|
||||
|
||||
val context: ApplicationContext = ...
|
||||
val handler = context.getBean<RSocketMessageHandler>()
|
||||
|
||||
val requester = RSocketRequester.builder()
|
||||
.rsocketConnector { it.acceptor(handler.responder()) }
|
||||
.connectTcpAndAwait("localhost", 7000)
|
||||
.tcp("localhost", 7000)
|
||||
----
|
||||
|
||||
For the above you may also need to use `setHandlerPredicate` in `RSocketMessageHandler` to
|
||||
@@ -413,22 +361,21 @@ at that level as follows:
|
||||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
.Java
|
||||
----
|
||||
Mono<RSocketRequester> requesterMono = RSocketRequester.builder()
|
||||
RSocketRequester requester = RSocketRequester.builder()
|
||||
.rsocketConnector(connector -> {
|
||||
// ...
|
||||
})
|
||||
.connectTcp("localhost", 7000);
|
||||
.tcp("localhost", 7000);
|
||||
----
|
||||
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
.Kotlin
|
||||
----
|
||||
import org.springframework.messaging.rsocket.connectTcpAndAwait
|
||||
|
||||
val requester = RSocketRequester.builder()
|
||||
.rsocketConnector {
|
||||
//...
|
||||
}.connectTcpAndAwait("localhost", 7000)
|
||||
}
|
||||
.tcp("localhost", 7000)
|
||||
----
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user