当前位置: 首页 > 知识库问答 >
问题:

在反应器Netty HttpClient/HttpServer配置中,我无法在发送到服务器的邮件上获取检索请求对象

郭修平
2023-03-14

Reacter-Netty HttpClient正在向HttpServer发送请求HttpClientRequest。在服务器端,我无法从HttpServerRequest中获取请求对象。我可以从HttpServerRequest中提取FLuxByteBuf,但不能提取Flux对象中的ByteBuf对象。通常,订阅Flux将允许我获取请求对象,但这在这里不起作用。客户端成功接收到来自服务器的响应。有人知道为什么订阅Flux/Mono在Reacter-Netty客户端/服务器的服务器端不起作用吗?

客户代码为:

public class Client {

    private static final Logger log = Logger.getLogger(Client.class.getSimpleName());

    public static void main(String[] args) throws InterruptedException {

        Consumer<byte[]> onSuccess = (byte[] response) -> {
            ElectionResponse electionResponse = SerializationUtils.deserialize(response);
            log.info("response in onSuccess: "+electionResponse);

        };
        Consumer<Throwable> onError = (Throwable ex) -> {
            ex.getMessage();
        };

        Runnable onCompletion = () -> {
            System.out.println("Message Completed");

        };
        ElectionRequest electionRequest = new ElectionRequest("aRequest");
        byte[] requestBytes = SerializationUtils.serialize(electionRequest);
        ByteBuf requestByteBuf = Unpooled.copiedBuffer(requestBytes);

        HttpClient.create()
                .tcpConfiguration(tcpClient -> tcpClient.host("10.0.0.19"))
                .port(61005)
                .post()
                .uri("/echo")
                .send(Mono.just(requestByteBuf))
                .responseContent()
                .aggregate()
                .asByteArray()
                .subscribe(onSuccess, onError, onCompletion);

        try {
            Thread.sleep(15000);
        } catch(InterruptedException ie) {
            ie.printStackTrace();
        }
    }
}

服务器代码为:

public class Server {

    private static final Logger log = Logger.getLogger(Server.class.getSimpleName());

    public static void main(String[] args) {

        ElectionResponse electionResponse = new ElectionResponse("aResponse");
        byte[] responseArray = SerializationUtils.serialize(electionResponse);

        Consumer<ByteBuf> onSuccess = (ByteBuf request) -> {
            System.out.println("onSuccess: Request received!");
        };
        Consumer<Throwable> onError = (Throwable ex) -> {
            ex.getMessage();
        };
        Runnable onCompletion = () -> {
            System.out.println("Message Completed");

        };

        DisposableServer server =
            HttpServer.create()
                      .host("10.0.0.19")
                      .port(61005)
                      .route(routes ->
                          routes
                            .post("/echo",
                                (request, response) -> {
                                    request.receive().retain().next().subscribe(onSuccess, onError, onCompletion);
                                    return response.send(Mono.just(Unpooled.copiedBuffer(responseArray).retain()));
                                                       }
                                    ))
                        .bindNow();

        server.onDispose()
                .block();
    }
}

环境和maven依赖关系如下:

Apache Maven 3.6.1
Maven home: /usr/share/maven
Java version: 11.0.6, vendor: Oracle Corporation, runtime: /home/linuxlp/opt/graalvm/graalvm-svm-linux-20.1.0-ea+28
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.3.0-51-generic", arch: "amd64", family: "unix"

    <dependencies>
        <dependency>
            <groupId>io.projectreactor.netty</groupId>
            <artifactId>reactor-netty</artifactId>
            <version>0.9.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>3.3.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.reactivestreams</groupId>
            <artifactId>reactive-streams</artifactId>
            <version>1.0.3</version>
        </dependency>

共有1个答案

宋望
2023-03-14

你觉得这样编写请求/响应怎么样

public class Server {

    private static final Logger log = Logger.getLogger(Server.class.getSimpleName());

    public static void main(String[] args) {

        Consumer<byte[]> onSuccess = (byte[] request) -> {
            System.out.println("onSuccess: Request received!");
        };
        Consumer<Throwable> onError = (Throwable ex) -> {
            ex.getMessage();
        };
        Runnable onCompletion = () -> {
            System.out.println("Message Completed");

        };

        DisposableServer server =
                HttpServer.create()
                        .host("10.0.0.19")
                        .port(61005)
                        .route(routes ->
                                routes.post("/echo",
                                        (request, response) ->
                                                response.send(request.receive()
                                                                     .aggregate()
                                                                     .asByteArray()
                                                                     .doOnNext(onSuccess)
                                                                     .doOnError(onError)
                                                                     .doOnTerminate(onCompletion)
                                                                     .flatMap(bytes -> {
                                                                         ElectionRequest electionRequest = (ElectionRequest) SerializationUtils.deserialize(bytes);
                                                                         ElectionResponse electionResponse = new ElectionResponse(electionRequest.getStr());
                                                                         return Mono.just(Unpooled.copiedBuffer(SerializationUtils.serialize(electionResponse)));
                                                                     }))))
                        .bindNow();

        server.onDispose()
                .block();
    }
}
 类似资料:
  • 问题内容: 有没有一种方法可以为PHP 配置WAMP服务器以启用该功能? 问题答案: 从本地主机配置工作的电子邮件客户端是一件很麻烦的事情,我花了数小时无奈地尝试它。我确信更有经验的人可能会提供帮助,或者他们也许会同意我的看法。

  • Java: openjdk版本“11.0.11”2021-04-20 openjdk运行时环境(构建11.0.11+9-Ubuntu-0Ubuntu2.20.04) openjdk 64位服务器VM(构建11.0.11+9-Ubuntu-0Ubuntu2.20.04,混合模式,共享) 握手后新会话票证到达: SSL-Session: 协议:TLSv1.3 密码:TLS_AES_256_GCM_SH

  • 问题内容: 假设X.com将向Y.com发送发布请求,那么Y如何知道发件人是X?没有url查询字符串的课程。 http://php.net/manual/en/reserved.variables.server.php的 $ _SERVER [‘HTTP_REFERER’] 似乎不是答案。它自己的文档说“它不能真正被信任”。 应该使用密钥和秘密密钥参数吗? 问题答案: 在请求中发送秘密值,例如可以

  • 邮件提醒会使某些功能有更好的用户体验, 比如发送邮件提醒用户新消息到达. 请在seahub_settings.py中加入以下语句以开启邮件提醒功能 (同时需要对你的邮箱进行设置). EMAIL_USE_TLS = False EMAIL_HOST = 'smtp.domain.com' # smpt 服务器 EMAIL_HOST_USER = 'username@domain.com

  • 当我在本地机器上运行脚本时 客户- 但是当我在主机上运行相同的脚本时 SMTP- 你有办法解决这个问题吗? 提前感谢。

  • 当前,我已经在Express API上为服务器发送的事件设置了一个endpoint,当我从cmd调用endpoint时,使用http://localhost:3000/v1/devices,我得到了流,但当我使用新事件源从Vue应用程序调用endpoint时('http://localhost:3000/v1/devices“)但它只会继续加载,而不会流式传输任何数据。是否有我目前没有做的事情要做