专业的JAVA编程教程与资源

网站首页 > java教程 正文

Windows环境下springboot集成redis的安装与使用

temp10 2024-10-20 14:34:43 java教程 7 ℃ 0 评论

转载:https://www.cnblogs.com/JJJ1990/p/10438292.html

一,redis安装

首先我们需要下载Windows版本的redis压缩包地址如下:

Windows环境下springboot集成redis的安装与使用

https://github.com/MicrosoftArchive/redis/releases

连接打开后如下图所示

我们选择64位的压缩包,下载后需要解压,我们解压至D盘,如下图所示:

接下来我们需要执行一些安装命令

1,在如上图的目录中,直接键入“cmd“

2,在打开的cmd命令窗口中输入 “redis-server.exe redis.windows.conf” 用于启动redis服务

(注意采用这个命令相当于启动一个临时服务,如果当前窗口被关闭,则服务也会被关闭)

3,我们再打开一个同样的cmd命令窗口,在其中键入 “redis-cli” ,这个命令的作用是启动一个redis客户端。客户端默认的端口号是6379 如果我们要修改端口号,需要打开我们前面执行的命令中的conf文件redis.windows.conf 找到port 修改其后的端口号

4,在启动的客户端中检查redis服务是否正常,我们可以进行数据的设置和读取等操作,比如我们可以执行一个命令 “set redis jj”,然后我们再执行一个命令“get redis” 就可以将前面设置的123 查询出来 ,如下图所示

5,将redis服务注册到我们的Windows,我们继续新打开一个cmd窗口,执行命令 “redis-server --service-install redis.windows.conf” 这个时候呢,提示:Redis successfully installed as a service. 表示加入服务成功!

6,启动注册到Windows的redis服务。键入命令“redis-server.exe --service-start” 可能会报错,如下图。原因是我们已经在这个端口绑定了一个服务,就是我们前面启动的那个所谓的临时服务,这个时候我们需要关闭刚才启动的临时服务的窗口。

7,再启动如果还是报上面的错误,那可能需要我们调整注册到Windows的服务为本地系统服务,而不是网络服务,如下图所示。除此之外可能还会有防火墙等问题导致redis服务启动失败。

8,如下图所示,服务启动ok,这个时候我们在继续操作redis客户端是没有问题的。

二,springboot 集成redis

1,新建springboot工程,如果有不清楚如何新建的,请移步 https://www.cnblogs.com/JJJ1990/p/8384386.html

2,在pom文件中加入redis jar包的引用

我的整个工程的pom文件如下,注意第34-38行,即为引入的redis jar包

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3 <modelVersion>4.0.0</modelVersion>
 4 
 5 <groupId>com</groupId>
 6 <artifactId>redis</artifactId>
 7 <version>0.0.1-SNAPSHOT</version>
 8 <packaging>jar</packaging>
 9 
10 <name>redis</name>
11 <url>http://maven.apache.org</url>
12 
13 <properties>
14 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15 </properties>
16 
17 <parent>
18 <groupId>org.springframework.boot</groupId>
19 <artifactId>spring-boot-starter-parent</artifactId>
20 <version>1.5.8.RELEASE</version>
21 </parent>
22 
23 <dependencies>
24 <dependency>
25 <groupId>junit</groupId>
26 <artifactId>junit</artifactId>
27 <version>3.8.1</version>
28 <scope>test</scope>
29 </dependency>
30 <dependency>
31 <groupId>org.springframework.boot</groupId>
32 <artifactId>spring-boot-starter-web</artifactId>
33 </dependency>
34 <dependency>
35 <groupId>org.springframework.boot</groupId>
36 <artifactId>spring-boot-starter-data-redis</artifactId>
37 </dependency> 
38 </dependencies>
39 </project>

