错误排查1: 如何排查Connection refused异常?

837 阅读3分钟

问题出现场景

我在使用hdfs的时候,报出如下异常:

java.net.ConnectException: Connection refused

排查思路

 ConnectionRefused Exception的定义如下:

You get a ConnectionRefused Exception when there is a machine at the address specified, but there is no program listening on the specific TCP port the client is using -and there is no firewall in the way silently dropping TCP connection requests.

—— wiki.apache.org/hadoop/Conn…

即对应地址和端口没有进程进行TCP监听。

那么大致的排查思路就有了,从上到下为:

  1. 确保ip和端口正确(已经服务器应用是否启动)
  2. ping一下网络,看是否正常
  3. 查看端口是否有TCP监听
root@ecs-284712:~ netstat -tlpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      537/systemd-resolve 
tcp        0      0 0.0.0.0:50070           0.0.0.0:*               LISTEN      10442/java          
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2280/sshd           
tcp        0      0 0.0.0.0:50010           0.0.0.0:*               LISTEN      10613/java          
tcp        0      0 0.0.0.0:50075           0.0.0.0:*               LISTEN      10613/java          
tcp        0      0 0.0.0.0:50020           0.0.0.0:*               LISTEN      10613/java          
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      10442/java          
tcp        0      0 0.0.0.0:50090           0.0.0.0:*               LISTEN      10809/java       

当发现地址为127.0.0.1时要注意,该地址仅单机可使用,外界不能连接。(一般都是/etc/hosts文件的域名映射不正确,将localhost映射为127.0.0.1, 我个人将hdfs配置文件etc/hadoop/core-site.xml中的localhost改为0.0.0.0可行)

  1. 查看防火墙是否开放该端口
# 1. 查看ufw的状态
root@ecs-284712:~ sudo ufw status

# 2. 开启ufw
root@ecs-284712:~ sudo ufw enable


# 3. 重启防火墙
root@ecs-284712:~ sudo ufw reload

# 4. 对外开启端口
root@ecs-284712:~ sudo ufw allow 9000


# 5. 关闭防火墙
root@ecs-284712:~ sudo ufw disable
  1. 阿里云、华为云等ESC服务器,一般要在安全组配置对应端口

ps: 顺便吐槽一下,我的云服务器突然ssh登陆不上了,找了一下午才发现是因为我开启了防火墙,没把端口22开放。但是要开启防火墙,我就必须登录进去啊!

补充: linux常见网络请求

网络环境查看命令

  • ifconfig : 查看IP与子网掩码
  • 关闭与启动网卡
    • ifdown 网卡设备名 :禁用该网卡设备
    • ifup 网卡设备名 :启用该网卡设备
  • route 命令设置网关
    • route -n 查看路由列表(可以看到网关)
    • route add default gw 192.168.1.1 临时设置网关
  • netstat 选项 :查询网络状态
    • -t:列出TCP协议端口
    • -u:列出UDP协议端口
    • -n:不使用域名与服务名,而是使用IP地址和端口号
    • -l :仅列出在监听状态网络连接
    • -a:列出所有的网络连接
$ netstat -tn
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 172.16.247.46:47302     100.100.30.25:80        ESTABLISHED  //已经建立连接
tcp        0     36 172.16.247.46:22        171.14.145.224:32371    ESTABLISHED
tcp        0      0 172.16.247.46:22        171.14.145.224:32732    ESTABLISHED
$ netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN   //说明tomcat已经启动
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:8005          0.0.0.0:*               LISTEN
$ netsate -an | grep  ESTABLISHED | wc -l   统计建立的连接数 

网络通信命令

  • ping [选项] ip或域名 : 探测指定IP或域名的网络状况. ( -c 次数:指定ping包的次数)

  • telnet [ip或域名] [端口] : 远程管理与端口探测命令。由于telnet明文传输,不建议用于远程管理,但可以探测端口是否开启。

  • traceroute [选项] ip或域名 : 路由跟踪命令(采用ICMP协议,-n 使用IP,不使用域名,速度更快

  • wget命令:下载命令

Linux环境变量

Linux环境变量设置

  • /etc/environment : 设置整个系统的环境。

img

  • /etc/profile : 此文件为所有用户设置环境信息。当用户第一次登录时,该文件被执行。
  • /etc/bashrc : 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。
  • ~/.bash_profile : 文件为单个用户设置环境信息。当用户登录时,该文件仅仅执行一次。默认情况下,他设置一些环境变量,执行用户的.bashrc文件。
  • ~/.bashrc : 当登录时以及每次打开新的shell时,该文件被读取。(zsh配置 ~/.zshrc)
  • ~/.bash_logout: 当每次退出系统(退出bash shell)时,执行该文件。
  • export: 设置临时变量。终端关闭时失效。