【问题标题】:Spring Webclient always gives error timed outSpring Webclient 总是报错超时
【发布时间】:2022-01-13 08:11:56
【问题描述】:

我目前正在尝试构建一个从 url 提供图像(以字节数组的形式)的 API。这是我的代码:

@RestController
@SpringBootApplication
public class ReturnApplication {
    private static final Logger logger = Logger.getLogger(ReturnApplication.class.getSimpleName());

    @Autowired
    private  final WebClient.Builder webClientBuilder;

    public ReturnApplication() {
        this.webClientBuilder = WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(
                        HttpClient.create().followRedirect(true).wiretap(true)
                ));
    }

    @GetMapping("/quotes")
    public ResponseEntity<?> getRandomQuote() {
        logger.info("Get Random Quote");
        Object result = WebClient.create()
                .get()
                .uri("https://api.quotable.io/random")
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToMono(Object.class)
                .block();
        return new ResponseEntity(result, HttpStatus.OK);
    }
    public static void main(String[] args) {
        SpringApplication.run(ReturnApplication.class, args);
    }

}

但是,每次我尝试点击该服务时,都会出现这种错误:

java.net.ConnectException: Connection timed out: no further information
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_201]
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_201]
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:710) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986) ~[netty-common-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.72.Final.jar:4.1.72.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.72.Final.jar:4.1.72.Final]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]

我尝试调试网络客户端,但它总是让我超时。我可以在浏览器中顺利打开网址。我的代码实际上有什么问题,我该如何解决?


编辑

我决定再做一个spring boot api项目来测试是不是Webclient的问题,其实不是。之后,我尝试从命令行 ping 那个 API url(我在上面使用的那个),它一直超时。我也不能从 CURL 发出请求。我可以通过 Postman 或网络浏览器顺利完成请求。因此,我应该怎么做才能使 Spring Webclient 能够从该 API 获得响应?

【问题讨论】:

  • 这个应用程序部署在哪里,你能从那个位置连接到api.quotable.io吗?
  • 不要使用block(),这违背了使用 Spring WebFlux 的全部目的。
  • @MichaelMcFadyen 我可以通过浏览器和邮递员连接到api.quotable.io,但我不能从 spring、curl 和 ping 连接。
  • @JoãoDias 你能给我更多的建议吗?我只是不知道如何为该服务的 json 输出变异/添加另一个响应字段...
  • 这听起来像是网络问题,而不是 app/code/webflux 问题。我建议关闭这个问题,因为它不再准确。

标签: spring-boot httprequest spring-webflux spring-webclient


【解决方案1】:
@GetMapping("/quotes")
    public ResponseEntity<?> getRandomQuote() {
        logger.info("Get Random Quote");
        Object result = WebClient.create()
                .get()
                .uri("https://api.quotable.io/random")
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToMono(Object.class)
                .timeout(Duration.ofSeconds(35));
                .block();
        return new ResponseEntity(result, HttpStatus.OK);
    }
    public static void main(String[] args) {
        SpringApplication.run(ReturnApplication.class, args);
    }

添加以下行--- .timeout(Duration.ofSeconds(35)); 调用图像API时可能超过了默认超时时间

【讨论】:

  • 不,我试过 60 秒超时,所以显然不是
  • 不要尝试使用块。在 spring webflux 中不推荐。
  • 但这不是根本原因,因为当我删除块时,它仍然是一样的
猜你喜欢
  • 1970-01-01
  • 2021-01-08
  • 2018-08-06
  • 2012-04-02
  • 2021-12-27
  • 2021-08-13
  • 2013-01-11
  • 1970-01-01
  • 2021-06-13
相关资源
最近更新 更多