第一个类
package com.baibei.pay.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.nio.charset.Charset;
/**
* @author hwq
* @date 2018/11/28
*/
@Configuration
public class RedisConfiguration {
//配置
@Resource
private JedisConnectionFactory jedisConnectionFactory;
@Bean
public StringRedisTemplate redisTemplate() {
StringRedisTemplate redisTemplate = new StringRedisTemplate(jedisConnectionFactory);
CustomStringRedisSerializer customStringRedisSerializer = new CustomStringRedisSerializer();
redisTemplate.setValueSerializer(customStringRedisSerializer);
redisTemplate.setKeySerializer(customStringRedisSerializer);
redisTemplate.setHashValueSerializer(customStringRedisSerializer);
redisTemplate.setHashKeySerializer(customStringRedisSerializer);
redisTemplate.setConnectionFactory(jedisConnectionFactory);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
//序列化
class CustomStringRedisSerializer implements RedisSerializer<Object> {
private final Charset charset;
public CustomStringRedisSerializer() {
this(Charset.forName("UTF8"));
}
public CustomStringRedisSerializer(Charset charset) {
Assert.notNull(charset, "Charset must not be null!");
this.charset = charset;
}
public String deserialize(byte[] bytes) {
return (bytes == null ? null : new String(bytes, charset));
}
public byte[] serialize(Object o) {
return (o == null ? null : o.toString().getBytes(charset));
}
}
}
第二个类
package com.baibei.pay.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import java.util.HashMap;
import java.util.Map;
/**
* @author hwq
* @date 2018/11/28
*/
@Configuration
public class RedisFactoryConfig {
@Autowired
private Environment environment;
@Bean
public RedisConnectionFactory jedisConnectionFactory() {
Map<String, Object> source = new HashMap<String, Object>();
source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes"));
source.put("spring.redis.cluster.timeout", environment.getProperty("spring.redis.cluster.timeout"));
source.put("spring.redis.cluster.max-redirects", environment.getProperty("spring.redis.cluster.max-redirects"));
RedisClusterConfiguration redisClusterConfiguration;
redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
return new JedisConnectionFactory(redisClusterConfiguration);
}
}
application.properties类中
#redisCluter 集群
spring.redis.cluster.nodes=
R
E
D
I
S
.
C
L
U
S
T
E
R
.
S
E
R
V
E
R
S
s
p
r
i
n
g
.
r
e
d
i
s
.
c
l
u
s
t
e
r
.
t
i
m
e
o
u
t
=
{REDIS.CLUSTER.SERVERS} spring.redis.cluster.timeout=
REDIS.CLUSTER.SERVERSspring.redis.cluster.timeout={REDIS.CLUSTER.TIMEOUT}
spring.redis.cluster.max-redirects=3
RedisKeys类 一个定义key值的枚举类,另外还有两个工具类
这些工具在下面链接中
https://note.youdao.com/share/?id=8981a12df01655908dda27d5d06a8b82&type=note#/