Redis-实现SpringBoot集成Redis多数据源

背景

​ 有些时候在一个项目里,由于业务问题,可能仅仅操作一个Redis数据源已经不能满足,比如某个运营系统,对接着多个不同的服务,处理数据时又不想通过远程调用,那只能增加一个数据源来解决问题,像MySQL的多数据源一样。

配置示例

这里连接池选用的是lettuce

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yaml复制代码redis:
host: localhost
port: 6379
password: 123456
timeout: 60000
database: 10
lettuce:
pool:
min-idle: 0
max-idle: 10
max-wait: -1
max-active: 200
time-between-eviction-runs: -1
redis-live:
host: localhost
port: 6379
password: 123456
database: 0

实现代码

关于序列化使用的是jackson

以下提供了创建RedisTemplate以及StringRedisTemplate

关于二者的区别:

  • 两者的关系是StringRedisTemplate继承RedisTemplate。
  • 两者的数据是不共通的;也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。
  • SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。

StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。
RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
java复制代码/**
* <p>
* RedisLettuce 连接实例配置
* </p>
*
* @author zhengshangjin
* @version 1.0.0
* @since 1.0.0
* created on 2020-04-23
*/
@Configuration
@EnableCaching
public class RedisLettuceConfig {

/**
* live数据源
*/
@Value("${spring.redis-live.host}")
private String redisLiveHost;

@Value("${spring.redis-live.port}")
private int redisLivePort;

@Value("${spring.redis-live.password}")
private String redisLivePass;

@Value("${spring.redis-live.database}")
private int redisLiveDb;

/**
* 公共配置
*/
@Value("${spring.redis.timeout}")
private long timeout;

@Value("${spring.redis.lettuce.pool.min-idle}")
private int minIdle;

@Value("${spring.redis.lettuce.pool.max-idle}")
private int maxIdle;

@Value("${spring.redis.lettuce.pool.max-active}")
private int maxActive;

@Value("${spring.redis.lettuce.pool.max-wait}")
private int maxWait;

/**
* 装配 RedisTemplate
* <p>
* 这里根据默认连接配置 装配实例
* </>
*
* @param redisConnectionFactory 默认
* @return redisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
@Bean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
return createRedisTemplate(redisConnectionFactory);
}

/**
* 装配 StringRedisTemplate
* <p>
* 这里根据默认连接配置 装配实例
* </>
* @param redisConnectionFactory 默认
* @return StringRedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
@Bean(name = "stringRedisTemplate")
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
return createStringRedisTemplate(redisConnectionFactory);
}

/**
* 装配 Live数据源
*
* @return liveStringRedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
@Bean(name = "liveStringRedisTemplate")
public StringRedisTemplate liveStringRedisTemplate() {
return createStringRedisTemplate(redisLiveHost, redisLivePort, redisLivePass, redisLiveDb);
}

/**
* 创建 RedisTemplate
*
* @param redisConnectionFactory redisConnectionFactory
* @return RedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
public RedisTemplate<Object, Object> createRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

Jackson2JsonRedisSerializer<?> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
serializer.setObjectMapper(objectMapper);

RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(serializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}

/**
* 创建 StringRedisTemplate
*
* @param redisConnectionFactory redisConnectionFactory
* @return StringRedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
public StringRedisTemplate createStringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
return stringRedisTemplate;
}

/**
* 创建 StringRedisTemplate
*
* @param host 主机
* @param port 端口
* @param password 密码
* @param database 库
* @return StringRedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
public StringRedisTemplate createStringRedisTemplate(String host, int port, String password, int database) {
// 基本配置
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(host);
configuration.setPort(port);
configuration.setDatabase(database);
if (ObjectUtils.isNotEmpty(password)) {
RedisPassword redisPassword = RedisPassword.of(password);
configuration.setPassword(redisPassword);
}

// 连接池通用配置
GenericObjectPoolConfig<?> genericObjectPoolConfig = new GenericObjectPoolConfig<>();
genericObjectPoolConfig.setMaxTotal(maxActive);
genericObjectPoolConfig.setMinIdle(minIdle);
genericObjectPoolConfig.setMaxIdle(maxIdle);
genericObjectPoolConfig.setMaxWaitMillis(maxWait);

// Lettuce Pool
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
builder.poolConfig(genericObjectPoolConfig);
builder.commandTimeout(Duration.ofSeconds(timeout));
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build());
connectionFactory.afterPropertiesSet();

StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(connectionFactory);
return stringRedisTemplate;
}

}

注入使用

根据@Qualifier指定beanname来获取

1
2
3
java复制代码@Autowired
@Qualifier("liveStringRedisTemplate")
public StringRedisTemplate stringRedisTemplate;

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%