SpringMVC集成redis cluster集群模式

1、cluster相对于哨兵模式是去中心化的,它的每个节点都存储了其它集群的信息,因此每个节点都可以做为集群的中心,容错能力强,具有更高的可用性和在线扩容能力。

2、简单的说,他就是将key通过hash算法,然后进行取模,将不同的key存储到对应的节点上,每个节点又可以有自己的从节点,也有一些局限性:

  •  mget,mset 命令不支持(客户端可能提供了兼容的操作方法,但不建议使用)

  • pipeline 管道操作去掉,改成单次操作

  •  watch,multi,exec 事务操作过程中,要保证只能对同一个key操作

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

    <!-- jedis pool通用配置  -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="minIdle" value="${redis.minIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />
        <property name="testWhileIdle" value="${redis.testWhileIdle}" />
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
    </bean>

    <bean id="mapPropertySource" class="org.springframework.core.env.MapPropertySource">
        <constructor-arg>
            <value type="java.lang.String">redis.propertis</value>
        </constructor-arg>
        <constructor-arg>
            <map>
                <entry key="spring.redis.cluster.nodes" value="${spring.redis.cluster.nodes}"></entry>
                <entry key="spring.redis.cluster.max-redirects" value="${spring.redis.cluster.max-redirects}"></entry>
            </map>
        </constructor-arg>
    </bean>

    <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <constructor-arg name="propertySource" ref="mapPropertySource" />
    </bean>
    <bean id="redisClusterConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg ref="redisClusterConfiguration" />
        <constructor-arg ref="jedisPoolConfig" />
        <property name="password" value="${spring.redis.cluster.password}" />
        <property name="timeout" value="${redis.timeout}" />
    </bean>
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisClusterConnectionFactory" />
        <property name="keySerializer" ref="stringRedisSerializer" />
        <property name="valueSerializer" ref="stringRedisSerializer" />
    </bean>
</beans>

猜你喜欢

转载自blog.csdn.net/lq18050010830/article/details/84346062