• Redis学习笔记3:基于springboot的lettuce redis客户端validateConnection连接有效性检查


    LettuceConnectionFactory连接工厂默认对redis操作时不会对本地共享连接进行有效性检测,不进行有效性检测可以 提升应用程序的性能,但是也会带来一定的连接无效性的风险,LettuceConnectionFactory提供了一个validateConnection属性,默认值是false,可以在我们对性能要求不是很高的场景下对redis操作之前进行有效性检查,如果无效则重新建立连接。

    一个对springboot redis框架进行重写,支持lettuce、jedis、连接池、同时连接多个集群、多个redis数据库、开发自定义属性配置的开源SDK

    <dependency>
        <groupId>io.github.mingyang66groupId>
        <artifactId>emily-spring-boot-redisartifactId>
        <version>4.3.9version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    GitHub地址:https://github.com/mingyang66/spring-parent

    一、LettuceConnectionFactory连接工厂属性
    //是否进行连接有效性检查,默认:false
    private boolean validateConnection = false;
    
    • 1
    • 2
    二、LettuceConnectionFactory.SharedConnection#getConnection获取连接并进行有效性检查
    		@Nullable
    		StatefulConnection<E, E> getConnection() {
    
    			synchronized (this.connectionMonitor) {
    
    				if (this.connection == null) {
              //获取共享本地连接
    					this.connection = getNativeConnection();
    				}
            //判定是否进行连接有效性检查
    				if (getValidateConnection()) {
    					validateConnection();
    				}
    
    				return this.connection;
    			}
    		}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    三、LettuceConnectionFactory.SharedConnection#validateConnection对连接进行检查或重建
    		void validateConnection() {
    
    			synchronized (this.connectionMonitor) {
    				//连接是否有效,默认:无效
    				boolean valid = false;
    				//如果连接不为空,并且是打开状态,则对连接进行ping操作
    				if (connection != null && connection.isOpen()) {
    					try {
    
    						if (connection instanceof StatefulRedisConnection) {
    							((StatefulRedisConnection) connection).sync().ping();
    						}
    
    						if (connection instanceof StatefulRedisClusterConnection) {
    							((StatefulRedisClusterConnection) connection).sync().ping();
    						}
                //如果可以ping通,则说明是有效连接
    						valid = true;
    					} catch (Exception e) {
    						log.debug("Validation failed", e);
    					}
    				}
    
    				if (!valid) {
    
    					log.info("Validation of shared connection failed; Creating a new connection.");
              //如果是无效连接,则重置连接
    					resetConnection();
              //重新建立本地连接
    					this.connection = getNativeConnection();
    				}
    			}
    		}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    java毕业设计成品基于SpringBoot体育用品购物商城-协同过滤推荐算法[包运行成功]
    第十七篇:稳定性之告警体系
    华为机考:HJ43 迷宫问题
    Jenkins 执行远程shell脚本部署jar文件问题起不来
    【数据库原理与应用(第3版)】第二章:数据模型(选择与填空)
    es和kibana单机搭建
    LabVIEW流量控制系统
    Docker安装Redis
    技术20期:结构化数据与非结构化数据:有什么区别?
    uvm白皮书练习_ch2_ch221只有driver的验证平台之*2.2.1 最简单的验证平台
  • 原文地址:https://blog.csdn.net/yaomingyang/article/details/134087419