0%

SpringBoot整合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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>redis-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-springboot</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.7</version>
</dependency>

<!-- redis序列化用的 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
<scope>compile</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

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
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
* Standard Redis configuration.
*/
@Configuration
public class RedisConfiguration {

/**
* 这样配置,功能相当于
* private StringRedisTemplate stringRedisTemplate;
*/
// @Bean
// public RedisTemplate<Object, Object> redisTemplateString(RedisConnectionFactory redisConnectionFactory) {
// RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
// redisTemplate.setConnectionFactory(redisConnectionFactory);
//
// // 设置value的序列化规则和 key的序列化规则
// redisTemplate.setKeySerializer(new StringRedisSerializer());
// redisTemplate.setValueSerializer(new StringRedisSerializer());
// redisTemplate.afterPropertiesSet();
// return redisTemplate;
// }

/**
* 在界面工具上看,这个会序列化两次,会多了反斜杠
*/
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);

// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

// 设置value的序列化规则和 key的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}

}

application.properties

1
2
3
4
5
# 单机
#spring.redis.host=192.168.0.5

# 集群
spring.redis.cluster.nodes=192.168.0.6:6379,192.168.0.7:6379,192.168.0.8:6379,192.168.0.8:6379,192.168.0.10:6379,192.168.0.11:6379

User.java

1
2
3
4
5
6
7
@Data
public class User implements Serializable {

private String name;

private Integer age;
}

Test.java

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

import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

/**
* 简单测试使用springboot集成redis设置值
* 1.如果只是用字符串类型,可以直接使用StringRedisTemplate,这个是框架已经定义好的了
* 2.如果使用RedisTemplate,需要自定义Config
*/
@SpringBootTest
class SimpleTests {

@Resource
private RedisTemplate<Object,Object> redisTemplate;

@Resource
private StringRedisTemplate stringRedisTemplate;

/**
* value,直接设置一个对象
*/
@Test
void setObj() {
User user = new User();
user.setAge(11);
user.setName("anthony");
redisTemplate.opsForValue().set("setObj",user);
}

/**
* value,对象转成字符串
*/
@Test
void setObjStr() {
User user = new User();
user.setAge(11);
user.setName("anthony");
redisTemplate.opsForValue().set("setObjStr", JSONUtil.parse(user).toString());
}

/**
* value,对象转成字符串
*/
@Test
void setObjStringredistemplate() {
User user = new User();
user.setAge(11);
user.setName("anthony");
stringRedisTemplate.opsForValue().set("setObjStringredistemplate", JSONUtil.parse(user).toString());
}
}

运行三个测试方法,会有三种测试结果

第一种:序列化的时候,会带上实体类的包路径,如果再get()的时候,在微服务可能会报错

第二种:序列化的时候,会序列化两次,因为redis配置文件redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);这里又序列化了一次,所有会看到反斜杠,很不舒服

第三种:才是我想要的结果,只需要序列化一次,而且json可以被redis界面工具识别

image-20210109160230915

image-20210109160304331

image-20210109160322272

StringRedisTemplate

说下StringRedisTemplate这个类,只能对key=String,value=String的键值对进行操作,RedisTemplate可以对任何类型的key-value键值对操作。

如果在项目中通常都是字符串操作,通常就这个类就已经够用了