springboot-redis相关配置整理

1.pom.xml引入对应数据文件

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
2.yml配置redis连接信息

redis:
database: 0
port: 7000
jedis:
pool:
max-idle: 20
min-idle: 2
max-active: 50
max-wait: 3000
host: 192.168.1.140
timeout: 5000
1
2
3
4
5
6
7
8
9
10
11
3.RedisConfig配置

RedisConfig
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.CachingConfigurerSupport;
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.StringRedisSerializer;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {
// @Bean
// public RedisTemplate<Object,SendParameter> empRedisTemplate(RedisConnectionFactory redisConnectionFactory)
// throws UnknownHostException{
// RedisTemplate<Object,SendParameter> template = new RedisTemplate<Object, SendParameter>();
// template.setConnectionFactory(redisConnectionFactory);
// Jackson2JsonRedisSerializer<SendParameter> ser = new Jackson2JsonRedisSerializer<SendParameter>(SendParameter.class);
// template.setDefaultSerializer(ser);
// return template;
// }

@Bean
public RedisTemplate<String, List<Map<String, Object>>> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

RedisTemplate<String, List<Map<String, Object>>> template = new RedisTemplate<String, List<Map<String, Object>>>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
return template;
}
}
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
RedisObjectSerializer
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

public class RedisObjectSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter(http://www.my516.com);
static final byte[] EMPTY_ARRAY = new byte[0];

@Override
public Object deserialize(byte[] bytes) {
if (isEmpty(bytes)) {
return null;
}
try {
return deserializer.convert(bytes);
} catch (Exception ex) {
throw new SerializationException("Cannot deserialize", ex);
}
}

@Override
public byte[] serialize(Object object) {
if (object == null) {
return EMPTY_ARRAY;
}
try {
return serializer.convert(object);
} catch (Exception ex) {
return EMPTY_ARRAY;
}
}

private boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
}

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

---------------------

posted on 2019-07-11 00:53  激流勇进1  阅读(266)  评论(0编辑  收藏  举报