<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    常言笑的家

    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 常言笑 閱讀(1198) 評論(0)  編輯  收藏


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


    網站導航:
     

    My Links

    Blog Stats

    常用鏈接

    留言簿(5)

    隨筆分類

    隨筆檔案

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 无码专区永久免费AV网站| 色猫咪免费人成网站在线观看| 四虎免费影院ww4164h| 亚洲AV午夜成人片| 无码午夜成人1000部免费视频| 亚洲精品国产精品乱码不99| 99久久99这里只有免费的精品| 午夜在线免费视频| 久久久久久国产a免费观看黄色大片| 久久久久亚洲av无码专区喷水| 亚洲视频在线观看免费视频| 亚洲欧洲春色校园另类小说| 免费做爰猛烈吃奶摸视频在线观看 | 亚洲av无码专区青青草原| 午夜高清免费在线观看| 亚洲av无码成人精品国产| 波多野结衣一区二区免费视频| 人禽伦免费交视频播放| 亚洲国产精品va在线播放| 噜噜噜亚洲色成人网站| 亚洲一级特黄大片无码毛片| 日韩精品无码免费专区午夜不卡| 久久丫精品国产亚洲av不卡 | 韩国18福利视频免费观看| 国产亚洲高清在线精品不卡| 国产亚洲精午夜久久久久久| 91精品国产免费久久国语蜜臀 | 永久亚洲成a人片777777| 日韩亚洲综合精品国产| 国产精品V亚洲精品V日韩精品| 在线涩涩免费观看国产精品 | 男人j进女人p免费视频| 久久亚洲AV无码精品色午夜麻| 日韩一区二区a片免费观看| 国产成人综合亚洲| 久久久久亚洲Av片无码v| 真实乱视频国产免费观看| 中文在线免费视频| 亚洲熟女综合色一区二区三区| 91麻豆国产自产在线观看亚洲| 免费国产作爱视频网站|