3,编写RedisConfig 类代码

 1 package com.redis;
 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.PropertySource;
 7 import org.springframework.data.redis.connection.RedisConnectionFactory;
 8 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
 9 import org.springframework.data.redis.core.RedisTemplate;
 10 import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
 11 import org.springframework.data.redis.serializer.StringRedisSerializer;
 12 
 13 import redis.clients.jedis.JedisPoolConfig;
 14 
 15 @Configuration
 16 @PropertySource("classpath:config/redis.properties")
 17 public class RedisConfig {
 18 
 19 @Value("${redis.maxIdle}")
 20 private Integer maxIdle;
 21 
 22 @Value("${redis.maxTotal}")
 23 private Integer maxTotal;
 24 
 25 @Value("${redis.maxWaitMillis}")
 26 private Integer maxWaitMillis;
 27 
 28 @Value("${redis.minEvictableIdleTimeMillis}")
 29 private Integer minEvictableIdleTimeMillis;
 30 
 31 @Value("${redis.numTestsPerEvictionRun}")
 32 private Integer numTestsPerEvictionRun;
 33 
 34 @Value("${redis.timeBetweenEvictionRunsMillis}")
 35 private long timeBetweenEvictionRunsMillis;
 36 
 37 @Value("${redis.testOnBorrow}")
 38 private boolean testOnBorrow;
 39 
 40 @Value("${redis.testWhileIdle}")
 41 private boolean testWhileIdle;
 42 
 43 
 44 @Value("${spring.redis.cluster.nodes}")
 45 private String clusterNodes; 
 46 
 47 @Value("${spring.redis.cluster.max-redirects}")
 48 private Integer mmaxRedirectsac;
 49 
 50 /**
 51 * JedisPoolConfig 连接池
 52 * @return
 53 */
 54 @Bean
 55 public JedisPoolConfig jedisPoolConfig() {
 56 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
 57 // 最大空闲数
 58 jedisPoolConfig.setMaxIdle(maxIdle);
 59 // 连接池的最大数据库连接数
 60 jedisPoolConfig.setMaxTotal(maxTotal);
 61 // 最大建立连接等待时间
 62 jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
 63 // 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
 64 jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
 65 // 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
 66 jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
 67 // 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
 68 jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
 69 // 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
 70 jedisPoolConfig.setTestOnBorrow(testOnBorrow);
 71 // 在空闲时检查有效性, 默认false
 72 jedisPoolConfig.setTestWhileIdle(testWhileIdle);
 73 System.out.println("redis 连接池配置完成!");
 74 return jedisPoolConfig;
 75 }
 76 /**
 77 * 单机版配置
 78 * @throws
 79 */
 80 @Bean
 81 public JedisConnectionFactory JedisConnectionFactory(JedisPoolConfig jedisPoolConfig){
 82 JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
 83 //连接池 
 84 JedisConnectionFactory.setPoolConfig(jedisPoolConfig); 
 85 //IP地址 
 86 JedisConnectionFactory.setHostName("127.0.0.1"); 
 87 //端口号 
 88 JedisConnectionFactory.setPort(6379); 
 89 //如果Redis设置有密码 
 90 //JedisConnectionFactory.setPassword(password); 
 91 //客户端超时时间单位是毫秒 
 92 JedisConnectionFactory.setTimeout(5000); 
 93 System.out.println("jedis 连接工厂配置完成!");
 94 return JedisConnectionFactory; 
 95 }
 96 
 97 /**
 98 * 实例化 RedisTemplate 对象
 99 * @return
100 */
101 @Bean
102 public RedisTemplate<String, Object> functionDomainRedisTemplate(JedisConnectionFactory redisConnectionFactory) {
103 RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
104 initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
105 System.out.println("functionDomainRedisTemplates 配置完成!");
106 return redisTemplate;
107 }
108 /**
109 * 设置数据存入 redis 的序列化方式,并开启事务
110 * @param redisTemplate
111 * @param factory
112 */
113 private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
114 //如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String! 
115 redisTemplate.setKeySerializer(new StringRedisSerializer());
116 redisTemplate.setHashKeySerializer(new StringRedisSerializer());
117 redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
118 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
119 // 开启事务
120 redisTemplate.setEnableTransactionSupport(true);
121 redisTemplate.setConnectionFactory(factory);
122 }
123 /**
124 * 注入封装RedisTemplate
125 * @Title: redisUtil 
126 * @return RedisUtil
127 * @throws
128 */
129 @Bean(name = "redisUtil")
130 public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate) {
131 RedisUtil redisUtil = new RedisUtil();
132 redisUtil.setRedisTemplate(redisTemplate);
133 System.out.println("redisUtil 配置完成!");
134 return redisUtil;
135 }
136 }

