Java-No.13 基於redis分布式緩衝實現

來源:互聯網
上載者:User

標籤:

1、redis實現分布式緩衝

package com.shma.redis;import java.util.List;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;import redis.clients.jedis.JedisShardInfo;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;public class RedisService {private GenericObjectPoolConfig jedisPoolConfig;private List<JedisShardInfo> jedisShardInfos;private ShardedJedisPool shareJedisPool;public void init() {shareJedisPool =new ShardedJedisPool(jedisPoolConfig, jedisShardInfos);}public ShardedJedis getShareJedisPoolConnection() {ShardedJedis shardedJedis = shareJedisPool.getResource();return shardedJedis;}public GenericObjectPoolConfig getJedisPoolConfig() {return jedisPoolConfig;}public void setJedisPoolConfig(GenericObjectPoolConfig jedisPoolConfig) {this.jedisPoolConfig = jedisPoolConfig;}public List<JedisShardInfo> getJedisShardInfos() {return jedisShardInfos;}public void setJedisShardInfos(List<JedisShardInfo> jedisShardInfos) {this.jedisShardInfos = jedisShardInfos;}public ShardedJedisPool getShareJedisPool() {return shareJedisPool;}public void setShareJedisPool(ShardedJedisPool shareJedisPool) {this.shareJedisPool = shareJedisPool;}}
package com.shma.redis;import redis.clients.jedis.ShardedJedis;public class RedisCache {private RedisService redisService;public void set(String key, String value) {ShardedJedis shardedJedis = null;try {shardedJedis = redisService.getShareJedisPoolConnection();shardedJedis.set(key, value);} catch (Throwable e) {e.printStackTrace();} finally {shardedJedis.close();}}public String get(String key) {ShardedJedis shardedJedis = null;try {shardedJedis = redisService.getShareJedisPoolConnection();return shardedJedis.get(key);} catch (Throwable e) {e.printStackTrace();} finally {shardedJedis.close();}return null;}public boolean del(String key) {ShardedJedis shardedJedis = null;try {shardedJedis = redisService.getShareJedisPoolConnection();return shardedJedis.del(key) > 0 ? true : false;} catch (Throwable e) {e.printStackTrace();} finally {shardedJedis.close();}return false;}public RedisService getRedisService() {return redisService;}public void setRedisService(RedisService redisService) {this.redisService = redisService;}}

    spring-application.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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"xmlns:p="http://www.springframework.org/schema/p"><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location">    <value>config/redis.properties</value></property></bean><bean id="jedisPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig"><property name="maxTotal" value="${redis.maxTotal}"></property><property name="maxIdle" value="${redis.maxIdle}"></property><property name="minIdle" value="${redis.minIdle}"></property><property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property><property name="testOnBorrow" value="${redis.testOnBorrow}"></property></bean><bean id="jedisShardInfo01" class="redis.clients.jedis.JedisShardInfo"><constructor-arg value="${redis.host.shard01}"></constructor-arg><constructor-arg value="${redis.port.shard01}"></constructor-arg><constructor-arg value="${redis.timeout}"></constructor-arg></bean><bean id="jedisShardInfo02" class="redis.clients.jedis.JedisShardInfo"><constructor-arg value="${redis.host.shard02}"></constructor-arg><constructor-arg value="${redis.port.shard02}"></constructor-arg><constructor-arg value="${redis.timeout}"></constructor-arg></bean><bean id="redisService" class="com.shma.redis.RedisService" init-method="init"><property name="jedisPoolConfig"><ref bean="jedisPoolConfig"/></property><property name="jedisShardInfos"><list><ref bean="jedisShardInfo01"/><ref bean="jedisShardInfo02"/></list></property></bean><bean id="redisCache" class="com.shma.redis.RedisCache"><property name="redisService"><ref bean="redisService"/></property></bean></beans>

    redis.properties設定檔

#用戶端逾時時間單位是毫秒redis.timeout=10000#最大串連數redis.maxTotal=5000#最小空閑數redis.minIdle=100#最大空閑數redis.maxIdle=5000#最大建立串連等待時間redis.maxWaitMillis=5000redis.testOnBorrow=falseredis.host.shard01=183.131.6.62redis.port.shard01=6379redis.host.shard02=127.0.0.1redis.port.shard02=6379

2、在redis.clients.jedis源碼中發現,實現分布式如果有一台伺服器岩機,則會導致整個分布式無法使用

    修改redis.clients.jedis.JedisShardInfo類

@Override  public Jedis createResource() {    Jedis jedis = new Jedis(this);        try {    if("pong".equals(jedis.ping())) {        jedis.select(db);    return jedis;        }    } catch(Throwable e) {    System.out.println("串連異常=>" + getHost() + ":" + getPort() + ":" + db);    }        return null;  }

    修改redis.clients.util.Sharded類

private void initialize(List<S> shards) {    nodes = new TreeMap<Long, S>();    for (int i = 0; i != shards.size(); ++i) {        final S shardInfo = shards.get(i);        R r = shardInfo.createResource();        // 如果建立redis失敗,則自動剔除岩機伺服器        if (r != null) {            if (shardInfo.getName() == null)        for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {    nodes.put(this.algo.hash("SHARD-" + i + "-NODE-" + n), shardInfo);        }    else            for (int n = 0; n < 160 * shardInfo.getWeight(); n++) {    nodes.put(this.algo.hash(shardInfo.getName() + "*" + shardInfo.getWeight() + n), shardInfo);        }    resources.put(shardInfo, r);        }    }}

在預設redis分布式實現中不支援選擇db,你也可以修改構造器,傳入db,分布式在一台redis的多個db中實現

Java-No.13 基於redis分布式緩衝實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.