spring+springdataredist+Redis单机、集群(cluster模式,哨兵模式)

coding

一、单机redis配置

1. 配置redis连接池

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

<!-- 控制一个pool可分配多少个jedis实例 -->

<property name="maxTotal" value="${redis.maxTotal}"/>

<!-- 控制一个pool最多有多少个状态为idle(空闲)的jedis实例 -->

<property name="maxIdle" value="${redis.maxIdle}"/>

<!-- 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException -->

<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>

<property name="testOnBorrow" value="true"/>

<property name="testOnReturn" value="true"/>

</bean>

<!-- redis的连接池pool,不是必选项:timeout/password -->

<bean id="jedisConnectionFactory"

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

<property name="hostName" value="${redis.host}"/>

<property name="port" value="${redis.port}"/>

<property name="password" value="${redis.passWord}"/>

<property name="poolConfig" ref="jedisPoolConfig"/>

</bean>

2. 配置redis工具类

<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">

<property name="connectionFactory" ref="jedisConnectionFactory"/>

</bean>

<!-- 配置Redis自定义工具类 -->

<bean id="springRedis" class="com.wslook.common.redis.SpringRedis">

<property name="redisKeyPrefix" value="${redis.key.prefix}"/>

<property name="stringRedisTemplate" ref="stringRedisTemplate"/>

</bean>

3. properties文件

二、哨兵模式配置


1. 配置redis连接池

<!--配置JedisPoolConfig-->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

<!-- 控制一个pool可分配多少个jedis实例 -->

<property name="maxTotal" value="${redis.sentinel.maxTotal}"/>

<!-- 控制一个pool最多有多少个状态为idle(空闲)的jedis实例 -->

<property name="maxIdle" value="${redis.sentinel.maxIdle}"/>

<!-- 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException -->

<property name="maxWaitMillis" value="${redis.sentinel.maxWaitMillis}"/>

<property name="testOnBorrow" value="true"/>

<property name="testOnReturn" value="true"/>

</bean>

<!-- 构造JedisConnectionFactory实例 -->

<bean id="jedisConnectionFactory"

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

<constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"/>

<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>

<property name="password" value="${redis.sentinel.password}"/>

</bean>

2. 配置Redis Sentinel

  • 只需配置集群名称和哨兵地址即可

<bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">

<property name="master">

<bean class="org.springframework.data.redis.connection.RedisNode">

<property name="name" value="mymaster"/>

</bean>

</property>

<property name="sentinels">

<set>

<bean name="redisNode1" class="org.springframework.data.redis.connection.RedisNode">

<constructor-arg name="host" value="${redis.sentinel.node1.host}"/>

<constructor-arg name="port" value="${redis.sentinel.node1.port}"/>

</bean>

<bean name="redisNode2" class="org.springframework.data.redis.connection.RedisNode">

<constructor-arg name="host" value="${redis.sentinel.node2.host}"/>

<constructor-arg name="port" value="${redis.sentinel.node2.port}"/>

</bean>

</set>

</property>

</bean>

3. 配置redis工具类

<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">

<property name="connectionFactory" ref="jedisConnectionFactory"/>

</bean>

<!-- 配置springRedis -->

<bean id="springRedis" class="com.wslook.common.redis.SpringRedis">

<property name="redisKeyPrefix" value="${redis.sentinel.key.prefix}"/>

<property name="stringRedisTemplate" ref="stringRedisTemplate"/>

</bean>

4. properties文件

三、Cluster集群配置


1. 配置redis连接池

<!--配置JedisPoolConfig-->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

<!-- 控制一个pool可分配多少个jedis实例 -->

<property name="maxTotal" value="${redis.cluster.maxTotal}"/>

<!-- 控制一个pool最多有多少个状态为idle(空闲)的jedis实例 -->

<property name="maxIdle" value="${redis.cluster.maxIdle}"/>

<!-- 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException -->

<property name="maxWaitMillis" value="${redis.cluster.maxWaitMillis}"/>

<property name="testOnBorrow" value="true"/>

<property name="testOnReturn" value="true"/>

</bean>

<!-- 构造JedisConnectionFactory实例 -->

<bean id="jedisConnectionFactory"

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

<constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>

<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>

<property name="password" value="${redis.cluster.password}"/>

</bean>

2. 配置Cluster节点

<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">

<property name="clusterNodes">

<set>

<bean name="redisNode0" class="org.springframework.data.redis.connection.RedisNode">

<constructor-arg name="host" value="${redis.cluster.node.host}"/>

<constructor-arg name="port" value="${redis.cluster.node.port}"/>

</bean>

<bean name="redisNode1" class="org.springframework.data.redis.connection.RedisNode">

<constructor-arg name="host" value="${redis.cluster.node1.host}"/>

<constructor-arg name="port" value="${redis.cluster.node1.port}"/>

</bean>

<bean name="redisNode2" class="org.springframework.data.redis.connection.RedisNode">

<constructor-arg name="host" value="${redis.cluster.node2.host}"/>

<constructor-arg name="port" value="${redis.cluster.node2.port}"/>

</bean>

<bean name="redisNode3" class="org.springframework.data.redis.connection.RedisNode">

<constructor-arg name="host" value="${redis.cluster.node3.host}"/>

<constructor-arg name="port" value="${redis.cluster.node3.port}"/>

</bean>

<bean name="redisNode4" class="org.springframework.data.redis.connection.RedisNode">

<constructor-arg name="host" value="${redis.cluster.node4.host}"/>

<constructor-arg name="port" value="${redis.cluster.node4.port}"/>

</bean>

<bean name="redisNode5" class="org.springframework.data.redis.connection.RedisNode">

<constructor-arg name="host" value="${redis.cluster.node5.host}"/>

<constructor-arg name="port" value="${redis.cluster.node5.port}"/>

</bean>

</set>

</property>

</bean>

3. 配置redis工具类

<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">

<property name="connectionFactory" ref="jedisConnectionFactory"/>

</bean>

<!-- 配置springRedis -->

<bean id="springRedis" class="com.wslook.common.redis.SpringRedis">

<property name="redisKeyPrefix" value="${redis.cluster.key.prefix}"/>

<property name="stringRedisTemplate" ref="stringRedisTemplate"/>

</bean>

4. properties文件

 

以上是 spring+springdataredist+Redis单机、集群(cluster模式,哨兵模式) 的全部内容, 来源链接: utcz.com/z/509627.html

回到顶部