先行一步
VMware 提供培训和认证,助您快速进步。
了解更多时间:约 15 分钟
在上一篇文章中,您了解了 Spring Boot 如何简化编写 RSocket 服务器的任务。那么 RSocket 客户端呢?在本文中,您将学习如何编写自己的 RSocket 客户端,然后使用这个新客户端向 RSocket 服务器发送请求-响应消息。我们开始吧!
本教程使用 Linux shell。有关如何在 Windows 上运行 Linux shell 的详细信息,请参阅此 Microsoft 教程。
自己动手编写代码很有趣,因此对于 RSocket 客户端,我们从头开始。
如果您觉得这太麻烦,或者现在没有时间,可以在演示代码库的 rsocket-client 文件夹中找到代码。
打开浏览器并访问 start.spring.io,然后使用以下设置创建一个新的 Spring Boot 项目:
Maven
Java
2.2.5
(或最新的 GA 版本)io.pivotal
rsocket-client
RSocket
, Lombok
点击绿色的“Generate”按钮。下载 ZIP 文件并将其解压到一个文件夹中,然后在您喜欢的 Java IDE 中打开解压后的项目。
Spring Shell 帮助您使用 Spring Boot 编写简单的终端程序。在撰写本文时,Spring Initializr 没有提供 Spring Shell 选项,但您仍然可以通过手动将依赖项添加到项目来使用它。
在您的 IDE 中打开 Maven pom.xml
文件,并将以下 XML 添加到 <dependencies>
部分
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
这将使您的新 RSocket 客户端项目能够使用 Spring Shell。
需要进行一些简短的编码任务,以将 Spring Shell 和 RSocket 支持添加到您的新 RSocket 客户端应用程序。它们如下:
您可以删除自动生成的 RsocketclientApplicationTests
测试类。如果不删除,运行 Spring '集成' 测试时可能会发生奇怪的事情。详情请参阅 此处 Spring Shell 文档中的注意事项。
从 io.pivotal.rsocketserver.data
包中复制 Message.java
数据类。此包位于您在上一篇文章中使用的 rsocket-server
项目文件夹中。您需要此类的定义才能与服务器交换消息。
在 io.pivotal.rsocketclient
包中,创建一个名为 RSocketShellClient
的新类,并用 @Slf4j
和 @ShellComponent
注解标记此类。第一个注解告诉 Lombok 为此类添加日志功能,第二个注解告诉 Spring 您正在构建一个基于 Shell 的组件。
@Slf4j
@ShellComponent
public class RSocketShellClient {
// Your code will go here...
}
RSocket 客户端需要知道在哪里可以找到 RSocket 服务器并与之通信。Spring Boot 会自动配置 Spring RSocket 支持,您只需构建一个定制的 RSocketRequester
。您可以通过添加类构造函数并使用 RSocketRequester.Builder
来完成此操作,如下所示:
// Add a global class variable for the RSocketRequester
private final RSocketRequester rsocketRequester;
// Use an Autowired constructor to customize the RSocketRequester and store a reference to it in the global variable
@Autowired
public RSocketShellClient(RSocketRequester.Builder rsocketRequesterBuilder) {
this.rsocketRequester = rsocketRequesterBuilder
.connectTcp("localhost", 7000).block();
}
请求者的 connectTcp()
方法需要知道您的 RSocket 服务器的 IP 地址和端口,并且您需要告诉请求者 block()
直到连接建立。之后,您就可以通过 TCP 与 RSocket 服务器通信了。
在 Spring Shell 组件中添加 Shell 功能非常简单。首先,向 RSocketShellClient
中添加一个返回 void
的 public 方法 requestResponse()
,并在方法签名上使用 @ShellMethod
注解来激活 Spring Shell 并声明用户键入 help
时将看到的帮助文本。
在方法内部,使用 RSocketRequester
的全局引用,将 route()
设置为 "request-response"
,将 data()
设置为一个新的 Message
实例,并告诉请求者 retrieveMono()
类型为 Message
。最后,向 subscribe()
方法添加一个简单的日志记录功能。代码如下:
@ShellMethod("Send one request. One response will be printed.")
public void requestResponse() throws InterruptedException {
log.info("\nSending one request. Waiting for one response...");
Message message = this.rsocketRequester
.route("request-response")
.data(new Message(CLIENT, REQUEST))
.retrieveMono(Message.class)
.block();
log.info("\nResponse was: {}", message);
}
RSocket 旨在处理长期运行的异步数据流。使用您已经熟悉的 Java 8 Stream API 中的函数式编程风格可以最有效地完成此操作。
上面的代码说明了您希望在(并且仅在)有消息工作要做时发生的事情。在这种情况下,您承诺发送一条请求消息,路由到服务器的 "request-response"
处理程序方法,并期望收到一条响应消息。此过程由对 block()
方法的调用触发。如果没有它,什么都不会发生。
编码就到这里。让我们运行它!
我们将使用上一篇文章中的代码作为服务器。打开一个终端窗口,切换到 rsocket-server
目录,然后使用 Maven wrapper 运行服务器代码,如下所示:
cd rsocket-server
./mvnw clean package spring-boot:run -DskipTests=true
```bash
The server will start up on `localhost` port `7000` and wait for your client to connect.
# Step 5: Build And Run The RSocket Client
To run your RSocket client, open a second terminal window and move to your `rsocket-client` directory. From there, build and run the client as follows:
```bash
cd rsocket-client
./mvnw clean package spring-boot:run -DskipTests=true
客户端启动完成后,您将看到一个新的命令行提示符,如下所示:
shell:>
您可以使用此提示符向您的 Spring 应用程序发出命令,类似于通过常规终端应用程序发出命令。
让我们向服务器发送一条请求消息并等待响应。在提示符下键入 request-response
。然后您将看到请求和响应消息的发送和接收,如下所示:
shell:>request-response
Sending one request. Waiting for one response...
Response was: Message(origin=Server, interaction=Response, index=0, created=1582558295)
shell:>
为了简洁起见,我省略了大部分日志详情。您的输出将比所示的更详细,但结果是一样的。
现在您可以在 shell:>
提示符下键入 exit
来停止 rsocket-client
。您可以通过在其终端窗口中按 Ctrl-C
或关闭窗口来停止 rsocket-server
。
Spring Shell 允许您使用 Spring Boot 构建和运行类似终端的程序。在您的 Shell 组件的构造函数中,您配置了一个 RSocketRequester
,以便使用 TCP 在 localhost 的端口 7000 上与您的服务器通信。
然后您在 Shell 组件中创建了一个 requestResponse()
方法并对其进行注解,这样当您在 Shell 提示符下键入 request-response
时,Spring Shell 就会调用此方法。
当 requestResponse()
方法被调用时,客户端发送了一些描述要使用的 route
的元数据,然后将请求作为 Message
对象发送。服务器的响应消息使用一个简单的日志语句打印在屏幕上。
在本文中,您学习了如何使用 Spring Shell 编写一个简单的 RSocket 客户端。这个新客户端通过 TCP 与您的 RSocket 服务器通信。在下一篇文章中,我们将通过向客户端和服务器项目添加额外功能,来介绍 RSocket 和 Spring Boot 中的“即发即忘”(fire-and-forget)消息。