1. 程式人生 > >Spring配置Redis

Spring配置Redis

redis.properties

redis.master.name=redis.master.name

redis.master.password=redis.master.password
redis.master.timeout=100000

redis.sentinel1.address=redis.sentinel1.address:8001
redis.sentinel1.port=8001
redis.sentinel2.address=redis.sentinel2.address:8001
redis.sentinel2.port=8001
redis.sentinel3.address=redis.sentinel
3.address:8001 redis.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);
            }
        }
    }
}

相關推薦

spring配置redis(xml+java方式)

條件:引用好架包 <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</ar

Spring配置Redis

redis.properties redis.master.name=redis.master.name redis.master.password=redis.master.password redis.master.timeout=100000 r

Springredis配置

gin hostname 文件中 mapper work math 查找 () private 這個項目用到redis,所以學了一下怎樣在Spring框架下配置redis。 1、首先是在web.xml中添加Spring的配置文件。 <web-app version=

Spring Data Redis入門示例:程序配置(五)

dex port scl lis fault gre source inf 操作 單機配置 redis.properties配置 #redis的服務器地址 redis.host=127.0.0.1 #redis的服務端口 redis.port=6379 #客戶端超時時間

spring 整合 redis配置

index row val return created log idea pass truct   廢話不說,直接上代碼:   1、先添加依賴包 <!--redis--> <dependency>

Spring Boot配置redis集群

rim ember bool private mon err rem exc gin 1、編寫redis.properties配置文件 spring.redis.cluster.nodes=172.16.19.128:6300,172.16.1.281:6302,172.

spring-data-redis配置

1. 引入 jar包 : pom.xml <dependency>        <groupId>redis.clients</groupId>        <artifa

Spring註解方式配置Redis

@Configuration public class RedisConfiguraion { @Bean public JedisConnectionFactory redisConnectionFactory() { RedisStandaloneConfigura

springredis連線池版單節點使用(xml配置及非xml配置

1.依賴 <!--引入reids--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId

spring boot配置 Redis

pom依賴: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis<

Spring】SSM框架配置Redis進行快取

此過程是在SSM框架搭建完畢之後進行的擴充套件 首先引入redis依賴: <!-- redis --> <dependency> <groupId>redis.clients</groupId> <artifa

spring boot redis pool配置版本問題

spring boot 1.x版本redis配置 redis: database: 0 host: localhost port: 6379 password: pool: max-

Spring boot 配置 Redis集模式

Spring官方提供了Redis叢集的配置,這裡不復述官方的文件,這篇文章主要指導讀者手動配置Redis叢集 以下是我搜集並整理出來的一個configuration,不足之處或錯誤請大家提出並指正 package cn.xt.config; import o

spring boot2配置redis快取,快取使用Jackson2序列化

pom配置 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-b

Spring配置Redis

     1. redis是一種支援Key-Value等多種資料結構的儲存系統。可用於快取、事件釋出或訂閱、高速佇列等場景。該資料庫使用ANSI C語言編寫,支援網路,提供字串、雜湊、列表、佇列、集合結構直接存取,基於記憶體,可持久化。     2. Redis一共支援五種

spring整合redis配置檔案

說明:          1、檔案頭部分完整的除了redis還包含其他例如dubbo,aop等          2、 jedisPool,redisTemplate共用jedisPoolConfig,兩種可以同時存在,即在需要的類中@Autowired  JedisPo

spring-session redis叢集配置步驟總結

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSc

spring配置Redis,Sentinel 哨兵模式,Redis Cluster叢集,Redis Sharding叢集

spring 整合 Redis的方式有哪些  1、單機版 不解釋  配置如下: <!-- 載入配置屬性檔案 --> <context:property-placeholder ignore-unresolvable="true" location="

Spring Cloud+Redis cluster+Spring Cache配置

專案結構 personal +- example +- cacheTest +- config | +- RedisClusterConfig.java | +- co

spring Boot redis連線池配置,Spring Session配置

1.POM配置 <!-- redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifac