Springboot RestTemplate设置超时时间

Springboot RestTemplate设置超时时间
Java Config配置
 
@Configuration
 
public class RestTemplateConfig {
 
    @Bean
 
    public RestTemplate getRestTemplate() {
 
        //配置HTTP超时时间
 
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
 
        httpRequestFactory.setConnectionRequestTimeout(6000);
 
        httpRequestFactory.setConnectTimeout(6000);
 
        httpRequestFactory.setReadTimeout(6000);
 
        return new RestTemplate(httpRequestFactory);
 
    }
 
}
 
配置文件指定
 
custom.rest.connection.connection-request-timeout=3000
 
custom.rest.connection.connect-timeout=3000
 
custom.rest.connection.read-timeout=3000
 
@Configuration
 
public class AppConfig{
 
    @Bean
 
    @ConfigurationProperties(prefix = "custom.rest.connection")
 
    public HttpComponentsClientHttpRequestFactory customHttpRequestFactory() {
 
        return new HttpComponentsClientHttpRequestFactory();
 
    }
 
    @Bean
 
    public RestTemplate customRestTemplate(){
 
        return new RestTemplate(customHttpRequestFactory());
 
    }
 
}
 
重新部署后消费者迅速开始消费,堆积的消息已经消费完。

推荐阅读