package com.shinemo.mpush.tools.redis.jedis.services;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.shinemo.mpush.tools.redis.RedisGroup;
import com.shinemo.mpush.tools.redis.RedisNode;
import com.shinemo.mpush.tools.redis.RedisRegister;
import com.shinemo.mpush.tools.zk.manage.ServerAppManage;

public class JedisRegisterManager implements RedisRegister{

	private static final Logger LOGGER = LoggerFactory.getLogger(ServerAppManage.class);

    private static List<RedisGroup> groups = Lists.newArrayList();

    @Override
    public void init(List<RedisGroup> group) {
        if (group == null || group.isEmpty()) {
            throw new RuntimeException("init redis client error, redis server is none.");
        }
        groups = group;
        printGroupList();
    }


    @Override
    public List<RedisGroup> getGroupList() {
        return Collections.unmodifiableList(groups);
    }

    private void printGroupList() {
        for (RedisGroup app : groups) {
            LOGGER.warn(ToStringBuilder.reflectionToString(app, ToStringStyle.DEFAULT_STYLE));
        }
    }

    public int groupSize() {
        return groups.size();
    }

    /**
     * 随机获取一个redis 实例
     *
     * @param key
     * @return
     */
    @Override
    public RedisNode randomGetRedisNode(String key) {
        int size = groupSize();
        if (size == 1) return groups.get(0).get(key);
        int i = (int) ((Math.random() % size) * size);
        RedisGroup group = groups.get(i);
        return group.get(key);
    }

    /**
     * 写操作的时候，获取所有redis 实例
     *
     * @param key
     * @return
     */
    @Override
    public List<RedisNode> hashSet(String key) {
        List<RedisNode> nodeList = Lists.newArrayList();
        for (RedisGroup group : groups) {
            RedisNode node = group.get(key);
            nodeList.add(node);
        }
        return nodeList;
    }
	
}

@@ -732,24 +732,62 @@
 rayList();%0A%0A
+    /**%0A     * zk %E5%90%AF%E5%8A%A8%E7%9A%84%E6%97%B6%E5%80%99%E9%9C%80%E8%A6%81%E8%B0%83%E7%94%A8%E8%BF%99%E4%B8%AA%0A     */%0A
     @Overrid

修改 jedis register manage