redis主从配置、分片
2015-06-16 10:12
519 查看
说明:
1、redis 3以下不支持分布式集群,但可以通过主从配置实现
读写分离:数据写入主服务器:192.168.11.35:6379,reids自动复制到子服务器:192.168.11.35:6381(配置文件中需要配置slaveof)
2、使用ShardedJedisPool对数据进行切片存储,根据key保存到不同的实例中
3、打印结果,验证
ShardedJedis的哈希算法参见源码
redis.properties文件参考:
测试读写分离、分片代码:
运行结果:
1、redis 3以下不支持分布式集群,但可以通过主从配置实现
读写分离:数据写入主服务器:192.168.11.35:6379,reids自动复制到子服务器:192.168.11.35:6381(配置文件中需要配置slaveof)
2、使用ShardedJedisPool对数据进行切片存储,根据key保存到不同的实例中
3、打印结果,验证
ShardedJedis的哈希算法参见源码
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); 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, shardInfo.createResource()); } }
redis.properties文件参考:
#最大分配的对象数 redis.pool.maxTotal=1024 #最大能够保持idel状态的对象数 redis.pool.maxIdle=200 #当池内没有返回对象时,最大等待时间(秒) redis.pool.maxWait=100 #当调用borrow Object方法时,是否进行有效性检查 #false:当一个实例异常时会去寻找另一个示例 redis.pool.testOnBorrow=false #当调用return Object方法时,是否进行有效性检查 redis.pool.testOnReturn=true #IP redis-m.ip=192.168.11.35 redis-s.ip=192.168.11.35 #分片 redis-c.ip=192.168.11.35 #Port redis-m.port=6379 redis-s.port=6381 #分片 redis-c.port=6382
测试读写分离、分片代码:
public class RedisConnPool { private static Jedis jedisA = null; private static Jedis jedisB = null; private static Jedis jedisC = null; private static ResourceBundle bundle; private static JedisPoolConfig config; private static ShardedJedisPool npool = null; //静态代码块中初始化代码 static { bundle = ResourceBundle.getBundle("redis"); if (bundle == null) { throw new IllegalArgumentException( "[redis.properties] 没有找到文件!"); } config = new JedisPoolConfig(); config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxTotal"))); config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle"))); config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait"))*1000); config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow"))); config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn"))); //服务器信息 String hostA = bundle.getString("redis-m.ip"); Integer portA = Integer.valueOf(bundle.getString("redis-m.port")); String hostB = bundle.getString("redis-s.ip"); Integer portB = Integer.valueOf(bundle.getString("redis-s.port")); String hostC = bundle.getString("redis-c.ip"); Integer portC = Integer.valueOf(bundle.getString("redis-c.port")); JedisShardInfo jedisShardInfoA = new JedisShardInfo(hostA,portA,"masterA"); JedisShardInfo jedisShardInfoC = new JedisShardInfo("192.168.11.35",6382,"masterC"); List<JedisShardInfo> list = new LinkedList<JedisShardInfo>(); list.add(jedisShardInfoA); list.add(jedisShardInfoC); npool = new ShardedJedisPool(config, list); // jedisA = new Jedis(hostA,portA); jedisB = new Jedis(hostB,portB); //A的子服务器 jedisC = new Jedis(hostC,portC); //另外一个切片实例 } public static void main(String[] args) { // 从池中获取一个Jedis对象 // 散列分布 // ShardedJedis jedis = npool.getResource(); jedisA.flushDB(); jedisB.flushDB(); jedisC.flushDB(); jedis.set("name", "zhangsan-new"); jedis.set("home", "hangzhou-new"); jedis.set("age","16-new"); /** * ShardedJedis set内容时,根据传入的key做一致性哈希算法,将值散列的存放到ShardedJedisPool配置的服务器上。 * ShardedJedis get内容时,将综合ShardedJedisPool配置的服务器,将值互补并取出 */ System.out.println("slaveofA: "+jedisB.get("name")+" "+jedisB.get("home")+" "+jedisB.get("age")); //读写分离:从子服务器中读取数据 System.out.println("分片的另一个实例: "+jedisC.get("name")+" "+jedisC.get("home")+" "+jedisC.get("age")); System.out.println("SharedJedis:"+jedis.get("name")+" "+jedis.get("home")+" "+jedis.get("age")); // 释放对象池 npool.returnResource(jedis); } }
运行结果:
slaveofA: null hangzhou-new 16-new 分片的另一个实例: zhangsan-new null null SharedJedis: zhangsan-new hangzhou-new 16-new
相关文章推荐
- Redis配置文件介绍
- liunx环境下安装redis
- Visual C++ 2005 Redistributable Package不能安装的解决办法
- Redis安装问题
- redis常用内存优化手段和参数含义
- Redis 一致性hash
- Redis海量数据java测试代码
- Redis笔记
- linux下,PHP操作redis及redis、php-redis的安装
- Redis Key(键)操作
- Redis学习第二课:Redis String类型及操作
- 初步了解redis
- Redis 学习第一课:安装Linux Redis(Ubantu)
- Spring 整合 Redis
- 1. Redis--KEY(键)
- 0. Redis-Server(操作)
- redis cluster概览
- tomcat7 redis session 共享
- windows下安装redis
- MySQL 简单存储过程实现Redis的INCR功能