Spring配置Redis

来源:互联网 发布:vs怎样创建c语言程序 编辑:程序博客网 时间:2024/06/06 09:28

redis.properties

redis.master.name=redis.master.nameredis.master.password=redis.master.passwordredis.master.timeout=100000redis.sentinel1.address=redis.sentinel1.address:8001redis.sentinel1.port=8001redis.sentinel2.address=redis.sentinel2.address:8001redis.sentinel2.port=8001redis.sentinel3.address=redis.sentinel3.address:8001redis.sentinel3.port=8001

spring-redis-config.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">    <!-- ###### start   Redis Configuration (Owner) ####### -->    <!-- Redis Template -->    <bean id="redisTemplate" class="com.sf.acsp.inv.core.redis.RedisTemplate">        <property name="redisDataSource" ref="redisDataSource" />    </bean>    <bean id="redisDataSource" name="redisDataSource" class="com.sf.acsp.inv.core.redis.RedisDataSourceProvider">        <property name="shardedJedisPool" ref="shardedJedisSentinelPoolExt" />    </bean>    <!-- Cluster Redis Configuration Pool -->    <bean id="shardedJedisSentinelPoolExt" class="com.sf.acsp.inv.core.redis.ShardedJedisSentinelPoolExt">        <constructor-arg index="0">            <set>                <value>${redis.master.name}</value>            </set>        </constructor-arg>        <constructor-arg index="1">            <set>                <value>${redis.sentinel1.address}</value>                <value>${redis.sentinel2.address}</value>                <value>${redis.sentinel3.address}</value>            </set>        </constructor-arg>        <constructor-arg index="2" ref="jedisPoolConfig" />        <constructor-arg index="3" value="${redis.master.timeout}" type="int" />        <constructor-arg index="4" value="${redis.master.password}"/>    </bean>    <!--Jedis Pool Config-->    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxTotal" value="1400" />        <property name="maxIdle" value="1000" />        <property name="numTestsPerEvictionRun" value="1024" />        <property name="timeBetweenEvictionRunsMillis" value="30000" />        <property name="minEvictableIdleTimeMillis" value="-1" />        <property name="softMinEvictableIdleTimeMillis" value="10000" />        <property name="maxWaitMillis" value="1500" />        <property name="testOnBorrow" value="true" />        <property name="testWhileIdle" value="true" />        <property name="testOnReturn" value="false" />        <property name="jmxEnabled" value="true" />        <property name="jmxNamePrefix" value="ucmp" />        <property name="blockWhenExhausted" value="false" />    </bean>    <!-- ###### end   Redis Configuration (Owner)   ####### --></beans>

RedisDataSource.java接口

import redis.clients.jedis.ShardedJedis;/** * Redis 数据连接池 * @author 872677 * */public interface RedisDataSource {    /**     * 获取redis客户端,用于执行redis命令     * @return redis客户端     */    public ShardedJedis getRedisClient();    /**     * 将资源返回给pool     * @param shardedJedis redis客户端     */    public void returnResource(ShardedJedis shardedJedis);}

RedisDataSourceProvider.java实现类

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import redis.clients.jedis.ShardedJedis;public class RedisDataSourceProvider implements RedisDataSource {    private static final Logger logger = LoggerFactory.getLogger(RedisDataSourceProvider.class);    private ShardedJedisSentinelPoolExt shardedJedisPool;    @Override    public ShardedJedis getRedisClient() {        try {            ShardedJedis jedis = shardedJedisPool.getResource();            return jedis;        } catch (Exception t) {            logger.error(t.getMessage(), t);        }        return null;    }    @Override    public void returnResource(ShardedJedis shardedJedis) {        try {            shardedJedisPool.returnResourceObject(shardedJedis);        } catch (Exception t) {            logger.error(t.getMessage(), t);        }    }    public void setShardedJedisPool(ShardedJedisSentinelPoolExt shardedJedisPool) {        this.shardedJedisPool = shardedJedisPool;    }}

SharedJedisSentinelPoolExt.java

