SpringBoot+Redis集群版

第一个类 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… 版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://juejin.im/post/5cdbc2b6e51d453afb40d879

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值