4,编写RedisUtil类,用于操作redis数据库

 1 package com.redis;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.Set;
 6 import java.util.concurrent.TimeUnit;
 7 
 8 import org.springframework.data.redis.core.RedisTemplate;
 9 import org.springframework.util.CollectionUtils;
 10 
 11 public class RedisUtil {
 12 
 13 private RedisTemplate<String, Object> redisTemplate; 
 14 
 15 public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) { 
 16 this.redisTemplate = redisTemplate; 
 17 } 
 18 
 19 /** 
 20 * 指定缓存失效时间 
 21 * @param key 键 
 22 * @param time 时间(秒) 
 23 * @return 
 24 */ 
 25 public boolean expire(String key,long time){ 
 26 try { 
 27 if(time>0){ 
 28 redisTemplate.expire(key, time, TimeUnit.SECONDS); 
 29 } 
 30 return true; 
 31 } catch (Exception e) { 
 32 e.printStackTrace(); 
 33 return false; 
 34 } 
 35 } 
 36 
 37 /** 
 38 * 根据key 获取过期时间 
 39 * @param key 键 不能为null 
 40 * @return 时间(秒) 返回0代表为永久有效 
 41 */ 
 42 public long getExpire(String key){ 
 43 return redisTemplate.getExpire(key,TimeUnit.SECONDS); 
 44 } 
 45 
 46 /** 
 47 * 判断key是否存在 
 48 * @param key 键 
 49 * @return true 存在 false不存在 
 50 */ 
 51 public boolean hasKey(String key){ 
 52 try { 
 53 return redisTemplate.hasKey(key); 
 54 } catch (Exception e) { 
 55 e.printStackTrace(); 
 56 return false; 
 57 } 
 58 } 
 59 
 60 /** 
 61 * 删除缓存 
 62 * @param key 可以传一个值 或多个 
 63 */ 
 64 @SuppressWarnings("unchecked") 
 65 public void del(String ... key){ 
 66 if(key!=null&&key.length>0){ 
 67 if(key.length==1){ 
 68 redisTemplate.delete(key[0]); 
 69 }else{ 
 70 redisTemplate.delete(CollectionUtils.arrayToList(key)); 
 71 } 
 72 } 
 73 } 
 74 
 75 //============================String============================= 
 76 /** 
 77 * 普通缓存获取 
 78 * @param key 键 
 79 * @return 值 
 80 */ 
 81 public Object get(String key){ 
 82 return key==null?null:redisTemplate.opsForValue().get(key); 
 83 } 
 84 
 85 /** 
 86 * 普通缓存放入 
 87 * @param key 键 
 88 * @param value 值 
 89 * @return true成功 false失败 
 90 */ 
 91 public boolean set(String key,Object value) { 
 92 try { 
 93 redisTemplate.opsForValue().set(key, value); 
 94 return true; 
 95 } catch (Exception e) { 
 96 e.printStackTrace(); 
 97 return false; 
 98 } 
 99 
100 } 
101 
102 /** 
103 * 普通缓存放入并设置时间 
104 * @param key 键 
105 * @param value 值 
106 * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 
107 * @return true成功 false 失败 
108 */ 
109 public boolean set(String key,Object value,long time){ 
110 try { 
111 if(time>0){ 
112 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); 
113 }else{ 
114 set(key, value); 
115 } 
116 return true; 
117 } catch (Exception e) { 
118 e.printStackTrace(); 
119 return false; 
120 } 
121 } 
122 
123 /** 
124 * 递增 
125 * @param key 键 
126 * @param by 要增加几(大于0) 
127 * @return 
128 */ 
129 public long incr(String key, long delta){ 
130 if(delta<0){ 
131 throw new RuntimeException("递增因子必须大于0"); 
132 } 
133 return redisTemplate.opsForValue().increment(key, delta); 
134 } 
135 
136 /** 
137 * 递减 
138 * @param key 键 
139 * @param by 要减少几(小于0) 
140 * @return 
141 */ 
142 public long decr(String key, long delta){ 
143 if(delta<0){ 
144 throw new RuntimeException("递减因子必须大于0"); 
145 } 
146 return redisTemplate.opsForValue().increment(key, -delta); 
147 } 
148 
149 //================================Map================================= 
150 /** 
151 * HashGet 
152 * @param key 键 不能为null 
153 * @param item 项 不能为null 
154 * @return 值 
155 */ 
156 public Object hget(String key,String item){ 
157 return redisTemplate.opsForHash().get(key, item); 
158 } 
159 
160 /** 
161 * 获取hashKey对应的所有键值 
162 * @param key 键 
163 * @return 对应的多个键值 
164 */ 
165 public Map<Object,Object> hmget(String key){ 
166 return redisTemplate.opsForHash().entries(key); 
167 } 
168 
169 /** 
170 * HashSet 
171 * @param key 键 
172 * @param map 对应多个键值 
173 * @return true 成功 false 失败 
174 */ 
175 public boolean hmset(String key, Map<String,Object> map){ 
176 try { 
177 redisTemplate.opsForHash().putAll(key, map); 
178 return true; 
179 } catch (Exception e) { 
180 e.printStackTrace(); 
181 return false; 
182 } 
183 } 
184 
185 /** 
186 * HashSet 并设置时间 
187 * @param key 键 
188 * @param map 对应多个键值 
189 * @param time 时间(秒) 
190 * @return true成功 false失败 
191 */ 
192 public boolean hmset(String key, Map<String,Object> map, long time){ 
193 try { 
194 redisTemplate.opsForHash().putAll(key, map); 
195 if(time>0){ 
196 expire(key, time); 
197 } 
198 return true; 
199 } catch (Exception e) { 
200 e.printStackTrace(); 
201 return false; 
202 } 
203 } 
204 
205 /** 
206 * 向一张hash表中放入数据,如果不存在将创建 
207 * @param key 键 
208 * @param item 项 
209 * @param value 值 
210 * @return true 成功 false失败 
211 */ 
212 public boolean hset(String key,String item,Object value) { 
213 try { 
214 redisTemplate.opsForHash().put(key, item, value); 
215 return true; 
216 } catch (Exception e) { 
217 e.printStackTrace(); 
218 return false; 
219 } 
220 } 
221 
222 /** 
223 * 向一张hash表中放入数据,如果不存在将创建 
224 * @param key 键 
225 * @param item 项 
226 * @param value 值 
227 * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间 
228 * @return true 成功 false失败 
229 */ 
230 public boolean hset(String key,String item,Object value,long time) { 
231 try { 
232 redisTemplate.opsForHash().put(key, item, value); 
233 if(time>0){ 
234 expire(key, time); 
235 } 
236 return true; 
237 } catch (Exception e) { 
238 e.printStackTrace(); 
239 return false; 
240 } 
241 } 
242 
243 /** 
244 * 删除hash表中的值 
245 * @param key 键 不能为null 
246 * @param item 项 可以使多个 不能为null 
247 */ 
248 public void hdel(String key, Object... item){ 
249 redisTemplate.opsForHash().delete(key,item); 
250 } 
251 
252 /** 
253 * 判断hash表中是否有该项的值 
254 * @param key 键 不能为null 
255 * @param item 项 不能为null 
256 * @return true 存在 false不存在 
257 */ 
258 public boolean hHasKey(String key, String item){ 
259 return redisTemplate.opsForHash().hasKey(key, item); 
260 } 
261 
262 /** 
263 * hash递增 如果不存在,就会创建一个 并把新增后的值返回 
264 * @param key 键 
265 * @param item 项 
266 * @param by 要增加几(大于0) 
267 * @return 
268 */ 
269 public double hincr(String key, String item,double by){ 
270 return redisTemplate.opsForHash().increment(key, item, by); 
271 } 
272 
273 /** 
274 * hash递减 
275 * @param key 键 
276 * @param item 项 
277 * @param by 要减少记(小于0) 
278 * @return 
279 */ 
280 public double hdecr(String key, String item,double by){ 
281 return redisTemplate.opsForHash().increment(key, item,-by); 
282 } 
283 
284 //============================set============================= 
285 /** 
286 * 根据key获取Set中的所有值 
287 * @param key 键 
288 * @return 
289 */ 
290 public Set<Object> sGet(String key){ 
291 try { 
292 return redisTemplate.opsForSet().members(key); 
293 } catch (Exception e) { 
294 e.printStackTrace(); 
295 return null; 
296 } 
297 } 
298 
299 /** 
300 * 根据value从一个set中查询,是否存在 
301 * @param key 键 
302 * @param value 值 
303 * @return true 存在 false不存在 
304 */ 
305 public boolean sHasKey(String key,Object value){ 
306 try { 
307 return redisTemplate.opsForSet().isMember(key, value); 
308 } catch (Exception e) { 
309 e.printStackTrace(); 
310 return false; 
311 } 
312 } 
313 
314 /** 
315 * 将数据放入set缓存 
316 * @param key 键 
317 * @param values 值 可以是多个 
318 * @return 成功个数 
319 */ 
320 public long sSet(String key, Object...values) { 
321 try { 
322 return redisTemplate.opsForSet().add(key, values); 
323 } catch (Exception e) { 
324 e.printStackTrace(); 
325 return 0; 
326 } 
327 } 
328 
329 /** 
330 * 将set数据放入缓存 
331 * @param key 键 
332 * @param time 时间(秒) 
333 * @param values 值 可以是多个 
334 * @return 成功个数 
335 */ 
336 public long sSetAndTime(String key,long time,Object...values) { 
337 try { 
338 Long count = redisTemplate.opsForSet().add(key, values); 
339 if(time>0) expire(key, time); 
340 return count; 
341 } catch (Exception e) { 
342 e.printStackTrace(); 
343 return 0; 
344 } 
345 } 
346 
347 /** 
348 * 获取set缓存的长度 
349 * @param key 键 
350 * @return 
351 */ 
352 public long sGetSetSize(String key){ 
353 try { 
354 return redisTemplate.opsForSet().size(key); 
355 } catch (Exception e) { 
356 e.printStackTrace(); 
357 return 0; 
358 } 
359 } 
360 
361 /** 
362 * 移除值为value的 
363 * @param key 键 
364 * @param values 值 可以是多个 
365 * @return 移除的个数 
366 */ 
367 public long setRemove(String key, Object ...values) { 
368 try { 
369 Long count = redisTemplate.opsForSet().remove(key, values); 
370 return count; 
371 } catch (Exception e) { 
372 e.printStackTrace(); 
373 return 0; 
374 } 
375 } 
376 //===============================list================================= 
377 
378 /** 
379 * 获取list缓存的内容 
380 * @param key 键 
381 * @param start 开始 
382 * @param end 结束 0 到 -1代表所有值 
383 * @return 
384 */ 
385 public List<Object> lGet(String key,long start, long end){ 
386 try { 
387 return redisTemplate.opsForList().range(key, start, end); 
388 } catch (Exception e) { 
389 e.printStackTrace(); 
390 return null; 
391 } 
392 } 
393 
394 /** 
395 * 获取list缓存的长度 
396 * @param key 键 
397 * @return 
398 */ 
399 public long lGetListSize(String key){ 
400 try { 
401 return redisTemplate.opsForList().size(key); 
402 } catch (Exception e) { 
403 e.printStackTrace(); 
404 return 0; 
405 } 
406 } 
407 
408 /** 
409 * 通过索引 获取list中的值 
410 * @param key 键 
411 * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推 
412 * @return 
413 */ 
414 public Object lGetIndex(String key,long index){ 
415 try { 
416 return redisTemplate.opsForList().index(key, index); 
417 } catch (Exception e) { 
418 e.printStackTrace(); 
419 return null; 
420 } 
421 } 
422 
423 /** 
424 * 将list放入缓存 
425 * @param key 键 
426 * @param value 值 
427 * @param time 时间(秒) 
428 * @return 
429 */ 
430 public boolean lSet(String key, Object value) { 
431 try { 
432 redisTemplate.opsForList().rightPush(key, value); 
433 return true; 
434 } catch (Exception e) { 
435 e.printStackTrace(); 
436 return false; 
437 } 
438 } 
439 
440 /** 
441 * 将list放入缓存 
442 * @param key 键 
443 * @param value 值 
444 * @param time 时间(秒) 
445 * @return 
446 */ 
447 public boolean lSet(String key, Object value, long time) { 
448 try { 
449 redisTemplate.opsForList().rightPush(key, value); 
450 if (time > 0) expire(key, time); 
451 return true; 
452 } catch (Exception e) { 
453 e.printStackTrace(); 
454 return false; 
455 } 
456 } 
457 
458 /** 
459 * 将list放入缓存 
460 * @param key 键 
461 * @param value 值 
462 * @param time 时间(秒) 
463 * @return 
464 */ 
465 public boolean lSet(String key, List<Object> value) { 
466 try { 
467 redisTemplate.opsForList().rightPushAll(key, value); 
468 return true; 
469 } catch (Exception e) { 
470 e.printStackTrace(); 
471 return false; 
472 } 
473 } 
474 
475 /** 
476 * 将list放入缓存 
477 * @param key 键 
478 * @param value 值 
479 * @param time 时间(秒) 
480 * @return 
481 */ 
482 public boolean lSet(String key, List<Object> value, long time) { 
483 try { 
484 redisTemplate.opsForList().rightPushAll(key, value); 
485 if (time > 0) expire(key, time); 
486 return true; 
487 } catch (Exception e) { 
488 e.printStackTrace(); 
489 return false; 
490 } 
491 } 
492 
493 /** 
494 * 根据索引修改list中的某条数据 
495 * @param key 键 
496 * @param index 索引 
497 * @param value 值 
498 * @return 
499 */ 
500 public boolean lUpdateIndex(String key, long index,Object value) { 
501 try { 
502 redisTemplate.opsForList().set(key, index, value); 
503 return true; 
504 } catch (Exception e) { 
505 e.printStackTrace(); 
506 return false; 
507 } 
508 } 
509 
510 /** 
511 * 移除N个值为value 
512 * @param key 键 
513 * @param count 移除多少个 
514 * @param value 值 
515 * @return 移除的个数 
516 */ 
517 public long lRemove(String key,long count,Object value) { 
518 try { 
519 Long remove = redisTemplate.opsForList().remove(key, count, value); 
520 return remove; 
521 } catch (Exception e) { 
522 e.printStackTrace(); 
523 return 0; 
524 } 
525 } 
526 }

