https://www.freesion.com/article/41531004212/

环境:

jdk1.8+tomcat8+httpclient4.5.2

主要现象:

项目偶发出现org.apache.http.NoHttpResponseException: The target server failed to respond异常

定位原因:

查阅资料,此异常属于长连接keep-Alive的一种异常现象。当服务端某连接闲置超过keep-Alive超时时间后,服务端会关闭连接,进行四次挥手。如果此时,客户端再次拿此连接来访问服务端就会报NoHttpResponseException错误。

解决过程:

既然已经知道错误导致的原因,就可对症下药。主要解决思路有两种:

方案一:延长务端keep-Alive超时时间,拿tomcat举例,可以配置Connector 元素中的keepAliveTimeout参数;

方案二:降低客户端的keep-Alive时间,在服务端关闭闲置连接前关闭客户端连接。

方案一只能优化问题,但是并不能解决问题。因为keep-Alive超时时间不能设置为-1(永久),如果设置一直保持连接会极大的影响到服务端性能。

下面主要说一下方案二的解决方案,以httpClient4.5.2版本为例:

先贴最终的代码:

  1.  
    SSLContext sslcontext = SslUtils.createIgnoreVerifySSL();
  2.  
    //设置协议http和https对应的处理socket链接工厂的对象
  3.  
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
  4.  
    .register("http", PlainConnectionSocketFactory.INSTANCE)
  5.  
    .register("https", new SSLConnectionSocketFactory(sslcontext))
  6.  
    .build();
  7.  
    ConnectionKeepAliveStrategy connectionKeepAliveStrategy = (final HttpResponse response, final HttpContext context) -> {
  8.  
    Args.notNull(response, "HTTP response");
  9.  
    final HeaderElementIterator it = new BasicHeaderElementIterator(
  10.  
    response.headerIterator(HTTP.CONN_KEEP_ALIVE));
  11.  
    while (it.hasNext()) {
  12.  
    final HeaderElement he = it.nextElement();
  13.  
    final String param = he.getName();
  14.  
    final String value = he.getValue();
  15.  
    if (value != null && param.equalsIgnoreCase("timeout")) {
  16.  
    try {
  17.  
    return Long.parseLong(value) * 1000;
  18.  
    } catch (final NumberFormatException ignore) {
  19.  
    }
  20.  
    }
  21.  
    }
  22.  
    // keep alive 3秒 客户端维护这个连接最多3秒的有效期 在获取环节超过3秒就会关闭此连接org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking() entry.isExpired(System.currentTimeMillis())
  23.  
    return 3 * 1000;
  24.  
    };
  25.  
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
  26.  
    connManager.setDefaultMaxPerRoute(10);
  27.  
    connManager.setMaxTotal(100);
  28.  
    //获取连接后 再次校验是否空闲超时org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking() entry.getUpdated() + this.validateAfterInactivity <= System.currentTimeMillis()
  29.  
    connManager.setValidateAfterInactivity(3000);
  30.  
    //evictIdleConnections 超时之前 定期回收空闲连接 并发setMaxConnPerRoute=10 最多setMaxConnTotal=100个;注意,evictIdleConnections会在启动时线程sleep一个maxIdle时间
  31.  
    //创建自定义的httpclient对象
  32.  
    CloseableHttpClient client = HttpClients.custom()
  33.  
    // 注意:HttpClients的setDefaultMaxPerRoute和setMaxTotal不会覆盖connManager的值
  34.  
    .setConnectionManager(connManager)
  35.  
    .setConnectionManagerShared(false).evictIdleConnections(3000, TimeUnit.MILLISECONDS)
  36.  
    .setKeepAliveStrategy(connectionKeepAliveStrategy)
  37.  
    // 接口幂等 允许重试 注释掉disableAutomaticRetries 默认重试3次 会从连接池中获取 不会直接创建新的连接
  38.  
    .disableAutomaticRetries()
  39.  
    .build();

主要配置参数说明:

  1. org.apache.http.impl.conn.PoolingHttpClientConnectionManager#setValidateAfterInactivity
  2. org.apache.http.impl.client.HttpClientBuilder#setConnectionManagerShared
  3. org.apache.http.impl.client.HttpClientBuilder#evictIdleConnections(long, java.util.concurrent.TimeUnit)
  4. org.apache.http.impl.client.HttpClientBuilder#setKeepAliveStrategy
  5. org.apache.http.impl.client.HttpClientBuilder#disableAutomaticRetries

ORG.APACHE.HTTP.IMPL.CONN.POOLINGHTTPCLIENTCONNECTIONMANAGER#SETVALIDATEAFTERINACTIVITY

