RedisConfig.java
9.27 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.huaheng.framework.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaheng.framework.redis.serializer.SerializeUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
import java.time.Duration;
@Configuration
@EnableCaching // 开启缓存支持
public class RedisConfig extends CachingConfigurerSupport
{
@Value("${redis.database}")
private int database;
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private int port;
@Value("${redis.password}")
private String password;
@Value("${redis.ssl}")
private Boolean ssl;
@Value("${redis.lettuce.pool.max-idle}")
private int maxIdle;
@Value("${redis.lettuce.pool.min-idle}")
private int minIdle;
@Value("${redis.lettuce.pool.max-total}")
private int maxTotal;
@Value("${redis.lettuce.pool.max-waitMillis}")
private long maxWaitMillis;
@Value("${redis.timeout}")
private long timeout;
private Duration timeToLive = Duration.ofSeconds(600);
/**
* 在没有指定缓存Key的情况下,key生成策略
*/
@Bean
public KeyGenerator keyGenerator()
{
return new KeyGenerator()
{
@Override
public Object generate(Object target, Method method, Object... params)
{
StringBuffer sb = new StringBuffer();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params)
{
sb.append(obj.toString());
}
return sb.toString();
}
};
}
// 缓存管理器 使用Lettuce,和jedis有很大不同LettuceConnectionFactory lettuceConnectionFactory
@Bean
public CacheManager cacheManager()
{
// 关键点,spring cache的注解使用的序列化都从这来,没有这个配置的话使用的jdk自己的序列化,实际上不影响使用,只是打印出来不适合人眼识别
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))// key序列化方式
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(getValueSerializer()))// value序列化方式
.disableCachingNullValues().entryTtl(timeToLive).disableCachingNullValues();
;// 缓存过期时间
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(lettuceConnectionFactory())// 默认有锁 等待锁时间为0
.cacheDefaults(redisCacheConfiguration).transactionAware();
return builder.build();
}
/**
* RedisTemplate配置 使用自定义redisTemplate的时候 重新定义序列化方式 LettuceConnectionFactory lettuceConnectionFactory
*/
@Bean
public RedisTemplate<String, Object> redisTemplate()
{
// 配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory());
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);// key序列化
redisTemplate.setValueSerializer(getValueSerializer());// value序列化new LZ4Serializer(getValueSerializer())
redisTemplate.setHashKeySerializer(stringSerializer);// Hash key序列化
redisTemplate.setHashValueSerializer(getValueSerializer());// Hash value序列化
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/**
* shiroRedisTemplate配置 使用自定义shiroRedisTemplate的时候 重新定义序列化方式 LettuceConnectionFactory lettuceConnectionFactory
*/
@Bean
public RedisTemplate<String, Object> shiroRedisTemplate()
{
// 配置redisTemplate
RedisTemplate<String, Object> shiroRedisTemplate = new RedisTemplate<String, Object>();
shiroRedisTemplate.setConnectionFactory(lettuceConnectionFactory());
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
shiroRedisTemplate.setKeySerializer(stringSerializer);// key序列化
shiroRedisTemplate.setValueSerializer(new SerializeUtils<Object>());// value序列化
shiroRedisTemplate.setHashKeySerializer(stringSerializer);// Hash key序列化
shiroRedisTemplate.setHashValueSerializer(new SerializeUtils<Object>());// Hash value序列化
shiroRedisTemplate.afterPropertiesSet();
return shiroRedisTemplate;
}
private RedisSerializer<String> keySerializer()
{
return new StringRedisSerializer();
}
private RedisSerializer<Object> getValueSerializer()
{
// 设置序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
return jackson2JsonRedisSerializer;
}
// 单机版配置连接参数
@Bean
public RedisStandaloneConfiguration redisStandaloneConfiguration()
{
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setDatabase(database);
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
// 集群版配置
// RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
// String[] serverArray = clusterNodes.split(",");
// Set<RedisNode> nodes = new HashSet<RedisNode>();
// for (String ipPort : serverArray) {
// String[] ipAndPort = ipPort.split(":");
// nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.valueOf(ipAndPort[1])));
// }
// redisClusterConfiguration.setPassword(RedisPassword.of(password));
// redisClusterConfiguration.setClusterNodes(nodes);
// redisClusterConfiguration.setMaxRedirects(maxRedirects);
return redisStandaloneConfiguration;
}
/**
* 配置LettuceClientConfiguration 包括线程池配置和安全项配置 genericObjectPoolConfig common-pool2线程池GenericObjectPoolConfig
* genericObjectPoolConfig
*
* @return lettuceClientConfiguration
*/
@Bean
public LettuceClientConfiguration lettuceClientConfiguration()
{
LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder()
.commandTimeout(Duration.ofMillis(timeout)).shutdownTimeout(Duration.ofMillis(200))
.poolConfig(genericObjectPoolConfig()).build();
if (ssl)
{
lettuceClientConfiguration.isUseSsl();
}
return lettuceClientConfiguration;
}
// 设置连接工厂
@Bean
public LettuceConnectionFactory lettuceConnectionFactory()
{
return new LettuceConnectionFactory(redisStandaloneConfiguration(), lettuceClientConfiguration());
}
/**
* GenericObjectPoolConfig 连接池配置
*/
@Bean
@ConfigurationProperties(prefix = "redis.lettuce.pool")
@Scope(value = "prototype")
@SuppressWarnings("rawtypes")
public GenericObjectPoolConfig genericObjectPoolConfig()
{
return new GenericObjectPoolConfig();
}
}