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<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#/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值