import org.apache.commons.pool2.PooledObject;import org.apache.commons.pool2.PooledObjectFactory;import org.apache.commons.pool2.impl.DefaultPooledObject;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import redis.clients.jedis.*;import redis.clients.jedis.exceptions.JedisConnectionException;import redis.clients.util.Hashing;import redis.clients.util.Pool;import java.util.*;import java.util.concurrent.atomic.AtomicBoolean;import java.util.regex.Pattern;/** * Jedis不能同时支持Shareded和Sentinel。 * <p> * 并且是把单master改成多master,同时把Jedis改成ShardedJedis。 * 支持多主机集群 * * @author 872677 */public class ShardedJedisSentinelPoolExt extends Pool<ShardedJedis> {    public static final int MAX_RETRY_SENTINEL = 10;    private static final Logger logger = LoggerFactory.getLogger(ShardedJedisSentinelPoolExt.class);    protected GenericObjectPoolConfig poolConfig;    protected int timeout = Protocol.DEFAULT_TIMEOUT;    private int sentinelRetry = 0;    protected String password;    protected int database = Protocol.DEFAULT_DATABASE;    protected Set<MasterListener> masterListeners = new HashSet<MasterListener>();    private volatile List<HostAndPort> currentHostMasters;    public ShardedJedisSentinelPoolExt(Set<String> masters, Set<String> sentinels) {        this(masters, sentinels, new GenericObjectPoolConfig(),                Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);    }    public ShardedJedisSentinelPoolExt(Set<String> masters, Set<String> sentinels, String password) {        this(masters, sentinels, new GenericObjectPoolConfig(),                Protocol.DEFAULT_TIMEOUT, password);    }    public ShardedJedisSentinelPoolExt(final GenericObjectPoolConfig poolConfig, Set<String> masters, Set<String> sentinels) {        this(masters, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null,                Protocol.DEFAULT_DATABASE);    }    public ShardedJedisSentinelPoolExt(Set<String> masters, Set<String> sentinels,                                       final GenericObjectPoolConfig poolConfig, int timeout,                                       final String password) {        this(masters, sentinels, poolConfig, timeout, password,                Protocol.DEFAULT_DATABASE);    }    public ShardedJedisSentinelPoolExt(Set<String> masters, Set<String> sentinels,                                       final GenericObjectPoolConfig poolConfig, final int timeout) {        this(masters, sentinels, poolConfig, timeout, null,                Protocol.DEFAULT_DATABASE);    }    public ShardedJedisSentinelPoolExt(Set<String> masters, Set<String> sentinels,                                       final GenericObjectPoolConfig poolConfig, final String password) {        this(masters, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT,                password);    }    public ShardedJedisSentinelPoolExt(Set<String> masters, Set<String> sentinels,                                       final GenericObjectPoolConfig poolConfig, int timeout,                                       final String password, final int database) {        this.poolConfig = poolConfig;        this.timeout = timeout;        this.password = password;        this.database = database;        List<String> convertList = new ArrayList<String>(masters);        List<HostAndPort> masterList = initSentinels(sentinels, convertList);        initPool(masterList);    }    public void destroy() {        for (MasterListener m : masterListeners) {            m.shutdown();        }        super.destroy();    }    public List<HostAndPort> getCurrentHostMaster() {        return currentHostMasters;    }    private void initPool(List<HostAndPort> masters) {        if (!equalsTwoHostAndPort(currentHostMasters, masters)) {            //  StringBuffer sb = new StringBuffer();            StringBuilder sb = new StringBuilder();            for (HostAndPort master : masters) {                sb.append(master.toString());                sb.append(" ");            }            logger.info("Created ShardedJedisPool to master at [" + sb.toString() + "]");            List<JedisShardInfo> shardMasters = makeShardInfoList(masters);            initPool(poolConfig, new ShardedJedisFactory(shardMasters, Hashing.MURMUR_HASH, null));            currentHostMasters = masters;        }    }    private boolean equalsTwoHostAndPort(List<HostAndPort> currentShardMasters, List<HostAndPort> shardMasters) {        if (currentShardMasters != null && shardMasters != null && currentShardMasters.size() == shardMasters.size()) {            for (int i = 0; i < currentShardMasters.size(); i++) {                if (!currentShardMasters.get(i).equals(shardMasters.get(i)))                    return false;            }            return true;        }        return false;    }    private List<JedisShardInfo> makeShardInfoList(List<HostAndPort> masters) {        List<JedisShardInfo> shardMasters = new ArrayList<JedisShardInfo>();        for (HostAndPort master : masters) {            JedisShardInfo jedisShardInfo = new JedisShardInfo(master.getHost(), master.getPort(), timeout);            jedisShardInfo.setPassword(password);            shardMasters.add(jedisShardInfo);        }        return shardMasters;    }    private List<HostAndPort> initSentinels(Set<String> sentinels, final List<String> masters) {        Map<String, HostAndPort> masterMap = new HashMap<String, HostAndPort>();        List<HostAndPort> shardMasters = new ArrayList<HostAndPort>();        logger.info("Trying to find all master from available Sentinels...");        for (String masterName : masters) {            HostAndPort master = null;            boolean fetched = false;            while (!fetched && sentinelRetry < MAX_RETRY_SENTINEL) {                for (String sentinel : sentinels) {                    final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":")));                    logger.info("Connecting to Sentinel " + hap);                    try {                        @SuppressWarnings("resource")                        Jedis jedis = new Jedis(hap.getHost(), hap.getPort());                        master = masterMap.get(masterName);                        if (master != null) {                            continue;                        }                        List<String> hostAndPort = jedis.sentinelGetMasterAddrByName(masterName);                        if (!hostAndPort.isEmpty()) {                            master = toHostAndPort(hostAndPort);                            logger.info("Found Redis master at " + master);                            shardMasters.add(master);                            masterMap.put(masterName, master);                            fetched = true;                            jedis.disconnect();                            break;                        }                    } catch (JedisConnectionException e) {                        logger.error("Cannot connect to sentinel running @ " + hap + ". Trying next one.堆栈信息=" + e);                    }                }                if (null == master) {                    try {                        logger.info("All sentinels down, cannot determine where is "                                + masterName + " master is running... sleeping 1000ms, Will try again.");                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        logger.error("错误信息={},堆栈信息={}", e.getMessage(), e);                    }                    fetched = false;                    sentinelRetry++;                }            }            // Try MAX_RETRY_SENTINEL times.            // if (!fetched && sentinelRetry >= MAX_RETRY_SENTINEL) {            if (!fetched) {                logger.error("All sentinels down and try " + MAX_RETRY_SENTINEL + " times, Abort.");                throw new JedisConnectionException("Cannot connect all sentinels, Abort.");            }        }        // All shards master must been accessed.        if (!masters.isEmpty() && masters.size() == shardMasters.size()) {            logger.info("Starting Sentinel listeners...");            for (String sentinel : sentinels) {                final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":")));                MasterListener masterListener = new MasterListener(masters, hap.getHost(), hap.getPort());                masterListeners.add(masterListener);                masterListener.start();            }        }        return shardMasters;    }    private HostAndPort toHostAndPort(List<String> getMasterAddrByNameResult) {        String host = getMasterAddrByNameResult.get(0);        int port = Integer.parseInt(getMasterAddrByNameResult.get(1));        return new HostAndPort(host, port);    }    /**     * PoolableObjectFactory custom impl.     */    protected static class ShardedJedisFactory implements PooledObjectFactory<ShardedJedis> {        private List<JedisShardInfo> shards;        private Hashing algo;        private Pattern keyTagPattern;        public ShardedJedisFactory(List<JedisShardInfo> shards, Hashing algo, Pattern keyTagPattern) {            this.shards = shards;            this.algo = algo;            this.keyTagPattern = keyTagPattern;        }        public PooledObject<ShardedJedis> makeObject() throws Exception {            ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);            return new DefaultPooledObject<ShardedJedis>(jedis);        }        public void destroyObject(PooledObject<ShardedJedis> pooledShardedJedis) throws Exception {            final ShardedJedis shardedJedis = pooledShardedJedis.getObject();            for (Jedis jedis : shardedJedis.getAllShards()) {                try {                    jedis.quit();                    jedis.disconnect();                } catch (Exception e) {                    logger.error("destroyObject 失败,msg={},堆栈信息={}", e.getMessage(), e);                }            }        }        public boolean validateObject(PooledObject<ShardedJedis> pooledShardedJedis) {            try {                ShardedJedis jedis = pooledShardedJedis.getObject();                for (Jedis shard : jedis.getAllShards()) {                    if (!"PONG".equals(shard.ping())) {                        return false;                    }                }                return true;            } catch (Exception ex) {                logger.error("validateObject 失败,msg={},堆栈信息={}", ex.getMessage(), ex);                return false;            }        }        public void activateObject(PooledObject<ShardedJedis> p) {            logger.info("empty method do sth");        }        public void passivateObject(PooledObject<ShardedJedis> p) {            logger.info("empty method do sth");        }    }    protected class JedisPubSubAdapter extends JedisPubSub {       /* @Override        public void onMessage(String channel, String message) {        }        @Override        public void onPMessage(String pattern, String channel, String message) {        }        @Override        public void onPSubscribe(String pattern, int subscribedChannels) {        }        @Override        public void onPUnsubscribe(String pattern, int subscribedChannels) {        }        @Override        public void onSubscribe(String channel, int subscribedChannels) {        }        @Override        public void onUnsubscribe(String channel, int subscribedChannels) {        }*/    }    protected class MasterListener extends Thread {        protected List<String> masters;        protected String host;        protected int port;        protected long subscribeRetryWaitTimeMillis = 5000;        protected Jedis jedis;        protected AtomicBoolean running = new AtomicBoolean(false);        protected MasterListener() {        }        public MasterListener(List<String> masters, String host, int port) {            this.masters = masters;            this.host = host;            this.port = port;        }        public MasterListener(List<String> masters, String host, int port,                              long subscribeRetryWaitTimeMillis) {            this(masters, host, port);            this.subscribeRetryWaitTimeMillis = subscribeRetryWaitTimeMillis;        }        @Override        public void run() {            running.set(true);            while (running.get()) {                jedis = new Jedis(host, port);                try {                    jedis.subscribe(new JedisPubSubAdapter() {                        @Override                        public void onMessage(String channel, String message) {                            logger.info("Sentinel " + host + ":" + port + " published: " + message + ".");                            String[] switchMasterMsg = message.split(" ");                            if (switchMasterMsg.length > 3) {                                int index = masters.indexOf(switchMasterMsg[0]);                                if (index >= 0) {                                    HostAndPort newHostMaster = toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4]));                                    List<HostAndPort> newHostMasters = new ArrayList<HostAndPort>();                                    for (int i = 0; i < masters.size(); i++) {                                        newHostMasters.add(null);                                    }                                    Collections.copy(newHostMasters, currentHostMasters);                                    newHostMasters.set(index, newHostMaster);                                    initPool(newHostMasters);                                } else {                                    StringBuilder sb = new StringBuilder();                                    for (String masterName : masters) {                                        sb.append(masterName);                                        sb.append(",");                                    }                                    logger.info("Ignoring message on +switch-master for master name "                                            + switchMasterMsg[0]                                            + ", our monitor master name are ["                                            + sb + "]");                                }                            } else {                                logger.error("Invalid message received on Sentinel "                                        + host                                        + ":"                                        + port                                        + " on channel +switch-master: "                                        + message);                            }                        }                    }, "+switch-master");                } catch (JedisConnectionException e) {                    logger.error("JedisConnectionException e msg={},堆栈信息={}", e.getMessage(), e);                    if (running.get()) {                        logger.error("Lost connection to Sentinel at " + host                                + ":" + port                                + ". Sleeping 5000ms and retrying.");                        try {                            Thread.sleep(subscribeRetryWaitTimeMillis);                        } catch (InterruptedException e1) {                           // e1.printStackTrace();                            logger.error("JedisConnectionException e1 msg={},堆栈信息={}", e.getMessage(), e1);                        }                    } else {                        logger.info("Unsubscribing from Sentinel at " + host + ":" + port);                    }                }            }        }        public void shutdown() {            try {                logger.info("Shutting down listener on " + host + ":" + port);                running.set(false);                // This isn't good, the Jedis object is not thread safe                jedis.disconnect();            } catch (Exception e) {                logger.error("Caught exception while shutting down: " + e.getMessage() + ",堆栈信息=" + e);            }        }    }}
原创粉丝点击