• Spring-Cloud集成redis-cluster


    前言

    系统之前接入的是单点redis,为了条系统高可用能力,特增加集成redis-cluster集群的步骤

    依赖库引入

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    

    启动类配置

    @EnableScheduling
    @EnableCaching
    @MapperScan("com.haier.smarthome.intelligentfamilyconsole.mapper")
    public class IntelligentFamilyConsoleApplication {
    
       public static void main(String[] args) {
          SpringApplication.run(IntelligentFamilyConsoleApplication.class, args);
       }
    
    }
    

    配置类

    package com.haier.smarthome.intelligentfamilyconsole.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisClusterConfiguration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.RedisNode;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import redis.clients.jedis.JedisCluster;
    import redis.clients.jedis.JedisPoolConfig;
    
    import javax.annotation.Resource;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Author:zhanghongpeng
     * @Description:Redis 配置类
     * @CreateDate:13:48 2018/7/4
     */
    @Configuration
    @ConditionalOnClass(JedisCluster.class)
    public class RedisClusterConfig {
    
        @Resource
        private RedisProperties redisProperties;
    
        /**
         * 配置 Redis 连接池信息
         */
    //    @Bean
    //    public JedisPoolConfig getJedisPoolConfig() {
    //        JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();
    //        jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
    //        jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
    //        jedisPoolConfig.setTestOnBorrow(redisProperties.isTestOnBorrow());
    //
    //        return jedisPoolConfig;
    //    }
    
        /**
         * 配置 Redis Cluster 信息
         */
        @Bean
        public RedisClusterConfiguration getJedisCluster() {
            RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
            redisClusterConfiguration.setMaxRedirects(redisProperties.getMaxRedirects());
    
            List<RedisNode> nodeList = new ArrayList<>();
    
            String[] cNodes = redisProperties.getNodes().split(",");
            //分割出集群节点
            for(String node : cNodes) {
                String[] hp = node.split(":");
                nodeList.add(new RedisNode(hp[0], Integer.parseInt(hp[1])));
            }
            redisClusterConfiguration.setClusterNodes(nodeList);
    
            return redisClusterConfiguration;
        }
    
        /**
         * 配置 Redis 连接工厂
         */
        @Bean
        public JedisConnectionFactory getJedisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration, JedisPoolConfig jedisPoolConfig) {
            JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfig);
            return jedisConnectionFactory;
        }
    
        /**
         * 设置数据存入redis 的序列化方式
         *  redisTemplate序列化默认使用的jdkSerializeable
         *  存储二进制字节码,导致key会出现乱码,所以自定义序列化类
         */
        @Bean
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.afterPropertiesSet();
    
            return redisTemplate;
        }
    
    }
    
    package com.haier.smarthome.intelligentfamilyconsole.config;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * @Author:zhanghongpeng
     * @Description:Redis 基本环境信息
     * @CreateDate:13:48 2018/7/4
     */
    @Component
    //
    @ConfigurationProperties(prefix = "spring.redis.cluster")
    @Data
    public class RedisProperties {
    
        private String nodes;
    
        private Integer commandTimeout;
    
        private Integer maxAttempts;
    
        private Integer maxRedirects;
    
        private Integer maxActive;
    
        private Integer maxWait;
    
        private Integer maxIdle;
    
        private Integer minIdle;
    
        private boolean testOnBorrow;
    
        public String getNodes() {
            return nodes;
        }
    
        public void setNodes(String nodes) {
            this.nodes = nodes;
        }
    
        public Integer getCommandTimeout() {
            return commandTimeout;
        }
    
        public void setCommandTimeout(Integer commandTimeout) {
            this.commandTimeout = commandTimeout;
        }
    
        public Integer getMaxAttempts() {
            return maxAttempts;
        }
    
        public void setMaxAttempts(Integer maxAttempts) {
            this.maxAttempts = maxAttempts;
        }
    
        public Integer getMaxRedirects() {
            return maxRedirects;
        }
    
        public void setMaxRedirects(Integer maxRedirects) {
            this.maxRedirects = maxRedirects;
        }
    
        public Integer getMaxActive() {
            return maxActive;
        }
    
        public void setMaxActive(Integer maxActive) {
            this.maxActive = maxActive;
        }
    
        public Integer getMaxWait() {
            return maxWait;
        }
    
        public void setMaxWait(Integer maxWait) {
            this.maxWait = maxWait;
        }
    
        public Integer getMaxIdle() {
            return maxIdle;
        }
    
        public void setMaxIdle(Integer maxIdle) {
            this.maxIdle = maxIdle;
        }
    
        public Integer getMinIdle() {
            return minIdle;
        }
    
        public void setMinIdle(Integer minIdle) {
            this.minIdle = minIdle;
        }
    
        public boolean isTestOnBorrow() {
            return testOnBorrow;
        }
    
        public void setTestOnBorrow(boolean testOnBorrow) {
            this.testOnBorrow = testOnBorrow;
        }
    }
    

    使用测试

    package com.haier.smarthome.intelligentfamilyconsole.service;
    
    import com.haier.smarthome.intelligentfamilyconsole.entity.TestModel;
    import net.sf.json.JSONArray;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.CachePut;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Service;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 接口实现类 - Test
     * Created by zhanghongpeng on 2018/9/13.
     */
    @Service
    public class RedisClusterServiceImpl implements RedisClusterService{
    
            @Autowired
            private RedisTemplate redisTemplate;
    
            @Override
            @Cacheable(value = "TestmodelCache", key = "#key")
            public List<TestModel> getTestmodel(String key) {
    
                return new ArrayList<TestModel>();
            }
    
            @Override
            @CachePut(value = "TestmodelCache", key = "#key")
            public List<TestModel> updateTestmodel(String key, List<TestModel> testModelList) {
                redisTemplate.opsForValue().set(key, testModelList);
                return testModelList;
            }
    }
    
    定位问题原因* 根据原因思考问题解决方案* 实践验证方案有效性* 提交验证结果
  • 相关阅读:
    %u编码
    总结
    windows7 安装PHP7 本地网站搭建
    统计某个端口的链接数
    mysql连结查询
    mysql in
    读书笔记<白帽子讲web安全>
    Web攻防系列教程之文件上传攻防解析(转载)
    攻防:文件上传漏洞的攻击与防御
    weblogic检查项
  • 原文地址:https://www.cnblogs.com/jimoliunian/p/12964931.html
Copyright © 2020-2023  润新知