原地址
https://codeleading.com/article/24214844571/
下面是复制只是备份作用,直接看原地址即可
前言
Springboot 2.X系列中已经将默认的Redis客户端由Jedis改为Lettuce了,所以本文主要介绍通过Lettuce配置Redis多数据源。大家在配置时Springboot必须为2.0以上版本,我测试时使用的Springboot版本为2.2.0。
一、导入依赖
使用Lettuce连接Redis时用到了commons-pools,所以需要导入该依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
二、配置相关参数
我是在application.properties文件中配置的,yml格式的改一下即可。
# redis1 spring.redis.database=0 spring.redis.host=192.168.1.120 spring.redis.port=6379 spring.redis.password= spring.redis.timeout=1000 # 连接池最大连接数(使用负值表示没有限制) 默认为8 spring.redis.lettuce.pool.max-active=100 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认为-1 spring.redis.lettuce.pool.max-wait=-1 # 连接池中的最大空闲连接 默认为8 spring.redis.lettuce.pool.max-idle=8 # 连接池中的最小空闲连接 默认为 0 spring.redis.lettuce.pool.min-idle=0 # redis2 spring.redis2.database=1 spring.redis2.host=192.168.1.118 spring.redis2.port=6379 spring.redis2.password=
三、增加Redis配置文件
import org.apache.commons.lang3.StringUtils; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.EnableCaching; 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.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.StringRedisTemplate; import java.time.Duration; /** * @author 陈伟平 * @date 2020-10-20 14:56:00 */ @EnableCaching @Configuration public class RedisConfig { @Value("${spring.redis.lettuce.pool.max-idle}") int maxIdle; @Value("${spring.redis.lettuce.pool.max-active}") int maxActive; @Value("${spring.redis.lettuce.pool.max-wait}") long maxWaitMillis; @Value("${spring.redis.lettuce.pool.min-idle}") int minIdle; @Value("${spring.redis.timeout}") int timeout; @Bean(name = "LoginRedisTemplate") public StringRedisTemplate redisTemplate(@Value("${spring.redis.database}") int database, @Value("${spring.redis.host}") String hostName, @Value("${spring.redis.port}") int port, @Value("${spring.redis.password}") String password) { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory(connectionFactory(database, hostName, port, password)); return temple; } @Bean(name = "DeviceRedisTemplate") public StringRedisTemplate redisUatTemplate(@Value("${spring.redis2.database}") int database, @Value("${spring.redis2.host}") String hostName, @Value("${spring.redis2.port}") int port, @Value("${spring.redis2.password}") String password) { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory(connectionFactory(database, hostName, port, password)); return temple; } /** * 使用lettuce配置Redis连接信息 * * @param database Redis数据库编号 * @param hostName 服务器地址 * @param port 端口 * @param password 密码 * @return RedisConnectionFactory */ public RedisConnectionFactory connectionFactory(int database, String hostName, int port, String password) { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName(hostName); configuration.setPort(port); if (StringUtils.isNotBlank(password)) { configuration.setPassword(password); } if (database != 0) { configuration.setDatabase(database); } GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); genericObjectPoolConfig.setMaxIdle(maxIdle); genericObjectPoolConfig.setMinIdle(minIdle); genericObjectPoolConfig.setMaxTotal(maxActive); genericObjectPoolConfig.setMaxWaitMillis(maxWaitMillis); LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder() .commandTimeout(Duration.ofMillis(timeout)) .poolConfig(genericObjectPoolConfig) .build(); LettuceConnectionFactory lettuce = new LettuceConnectionFactory(configuration, clientConfig); lettuce.afterPropertiesSet(); return lettuce; } }
四、使用
使用时在需要的地方注入对应的Redis数据源即可。
@Autowired
@Resource(name = "LoginRedisTemplate")
private StringRedisTemplate stringRedisTemplate;
@Autowired
@Resource(name = "DeviceRedisTemplate")
StringRedisTemplate stringRedisTemplate;