首頁 >Java >java教程 >springboot中配置RestTemplate的方法

springboot中配置RestTemplate的方法

不言
不言原創
2018-09-20 14:44:094670瀏覽

這篇文章帶給大家的內容是關於springboot中配置RestTemplate的方法,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

背景:最近工作上搭建了一個中間系統,採用了RestTemplate框架調用第三系統restful接口,調用方採用輪詢的方式調用本系統的相關接口,期間多次出現堆記憶體溢出,系統假死,通用java自帶的java記憶體分析工具分析系統產生的dump檔案發現有一個物件一直沒被回收,佔用98%堆內存,使用MAT分析該物件所在的線程,發現是使用resttemplate的相關線程,及相關的線程產生的堆對象並沒有隨resttemplate的close後被清除掉,網上搜索相關資料顯示,傳統HttpClient 在高請求量和高並發情況,很容易出現堆內存溢出的情況,而使用Apache中的HttpClient的實例CloseableHttpClient很少發生該現象,而RestTemplate預設使用的是org.springframework.http.client.ClientHttpRequest,需在設定檔進行相關的替換;

#本文主要記錄springboot中配置RestTemplate 。

 1、新增相依性:

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>

#2、設定CloseableHttpClient向相關屬性,並設定定時清理連線

#
Package com.**.config;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.connectionKeepAliveStrategy;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.comn.ssl.TrustSelfsignedStrategy;
import org.apache.http.Impl.client.CloseableHttpClient;
import org.apache.http.Impl.client.Httpclients;
import org.apache.http.Impl.conn.PoolingHttpclientConnectionManager;
import org.apache.http.Message.BasicHeaderELementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;
/* *
 * Supports both HTTP and HTTPS
 * Uses a connect ion pool to re-use connect ions and save overhead of creat ing connect ions.
 * Has a custom connection keep-al ive strategy  (to apply a default keep-alive if one isn&#39;t specified)
 * starts an idle connection monitor to cont inuously clean up stale connections.
 */
@Slf4j
@Configuration
public class HttpClientConfig {

    //Determines the timeout in milliseconds until a  connection is established.
    private static final int CONNECT_TIMEOUT = 30000;

    //The  timeout when requesting a connection from the connection manager.
    private static final int REQUEST_ TIMEOUT = 30000;

    //The timeout for waiting for data
    private static final int SOCKET_ TIMEOUT = 60000;

    private static final int MAX_ TOTAL CONNECTIONS = 50;
    private static final int DEFAULT KEEP ALIVE_ TIME_ MIlLIS = 20 * 1000;
    private static final int CLOSE_ IDLE_ CONNECTION WAIT_ TIME_ SECS = 30;

    @Bean
    public PoolingHttpClientConnectionManager poolingConnectionManager() {

        SSLContextBuilder builder = new SSLContextBuilder ();
        try {
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        }catch (NoSuchAlgorithmException | KeyStoreException e) {
            log.error ("Pooling Connection Manager Initialisation failure because of"+ e.getMessage(), e);
        }
        SSLConnectionSocketFactory sslsf = null;
        try{
            sslsf = new SSLConnectionSocketFactory (builder.build());
        } catch (KeyManagementException | NoSuchAlgorithmException e) {
            log.error("Pooling Connection Manager Initialisation failure because of" + e.GetMessage(), e);
        }
        Registry <ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                .create ().register ("https", sslsf)
                .register (id: "http", new PlainConnectionSocketFactory ())
                .build ();

        PoolingHttpclientConnectionManager poolingConnectionManager = new PoolingHttpclientConnectionManager  (socketFactoryRegistry);
        poolingConnectionManager.setMaxTotal(MAX_ TOTAL CONNECTIONS);
        return poolingConnectionManager;
    }

    @Bean
    public ConnectionKeepAliveStrategy connectionKeepAliveStrategy () {
        return new ConnectionKeepAliveStrategy (){
            @override
            public long getKeepAliveDuration (HttpResponse response, HttpContext context) {
                HeaderElementIterator it = new BasicHeaderElementIterator
                        (response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                while (it.hasNext()) {
                    HeaderElement he = it.nextElement();
                    String param = he.getName();
                    String value = he.getValue();
                    if (value != null && param.equalsIgnoreCase("timeout")) {
                        return Long.parseLong(value) * 1000;
                    }
                }
                return DEFAULT_ KEEP_ALIVE_TIME_MILlIS;
            }
        };
    }
    @Bean
    public CloseableHttpClient httpClient () {
        RequestConfig requestConfig = RequestConfig.custom ()
                .setConnectionRequestTimeout(REQUEST_TIMEOUT)
                .setConnectTimeout (CONNECT_TIMEOUT)
                .setSocketTimeout (SOCKET_TIMEOUT).build();

        return Httpclients.custom ()
                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager (poolingConnectionManager ())
                .setKeepAliveStrategy (connectionKeepAliveStrategy ())
                .build ();
    }

    @Bean
    public Runnable idleConnectionMonitor (final PoolingHttpClientConnectionManager connectionManager){
        return new Runnable() {
            @override
            @Scheduled(fixedDelay = 10000)
            public void run() {
                try {
                    if (connectionManager != null) {
                        log.trace("run IdleConnectionMonitor - Closing expired and idle connections... ");
                        connectionManager.closeExpiredConnections();
                        connectionManager.closeIdleConnections(CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS, TimeUnit.SECONDS);
                    } else
                        log.info("run IdleConnectionMonitor - Http Client Connection manager is not initialised");
                } catch (Exception e) {
                    log.error("run IdleConnectionMonitor - Exception occurred.msg={}. ", e.getMessage());
                }
            }
        };
    }
}

3、替換TestTemplate中的預設httpClient

#
Package com.**.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.Httpcomponentsclienthttprequestfactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;


@Slf4j
@Configuration
public class RestTemplateConfig {

    @Autowired
    CloseableHttpClient httpClient;

    @Bean
    public RestTemplate restTemplate () {

        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());

        /**
         * StringHttpMessogeConverter 默认使用 IS0-8859-编码,此处修改为 UTF-8
         */
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        Iterator<HttpMessageConverter<?>> iterator = messageConverters.iterator();
        while (iterator.hasNext()) {
            HttpMessageConverter<?> converter = iterator.next();
            if (converter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) converter).setDefaultCharset(Charset.forName("UTF-8"));
            }
        }
        return restTemplate;
    }

    @Bean
    public HttpComponentsClientHttpRequestFactory clientHttpRequestfactory () {
        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory =  new HttpComponentsClientHttpRequestfactory();
        clientJttpRequestFactory.setHttpClient(httpClient);
        return clientHttpRequestFactory;
    }

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPooolTaskScheduler scheduler = new ThreadPooolTaskScheduler();
        scheduler.setThreadNamePrefix("poolScheduler");
        scheduler.setPoolSize (50);
        return scheduler;
    }
}

4、在啟動類別中註入RestTemplate類別:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
       return builder.build();
}

 至此,RestTemplate可以正常使用了。

以上是springboot中配置RestTemplate的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn