开发者社区> 问答> 正文

redis cluster配置集群与使用Jedis的ShardedJedis做Redis集群的区别

jedis客户端操作redis主要三种模式:单台模式、分片模式(ShardedJedis)、集群模式(BinaryJedisCluster),分片模式是一种轻量级集群

展开
收起
蛮大人123 2016-02-05 11:28:46 15612 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    直接上代码!在看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 客户端(Jedis)
    <span style="color: #333333; font-family: Arial, sans-serif;"><span style="color: #333333; font-family: Arial, sans-serif;"> private static BinaryJedisCluster jc;  
      static {  
           //只给集群里一个实例就可以  
            Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6380));  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6381));  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6382));  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6383));  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6384));  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7380));  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7381));  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7382));  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7383));  
            jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7384));  
            jc = new BinaryJedisCluster(jedisClusterNodes);  
        }  
    @Test  
        public void testBenchRedisSet() throws Exception {  
            final Stopwatch stopwatch = new Stopwatch();  
            List list = buildBlogVideos();  
            for (int i = 0; i < 1000; i++) {  
                String key = "key:" + i;  
                stopwatch.start();  
                byte[] bytes1 = protostuffSerializer.serialize(list);  
                jc.setex(key, 60 * 60, bytes1);  
                stopwatch.stop();  
            }  
            System.out.println("time=" + stopwatch.toString());  
        }</span></span>  
    2019-07-17 18:37:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
ApsaraDB for Redis——与创客同行 立即下载
微博的Redis定制之路 立即下载
云数据库Redis版的开源之路 立即下载