Redis在spring中的几种常用配置方法

来源:互联网 发布:非农数据最新消息 编辑:程序博客网 时间:2024/06/06 15:50

单机模式的redis就不说了。
一.首先贴一下多机分片的配置

<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" destroy-method="destroy">        <constructor-arg ref="jedisPoolConfig"/>         <constructor-arg>            <list>                 <!-- 配置分片实例 -->                <bean class="redis.clients.jedis.JedisShardInfo">                     <constructor-arg index="0" value="127.0.0.1"/>                    <constructor-arg index="1" value="6379"/>                    <constructor-arg index="2" value="instance:01"/>                </bean>            </list>        </constructor-arg>     </bean>     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxTotal" value="1000"/>         <property name="maxIdle" value="10"/>         <property name="minIdle" value="1"/>        <property name="maxWaitMillis" value="30000"/>         <property name="testOnBorrow" value="true"/>         <property name="testOnReturn" value="true"/>         <property name="testWhileIdle" value="true"/>    </bean>

使用的时候直接通过shardedJedisPool获得shardedJedis实例,最后记得返还连接

二.当我们需要HA提供监控和主从切换的时候或者可以参考Spring-session的连接方案,这种方案我们首先假设不分片。

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxTotal" value="35"/>        <property name="maxIdle" value="1000"/>        <property name="minIdle" value="100"/>        <property name="maxWaitMillis" value="1000"/>        <property name="testOnBorrow" value="true"/>        <property name="testOnReturn" value="true"/>        <property name="testWhileIdle" value="false"/>     </bean>     <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="def_master"></property>            </bean>       </property>       <property name="sentinels">           <set>               <bean class="org.springframework.data.redis.connection.RedisNode">                                       <constructor-arg name="host" value="127.0.0.1"/>                    <constructor-arg name="port" value="26379"/>                              </bean>            </set>       </property>    </bean>    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"/>        <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>    </bean>    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">        <property name="connectionFactory" ref="jedisConnectionFactory"/>    </bean>

但若使用了数据分片-sharing,数据被平均分布到多个不同的实例上,每个实例以主从结构部署,Jedis没有提供基于Sentinel的ShardedJedisPool,也就是说在多个分片中,如果其中一个分片发生主从切换,应用所使用的ShardedJedisPool无法获得通知,所有对那个分片的操作将会失败。

所以如果需要一种既可以实现多机配置,每片均为主从结构,sentinel统一进行监控,并且从ShardedJedisPool获取ShardedJedis实例,可以参考如下配置

 <bean id="shardedJedisPool" class="com.util.config.ShardedJedisSentinelPool">        <constructor-arg index="0" ref="jedisPoolConfig" />        <constructor-arg index="1">            <list>                <value>def_master</value>            </list>        </constructor-arg>        <constructor-arg index="2">            <set>                <value>127.0.0.1:26379</value>            </set>        </constructor-arg>    </bean>    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxTotal" value="200" />        <property name="maxIdle" value="100" />        <property name="maxWaitMillis" value="5000" />        <property name="testOnBorrow" value="true" />    </bean>

贴一下ShardedJedisSentinelPool的项目地址 https://github.com/warmbreeze/sharded-jedis-sentinel-pool
关于该项目请参考原贴 http://www.tuicool.com/articles/naeEJbv

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 走路有尿溢出来怎么办 篮球气嘴漏气了怎么办 暗线插座盒坏了怎么办 焊过电焊眼睛疼怎么办 烧电焊后眼睛痛怎么办? 焊完电焊眼睛疼怎么办 烧电焊后眼睛痛怎么办 用电焊后眼睛痛怎么办 看电焊后眼睛痛怎么办 眼睛焊电焊很痛怎么办 电焊没带眼睛痛怎么办 有东西进眼睛了怎么办 眼睛有东西磨眼怎么办 怀孕了眼睛肿疼怎么办 眼睛疼又红血丝怎么办 用眼过度眼睛疼怎么办 眼睛玩手机视力下降怎么办 看手机眼睛疼该怎么办 眼睛眨一下就痛怎么办 着火了怎么办教案详案 汽车尾灯磕破了怎么办 后尾灯灯罩裂了怎么办 七氟丙烷喷伤了怎么办 冒险岛2fps低怎么办 虐杀原形2很卡怎么办 玩虐杀原形2卡怎么办 虐杀原形2闪退怎么办 电脑显示不出u盘怎么办 电脑不显示u盘怎么办 u盘在电脑不显示怎么办 笔记本不识别u盘怎么办 u盘突然识别不了怎么办 xp电脑读不出u盘怎么办 电脑无法读取u盘怎么办 win7电脑不读u盘怎么办 电脑识别不出u盘怎么办 u盘电脑读不出来怎么办 u盘突然无法识别怎么办 u盘电脑无法识别怎么办 系统无法识别u盘怎么办 手机u盘无法识别怎么办