设置超时时间的原因:

RestTemplate默认情况下是无限的,有时候会发生阻塞。

配置超时时间:

#restTemplate config#
resttemplate.connectionRequestTimeout=30000
resttemplate.connectTimeout=30000
resttemplate.readTimeout=30000

 @bean

package com.fescoadecco.config;

import com.fescoadecco.util.HttpUtil;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;


@Configuration
@ConfigurationProperties(prefix = "resttemplate")
public class RestTemplateConfig {

    @Setter
    private int connectionRequestTimeout;
    @Setter
    private int connectTimeout;
    @Setter
    private int readTimeout;

    /**
     * 设置RestTemplate超时时间默认情况下是无限的(防止阻塞)
     *
     * @return
     */
    @Bean
    public void restTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(connectionRequestTimeout);
        httpRequestFactory.setConnectTimeout(connectTimeout);
        httpRequestFactory.setReadTimeout(readTimeout);
        RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
        HttpUtil.setRestTemplate(restTemplate);
    }
}

 RestTemplate 请求:

package com.fescoadecco.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;


@Slf4j
public class HttpUtil {
    private static final String CONTENT_TYPE = "Content-Type";
    private static final String CONTENT_TYPE_VALUE = "application/x-www-form-urlencoded";
    private static final String CONTENT_TYPE_VALUE_JSON = "application/json";

    private static RestTemplate restTemplate;

    /**
     *定义restTemplate
     * @param client
     */
    public static void setRestTemplate(RestTemplate client) {
        restTemplate = client;
    }

    /**
     * get/post请求
     * @param url
     * @param method
     * @param params
     * @return
     */
    public static String client(String url, HttpMethod method, MultiValueMap<String, Object> params) {
        Map<String, String> setHeaders = new HashMap<>();
        setHeaders.put(CONTENT_TYPE, CONTENT_TYPE_VALUE);
        return client(url, method, params, setHeaders);
    }

    /**
     * post请求
     * @param url
     * @param params
     * @return
     */
    public static String postClient(String url, Map<String, Object> params) {
        Map<String, String> setHeaders = new HashMap<>();
        setHeaders.put(CONTENT_TYPE, CONTENT_TYPE_VALUE);
        return clientJson(url, HttpMethod.POST, params, setHeaders);
    }

    /**
     * get请求
     * @param url
     * @param params
     * @return
     */
    public static String getClient(String url, Map<String, Object> params) {
        Map<String, String> setHeaders = new HashMap<>();
        setHeaders.put(CONTENT_TYPE, CONTENT_TYPE_VALUE);
        return clientJson(url, HttpMethod.GET, params, setHeaders);
    }

    /**
     * get/post请求
     * @param url
     * @param method
     * @param params
     * @return
     */
    public static String clientJson(String url, HttpMethod method, Map<String, Object> params) {
        Map<String, String> setHeaders = new HashMap<>();
        setHeaders.put(CONTENT_TYPE, CONTENT_TYPE_VALUE_JSON);
        return clientJson(url, method, params, setHeaders);
    }

    /**
     * post请求
     * @param url
     * @param params
     * @return
     */
    public static String postClientJson(String url, Map<String, Object> params) {
        Map<String, String> setHeaders = new HashMap<>();
        setHeaders.put(CONTENT_TYPE, CONTENT_TYPE_VALUE_JSON);
        return clientJson(url, HttpMethod.POST, params, setHeaders);
    }

    /**
     * get请求
     * @param url
     * @param params
     * @return
     */
    public static String getClientJson(String url, Map<String, Object> params) {
        Map<String, String> setHeaders = new HashMap<>();
        setHeaders.put(CONTENT_TYPE, CONTENT_TYPE_VALUE_JSON);
        return clientJson(url, HttpMethod.GET, params, setHeaders);
    }

    /**
     * form请求
     *
     * @param url
     * @param method
     * @param params MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
     *               map.add("test", "test_param");
     * @return
     */
    public static String client(String url, HttpMethod method, MultiValueMap<String, Object> params, Map<String, String> headers) {
        log.info("url:" + url + ",params:" + params + ",headers:" + headers);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params, setHeaders(headers));
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, method, httpEntity, String.class, params);
        log.info("res_code:" + responseEntity.getStatusCode());
        return JsonUtils.stringify(responseEntity.getBody());
    }

    /**
     * json请求
     *
     * @param url
     * @param method
     * @param params
     * @param headers
     * @return
     */
    public static String clientJson(String url, HttpMethod method, Map<String, Object> params, Map<String, String> headers) {
        log.info("url:" + url + ",params:" + params + ",headers:" + headers);
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(params, setHeaders(headers));
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, method, httpEntity, String.class, params);
        log.info("res_code:" + responseEntity.getStatusCode());
        return JsonUtils.stringify(responseEntity.getBody());
    }

    /**
     * 设置请求header
     *
     * @param Headers
     * @return
     */
    public static HttpHeaders setHeaders(Map<String, String> Headers) {
        HttpHeaders httpHeaders = new HttpHeaders();
        for (Map.Entry<String, String> entry : Headers.entrySet()) {
            httpHeaders.add(entry.getKey(), entry.getValue());
        }
        return httpHeaders;
    }
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