第一个类 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 {
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=REDIS.CLUSTER.SERVERSspring.redis.cluster.timeout=
REDIS.CLUSTER.SERVERSspring.redis.cluster.timeout={REDIS.CLUSTER.TIMEOUT}
spring.redis.cluster.max-redirects=3
复制代码
RedisKeys类 一个定义key值的枚举类,另外还有两个工具类 这些工具在下面链接中
note.youdao.com/share/?id=8…
作者:闷骚的狐狸 来源:CSDN 原文:blog.csdn.net/fox100629/a… 版权声明:本文为博主原创文章,转载请附上博文链接!