在网上搜索了好久,发现没有多少可以有用的。翻了一下源码,自己写一个。
有兴趣的可以了解一下lettuce。从SpringBoot2.x之后,redis原先使用的Jedis被lettuce替代。
1、导入pom依赖
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.6.7</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
2、在配置文件中写redis连接信息
# 自定义多个redis源
definition:
common:
redis:
host: 10.138.8.85
port: 6379
password: Haier,123
timeout: 5000
company:
redis:
host: 10.138.8.85
port: 6379
password: Haier,123
timeout: 5000
3、编写config文件
@Configuration
public class RedisConfig {
/*--------------------------- 公共信息使用的redis: commonRedisTemplate ----------------------------------*/
@Value("${definition.common.redis.host}")
private String commonHost;
@Value("${definition.common.redis.port}")
private String commonPort;
@Value("${definition.common.redis.password}")
private String commonPassword;
@Value("${definition.common.redis.timeout}")
private String commonTimeout;
@Bean("commonRedisTemplate")
public RedisTemplate<String,String> getCommonRedisTemplate(){
return build(commonHost,commonPort,commonPassword,commonTimeout);
}
// ---------------------------------------公司信息使用的redis: companyRedisTemplate---------------------------------------------------
@Value("${definition.company.redis.host}")
private String companyHost;
@Value("${definition.company.redis.port}")
private String companyPort;
@Value("${definition.company.redis.password}")
private String companyPassword;
@Value("${definition.company.redis.timeout}")
private String companyTimeout;
@Bean("companyRedisTemplate")
public RedisTemplate<String,String> getCompanyRedisTemplate(){
return build(companyHost,companyPort,companyPassword,companyTimeout);
}
private RedisTemplate<String,String> build(String host,String port,String password,String timeout){
//单机部署
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host,Integer.parseInt(port));
redisStandaloneConfiguration.setPassword(password);
//集群部署
//RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(commonHost.split(";")));
//redisClusterConfiguration.setPassword(commonPassword);
LettucePoolingClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.commandTimeout(Duration.ofMillis(Long.parseLong(timeout)))
.poolConfig(new GenericObjectPoolConfig())
.build();
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration,clientConfig);
//必须初始化实例
connectionFactory.afterPropertiesSet();
//如果要使pool参数生效,一定要关闭shareNativeConnection
//lettuceConnectionFactory.setShareNativeConnection(false);
RedisTemplate<String, String> commonRedisTemplate = new StringRedisTemplate();
commonRedisTemplate.setConnectionFactory(connectionFactory);
// key、hash的key 采用 String序列化方式 【可自行配置】
commonRedisTemplate.setKeySerializer(new StringRedisSerializer());
commonRedisTemplate.setHashKeySerializer(new StringRedisSerializer());
// value、hash的value 采用 String 序列化方式
commonRedisTemplate.setValueSerializer(n