【问题标题】:Getting connection timed out error in Java microservice在 Java 微服务中获取连接超时错误
【发布时间】:2018-11-21 17:18:49
【问题描述】:

我使用 Java 8 和 SpringBoot 2 构建了一个微服务。从这个微服务中,我尝试使用另一个 REST API 服务。但是,我在 Chrome 上收到以下错误

java.lang.IllegalStateException: 底层 HTTP 客户端完成 没有发出响应。

2018-06-12 15:21:29.300 错误 17996 --- [ctor-http-nio-3] .a.w.r.e.DefaultErrorWebExceptionHandler : 处理请求失败 [获取http://localhost:8080/category/search] io.netty.channel.AbstractChannel$AnnotatedConnectException:连接 超时:没有更多信息:test.usdemo.xyz.com/92.54.41.24:443 在 sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_171] 在 sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_171] 在 io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:325) ~[netty-transport-4.1.24.Final.jar:4.1.24.Final] 在 io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:340) ~[netty-transport-4.1.24.Final.jar:4.1.24.Final] 在 io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:633) ~[netty-transport-4.1.24.Final.jar:4.1.24.Final] 在 io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized (NioEventLoop.java:580) ~[netty-transport-4.1.24.Final.jar:4.1.24.Final] 在 io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497) ~[netty-transport-4.1.24.Final.jar:4.1.24.Final] 在 io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459) ~[netty-transport-4.1.24.Final.jar:4.1.24.Final] 在 io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-common-4.1.24.Final.jar:4.1.24.Final] 在 java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_171] 引起:java.net.ConnectException: Connection timed 出:没有更多信息 ...省略了10个常见的框架

我可以使用 PostMan 成功使用相同的服务,但不能通过我的微服务。

请协助提供建议。

【问题讨论】:

  • 我们需要更多信息。您可以发布一些访问其他服务的服务的代码 sn-ps 吗?
  • [附加详情] 我已经禁用了防火墙。
  • 我们需要一些代码,能否请您发布您尝试访问的控制器以及您正在从您的微服务进行的调用?
  • 没有任何代码,很少有人可以帮助你。请发布一些代码以及超时发生的确切位置
  • 代码sn-p参考如下:-

标签: spring spring-boot postman microservices


【解决方案1】:

我不知道如何在上面编辑我自己的问题,因为我没有看到编辑选项,因此我在此处添加其他详细信息以获得我的查询的解决方案。

控制器类:-

@RestController

公共类 CategorySearchController {

private final CategorySearchService categorySearchService;

@Autowired
public CategorySearchController(CategorySearchService categorySearchService) {
    this.categorySearchService = categorySearchService;
}


@GetMapping(path = "/search-category")
public Mono<CategoryResponse> searchCategories(SearchRequest categorySearchRequest){
    return categorySearchService
            .searchCategories()
            .switchIfEmpty(Mono.error(
                    new EntityNotFoundException("No category matching " + categorySearchRequest.getSearchTerm() + " was found")));
}

}

服务类:-

@Service
public class CategorySearchServiceImpl implements CategorySearchService {
    private String baseUrl = "https://demo0954903.mockable.io";

    @Override
    public Mono<CategoryResponse> searchCategories() {
        WebClient webClient = WebClient.create(baseUrl);
        return webClient.
                 get()
                .uri("/category-search")
                .retrieve()
                .bodyToMono(CategoryResponse.class);
    }
}

【讨论】:

    【解决方案2】:

    我找到了这个问题的解决方案。我需要在 webclient 中添加代理如下:-

    private final WebClient webClient;
    ReactorClientHttpConnector connector = new ReactorClientHttpConnector(
                    options -> options.httpProxy(addressSpec -> {
                    return addressSpec.host(PROXY_HOST).port(PROXY_PORT);
                    }));
    
            this.webClient = webClientBuilder.baseUrl(BASE_URL).clientConnector(connector).build();
    

    【讨论】:

      【解决方案3】:

      检查您的代理设置。在 Spring 应用程序中,您可以将以下内容添加到 VM 参数以设置代理:-Dhttp.proxyHost=your.proxy.net -Dhttp.proxyPort=8080

      【讨论】:

        【解决方案4】:

        我在 Scala 项目中遇到了类似的问题。根据报错信息,我发现 IntelliJ 将 Ant 作为编译器,而我期望 sbt 作为编译器。

        通过打开 sbt shell use for builds 选项解决问题(Preferences > Build, Execution, Deployment > Build Tools > sbt > sbt projects > sbt shell > check builds)

        Executing pre-compile tasks...
        Loading Ant configuration...
        Running Ant tasks...
        Abnormal build process termination: 
        {remove some sensitive logs here}
        io.netty.channel.AbstractChannel$AnnotatedConnectException: Operation timed out: /127.0.0.1:50154
        Caused by: java.net.ConnectException: Operation timed out
            at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
            at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:716)
            at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
            at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
            at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
            at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
            at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
            at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
            at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
            at java.lang.Thread.run(Thread.java:748)
        Exiting.
        Executing post-compile tasks...
        Loading Ant configuration...
        Running Ant tasks...
        Synchronizing output directories...
        2021/9/15, 8:52 AM - Build completed with 1 error and 0 warnings in 27 sec, 726 ms
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多