码迷,mamicode.com
首页 > 编程语言 > 详细

Java-No.13 基于redis分布式缓存实现

时间:2015-08-03 15:04:20      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

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.xsd
		http://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=5000

redis.testOnBorrow=false

redis.host.shard01=183.131.6.62
redis.port.shard01=6379

redis.host.shard02=127.0.0.1
redis.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分布式缓存实现

标签:

原文地址:http://my.oschina.net/shma1664/blog/487021

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!