Spring RestTemplate 超时

新手上路,请多包涵

我想为我的 Web 应用程序使用的休息服务设置连接超时。我正在使用 Spring 的 RestTemplate 与我的服务对话。我做了一些研究,发现并使用了下面的 xml(在我的应用程序 xml 中),我认为它是用来设置超时的。我正在使用 Spring 3.0。

我在这里也看到了同样的问题 Timeout configuration for spring webservices with RestTemplate 但解决方案似乎不那么 _干净_,我更愿意通过 Spring config 设置超时值

<bean id="RestOperations" class="org.springframework.web.client.RestTemplate">
    <constructor-arg>

      <bean class="org.springframework.http.client.CommonsClientHttpRequestFactory">
        <property name="readTimeout" value="${restURL.connectionTimeout}" />
      </bean>
    </constructor-arg>
</bean>

似乎无论我设置 readTimeout 是我得到以下内容:

网线断开: 等待约20秒,报如下异常:

org.springframework.web.client.ResourceAccessException :I/O错误:没有到主机的路由:连接;嵌套异常是 java.net.NoRouteToHostException :没有到主机的路由:连接

URL 不正确,因此 rest 服务返回 404: 等待大约 10 秒并报告以下异常:

org.springframework.web.client.HttpClientErrorException :404未找到

我的要求需要更短的超时,所以我需要能够更改这些。关于我做错了什么的任何想法?

非常感谢。

原文由 sardo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 868
2 个回答

我终于得到了这个工作。

我认为我们的项目有两个不同版本的 commons-httpclient jar 这一事实没有帮助。一旦我解决了这个问题,我发现你可以做两件事……

在代码中,您可以放置以下内容:

 HttpComponentsClientHttpRequestFactory rf =
    (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setReadTimeout(1 * 1000);
rf.setConnectTimeout(1 * 1000);

第一次调用此代码时,它将为 --- 使用的 RestTemplate HttpComponentsClientHttpRequestFactory 类设置超时。因此,由 RestTemplate 进行的所有后续调用将使用上面定义的超时设置。

或者更好的选择是这样做:

 <bean id="RestOperations" class="org.springframework.web.client.RestTemplate">
    <constructor-arg>
        <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
            <property name="readTimeout" value="${application.urlReadTimeout}" />
            <property name="connectTimeout" value="${application.urlConnectionTimeout}" />
        </bean>
    </constructor-arg>
</bean>

在我的代码中使用 RestOperations 接口并从属性文件中获取超时值的地方。

原文由 sardo 发布,翻译遵循 CC BY-SA 3.0 许可协议

对于 Spring Boot >= 1.4

 @Configuration
public class AppConfig
{
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder)
    {
        return restTemplateBuilder
           .setConnectTimeout(...)
           .setReadTimeout(...)
           .build();
    }
}


对于 Spring Boot <= 1.3

 @Configuration
public class AppConfig
{
    @Bean
    @ConfigurationProperties(prefix = "custom.rest.connection")
    public HttpComponentsClientHttpRequestFactory customHttpRequestFactory()
    {
        return new HttpComponentsClientHttpRequestFactory();
    }

    @Bean
    public RestTemplate customRestTemplate()
    {
        return new RestTemplate(customHttpRequestFactory());
    }
}

然后在你的 application.properties

 custom.rest.connection.connection-request-timeout=...
custom.rest.connection.connect-timeout=...
custom.rest.connection.read-timeout=...

This works because HttpComponentsClientHttpRequestFactory has public setters connectionRequestTimeout , connectTimeout , and readTimeout and @ConfigurationProperties sets them for you.


对于 没有 Spring Boot 的 Spring 4.1 或 Spring 5, 使用 @Configuration 而不是 XML

 @Configuration
public class AppConfig
{
    @Bean
    public RestTemplate customRestTemplate()
    {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(...);
        httpRequestFactory.setConnectTimeout(...);
        httpRequestFactory.setReadTimeout(...);

        return new RestTemplate(httpRequestFactory);
    }
}

原文由 dustin.schultz 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题