SpringBoot 基于lettuce 连接池 配置redis多数据源操作 生产配置

添加pom
<dependency>
    <groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.1</version>
</dependency>

package com.atirm.mybatismutiplesource.config.RedisConfig; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; 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.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; /** * redis MultipleDataSource configuration */ @Configuration public class RedisGlobalConfig { @Bean public RedisTemplate redisTemplateLocal( @Value("${spring.redis.local.database}") int database, @Value("${spring.redis.lettuce.pool.max-active}") int maxActive, @Value("${spring.redis.lettuce.pool.max-idle}") int maxIdle, @Value("${spring.redis.lettuce.pool.min-idle}") int minIdle, @Value("${spring.redis.timeout}") long timeout, @Value("${spring.redis.local.host}") String hostName, @Value("${spring.redis.local.port}") int port ) { /* ========= 基本配置 ========= */ RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName(hostName); configuration.setPort(port); configuration.setDatabase(database); /* ========= 连接池通用配置 ========= */ GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); genericObjectPoolConfig.setMaxIdle(maxIdle); genericObjectPoolConfig.setMinIdle(minIdle); genericObjectPoolConfig.setMaxTotal(maxActive); /* ========= lettuce pool ========= */ LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder(); builder.poolConfig(genericObjectPoolConfig); builder.commandTimeout(Duration.ofSeconds(timeout)); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build()); connectionFactory.afterPropertiesSet(); return createRedisTemplate(connectionFactory); } public RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } }

  

转载于:https://www.cnblogs.com/leigepython/p/10483230.html

可以通过在Spring Boot中配置Lettuce连接池的validateConnection属性来检测连接的可用性。validateConnection属性默认为false,需要手动设置为true。 具体配置方式如下: 1. application.properties文件中添加以下配置: ``` spring.redis.lettuce.pool.validate-connection=true spring.redis.lettuce.pool.min-idle=1 spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.max-active=10 spring.redis.lettuce.pool.max-wait=-1ms ``` 其中,validate-connection设置为true表示开启连接可用性检测,min-idle、max-idle、max-active和max-wait分别表示连接池中的最小空闲连接数、最大空闲连接数、最大活跃连接数和最大等待时间。 2. 通过配置LettucePoolingClientConfiguration类来创建Lettuce连接池: ``` @Configuration public class RedisConfig { @Bean public LettuceConnectionFactory redisConnectionFactory() { LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder() .validateConnection(true) .build(); RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName("localhost"); redisStandaloneConfiguration.setPort(6379); return new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } } ``` 在LettucePoolingClientConfiguration中通过builder方法创建一个Lettuce连接池配置对象,并将validateConnection设置为true,然后将该配置对象和RedisStandaloneConfiguration对象一起传入LettuceConnectionFactory的构造方法中来创建Lettuce连接池。 最后,将LettuceConnectionFactory对象注入到RedisTemplate中即可使用。 以上是配置Lettuce连接池检测连接可用性的方法,希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值