1. 程式人生 > >Redis JedisCluster Spring整合

Redis JedisCluster Spring整合

package com.system.util;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
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;

/**
 * 類描述:
 *
 * @author fengyong
 * @version 1.0
 * @since 1.0
 * Created by fengyong on 16/10/29 下午4:07.
 */
public class SimpleJedisCluster implements FactoryBean<JedisCluster>, InitializingBean {

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

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

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

    public boolean isSingleton() {
        return true;
    }

    private Set<HostAndPort> parseHostAndPort() {
        try {
            Properties prop = new Properties();
            prop.load(this.addressConfig.getInputStream());

            Set<HostAndPort> haps = new HashSet<HostAndPort>();
            for (Object key : prop.keySet()) {

                if (!((String) key).startsWith(addressKeyPrefix)) {
                    continue;
                }

                String val = (String) prop.get(key);

                boolean isIpPort = p.matcher(val).matches();

                if (!isIpPort) {
                    throw new IllegalArgumentException("ip 或 port 不合法");
                }
                String[] ipAndPort = val.split(":");

                HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
                haps.add(hap);
            }

            return haps;
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public void afterPropertiesSet() throws Exception {
        Set<HostAndPort> haps = this.parseHostAndPort();
        jedisCluster = new JedisCluster(haps, timeout, maxRedirections,poolConfig);
    }
    public void setAddressConfig(Resource addressConfig) {
        this.addressConfig = addressConfig;
    }

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

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

    public void setAddressKeyPrefix(String addressKeyPrefix) {
        this.addressKeyPrefix = addressKeyPrefix;
    }

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

3:redis.properties配置

#redis中心
#redis的伺服器地址
redis.host=127.0.0.1
#redis的服務埠
redis.port=6379
#密碼
redis.password=
#最大空閒數
redis.maxIdle=100
#最大連線數
redis.maxActive=300
#最大建立連線等待時間
redis.maxWait=1000
#客戶端超時時間單位是毫秒
redis.timeout=100000
redis.maxTotal=1000
redis.minIdle=8
#明是否在從池中取出連線前進行檢驗,如果檢驗失敗,則從池中去除連線並嘗試取出另一個
redis.testOnBorrow=true

#sentinel
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.node1.host=127.0.0.1
spring.redis.sentinel.node2.host=127.0.0.1
spring.redis.sentinel.node3.host=127.0.0.1
spring.redis.sentinel.node1.port=26379
spring.redis.sentinel.node2.port=26479
spring.redis.sentinel.node3.port=26579
#sentinel

#cluster
cluster1.host.port=127.0.0.1:7000
cluster2.host.port=127.0.0.1:7001
cluster3.host.port=127.0.0.1:7002
cluster4.host.port=127.0.0.1:7003
cluster5.host.port=127.0.0.1:7004
cluster6.host.port=127.0.0.1:7005
cluster7.host.port=127.0.0.1:7006
cluster8.host.port=127.0.0.1:7007
#cluster