lettuce redis连接池事件监控

实现

public class ConnectionPoolWatcher implements InitializingBean {

    @Autowired(required = false)
    private ClientResources clientResources;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (clientResources != null) {
            clientResources.eventBus().get()
                    .subscribe(e -> log.info("event:{}", e));
        }
    }
}

lettuce事件

1. Redis 连接相关事件:

ConnectEvent:当尝试与 Redis 建立连接之前,就会发出这个事件。
ConnectedEvent:连接建立的时候会发出的事件,包含建立连接的远程 IP 与端口以及使用的 Redis URI 等信息,对应 Netty 其实就是 ChannelHandler 中的 channelActive 回调一开始就会发出的事件。
ConnectionActivatedEvent:在完成 Redis 连接一系列初始化操作之后(例如 SSL 握手,发送 PING 心跳命令等等),这个连接可以用于执行 Redis 命令时发出的事件。
ConnectionDeactivatedEvent:在没有任何正在处理的命令并且 isOpen() 是 false 的情况下,连接就不是活跃的了,准备要被关闭。这个时候就会发出这个事件。
DisconnectedEvent:连接真正关闭或者重置时,会发出这个事件。
ReconnectAttemptEvent:Lettuce 中的 Redis 连接会被维护为长连接,当连接丢失,会自动重连,需要重连的时候,会发

### Spring Boot 中使用 Lettuce 配置 Redis 连接池 在 Spring Boot 应用程序中,可以使用 Lettuce 客户端来配置 Redis连接池Lettuce 是一种线程安全的 Redis 客户端,支持异步操作和集群模式。以下是基于 `spring-boot-starter-data-redis` 和 Lettuce 实现 Redis 连接池的具体方法。 #### 1. 添加依赖项 首先,在项目的 `pom.xml` 文件中引入必要的 Maven 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 如果需要使用 Lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> ``` #### 2. 配置文件设置 (`application.yml`) 通过修改 `application.yml` 或 `application.properties` 来指定 Redis 的主机地址、端口以及连接池参数。以下是一个典型的 YAML 配置示例: ```yaml spring: redis: host: localhost port: 6379 password: your_password_here lettuce: pool: max-active: 8 # 最大活动连接数 max-idle: 8 # 最大空闲连接数 min-idle: 0 # 最小空闲连接数 max-wait: -1 # 获取连接的最大等待时间 (毫秒),负值表示无限期等待 ``` 此部分配置会自动映射到 `RedisProperties.Lettuce.Pool` 对象[^1]。 #### 3. 自定义 Redis 配置类 如果默认配置无法满足需求,则可以通过创建自定义 Bean 来进一步调整 Redis 配置。下面展示了一个完整的 Java 配置类示例: ```java import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public LettuceClientConfiguration clientConfiguration() { return LettucePoolingClientConfiguration.builder() .poolConfig(new io.lettuce.core.client.ClientOptions()) .build(); } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE)); template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer(objectMapper)); template.setConnectionFactory(factory); return template; } } ``` 在此代码片段中,我们设置了 JSON 序列化器并启用了连接池功能[^1]。 #### 4. 使用 RedisTemplate 执行 CRUD 操作 完成以上步骤后,即可利用 `RedisTemplate` 类执行基本的数据存储与检索操作。例如: ```java @Autowired private RedisTemplate<String, Object> redisTemplate; // 存储键值对 redisTemplate.opsForValue().set("key", "value"); // 查询键对应的值 Object value = redisTemplate.opsForValue().get("key"); System.out.println(value); // 删除某个键 redisTemplate.delete("key"); ``` 这些 API 提供了丰富的接口用于管理缓存中的数据结构[^3]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值