美文网首页
Redis系列之集群环境SpringBoot集成

Redis系列之集群环境SpringBoot集成

作者: lyxshike | 来源:发表于2020-01-05 23:04 被阅读0次

    集群Redis的配置


    package com.terrylmay.redis.example.config;

    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

    import org.springframework.boot.context.properties.ConfigurationProperties;

    import org.springframework.context.annotation.Configuration;

    @Configuration

    @ConfigurationProperties(prefix = "spring.redis.cluster")

    @ConditionalOnProperty(name = {"spring.redis.cluster.nodes"})

    public class ClusterRedisConfig {

        private String nodes;

        private String password;

        private int maxRedirects;

        public String getNodes() {

            return nodes;

        }

        public void setNodes(String nodes) {

            this.nodes = nodes;

        }

        public String getPassword() {

            return password;

        }

        public void setPassword(String password) {

            this.password = password;

        }

        public int getMaxRedirects() {

            return maxRedirects;

        }

        public void setMaxRedirects(int maxRedirects) {

            this.maxRedirects = maxRedirects;

        }

    }


    新增一个关于集群信息的配置类, 方便后面创建JedisConnectionFactory的时候用到. 修改Application类


    package com.terrylmay.redis.example;

    import com.terrylmay.redis.example.config.ClusterRedisConfig;

    import com.terrylmay.redis.example.config.StandaloneRedisConfig;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;

    import org.springframework.context.annotation.Bean;

    import org.springframework.data.redis.connection.RedisClusterConfiguration;

    import org.springframework.data.redis.connection.RedisConnectionFactory;

    import org.springframework.data.redis.connection.RedisStandaloneConfiguration;

    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

    import org.springframework.data.redis.core.StringRedisTemplate;

    import redis.clients.jedis.JedisPool;

    import redis.clients.jedis.JedisPoolConfig;

    import java.util.Arrays;

    import java.util.Collections;

    @SpringBootApplication(scanBasePackages = {"com.terrylmay.redis.example"})

    public class RedisExampleApplication {

        public static void main(String[] args) {

            SpringApplication.run(RedisExampleApplication.class, args);

        }

        @Autowired(required = false)

        StandaloneRedisConfig standaloneRedisConfig;

        @Autowired(required = false)

        ClusterRedisConfig clusterRedisConfig;

        @Autowired

        RedisConnectionFactory redisConnectionFactory;

        @Bean

        public RedisConnectionFactory redisConnectionFactory() {

            JedisConnectionFactory factory = null;

            if (standaloneRedisConfig != null) {

                factory = new JedisConnectionFactory(new RedisStandaloneConfiguration(standaloneRedisConfig.getHost(), standaloneRedisConfig.getPort()));

                return factory;

            }

            if (clusterRedisConfig != null) {

                JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

                RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(clusterRedisConfig.getNodes().split(",")));

                redisClusterConfiguration.setMaxRedirects(clusterRedisConfig.getMaxRedirects());

                redisClusterConfiguration.setPassword(clusterRedisConfig.getPassword());

                factory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfig);

            }

            return factory;

        }

        @Bean

        public StringRedisTemplate stringRedisTemplate() {

            return new StringRedisTemplate(redisConnectionFactory);

        }

    }


    这两个类StandaloneRedisConfig,ClusterRedisConfig 的注解之所以用required=false 是因为这些类的Bean只有在满足配置文件中有特定的属性Key的时候才会生成, 所以对于某一个特定环境, 只可能使用一种Redis模式; 在创建RedisConnectionFactory 根据Bean是否存在, 创建出来不同模式的集群访问类;

    最后, 对工程进行一下完善, 因为原来是打算把所有模式下的redis配置信息放到一个配置文件中, 然后通过注释的方式做个演示; 后面发现多创建几个application-xxx.properties文件更加方便;

    创建出来的不同模式下的Redis配置信息文件如下:

    application.properties 里面只放置当前运行时的spring.profiles.active 信息

    spring.profiles.active=cluster

    application-standalone.properties 文件内容如下:

    spring.redis.host=localhost

    spring.redis.port=6379

    application-cluster.properties 文件内容如下:

    spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,

    spring.redis.cluster.password=

    spring.redis.cluster.max-redirects=12

    这样, 如果测试的时候, 只需要切换application.properties文件中的spring.profiles.active属性值即可; 可选项有: standalone|cluster|sentinel

    相关文章

      网友评论

          本文标题:Redis系列之集群环境SpringBoot集成

          本文链接:https://www.haomeiwen.com/subject/wvxhactx.html