5,新建 redis.properties 文件

注意文件位置,在我们新建RedisConfig 的时候有指定 为config/redis.properties ,所以我们的redis.properties 文件位置如下图所示

6,编写redis.properties 文件内容

 1 #Matser的ip地址 
 2 redis.hostName=172.0.0.1
 3 #端口号 
 4 redis.port=6379
 5 #如果有密码 
 6 redis.password=
 7 #客户端超时时间单位是毫秒 默认是2000 
 8 redis.timeout=10000 
 9 
10 #最大空闲数 
11 redis.maxIdle=300 
12 #连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal 
13 #redis.maxActive=600 
14 #控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性 
15 redis.maxTotal=1000 
16 #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 
17 redis.maxWaitMillis=1000 
18 #连接的最小空闲时间 默认1800000毫秒(30分钟) 
19 redis.minEvictableIdleTimeMillis=300000 
20 #每次释放连接的最大数目,默认3 
21 redis.numTestsPerEvictionRun=1024 
22 #逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1 
23 redis.timeBetweenEvictionRunsMillis=30000 
24 #是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 
25 redis.testOnBorrow=true 
26 #在空闲时检查有效性, 默认false 
27 redis.testWhileIdle=true 
28 
29 #redis集群配置 
30 spring.redis.cluster.nodes=192.168.177.128:7001,192.168.177.128:7002,192.168.177.128:7003,192.168.177.128:7004,192.168.177.128:7005,192.168.177.128:7006
31 spring.redis.cluster.max-redirects=3

7,编写测试controller

 1 package com.redis;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 
10 @Controller
11 public class Firstblood {
12 
13 @Autowired
14 RedisUtil redisUtil;
15 
16 @RequestMapping(value="/FristBlood/{name}",method= RequestMethod.GET)
17 @ResponseBody
18 public String hello(@PathVariable("name") String name) {
19 return "查询结果:" + redisUtil.get(name) ;
20 }
21 }

8,测试验证

打开浏览器,输入 “http://localhost:8080/FristBlood/redis” 这个url中的redis就等于是我们本次请求要查询的key ,

但是这块有一个问题是,我们使用了@ResponseBody 注解,但是我们在redis数据库中,之前存入的时候,我们执行了一个命令"set redis jj" ,

虽然我们认为存入的是一个字符串但是在我们代码中执行获取这个key的value的时候不会这么认为,会导致解析出错。

这个时候我们从新set一下,如下图

然后在浏览器中我们继续输入上面的url,返回的结果页面如下,则说明整个工程ok

如果输入的key不存在 则返回null,如下图所示:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表