SpringBoot——redis(十九)

前言

Lettuce 与 Jedis 比较

Lettuce 和 Jedis 的都是连接 Redis Server的客户端。

Jedis 在实现上是直连 redis server,多线程环境下非线程安全,除非使用连接池,为每个 redis实例增加物理连接。

Lettuce 是 一种可伸缩,线程安全,完全非阻塞的Redis客户端,多个线程可以共享一个RedisConnection,它利用Netty NIO 框架来高效地管理多个连接,从而提供了异步和同步数据访问方式,用于构建非阻塞的反应性应用程序。

在2.x版本中,默认是使用lettuce;1.x版本的时候,默认使用的就是Jedis;

下面主介绍Lettuce集成Redis服务

依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
spring:	
redis:
cluster:
nodes:
- 192.168.43.32:7001
- 192.168.43.32:7002
- 192.168.43.32:7003
- 192.168.43.32:7004
- 192.168.43.32:7005
- 192.168.43.32:7006
max-redirects: 3
lettuce:
pool:
max-idle: 8
max-wait: -1
max-active: 8
min-idle: 0

自定义 RedisTemplate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.qn.config.redisConfig;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

@SpringBootConfiguration
public class LettuceRedisConfig {
@Bean
public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}

测试实体序列化类

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
package com.qn.eneity;

import java.io.Serializable;

public class UserEntity implements Serializable {

private static final long serialVersionUID = -41408631575559586L;
private Long id;
private String userName;
private String userSex;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}

@Override
public String toString() {
return "UserEntity{" +
"id=" + id +
", userName='" + userName + '\'' +
", userSex='" + userSex + '\'' +
'}';
}
}

Service

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
package com.qn.service;

import com.qn.eneity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class RedisService {
@Autowired
private RedisTemplate<String,String> strRedisTemplate;

@Autowired
private RedisTemplate<String, Serializable> serializableRedisTemplate;

public String getValue(String key,String value){
strRedisTemplate.opsForValue().set(key,value);
return strRedisTemplate.opsForValue().get(key);
}

public UserEntity testSerializable() {
UserEntity user=new UserEntity();
user.setId(1L);
user.setUserName("朝雾轻寒");
user.setUserSex("男");
serializableRedisTemplate.opsForValue().set("user", user);
return (UserEntity) serializableRedisTemplate.opsForValue().get("user");
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.qn.service;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
class RedisServiceTest {
@Autowired
RedisService redisService;

@Test
void getValue() {
System.out.println("获取数据: "+redisService.getValue("sex","nan"));
}

@Test
void testSerializable(){
System.out.println(redisService.testSerializable().toString());
}
}

测试

测试跑测试类会报错:ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig

此时需要引入依赖

1
2
3
4
5
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>

参考文档

sprngboot集成redis集群

文章目录
  1. 1. 前言
  2. 2. Lettuce 与 Jedis 比较
  3. 3. 依赖
  4. 4. 配置
  5. 5. 自定义 RedisTemplate
  6. 6. 测试实体序列化类
  7. 7. Service
  8. 8. 测试
  9. 9. 测试
  10. 10. 参考文档
|