Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot “如何修复”;NoHttpResponseException“;在詹金斯身上运行Wiremock时?_Spring Boot_Wiremock - Fatal编程技术网

Spring boot “如何修复”;NoHttpResponseException“;在詹金斯身上运行Wiremock时?

Spring boot “如何修复”;NoHttpResponseException“;在詹金斯身上运行Wiremock时?,spring-boot,wiremock,Spring Boot,Wiremock,我在集成测试中启动了wiremock服务器 在my local中进行IT传递,但在jenkins服务器中失败,错误为 localhost:8089 failed to respond; nested exception is org.apache.http.NoHttpResponseException: localhost:8089 failed to respond 我尝试在测试中添加sleep(3000),这可以解决问题,但我不知道问题的根本原因,因此解决这一问题不是一个好主意 我还尝试

我在集成测试中启动了wiremock服务器

在my local中进行IT传递,但在jenkins服务器中失败,错误为

localhost:8089 failed to respond; nested exception is org.apache.http.NoHttpResponseException: localhost:8089 failed to respond
我尝试在测试中添加
sleep(3000)
,这可以解决问题,但我不知道问题的根本原因,因此解决这一问题不是一个好主意

我还尝试使用
@AutoConfigureWireMock(port=8089)
替换
WireMockServer
来启动wiremock服务器,这可以解决问题,但我不知道如何使用注释
@autoconfigurewick(port=8089)
对wiremock服务器进行一些配置

这里是我启动wiremock服务器的代码,有没有修复“NoHttpResponseException”的建议


Apache HttpClient不时遭受
NoHttpResponseException
的困扰。这是一个非常古老的问题

无论如何,我猜在您的案例中,问题可能是由于在测试之间重新启动WireMock服务器,同时,ApacheHttpClient将HTTP连接池化,并尝试在测试之间重用这些连接。如果是这种情况,有两种解决方案:

  • 在测试中禁用HTTP连接池。这是有意义的,因为在测试执行期间可以重新启动WireMock服务器被认为是正常的。或者,精心制作WireMock存根,以便始终在标题中发送
    “连接”:“关闭”
    。结果将是一样的

  • 从Apache HttpClient切换到Square OkHttp。虽然默认情况下OkHttp会汇集http连接,但它始终能够从诸如陈旧连接之类的情况中正常恢复。不幸的是,Apache的库并不是那么聪明


  • Coorect,正如G.Demecki所写,它与Wiremock无关。 它与调用wiremock的应用程序服务器相关。如今,重用连接以提高微服务基础架构中的性能已司空见惯。因此,连接关闭头、RequestScope客户机等都是无用的

    检查apache http客户端:

    在版本4.4中更改了过时连接的处理。以前,代码会在重新使用之前默认检查每个连接。该代码现在仅在自上次使用连接以来经过的时间超过已设置的超时时检查连接。默认超时设置为2000ms

    每次销毁wiremock端点并为新测试类创建新端点时,都需要2秒钟,直到应用程序检测到前一个连接已断开且必须打开新连接。 如果您不等待2秒钟,则可能会抛出这样一个NoHttpResponseException,具体取决于最后一次检查


    所以a
    Thread.sleep(2000)看起来很难看。但这并不是那么糟糕,只要我们知道为什么需要这样做。

    在这种情况下对我们有效的解决方案只是将重试添加到apache客户端:

    @Configuration
    public class FeignTestConfig {
    
        @Bean
        @Primary
        public HttpClient testClient() {
            return HttpClientBuilder.create().setRetryHandler((exception, executionCount, context) -> {
                if (executionCount > 3) {
                    return false;
                }
                return exception instanceof org.apache.http.NoHttpResponseException || exception instanceof SocketException;
            }).build();
        }
    }
    

    套接字异常也存在,因为有时会抛出此异常而不是NoHttpResponse

    感谢这篇有用的文档-我不知道它被精确设置为2秒!顺便说一句:在测试中睡2秒钟是一个非常低效的想法,很抱歉,但我不能投票;)除了在测试类之间使用2s超时之外,这个问题还有什么解决方案吗?我使用MockMvc向WireMock发出请求,我也遇到了同样的问题。为其测试实例定制apache http客户端为我解决了这个问题。修复方法是创建新的PoollightTPClientConnectionManager,并将超时时间从2秒更改为10毫秒:
    setValidateAfterInactivity(10)
    ,如中所建议
    @Configuration
    public class FeignTestConfig {
    
        @Bean
        @Primary
        public HttpClient testClient() {
            return HttpClientBuilder.create().setRetryHandler((exception, executionCount, context) -> {
                if (executionCount > 3) {
                    return false;
                }
                return exception instanceof org.apache.http.NoHttpResponseException || exception instanceof SocketException;
            }).build();
        }
    }