从连接池中获取到空闲连接后,在使用之前校验空闲时间是否超过指定的时间,单位毫秒;注意,如果你像楼主一样,使用了

PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

这块代码,那么请注意此处会把时间设置为2000ms.(ps:楼主在本地环境一直复现不了NoHttpResponseException的罪魁祸首)

方法路径:

org.apache.http.impl.conn.PoolingHttpClientConnectionManager#PoolingHttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionOperator, org.apache.http.conn.HttpConnectionFactory<org.apache.http.conn.routing.HttpRoute,org.apache.http.conn.ManagedHttpClientConnection>, long, java.util.concurrent.TimeUnit)

逻辑上只要配置此处,即可保证连接在超时后关闭并重新从池子中获取(如果还超时,继续关闭连接并重新拿),无论哪一种配置,一定要配置此处,否则都会安装默认2秒过期时间来回收连接。感兴趣的可以看下源码:

  1.  
    private E getPoolEntryBlocking(
  2.  
    final T route, final Object state,
  3.  
    final long timeout, final TimeUnit tunit,
  4.  
    final PoolEntryFuture<E> future)
  5.  
    throws IOException, InterruptedException, TimeoutException {
  6.  
     
  7.  
    Date deadline = null;
  8.  
    if (timeout > 0) {
  9.  
    deadline = new Date
  10.  
    (System.currentTimeMillis() + tunit.toMillis(timeout));
  11.  
    }
  12.  
     
  13.  
    this.lock.lock();
  14.  
    try {
  15.  
    final RouteSpecificPool<T, C, E> pool = getPool(route);
  16.  
    E entry = null;
  17.  
    while (entry == null) {
  18.  
    Asserts.check(!this.isShutDown, "Connection pool shut down");
  19.  
    for (;;) {
  20.  
    entry = pool.getFree(state);
  21.  
    if (entry == null) {
  22.  
    break;
  23.  
    }
  24.  
    if (entry.isExpired(System.currentTimeMillis())) {
  25.  
    entry.close();
  26.  
    } else if (this.validateAfterInactivity > 0) {
  27.  
    //看这里 连接最后修改时间+超时时间是否小于now
  28.  
    if (entry.getUpdated() + this.validateAfterInactivity <= System.currentTimeMillis()) {
  29.  
    if (!validate(entry)) {
  30.  
    entry.close();
  31.  
    }
  32.  
    }
  33.  
    }
  34.  
    if (entry.isClosed()) {
  35.  
    this.available.remove(entry);
  36.  
    pool.free(entry, false);
  37.  
    } else {
  38.  
    break;
  39.  
    }
  40.  
    }
  41.  
    if (entry != null) {
  42.  
    this.available.remove(entry);
  43.  
    this.leased.add(entry);
  44.  
    onReuse(entry);
  45.  
    return entry;
  46.  
    }
  47.  
     
  48.  
    // New connection is needed
  49.  
    final int maxPerRoute = getMax(route);
  50.  
    // Shrink the pool prior to allocating a new connection
  51.  
    final int excess = Math.max(0, pool.getAllocatedCount() + 1 - maxPerRoute);
  52.  
    if (excess > 0) {
  53.  
    for (int i = 0; i < excess; i++) {
  54.  
    final E lastUsed = pool.getLastUsed();
  55.  
    if (lastUsed == null) {
  56.  
    break;
  57.  
    }
  58.  
    lastUsed.close();
  59.  
    this.available.remove(lastUsed);
  60.  
    pool.remove(lastUsed);
  61.  
    }
  62.  
    }
  63.  
     
  64.  
    if (pool.getAllocatedCount() < maxPerRoute) {
  65.  
    final int totalUsed = this.leased.size();
  66.  
    final int freeCapacity = Math.max(this.maxTotal - totalUsed, 0);
  67.  
    if (freeCapacity > 0) {
  68.  
    final int totalAvailable = this.available.size();
  69.  
    if (totalAvailable > freeCapacity - 1) {
  70.  
    if (!this.available.isEmpty()) {
  71.  
    final E lastUsed = this.available.removeLast();
  72.  
    lastUsed.close();
  73.  
    final RouteSpecificPool<T, C, E> otherpool = getPool(lastUsed.getRoute());
  74.  
    otherpool.remove(lastUsed);
  75.  
    }
  76.  
    }
  77.  
    final C conn = this.connFactory.create(route);
  78.  
    entry = pool.add(conn);
  79.  
    this.leased.add(entry);
  80.  
    return entry;
  81.  
    }
  82.  
    }
  83.  
     
  84.  
    boolean success = false;
  85.  
    try {
  86.  
    pool.queue(future);
  87.  
    this.pending.add(future);
  88.  
    success = future.await(deadline);
  89.  
    } finally {
  90.  
    // In case of 'success', we were woken up by the
  91.  
    // connection pool and should now have a connection
  92.  
    // waiting for us, or else we're shutting down.
  93.  
    // Just continue in the loop, both cases are checked.
  94.  
    pool.unqueue(future);
  95.  
    this.pending.remove(future);
  96.  
    }
  97.  
    // check for spurious wakeup vs. timeout
  98.  
    if (!success && (deadline != null) &&
  99.  
    (deadline.getTime() <= System.currentTimeMillis())) {
  100.  
    break;
  101.  
    }
  102.  
    }
  103.  
    throw new TimeoutException("Timeout waiting for connection");
  104.  
    } finally {
  105.  
    this.lock.unlock();
  106.  
    }
  107.  
    }

