常言笑的家

          Spring, Hibernate, Struts, Ajax, RoR

          Redis 3.X版本引入了集群的新特性的三種用法

          一、利用Jedis來實現

                  Set<HostAndPort>  jedisClusterNodes = new HashSet<HostAndPort>();

                    //Jedis Cluster will attempt to discover cluster nodes automatically

                   jedisClusterNodes.add(new HostAndPort("10.96.5.183",9001));

                   jedisClusterNodes.add(new HostAndPort("10.96.5.183",9002));

                   jedisClusterNodes.add(new HostAndPort("10.96.5.183",9003));

                  JedisCluster jc = new JedisCluster(jedisClusterNodes);

          二、利用spring-data-redis來實現

               <!--通過構造方法注入RedisNode-->

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

                      <constructor-arg value="10.96.5.183" />

                     <constructor-arg value="9002" type="int" />

               </bean>

               ....

              <!--setter方式注入-->

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

                 <property name="clusterNodes">

                        <set>

                                  <ref bean="clusterRedisNodes1"/>

                                 <ref bean="clusterRedisNodes2"/>

                                 <ref bean="clusterRedisNodes3"/>

                        </set>

                 </property>
              </bean>

            <!--setter方式注入,對應的屬性需存在setterXXX方法-->


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

                      <property name="maxToal" value="1000" />

                     <property name="maxIdle" value="1000" />

                     <property name="maxWaitMillis" value="1000" />

             </bean>

            <bean id="jedisConnFactory"   class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true">

                     <constructor-arg ref="redisClusterConfiguration" />

                     <constructor-arg ref="jedisPoolConfig" />

            </bean>

           <bean id="redisTemplate"   class="org.springframework.data.redis.core.RedisTemplate"  p:connection-factory-ref="jedisConnFactory" />

          <!--setter方式注入PersonRepoImpl-->

          <bean id="personRepo"  class="com.example.repo.impl.PersonRepoImpl">

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

          </bean>

          三、簡單集成Spring

              自己編寫jedisCluster的工廠類JedisClusterFactory,然后通過Spring注入的方式獲取jedisCluster,實現客戶端使用Redis3.0版本的集群特性。

          posted on 2016-08-07 17:32 常言笑 閱讀(590) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 浦县| 潞西市| 高邑县| 巴中市| 那坡县| 垦利县| 遂宁市| 社旗县| 呼和浩特市| 保德县| 阿坝| 葫芦岛市| 平南县| 登封市| 绿春县| 卓资县| 衡水市| 康保县| 抚宁县| 拉萨市| 米脂县| 同德县| 措勤县| 波密县| 郸城县| 龙泉市| 鄂托克前旗| 涞源县| 民权县| 塔城市| 绵阳市| 手机| 西林县| 交城县| 浦江县| 敦煌市| 宁武县| 额尔古纳市| 柳江县| 灵台县| 卓资县|