spring集成Redis各种模式 单Redis,Sentinel 哨兵模式,Redis Cluster集群,Redis Sharding集群
2018-04-04 12:55:36
1511次阅读
0个评论
spring 集成 Redis的各种方式
1、单机版 配置如下:
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" /> <!-- 最大能够保持idel状态的对象数 -->
<property name="maxTotal" value="60000" /> <!-- 最大分配的对象数 -->
<property name="testOnBorrow" value="true" /> <!-- 当调用borrow Object方法时,是否进行有效性检查 -->
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.host}" />
<constructor-arg index="2" value="${redis.port}" type="int" />
<constructor-arg index="3" value="${redis.timeout}"/>
<constructor-arg index="4" value="${redis.password}"/>
</bean>
2、Sentinel 哨兵模式 主从模式,一主多从模式
<context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxWaitMillis" value="${redis.maxWaitTime}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
</bean>
<bean id="jedisSentinelPool" class="redis.clients.jedis.JedisSentinelPool">
<constructor-arg index="0" value="mymaster" />
<constructor-arg index="1">
<set>
<value>${redis.sentinel1.host}:${redis.sentinel1.port}</value>
<value>${redis.sentinel2.host}:${redis.sentinel2.port}</value>
</set>
</constructor-arg>
<constructor-arg index="2" ref="poolConfig" />
<constructor-arg index="3" value="${redis.password}" />
</bean>
redis.properties内容
##sentinel1的IP和端口
redis.sentinel1.host=192.168.1.55
redis.sentinel1.port=26379
##sentinel2的IP和端口
redis.sentinel2.host=192.168.1.56
redis.sentinel2.port=26380
redis.password=abc@123$321
#最大闲置连接数
redis.maxIdle=50
#最大连接数,超过此连接时操作redis会报错
redis.maxTotal=200
redis.maxWaitTime=3000
redis.testOnBorrow=true
#最小闲置连接数,spring启动的时候自动建立该数目的连接供应用程序使用,不够的时候会申请。
redis.minIdle=30
3、Redis Cluster, Redis官方集群方案
<!-- Jedis链接池配置,注意:Jedis版本建议升级到最新 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="100" />
<property name="maxIdle" value="20" />
<property name="minIdle" value="10" />
<property name="blockWhenExhausted" value="true"></property>
<property name="maxWaitMillis" value="3000" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="minEvictableIdleTimeMillis" value="60000" />
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="numTestsPerEvictionRun" value="-1" />
</bean>
<!-- JedisCluster -->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg index="0">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.1.111" />
<constructor-arg index="1" value="7111" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.1.112" />
<constructor-arg index="1" value="7112" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.1.113" />
<constructor-arg index="1" value="7113" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.1.114" />
<constructor-arg index="1" value="7114" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.1.115" />
<constructor-arg index="1" value="7115" type="int" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.1.116" />
<constructor-arg index="1" value="7116" type="int" />
</bean>
</set>
</constructor-arg>
<constructor-arg index="1" value="2000" type="int"></constructor-arg>
<constructor-arg index="2" value="100" type="int"></constructor-arg>
<constructor-arg index="3" ref="jedisPoolConfig"></constructor-arg>
</bean>
4、Redis Sharding集群
<!-- jedis 连接池配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="${redis.pool.maxActive}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWait" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- jedis 多个服务器配置-->
<bean id="jedisShardInfo1" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis2.ip}" />
<constructor-arg index="1" value="${redis.port}" type="int" />
<property value="${redis.password}" name="password"/>
</bean>
<bean id="jedisShardInfo2" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.ip}" />
<constructor-arg index="1" value="${redis.port}" type="int" />
<property value="${redis.password}" name="password"/>
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedisShardInfo1" />
<ref bean="jedisShardInfo2"/>
</list>
</constructor-arg>
</bean>
00