Memcached:为什么 Memcached 服务器不断断开连接?

Memcached: why memcached server constantly drops connection?

我有两个 spring 应用程序,我都使用 spymemcached。 memcached 客户端是这样创建的

private MemcachedClient buildClient() {
    String address = this.serverAddress;
    MemcachedClient result;
    if (StringUtils.isNotEmpty(address)) {
      log.info("connecting to memcached server {}", address);
 
      try {
        result = new MemcachedClient(
            new ConnectionFactoryBuilder()
                .setTranscoder(new CustomSerializingTranscoder())
                .setProtocol(protocol)
                .setTimeoutExceptionThreshold(2)
                .build(),
            AddrUtil.getAddresses(address));
      } catch (IOException e) {
        throw new IllegalStateException("Can't create client", e);
      }
 
      log.info("connected to memcached server {}", address);
    } else {
      log.warn("cannot initialize memcached client: memcached address is not set");

      result = null;
    }
    return result;
  }

一个 spring 应用程序放入(总是替换一个)key:value 在 memcached 中。 另一个读取此密钥。并且读取值的应用程序不断断开连接。

内存缓存配置:

[r.gomboev@netris-gps-stage60-01 netris]$ sudo systemctl status memcached
● memcached.service - Memcached
   Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled)
   Active: active (running) since Пн 2022-01-24 18:29:39 MSK; 1 day 1h ago
 Main PID: 16747 (memcached)
    Tasks: 6
   Memory: 18.9M
   CGroup: /system.slice/memcached.service
           └─16747 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024 -vv

但问题是 memcached 服务器不断断开连接。

memcached 日志:

Jan 25 13:21:50 memcached-server-01 memcached: <33 new auto-negotiating client connection
Jan 25 13:22:44 memcached-server-01 memcached: <33 connection closed.
Jan 25 13:23:19 memcached-server-01 memcached: <33 new auto-negotiating client connection
Jan 25 13:23:33 memcached-server-01 memcached: <33 connection closed.
Jan 25 13:23:55 memcached-server-01 memcached: <33 new auto-negotiating client connection
Jan 25 13:26:34 memcached-server-01 memcached: <33 connection closed.
Jan 25 13:31:36 memcached-server-01 memcached: <33 new auto-negotiating client connection
Jan 25 13:45:03 memcached-server-01 memcached: <33 connection closed.
Jan 25 16:11:43 memcached-server-01 memcached: <33 new auto-negotiating client connection
Jan 25 18:45:12 memcached-server-01 memcached: <33 connection closed.
Jan 25 19:39:36 memcached-server-01 memcached: <33 new auto-negotiating client connection
Jan 25 19:41:11 memcached-server-01 memcached: <33 connection closed.

当我通过 lsof 查看连接时

on app server 81.166
[user@test-dmz-02 ~]$ sudo lsof -i -P -n | grep 11211 | grep 82.80
java      12150      cctv   90u  IPv6 1037210363      0t0  TCP 10.200.81.166:35742->10.200.82.80:11211 (ESTABLISHED)
connection established 

but on memcached server 82.80
[user@memcached-server-01]$ sudo lsof -i -P -n | grep 11211 | grep 81.166
no connection

memcached 有下一个统计数据

Escape character is '^]'.
stats
STAT pid 16747
STAT uptime 93258
STAT time 1643131435
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 10.729242
STAT rusage_system 17.056611
STAT curr_connections 15
STAT total_connections 62
STAT connection_structures 20
STAT reserved_fds 20
STAT cmd_get 738
STAT cmd_set 84624
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 737
STAT get_misses 1
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 156369146
STAT bytes_written 100524068
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 85
STAT curr_items 1
STAT total_items 84623
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END

我找不到触发器为什么连接被 memcached 服务器关闭。谁能告诉我找到解决方案的方法?谢谢

找到原因了。 idle tcp 连接被 unix 系统关闭。 参数在文件中 /proc/sys/net/ipv4/tcp_keepalive_time 秒

cat /proc/sys/net/ipv4/tcp_keepalive_time
7200

作为解决方案,如果第一次尝试失败,我会尝试另一种尝试连接到 memcached。