前言
如果需要Redis存储海量的内存数据,使用单台redis不能满足用户的需求,所以可以采用Redis分片机制实现数据存储
简单实现
- 配置多台Redis服务器,在Java中使用Jedis提供的JedisShardInfo对象进行封装,并放在一个集合中
- 将JedisShardInfo对象集合作为参数,创建ShardedJedis对象封装分片,实现自动的管理
1 2 3 4 5 6 7 8 9 10
| @Test public void testShards(){ List<JedisShardInfo> shards = new ArrayList<>(); shards.add(new JedisShardInfo("127.0.0.1", 6379)); shards.add(new JedisShardInfo("127.0.0.1", 6380)); shards.add(new JedisShardInfo("127.0.0.1", 6381)); ShardedJedis shardedJedis = new ShardedJedis(shards); shardedJedis.set("key", "value"); System.out.println(shardedJedis.get("shards")); }
|
完成