我是靠谱客的博主 有魅力宝马,这篇文章主要介绍spring-boot 速成(12) - 如何注入多个redis StringRedisTemplate,现在分享给大家,希望可以做个参考。

默认情况下,spring-boot的redis自动配置,只能注册一个StringRedisTemplate实例,如果希望注入多个,比如:1个读写database 0,1个读写database 1 ... ,默认的自动配置就不行了,可以参考下面的做法:

一、创建多实例配置类

复制代码
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
1 package cn.mwee.order.cloud.admin.common.config; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.context.annotation.Primary; 7 import org.springframework.data.redis.connection.RedisConnectionFactory; 8 import org.springframework.data.redis.connection.RedisSentinelConfiguration; 9 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 10 import org.springframework.data.redis.core.StringRedisTemplate; 11 import org.springframework.data.redis.serializer.StringRedisSerializer; 12 import redis.clients.jedis.JedisPoolConfig; 13 14 import java.util.HashSet; 15 import java.util.Set; 16 17 /** 18 * Created by 菩提树下的杨过(http://yjmyzz.cnblogs.com/) on 19/09/2017. 19 */ 20 @Configuration 21 public class RedisConfig { 22 23 @Value("${redis.sentinel.group}") 24 protected String sentinelGroupName; 25 26 @Value("${redis.sentinel.nodes}") 27 protected String sentinelNodes; 28 29 @Value("${redis.maxTotal}") 30 protected int maxTotal; 31 32 @Value("${redis.minIdle}") 33 protected int minIdle; 34 35 @Value("${redis.maxIdle}") 36 protected int maxIdle; 37 38 @Value("${redis.maxWaitMillis}") 39 protected long maxWaitMillis; 40 41 @Value("${redis.testOnBorrow}") 42 protected boolean testOnBorrow; 43 44 @Value("${redis.testOnReturn}") 45 protected boolean testOnReturn; 46 47 @Value("${redis.password}") 48 protected String password; 49 50 @Value("${redis.timeout}") 51 protected int timeout; 52 53 @Bean 54 public RedisSentinelConfiguration redisSentinelConfiguration() { 55 String[] nodes = sentinelNodes.split(","); 56 Set<String> setNodes = new HashSet<>(); 57 for (String n : nodes) { 58 setNodes.add(n.trim()); 59 } 60 RedisSentinelConfiguration configuration = new RedisSentinelConfiguration(sentinelGroupName, setNodes); 61 return configuration; 62 } 63 64 @Bean 65 public JedisPoolConfig jedisPoolConfig() { 66 JedisPoolConfig poolConfig = new JedisPoolConfig(); 67 poolConfig.setMaxTotal(maxTotal); 68 poolConfig.setMinIdle(minIdle); 69 poolConfig.setMaxIdle(maxIdle); 70 poolConfig.setMaxWaitMillis(maxWaitMillis); 71 poolConfig.setTestOnBorrow(testOnBorrow); 72 poolConfig.setTestOnReturn(testOnReturn); 73 return poolConfig; 74 } 75 76 @Bean 77 public StringRedisSerializer stringRedisSerializer() { 78 return new StringRedisSerializer(); 79 } 80 81 @Bean(name = "redisTemplate00") 82 @Primary 83 public StringRedisTemplate redisTemplate00() { 84 return buildRedisTemplate(buildConnectionFactory(0)); 85 } 86 87 @Bean(name = "redisTemplate01") 88 public StringRedisTemplate redisTemplate01() { 89 return buildRedisTemplate(buildConnectionFactory(1)); 90 } 91 92 @Bean(name = "redisTemplate02") 93 public StringRedisTemplate redisTemplate02() { 94 return buildRedisTemplate(buildConnectionFactory(2)); 95 } 96 97 @Bean(name = "redisTemplate03") 98 public StringRedisTemplate redisTemplate03() { 99 return buildRedisTemplate(buildConnectionFactory(3)); 100 } 101 102 @Bean(name = "redisTemplate04") 103 public StringRedisTemplate redisTemplate04() { 104 return buildRedisTemplate(buildConnectionFactory(4)); 105 } 106 107 private JedisConnectionFactory buildConnectionFactory(int database) { 108 JedisConnectionFactory connectionFactory = new JedisConnectionFactory(redisSentinelConfiguration(), jedisPoolConfig()); 109 connectionFactory.setUsePool(true); 110 connectionFactory.setTimeout(timeout); 111 connectionFactory.setDatabase(database); 112 connectionFactory.setPassword(password); 113 connectionFactory.afterPropertiesSet(); 114 return connectionFactory; 115 } 116 117 protected StringRedisTemplate buildRedisTemplate(RedisConnectionFactory connectionFactory) { 118 StringRedisTemplate template = new StringRedisTemplate(); 119 template.setConnectionFactory(connectionFactory); 120 template.setValueSerializer(stringRedisSerializer()); 121 template.afterPropertiesSet(); 122 return template; 123 } 124 125 }
View Code

 

上面的示例,注入了5个redisTemplate实例,分别对应redis的0到4,共5个database。

 

二、配置类application.yml

复制代码
1
2
3
4
5
6
7
8
9
10
11
redis: sentinel: group: ${redis.sentinel.group} nodes: ${redis.sentinel.nodes} maxTotal: ${redis.maxTotal} minIdle: ${redis.minIdle} maxWaitMillis: ${redis.maxWait} testOnBorrow: ${redis.testOnBorrow} testOnReturn: ${redis.testOnReturn} password: timeout: ${redis.timeout}

  大家把里面的占位符,换成具体值就可以了。

最后

以上就是有魅力宝马最近收集整理的关于spring-boot 速成(12) - 如何注入多个redis StringRedisTemplate的全部内容,更多相关spring-boot内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部