Redis的集成与应用
# Redis的集成与应用
# 一、Jedis集成Redis
# 1、引入相关依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 2、代码测试
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7380));
JedisCluster jedis = new JedisCluster(jedisClusterNodes);
jedis.sadd("planets", "Mars");
1
2
3
4
5
6
2
3
4
5
6
# 二、Springboot集成Redis
# 1、引入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2、Springboot的application.yml
增加配置
server:
port: 8080
spring:
redis:
database: 0
timeout: 3000
password: foobared
cluster:
nodes: 10.66.50.127:7001,10.66.50.127:7002,10.66.50.131:7001,10.66.50.131:7002,10.66.50.132:7001,10.66.50.132:7002
lettuce:
pool:
max-idle: 50
min-idle: 10
max-active: 100
max-wait: 1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 3、代码测试
@RestController
public class IndexController {
private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/test")
public void test() throws InterruptedException {
stringRedisTemplate.opsForValue().set("100", "1");
System.out.println(stringRedisTemplate.opsForValue().get("100"));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 4、在jedis中使用pipeline示例
Redis Pipeline是一种可以在一次网络往返中向Redis服务器发送多个命令的技术,可以显著提高Redis客户端的性能。
在Java中,可以使用Jedis库来与Redis进行交互,并使用它的Pipeline类来实现Redis Pipeline功能。下面是一个使用Jedis库实现Redis Pipeline的示例代码:
Jedis jedis = new Jedis("localhost", 6379);
// 开始Pipeline
Pipeline pipeline = jedis.pipelined();
// 向Pipeline中添加多个命令
pipeline.set("key1", "value1");
pipeline.set("key2", "value2");
pipeline.set("key3", "value3");
pipeline.incr("counter");
// 执行Pipeline中的所有命令,并获取结果
List<Object> results = pipeline.syncAndReturnAll();
// 输出执行结果
for (Object result : results) {
System.out.println(result);
}
// 关闭Jedis连接
jedis.close();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 三、Redisson集成Redis
参考 Spring Boot Starter (opens new window) 集成
# 1、引入相关依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.17.4</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 2、Springboot的application.yml
增加配置
使用Springboot的配置
server:
port: 8080
spring:
redis:
database: 0
timeout: 3000
password: foobared
cluster:
nodes: 10.66.50.127:7001,10.66.50.127:7002,10.66.50.131:7001,10.66.50.131:7002,10.66.50.132:7001,10.66.50.132:7002
lettuce:
pool:
max-idle: 50
min-idle: 10
max-active: 100
max-wait: 1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
使用redisson的配置
spring:
redis:
redisson:
file: classpath:redisson.yaml
config: |
clusterServersConfig:
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
failedSlaveReconnectionInterval: 3000
failedSlaveCheckInterval: 60000
password: null
subscriptionsPerConnection: 5
clientName: null
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
slaveConnectionMinimumIdleSize: 24
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 24
masterConnectionPoolSize: 64
readMode: "SLAVE"
subscriptionMode: "SLAVE"
nodeAddresses:
- "redis://127.0.0.1:7004"
- "redis://127.0.0.1:7001"
- "redis://127.0.0.1:7000"
scanInterval: 1000
pingConnectionInterval: 0
keepAlive: false
tcpNoDelay: false
threads: 16
nettyThreads: 32
codec: !<org.redisson.codec.MarshallingCodec> {}
transportMode: "NIO"
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
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
# 3、代码测试
@RestController
public class RedissonController {
private static final Logger log = LoggerFactory.getLogger(IndexController.class);
@Autowired
private RedissonClient redissonClient;
@RequestMapping("/test_redisson")
public void testRedisson() throws InterruptedException {
RLock lock = redissonClient.getLock("myLock");
String threadName = Thread.currentThread().getName();
try {
lock.lock();
//TODO 处理业务逻辑
log.info("{}开始加锁", threadName);
Thread.sleep(10000);
} catch (Exception e) {
throw e;
} finally {
log.info("{}开始释放锁", threadName);
lock.unlock();
}
}
}
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
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
4、测试结果
同时请求接口两次,输出如下
c.r.redis.controller.IndexController : http-nio-8080-exec-1开始加锁
c.r.redis.controller.IndexController : http-nio-8080-exec-1开始释放锁
c.r.redis.controller.IndexController : http-nio-8080-exec-3开始加锁
c.r.redis.controller.IndexController : http-nio-8080-exec-3开始释放锁
1
2
3
4
2
3
4