常言笑的家

          Spring, Hibernate, Struts, Ajax, RoR

          redis cluster java client jedisCluster spring集成方法

          1、使用jedis的原生JedisCluster

          spring的applicationContext.xml配置redis的連接、連接池、jedisCluster Bean
               <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                       <property name="locations">
                           <list>
                                <value>classpath:redis.properties</value>
                           </list>
                       </property>
               </bean>

               <!-- redis config start -->
              <!-- redis pool config -->
              <bean id="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
                  <property name="maxTotal"  value="${redis.maxActive}" />
                  <property name="maxIdle"   value="${redis.maxIdle}" />
                  <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
                  <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
              </bean>

              <!-- jedisCluster config -->
              <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
                  <constructor-arg index="0">
                      <set>
                          <bean class="redis.clients.jedis.HostAndPort">
                              <constructor-arg type="String" value="${redis.host1}"/>
                              <constructor-arg type="int" value="${redis.port1}"/>
                          </bean>
                          <bean class="redis.clients.jedis.HostAndPort">
                              <constructor-arg type="String" value="${redis.host2}"/>
                              <constructor-arg type="int" value="${redis.port2}"/>
                          </bean>
                      </set>
                  </constructor-arg>
                  <constructor-arg index="1" ref="genericObjectPoolConfig" />
              </bean>
              <!-- redis config end -->
          redis.properties的配置:
          #redis config
          redis.maxActive=1000
          redis.maxIdle=10
          redis.maxWaitMillis=30000
          redis.testOnBorrow=true

          #redis host and port config
          redis.host1=192.168.1.2
          redis.port1=6379
          redis.host2=192.168.1.2
          redis.port2=6380

          jedisCluster的使用:

          @Autowired
          private JedisCluster jedisClust;
          2、自定義spring工廠類生產jedisCluster

          JedisClusterFactory.java

          package com.www.core.utils;

          import java.util.HashSet;
          import java.util.Properties;
          import java.util.Set;
          import java.util.regex.Pattern;

          import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
          import org.springframework.beans.factory.FactoryBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.core.io.Resource;

          import redis.clients.jedis.HostAndPort;
          import redis.clients.jedis.JedisCluster;

          public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {

              private String address;

              private JedisCluster jedisCluster;
              private Integer timeout;
              private Integer maxRedirections;
              private GenericObjectPoolConfig genericObjectPoolConfig;
              
              private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");

              @Override
              public JedisCluster getObject() throws Exception {
                  return jedisCluster;
              }

              @Override
              public Class<? extends JedisCluster> getObjectType() {
                  return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
              }

              @Override
              public boolean isSingleton() {
                  return true;
              }



              private Set<HostAndPort> parseHostAndPort() throws Exception {
                  try {
                      String[] addressArr=address.trim().split(",");
                      Set<HostAndPort> haps = new HashSet<HostAndPort>();
                      for(String addressStr:addressArr){
                          String[] ipAndPort = addressStr.trim().split(":");
                          HostAndPort hap = new HostAndPort(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1].trim()));
                          haps.add(hap);
                      }
                      
                      return haps;
                  } catch (IllegalArgumentException ex) {
                      throw ex;
                  } catch (Exception ex) {
                      throw new Exception("解析 jedis 配置文件失敗", ex);
                  }
              }
              
              @Override
              public void afterPropertiesSet() throws Exception {
                  Set<HostAndPort> haps = this.parseHostAndPort();
                  
                  jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);
                  
              }

              public void setTimeout(int timeout) {
                  this.timeout = timeout;
              }

              public void setMaxRedirections(int maxRedirections) {
                  this.maxRedirections = maxRedirections;
              }



              /**
               * @Param String address to set
               
          */
              public void setAddress(String address) {
                  this.address = address;
              }

              public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
                  this.genericObjectPoolConfig = genericObjectPoolConfig;
              }

          }
          spring的applicationContext.xml配置redis的連接池和工廠bean
           <!-- redis連接配置 start-->
              
              <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >
                      <property name="maxIdle" value="${redis.maxIdle}"/>
                      <property name="maxTotal" value="${redis.maxTotal}"/>
                      <property name="minIdle" value="${redis.minIdle}" />
              </bean>
              
              <!-- redis連接配置 end-->
              
              <bean id="jedisCluster" class="com.www.core.utils.JedisClusterFactory">
                  <property name="address" value="${redis.adress}" />
                  <property name="timeout" value="${redis.timeout}" />
                  <property name="maxRedirections" value="${redis.maxRedirections}"  />
                  <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
              </bean>
          redis.properties配置
          #redis config
          redis.maxTotal=200
          redis.maxIdle=50
          redis.minIdle=10

          #redis host and port config
          redis.adress=192.168.1.2:6379,192.168.1.2:6380,192.168.1.2:6381
          redis.timeout=300000
          redis.maxRedirections=6

          jedisCluster的使用:

          @Autowired
          private JedisCluster jedisCluster;

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


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


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           

          My Links

          Blog Stats

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 苏尼特左旗| 保康县| 陇南市| 五指山市| 洪湖市| 富宁县| 当阳市| 分宜县| 武山县| 通渭县| 凌源市| 平塘县| 大埔县| 礼泉县| 漳州市| 汕头市| 平乡县| 邵东县| 仁寿县| 连城县| 武夷山市| 松滋市| 靖西县| 新兴县| 始兴县| 安乡县| 邵武市| 建湖县| 乐平市| 汾西县| 靖江市| 禄劝| 延吉市| 南投县| 曲靖市| 泽州县| 龙里县| 三河市| 凌云县| 子长县| 太谷县|