方法路径:

org.apache.http.pool.AbstractConnPool#getPoolEntryBlocking

ORG.APACHE.HTTP.IMPL.CLIENT.HTTPCLIENTBUILDER#SETCONNECTIONMANAGERSHARED和ORG.APACHE.HTTP.IMPL.CLIENT.HTTPCLIENTBUILDER#EVICTIDLECONNECTIONS(LONG, JAVA.UTIL.CONCURRENT.TIMEUNIT)

启动异步定时线程,关闭回收指定超时时间的空闲连接。如果在获取空闲连接前已经回收就没问题了,但是极端情况下也会出现NoHttpResponseException问题。比如:keep-Alive超时时间是20秒,然后定时配置15秒,假设第一次使用连接并释放时间为x,定时上次结束时间为y,y+15<x+20,也就是定时下次处理时,连接空闲时间还没有超过20秒,那么此处定时不会回收此连接,但是如果5秒后获取这个连接使用,肯定会报NoHttpResponseException异常。

evictIdleConnections需要配合setConnectionManagerShared=false使用,ConnectionManagerShared默认false。关键代码如下:

  1.  
    if (!this.connManagerShared) {
  2.  
    if (closeablesCopy == null) {
  3.  
    closeablesCopy = new ArrayList<Closeable>(1);
  4.  
    }
  5.  
    final HttpClientConnectionManager cm = connManagerCopy;
  6.  
     
  7.  
    if (evictExpiredConnections || evictIdleConnections) {
  8.  
    final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor(cm,
  9.  
    maxIdleTime > 0 ? maxIdleTime : 10, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS);
  10.  
    closeablesCopy.add(new Closeable() {
  11.  
     
  12.  
    @Override
  13.  
    public void close() throws IOException {
  14.  
    connectionEvictor.shutdown();
  15.  
    }
  16.  
     
  17.  
    });
  18.  
    connectionEvictor.start();
  19.  
    }
  20.  
    closeablesCopy.add(new Closeable() {
  21.  
     
  22.  
    @Override
  23.  
    public void close() throws IOException {
  24.  
    cm.shutdown();
  25.  
    }
  26.  
     
  27.  
    });
  28.  
    }

注意:evictIdleConnections会在启动时,线程sleep一个maxIdle时间。源码如下:

  1.  
    public IdleConnectionEvictor(
  2.  
    final HttpClientConnectionManager connectionManager,
  3.  
    final ThreadFactory threadFactory,
  4.  
    final long sleepTime, final TimeUnit sleepTimeUnit,
  5.  
    final long maxIdleTime, final TimeUnit maxIdleTimeUnit) {
  6.  
    this.connectionManager = Args.notNull(connectionManager, "Connection manager");
  7.  
    this.threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory();
  8.  
    this.sleepTimeMs = sleepTimeUnit != null ? sleepTimeUnit.toMillis(sleepTime) : sleepTime;
  9.  
    this.maxIdleTimeMs = maxIdleTimeUnit != null ? maxIdleTimeUnit.toMillis(maxIdleTime) : maxIdleTime;
  10.  
    this.thread = this.threadFactory.newThread(new Runnable() {
  11.  
    @Override
  12.  
    public void run() {
  13.  
    try {
  14.  
    while (!Thread.currentThread().isInterrupted()) {
  15.  
    //此处休眠一个sleepTimeMs时间 可追溯代码发现sleepTimeMs来源于maxIdleTime
  16.  
    Thread.sleep(sleepTimeMs);
  17.  
    connectionManager.closeExpiredConnections();
  18.  
    if (maxIdleTimeMs > 0) {
  19.  
    connectionManager.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS);
  20.  
    }
  21.  
    }
  22.  
    } catch (final Exception ex) {
  23.  
    exception = ex;
  24.  
    }
  25.  
     
  26.  
    }
  27.  
    });
  28.  
    }

