image
NightSnow

池浅王八多 遍地是大哥

免责声明:网站内容仅供个人学习记录,禁做商业用途,转载请注明出处。

版权所有 © 2017-2020 NEUSNCP个人学习笔记 辽ICP备17017855号-2

记一次 Redis 哨兵连接的问题

NightSnow    2019年6月18日 11:54:26

问题: 个人做了一个 java 连接 redis 的工具jar; 支持连接单机模式,哨兵模式,集群模式。

测试链接哨兵配置的时候发生异常: 

抛出异常后执行下面的, jedis.close 抛出异常 Connection reset ………  Socket closed by peer .

跟踪代码是 outStream.flush Socket 链接异常.

这里有个关键的地方:

配置文件配置的是 127.0.0.1; 经过解析后 获取的IP 是192 的内网地址; 关键问题就在这,使用内网地址调用哨兵 127.0.0.1:26380 调不通。  所以抛出上面的异常。 

然后看一下这个内网IP 是怎么来的。

package com.yonyou.ucf.mdd.start;

import redis.clients.jedis.HostAndPort;

import java.net.InetAddress;
import java.net.UnknownHostException;


public class TestLocalHost {

    public static String convertHost(String host) {
        try {
            InetAddress inetAddress = InetAddress.getByName(host);
            return !inetAddress.isLoopbackAddress() && !host.equals("0.0.0.0") && !host.startsWith("169.254") ? host : getLocalhost();
        } catch (Exception var2) {
            System.out.println(String.format("{}.convertHost '" + host + "' is not a valid IP address. ", HostAndPort.class.getName(), var2));
            return host;
        }

    }


    public static String getLocalhost() {
        String localAddress = null;
        try {
            InetAddress ia = InetAddress.getLocalHost();
            localAddress = ia.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return localAddress;
    }

    public static String getLocalHostQuietly() {
        String localAddress;
        try {
            localAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (Exception var2) {
            System.out.println(String.format("{}.getLocalHostQuietly : cant resolve localhost address", HostAndPort.class.getName(), var2));
            localAddress = "localhost";
        }
        return localAddress;
    }

    public static void main(String[] args) {
        System.out.println(convertHost("127.0.0.1"));
    }
}

这个是个测试类,逻辑就是 上面 parseString 的里面逻辑, 通过jdk 获取的网卡id 就是内网地址了。

但是这里没有入手点可以改。所以寻求别的思路了。。。

发现了一篇文章 https://hzkeung.com/2017/06/27/redis-sentinel-bind ,这里情况很相似。

继续增加配置

增加了bind 配置, 并且 内网地址放在第一位。 问题解决!!

 

 

 

最近更新: 2019年6月18日 11:54:26
浏览: 3.4K

[[total]] 条评论

添加评论
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]楼
  2. 点击加载更多……
  3. 添加评论