Quantcast
Channel: IT社区推荐资讯 - ITIndex.net
Viewing all articles
Browse latest Browse all 15843

使用Jedis的ShardedJedis做Redis集群

$
0
0

之前一直没仔细看过ShardedJedis的代码,最近遇到了shard后集群扩容后的 数据迁移问题

今天在看ShardedJedis源码的时候,发现ShardedJedis并没有使用节点的Ip和port做hash,而是用的instance的顺序或者name,太赞了。


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());
 }
 }

配置的时候也非常简单:

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxTotal" value="1000"/><property name="maxIdle" value="10"/><property name="minIdle" value="1"/><property name="maxWaitMillis" value="30000"/><property name="testOnBorrow" value="true"/><property name="testOnReturn" value="true"/><property name="testWhileIdle" value="true"/></bean><bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" destroy-method="destroy"><constructor-arg ref="jedisPoolConfig"/><constructor-arg><list><bean class="redis.clients.jedis.JedisShardInfo"><constructor-arg value="127.0.0.1"/><constructor-arg type="int" value="7000"/><constructor-arg value="instance:01"/></bean><bean class="redis.clients.jedis.JedisShardInfo"><constructor-arg value="127.0.0.1"/><constructor-arg type="int" value="7001"/><constructor-arg value="instance:02"/></bean><bean class="redis.clients.jedis.JedisShardInfo"><constructor-arg value="127.0.0.1"/><constructor-arg type="int" value="7003"/><constructor-arg value="instance:03"/></bean></list></constructor-arg></bean>

一开始你可以设置足够多的instance,数据扩容的时候,只需要将几个instance的数据copy到别的机器上。
然后修改配置文件的ip和端口即可。很方便吧?

另外,Jedis还提供了对jedis sentinel pool的封装,所以发生主从切换的时候,web server都不需要重新配置和deploy。高可用性的极佳体现啊。


@Autowired private JedisSentinelPool pool;

public void mymethod() {
 Jedis jedis = null;
 try {
 jedis = pool.getResource();
 jedis.hset(....
 } catch (JedisException je) {
 throw je;
 } finally {
 if (jedis != null) pool.returnResource(jedis);
 }
 }

spring bean的配置:

<bean id="redisSentinel" class="redis.clients.jedis.JedisSentinelPool"><constructor-arg index="0" value="mymaster" /><constructor-arg index="1"><set><value>hostofsentinel:26379</value></set></constructor-arg><constructor-arg index="2" ref="jedisPoolConfig" /></bean>
您可能也喜欢:

Redis Cluster的安装和配置(beta-6)

Redis集群增删节点的数据迁移问题

Jedis sharding

Redis在新浪微博中的应用

关于我
无觅

Viewing all articles
Browse latest Browse all 15843

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>