RedisSentinelUtils

功能:

  • getMasterNames 从Sentinel集群从获取master名称列表
依赖:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.1</version>
</dependency>
源码:

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * @author Dotions 2016年4月7日 下午4:12:11
 */
public class RedisSentinelUtils {

    private static final Logger LOG = LoggerFactory.getLogger(RedisSentinelUtils.class);

    /**
     * 从 Sentinel 集群中获取 Master 名称列表
     * Set value format is ip:port
     */
    public static Set<String> getMasterNames(Set<String> sentinels) {

        if (sentinels == null || sentinels.isEmpty()) {
            return Collections.emptySet();
        }

        Set<String> masterNames = new LinkedHashSet<String>();
        Iterator<String> it = sentinels.iterator();
        Jedis sentinel = null;
        String shap = null;

        while (it.hasNext()) {
            shap = it.next();
            try {
                final HostAndPort hap = toHostAndPort(Arrays.asList(shap.split(":")));

                sentinel = new Jedis(hap.getHost(), hap.getPort());

                String sInfo = sentinel.info();

                if (StringUtils.isNotBlank(sInfo)) {
                    InputStream is = new ByteArrayInputStream(sInfo.getBytes());
                    Properties p = new Properties();
                    p.load(is);

                    // check master number
                    int masterN = Integer.parseInt(p.getProperty("sentinel_masters"));

                    if (masterN > 0) {
                        String val = null;
                        String status = null;
                        String masterName = null;
                        for (int i = 0; i < masterN; i++) {
                            val = p.getProperty("master" + i);
                            String[] kv = val.split(",");

                            status = kv[1].split("=")[1];
                            masterName = kv[0].split("=")[1];

                            // Master status=ok
                            if ("ok".equals(status)) {
                                masterNames.add(masterName);
                            }
                            LOG.info("masterName={}, status={}", masterName, status);
                        }
                    } else {
                        LOG.info("Not found any master in sentinel {}", shap);
                    }
                } else {
                    LOG.error("Sentinel command execute fail, result is blank.");
                }
            } catch (JedisConnectionException ce) {
                sentinels.remove(shap);
                LOG.error(String.format("[%s] connection exception, cause by: ", shap), ce);
            } catch (Exception e) {
                LOG.error(String.format("[%s] runtime exception, cause by: ", shap), e);
            } finally {
                sentinel.close();
            }
        }
        return masterNames;
    }

    private static HostAndPort toHostAndPort(List<String> hostAndPort) {
        return new HostAndPort(hostAndPort.get(0), Integer.parseInt(hostAndPort.get(1)));
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-s...
    神秘者007阅读 769评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,997评论 19 139
  • 1.1 资料 ,最好的入门小册子,可以先于一切文档之前看,免费。 作者Antirez的博客,Antirez维护的R...
    JefferyLcm阅读 17,121评论 1 51
  • 三欣: 欣赏团队 欣赏自己 欣赏队友 欣赏是什么呢? 欣赏是...
    云梦_d725阅读 403评论 1 0
  • 在日常生活中,处理不好愤怒的情绪,会使自己的人际关系变得糟糕。所以要适当的控制自己的愤怒,巧妙的化解愤怒在生活中带...
    田真十阅读 273评论 0 1