1.首先是引入配置文件
1 2 |
|
2.application.yml的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
3.新增类RedisProperties
1 2 3 4 5 6 7 8 9 |
|
4.JedisCluster方式连接集群的配置
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 |
|
5.redis帮助
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 |
|
6.帮助类的使用方式
1 2 |
|
除了以上
也可以使用如下spring配置
使用jedis连接池
yml配置文件:
spring:
redis:
password: # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒)
cluster:
nodes:
- 192.168.1.236:7001
- 192.168.1.236:7002
- 192.168.1.236:7003
- 192.168.1.244:7004
- 192.168.1.244:7005
- 192.168.1.244:7006
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
//连接池注入配置信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
在使用的地方直接注入即可
1 2 |
|
需要引入
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.9.RELEASE</version>
</dependency>
直接使用spring data redis 中封装的方法进行操作
package com.web.test.controller;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestController {
@Resource(name="redisTemplate")
private RedisTemplate<String,String> redisTemplate;
@RequestMapping("/redisTest")
public String redisTest(){
return redisTemplate.opsForValue().get("username");
}
}