SpringBoot:redis哨兵模式配置

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 21600,redisNamespace ="CBOARD")
public class RedisConfig {
    //节点之间以逗号隔开: 172.0.0.0,172.1.1.1
    @Value("${redis.sentinel.nodes}")
    private String sentinelNodes;
    //主节点名称
    @Value("${redis.sentinel.mastername}")
    private String masterName;

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(1024);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMaxWaitMillis(10000);
        jedisPoolConfig.setTestOnBorrow(true);
        return jedisPoolConfig;
    }

    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration(){
        RedisSentinelConfiguration config = new RedisSentinelConfiguration();
        String[] nodeConfigs = sentinelNodes.split(",");
        List<RedisNode> nodes = new ArrayList<>(nodeConfigs.length);
        for(String nodeConfig : nodeConfigs) {
            String[] hp = nodeConfig.split(":");
            RedisNode node = new RedisNode(hp[0].trim(),Integer.valueOf(hp[1].trim()));
            nodes.add(node);
        }
        config.setSentinels(nodes);
        config.setMaster(masterName);
        return config;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory(RedisSentinelConfiguration sentinelConfig, JedisPoolConfig jedisPoolConfig) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfig,jedisPoolConfig);
        return jedisConnectionFactory;
    }

    @Bean(name="redisTemplate")
    public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

    @Bean(name="stringRedisTemplate")
    public RedisTemplate stringRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        return redisTemplate;
    }
}

XML配置方式:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id = "poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲数 -->
        <property name="maxIdle" value="50"></property>
        <!-- 最大连接数 -->
        <property name="maxTotal" value="100"></property>
        <!-- 最大等待时间 -->
        <property name="maxWaitMillis" value="${spring.session.redisTimeout}"></property>
    </bean>

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
        <constructor-arg name="sentinelConfig" ref="sentinelConfig"></constructor-arg>
    </bean>

    <!-- JDK序列化器 -->
    <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>

    <!-- String序列化器 -->
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="keySerializer" ref="stringRedisSerializer"></property>
        <property name="defaultSerializer" ref="stringRedisSerializer"></property>
        <property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
    </bean>

    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

    <!-- 哨兵配置 -->
    <bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <!-- 服务名称 -->
        <property name="master">
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <property name="name" value="${redis.sentinel.mastername}"></property>
            </bean>
        </property>
        <!-- 哨兵服务IP和端口 -->
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.sentinel.node1}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.sentinel.node1.port}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.sentinel.node2}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.sentinel.node2.port}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.sentinel.node3}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.sentinel.node3.port}"></constructor-arg>
                </bean>
            </set>
        </property>
    </bean>
</beans>
相关推荐
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页