方法路径:

org.apache.http.impl.client.IdleConnectionEvictor#IdleConnectionEvictor(org.apache.http.conn.HttpClientConnectionManager, java.util.concurrent.ThreadFactory, long, java.util.concurrent.TimeUnit, long, java.util.concurrent.TimeUnit)

ORG.APACHE.HTTP.IMPL.CLIENT.HTTPCLIENTBUILDER#SETKEEPALIVESTRATEGY

此方法是设置客户端连接池维护的连接的keep-Alive时间。如果连接空闲时间超过设置的时间,则会关闭此连接并重新获取。主要相关源码如下:在初始化线程池时设置了过期时间expiry是创建时间+keep-Alive时间,已经过期时间在updateExpiry(连接池回收会调接方法)中被修改成最新时间。

  1.  
    //方法路径:org.apache.http.pool.PoolEntry#PoolEntry(java.lang.String, T, C, long, java.util.concurrent.TimeUnit) 此处的timeToLive 就是设置的keep-Alive时间
  2.  
    public PoolEntry(final String id, final T route, final C conn,
  3.  
    final long timeToLive, final TimeUnit tunit) {
  4.  
    super();
  5.  
    Args.notNull(route, "Route");
  6.  
    Args.notNull(conn, "Connection");
  7.  
    Args.notNull(tunit, "Time unit");
  8.  
    this.id = id;
  9.  
    this.route = route;
  10.  
    this.conn = conn;
  11.  
    this.created = System.currentTimeMillis();
  12.  
    if (timeToLive > 0) {
  13.  
    this.validityDeadline = this.created + tunit.toMillis(timeToLive);
  14.  
    } else {
  15.  
    this.validityDeadline = Long.MAX_VALUE;
  16.  
    }
  17.  
    this.expiry = this.validityDeadline;
  18.  
    }
  19.  
     
  20.  
     
  21.  
    //方法路径:org.apache.http.pool.PoolEntry#updateExpiry
  22.  
    public synchronized void updateExpiry(final long time, final TimeUnit tunit) {
  23.  
    Args.notNull(tunit, "Time unit");
  24.  
    this.updated = System.currentTimeMillis();
  25.  
    final long newExpiry;
  26.  
    if (time > 0) {
  27.  
    newExpiry = this.updated + tunit.toMillis(time);
  28.  
    } else {
  29.  
    newExpiry = Long.MAX_VALUE;
  30.  
    }
  31.  
    this.expiry = Math.min(newExpiry, this.validityDeadline);
  32.  
    }

以上配置不是互斥也不少都需要配置,楼主亲自验证发现,只配置setValidateAfterInactivity或只配置setKeepAliveStrategy都可以。evictIdleConnections极端情况会有问题。

ORG.APACHE.HTTP.IMPL.CLIENT.HTTPCLIENTBUILDER#DISABLEAUTOMATICRETRIES

随便一提,httpclient默认会重试3次。如果接口不支持幂等,请注意不要使用重试。

OK,为了解决个问题,把源码看了一遍,特写博客以备以后注意使用。

