java客户端Jedis操作Redis Sentinel 连接池

pom 配置:

    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.0</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>  

java 代码:

public class JedisPoolUtil {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> JedisSentinelPool pool = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Properties getJedisProperties() {

    Properties config </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Properties();
    InputStream is </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
        is </span>= JedisPoolUtil.<span style="color: rgba(0, 0, 255, 1)">class</span>.getClassLoader().getResourceAsStream("cacheConfig.properties"<span style="color: rgba(0, 0, 0, 1)">);
        config.load(is);
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) {
        logger.error(</span>""<span style="color: rgba(0, 0, 0, 1)">, e);
    } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (is != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                is.close();
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) {
                logger.error(</span>""<span style="color: rgba(0, 0, 0, 1)">, e);
            }
        }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> config;
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 创建连接池
 *
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> createJedisPool() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 建立连接池配置参数</span>
    JedisPoolConfig config = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig();
    Properties prop </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedisProperties();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置最大连接数</span>
    config.setMaxTotal(StringUtil.nullToInteger(prop.getProperty("MAX_ACTIVE"<span style="color: rgba(0, 0, 0, 1)">)));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置最大阻塞时间,记住是毫秒数milliseconds</span>
    config.setMaxWaitMillis(StringUtil.nullToInteger(prop.getProperty("MAX_WAIT"<span style="color: rgba(0, 0, 0, 1)">)));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置空间连接</span>
    config.setMaxIdle(StringUtil.nullToInteger(prop.getProperty("MAX_IDLE"<span style="color: rgba(0, 0, 0, 1)">)));
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> jedis实例是否可用</span>
    <span style="color: rgba(0, 0, 255, 1)">boolean</span> borrow = prop.getProperty("TEST_ON_BORROW") == "false" ? <span style="color: rgba(0, 0, 255, 1)">false</span> : <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    config.setTestOnBorrow(borrow);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建连接池

// pool = new JedisPool(config, prop.getProperty("ADDR"), StringUtil.nullToInteger(prop.getProperty("PORT")), StringUtil.nullToInteger(prop.getProperty("TIMEOUT")));// 线程数量限制,IP 地址,端口,超时时间
//获取 redis 密码
String password = StringUtil.nullToString(prop.getProperty("PASSWORD"));

     String masterName </span>= "mymaster"<span style="color: rgba(0, 0, 0, 1)">;
    Set</span>&lt;String&gt; sentinels = <span style="color: rgba(0, 0, 255, 1)">new</span> HashSet&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)">();
    sentinels.add(</span>"192.168.137.128:26379"<span style="color: rgba(0, 0, 0, 1)">);
    sentinels.add(</span>"192.168.137.128:26380"<span style="color: rgba(0, 0, 0, 1)">);
    sentinels.add(</span>"192.168.137.128:26381"<span style="color: rgba(0, 0, 0, 1)">);
    pool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisSentinelPool(masterName, sentinels, config);
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 在多线程环境同步初始化
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> poolInit() {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (pool == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
        createJedisPool();
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 获取一个jedis 对象
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Jedis getJedis() {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (pool == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
        poolInit();
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> pool.getResource();
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 释放一个连接
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> returnRes(Jedis jedis) {
    pool.returnResource(jedis);
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 销毁一个连接
 *
 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> returnBrokenRes(Jedis jedis) {
    pool.returnBrokenResource(jedis);
}
 
 
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args){
    Jedis jedis</span>=<span style="color: rgba(0, 0, 0, 1)">getJedis();
     
}

}