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; } }

  

posted @ 2019-03-06 14:56  雷神约  阅读(2353)  评论(0编辑  收藏  举报