• 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.xml的配置:

    #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.xml配置

    #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;
  • 相关阅读:
    python基础-递归
    python基础-三元表达式/列表推导式/生成器表达式
    python基础-生成器
    python基础-迭代器
    python基础-函数
    python基础-文件操作
    Docker(六)安装Red5进行rtmp推流
    Docker(五)安装Fastdfs
    Docker(四)安装Redis
    Docker(三)安装Mysql
  • 原文地址:https://www.cnblogs.com/niceplay/p/4992614.html
Copyright © 2020-2023  润新知