• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot中配置RestTemplate的方法

springboot 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

本篇文章给大家带来的内容是关于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't specified) * starts an idle connection monitor to cont inuously clean up stale connections. */@Slf4j@Configurationpublic 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();        <div style="color:transparent">本文来源gaodai.ma#com搞##代!^码@网*</div>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());                }            }        };    }}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot中配置RestTemplate的方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址