【转载】 一次生产环境的NOHTTPRESPONSEEXCEPTION异常的排查记录的更多相关文章

  1. 教你50招提升ASP.NET性能(十二):在生产环境,仔细考虑你需要记录哪些日志

    (18)When in production, carefully consider what you need to log 招数18: 在生产环境,仔细考虑你需要记录哪些日志 Many peopl ...

  2. 一次生产环境CPU占用高的排查

    1. 项目背景 甲方是保密级别非常高的政府部门.所以我们全程拿不到任何测试数据,只能是自己模拟数据进行测试. 项目部署的时候,公司派了一人到甲方现场,在甲方客户全程监督下,进行部署,调试,导入数据等工 ...

  3. 记一次 MySQL 主从同步异常的排查记录,百转千回

    你好,我是悟空. 这是悟空的第 183 篇原创文章 官网:www.passjava.cn 本文主要内容如下: 一.现象 最近项目的测试环境遇到一个主备同步的问题: 备库的同步线程停止了,无法同步主库的 ...

  4. wordpress后台加载速度异常缓慢排查记录(原创)

    原因在于在function.php函数中加入了下面的代码导致了缓慢: //停用版本更新通知remove_action('load-update-core.php', 'wp_update_themes ...

  5. SpringBoot+ShardingSphere彻底解决生产环境数据库字段加解密问题

    前言   互联网行业公司,对于数据库的敏感字段是一定要进行加密的,方案有很多,最直接的比如写个加解密的工具类,然后在每个业务逻辑中手动处理,在稍微有点规模的项目中这种方式显然是不现实的,不仅工作量大而 ...

  6. vite项目生产环境去掉console信息【转载】

    环境变量引入 通常去掉console为生产环境,即需要引入环境变量.具体请看这篇文章: vite项目初始化之~环境变量 注意 与webpacak相比,vite已经将这个功能内置到了,所以我们只需要配置 ...

  7. 分布式日志框架Exceptionless之生产环境部署步骤

    Exceptionless 是一个开源的实时的日志收集框架,它将日志收集变得简单易用并且不需要了解太多的相关技术细节及配置.本篇基于我的上一篇<基于Exceptionless实现分布式日志> ...

  8. Dubbo Mesh 在闲鱼生产环境中的落地实践

    本文作者至简曾在 2018 QCon 上海站以<Service Mesh 的本质.价值和应用探索>为题做了一次分享,其中谈到了 Dubbo Mesh 的整体发展思路是“借力开源.反哺开源” ...

  9. 如何在生产环境使用Btrace进行调试

    占小狼 转载请注明原创出处,谢谢! 背景 记得前几天有人问我:在生产环境中可能经常遇到各种问题,你们一般是如何进行调试的? 很惭愧,没有经验.因为平时碰不到生产环境的服务器,定位问题需要各种数据,所以 ...

  10. Flink 实战:如何解决生产环境中的技术难题?

    大数据作为未来技术的基石已成为国家基础性战略资源,挖掘数据无穷潜力,将算力推至极致是整个社会面临的挑战与难题. Apache Flink 作为业界公认为最好的流计算引擎,不仅仅局限于做流处理,而是一套 ...

随机推荐

  1. 安装并运行tomcat8

    ps:tomcat7对应 jdk 1.7 tomcat8对应 jdk 1.8 注意要对应自己的项目选择下载tomcat版本 1. 软件商城搜索安装 tokcat 找到自己的tomcat的端口 8023 ...

  2. EDGE 浏览器占用内存优化

    windows + s 搜索 service 打开服务 : 找到下面 edge 三项 双击 把启动类型都改成 手动触发

  3. MYSQL存储过程-练习3 repeat循环

    MYSQL存储过程-练习3 repeat循环 1 DELIMITER $$ 2 3 CREATE PROCEDURE `sp_repeat`() 4 BEGIN 5 DECLARE i INT; 6 ...

  4. feign调用其他服务解码错误

    1.在使用A服务通过feign调用B服务时报错: feign.codec.DecodeException: Error while extracting response for type [clas ...

  5. .NET 隐藏/自定义windows系统光标

    本文介绍如何操作windows系统光标.正常我们设置/隐藏光标,只能改变当前窗体或者控件范围,无法全局操作windows光标.接到一个需求,想隐藏windows全局的鼠标光标显示,下面讲下如何操作 先 ...

  6. Web渗透02_信息搜集

    以两个测试工具官方给出的用于工具实践的网站.一定不要拿在运营的网站做测试. http://testfire.net http://vulnweb.com DNS信息搜集 关注域名注册商,管理员的邮箱电 ...

  7. git安装-Tortoise git 安装汉化教程

    1.首先下载 去官网下载 如果下载比较慢的,链接自取 https://pan.quark.cn/s/fcb9d0b39c7f 2. 安装git 3. 安装git图形化工具Tortoise git 4. ...

  8. games101_Homework1

    本次作业的任务是填写一个旋转矩阵和一个透视投影矩阵.给定三维下三个 点 v0(2.0, 0.0, −2.0), v1(0.0, 2.0, −2.0), v2(−2.0, 0.0, −2.0), 你需要 ...

  9. 使用 C# 入门深度学习:Pytorch 基础

    教程名称:使用 C# 入门深度学习 作者:痴者工良 地址: https://torch.whuanle.cn 1.2 Pytorch 基础 本文内容介绍 Pytorcn 的基础 API,主要是数组的创 ...

  10. SPI接口,如何对W25Q64进行读写操作?深度解析

    ​ 一.SPI概述 SPI(SerialPeripheralInterface)是一种同步串行通信协议,广泛应用于微控制器和外围设备之间的数据传输.它由摩托罗拉公司开发,具有全双工通信能力,即可以同时 ...