Vertx.io GET 静默失败 [英] Vertx.io GET silently fails

查看:36
本文介绍了Vertx.io GET 静默失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用 vertx 编写一个 POC,当我们必须将 Spring Web 从 4.x 迁移到 5 以符合 java 9 时寻找替代方案.我写了一个简单的客户端,只是一个对公开可用服务器的 GET 只是为了让某些东西正常工作,但它默默地让我失败了.

 public List拉() {Vertx vertx = Vertx.vertx();HttpClientOptions options = new HttpClientOptions().setLogActivity(true);HttpClient hc = vertx.createHttpClient(options);hc.getNow(80, "http://sunet.se", "/",r -> {System.out.println("\n****** Handler 被调用!***\n");});返回新的 ArrayList();}

这会默默地失败,我不明白为什么.据我所知,我完全按照文档中给出的示例进行操作.无奈之下,我启动了 Wireshark,根据 WS 的说法,没有实际调用(当我使用浏览器时,WS 捕获了该调用).所以,似乎我的电话从未真正完成.我没有得到任何例外或任何东西.除了

之外,将日志级别设置为调试没有什么值得注意的

无法从 sysctl 和文件/proc/sys/net/core/somaxconn 获取 SOMAXCONN.默认值:128

这不应该使呼叫失败.我也试过使用 vertx.io WebClient 但也以同样的方式失败了.
更新
我已经设法让它工作,但有一个警告.
正如@tsegismont 在他的回答中所说,URI 的协议部分不应该在那里,这不在示例中,我只是自己错过了.
我将我的示例作为独立运行,然后它就起作用了.
我最初的示例是作为 junit 测试运行的(这是一种测试代码的简单方法,我通常尝试先编写测试代码),当它作为 junit 测试运行时,它仍然不起作用.这是为什么,我不知道.如果有人能告诉我如何让它发挥作用,我将不胜感激.

解决方案

您使用的 getNow 变体需要服务器主机,而不是 URL.应该是:

hc.getNow(80, "sunet.se", "/",r -> {System.out.println("\n****** Handler 被调用!***\n");}

如果您在 Vert.x 文档中发现了这样的片段,那就是一个错误.你介意举报吗?

现在有一些评论.

1/HttpClient 是一个低级客户端.

大多数用户应该更喜欢 Vert.x Web 客户端

以下是您的用例示例:

WebClient 客户端 = WebClient.create(vertx);客户.get(80, "sunet.se", "/").send(ar -> {如果(ar.succeeded()){//获取响应HttpResponse<缓冲区>响应 = ar.result();System.out.println("收到带有状态码的响应" + response.statusCode());} 别的 {System.out.println("出错了" + ar.cause().getMessage());}});

2/创建单个 Vert.x 和 WebClient 实例

不要在每次方法调用时都创建 Vert.x 和 WebClient 实例.浪费资源,效率低下.


I'm writing a POC using vertx, looking for alternatives when we have to migrate Spring Web from 4.x to 5 to be java 9 compliant. I've written a simple client, just a GET towards a publicly available server just to get something working but it silently fails me.

    public List<String> pull() {

      Vertx vertx = Vertx.vertx();

      HttpClientOptions options = new HttpClientOptions().setLogActivity(true);
      HttpClient hc = vertx.createHttpClient(options);
      hc.getNow(80, "http://sunet.se", "/",r -> {
        System.out.println("\n****** Handler called! ***\n");
      });
      return new ArrayList<>();
  }

This will silently fail and I cannot understand why. As far as I can tell, I do exactly as in the examples given in the docs. In desperation I fired up wire shark and according to WS, there is no actual call (when I use the browser WS captures that). So, it seems my call is never actually done. I don't get any exceptions or anything. Setting the log level to debug gives nothing noteworthy other than

Failed to get SOMAXCONN from sysctl and file /proc/sys/net/core/somaxconn. Default: 128

And that should not fail the call. I've also tried using vertx.io WebClient but that fails also, in the same manner.
UPDATE
I've managed to get it to work but with a caveat.
As @tsegismont states in his answer, the protocol part of the URI shouldn't be there, that was not in the examples, I just missed it myself.
I ran my example as a stand-alone and then it worked.
My original example was run as a junit test (it's an easy way to test code and I usually try to write the test code first) and when it's run as a junit test it still doesn't work. Why that is, I have no idea. I would greatly appreciate if someone could tell me how to get that to work.

解决方案

The getNow variant you use expects the server host, not a URL. It should be:

hc.getNow(80, "sunet.se", "/",r -> {
  System.out.println("\n****** Handler called! ***\n");
}

If you found a snippet like this in the Vert.x docs it's a bug. Would you mind to report it?

Now a few comments.

1/ The HttpClient is a low-level client.

Most users should prefer the Vert.x Web Client

Here's an example for your use case:

WebClient client = WebClient.create(vertx);

client
  .get(80, "sunet.se", "/")
  .send(ar -> {
    if (ar.succeeded()) {
      // Obtain response
      HttpResponse<Buffer> response = ar.result();
      System.out.println("Received response with status code" + response.statusCode());
    } else {
      System.out.println("Something went wrong " + ar.cause().getMessage());
    }
  });

2/ Create a single Vert.x and WebClient instance

Do not create a Vert.x and WebClient instance on every method call. It wastes resources and is inefficient.

这篇关于Vertx.io GET 静默失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