SpringBoot RestTemplate 整合 HttpClient 连接池 配置长连接

本文介绍了如何在SpringBoot应用中配置RestTemplate,以整合HttpClient并设置连接池,实现长连接。详细步骤包括创建配置属性文件、定义配置属性类、设置连接池,以及展示实际使用案例。
摘要由CSDN通过智能技术生成

一,建立配置属性文件

spring:
  http-client:
   pool:
     #连接池的最大连接数,0代表不限;如果取0,需要考虑连接泄露导致系统崩溃的后果
     maxTotalConnect: 1000
     #每个路由的最大连接数,如果只调用一个地址,可以将其设置为最大连接数
     maxConnectPerRoute: 200
     # 指客户端和服务器建立连接的超时时间,ms , 最大约21秒,因为内部tcp在进行三次握手建立连接时,默认tcp超时时间是20秒
     connectTimeout: 3000
     # 指客户端从服务器读取数据包的间隔超时时间,不是总读取时间,也就是socket timeout,ms
     readTimeout: 5000
     # 从连接池获取连接的timeout,不宜过大,ms
     connectionRequestTimout: 200
     # 重试次数
     retryTimes: 3
     charset: UTF-8
     # 长连接保持时间 单位s,不宜过长
     keepAliveTime: 10
     keepAliveTargetHost:
             www.baidu.com: 5

二,创建配置属性类

package com.aishua.zdjfzxq.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: zh
 * @Date:
 * @Description:
 */
@Component
@ConfigurationProperties(prefix = "spring.http-client.pool")
public class HttpClientPoolConfig {

    /**
     * java配置的优先级低于yml配置;如果yml配置不存在,会采用java配置
     */

    /**

     * 连接池的最大连接数
     */
    private int maxTotalConnect ;
    /**
     * 同路由的并发数
     */
    private int maxConnectPerRoute ;
    /**
     * 客户端和服务器建立连接超时,默认2s
     */
    private int connectTimeout = 2 * 1000;
    /**
     * 指客户端从服务器读取数据包的间隔超时时间,不是总读取时间,默认30s
     */
    private int readTimeout = 30 * 1000;

    private String charset = "UTF-8";
    /**
     * 重试次数,默认2次
     */
    private int retryTimes = 2;
    /**
     * 从连接池获取连接的超时时间,不宜过长,单位ms
     */
    private int connectionRequestTimout = 200;
    /**
     * 针对不同的地址,特别设置不同的长连接保持时间
     */
    private Map<String,Integer> keepAliveTargetHost;
    /**
     * 针对不同的地址,特别设置不同的长连接保持时间,单位 s
     */
    private int keepAliveTime = 60;

    public int getMaxTotalConnect() {
        return maxTotalConnect;
    }

    public void setMaxTotalConnect(int maxTotalConnect) {
        this.maxTotalConnect = maxTotalConnect;
    }

    public int getMaxConnectPerRoute() {
        return maxConnectPerRoute;
    }

    public void setMaxConnectPerRoute(int maxConnectPerRoute) {
        this.maxConnectPerRoute = maxConnectPerRoute;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public int getRetryTimes() {
        return retryTimes;
    }

    public void setRetryTimes(int retryTimes) {
        this.retryTimes = retryTimes;
    }

    publ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值