Linux中查看、分析8080端口占用情况,以及对应的解决方案

在启动一个应用时,发现8080 端口被占用了。

Address already in use

Linux中查看、分析8080端口占用情况,以及对应的解决方案

如何分析、查看端口8080的占用呢?

/etc/service文件只是让你清楚一些公开的应用占用的是哪些端口,不至于造成一些冲突。

真正启用什么端口,应该是你的网络应用决定的。

也是说,下面的命令,仅仅是一些建议或说明,并不表示端口8080的实际占用情况。

grep 8080 /etc/services

webcache 8080/tcp # WWW caching service

webcache 8080/udp # WWW caching service

(1)使用lsof 查询8080 端口的运行进程

lsof -i :8080

Linux中查看、分析8080端口占用情况,以及对应的解决方案

(2)通过 ps -ef 命令获取更详细的信息

ps -ef | grep 1837【PID】

Linux中查看、分析8080端口占用情况,以及对应的解决方案

通过上述信息,可以看出是Zookeeper应用占用了 8080 端口。

Zookeeper 3.5.0新增内容:

AdminServer是一个内置的Jetty服务。默认的服务启动在8080端口。例如访问,http://localhost:8080/commands/stat,命令响应以JSON的格式返回。

为了查看所有可用命令的列表,可以访问URL /commands。

(3)解决端口8080被占用问题

针对Zookeeper AdminServer占用8080 端口问题,不能简单 kill 掉这个进程,因为Zookeeper 应用是必须要正常运行的。

如下是来自Zookeeper官方文档的内容,讲述了AdminServer的配置参数。

http://zookeeper.apache.org/doc/current/zookeeperAdmin.html

New in 3.5.0: The following options are used to configure the AdminServer.

  • admin.enableServer : (Java system property: zookeeper.admin.enableServer) Set to "false" to disable the AdminServer. By default the AdminServer is enabled.
  • admin.serverAddress : (Java system property: zookeeper.admin.serverAddress) The address the embedded Jetty server listens on. Defaults to 0.0.0.0.
  • admin.serverPort : (Java system property: zookeeper.admin.serverPort) The port the embedded Jetty server listens on. Defaults to 8080.
  • admin.idleTimeout : (Java system property: zookeeper.admin.idleTimeout) Set the maximum idle time in milliseconds that a connection can wait before sending or receiving data. Defaults to 30000 ms.
  • admin.commandURL : (Java system property: zookeeper.admin.commandURL) The URL for listing and issuing commands relative to the root URL. Defaults to "/commands".

ZooKeeper Server启动时,默认读取$ZOOKEEPER_HOME/conf/zoo.cfg文件,zoo.cfg文件配置了Zookeeper所有的运行参数。

通过查看Zookeeper的官方文档,发现有几种解决途径:

1. 禁用AdminServer

在zoo.cfg中增加配置 admin.enableServer=false

或者在启动脚本中增加-Dzookeeper.admin.enableServer=false

2. 修改AdminServer端口号

修改方法的方法有两种:

一种是在启动脚本中增加 -Dzookeeper.admin.serverPort=没有被占用的端口号;

另一种是在zoo.cfg中增加admin.serverPort=没有被占用的端口号,如8090端口号。

vim /opt/apache-zookeeper/conf/zoo.cfg

Linux中查看、分析8080端口占用情况,以及对应的解决方案

下面采用方案2,修改AdminServer端口号为8090。

/opt/apache-zookeeper/bin/zkServer.sh start

重新启动Zookeeper,发现AdminServer端口号已经更改为 8090了。

访问Zookeeper AdminServer

http://192.168.56.103:8090/commands

Linux中查看、分析8080端口占用情况,以及对应的解决方案

如下是Zookeeper AdminServer的配置参数说明:

Linux中查看、分析8080端口占用情况,以及对应的解决方案

这样,Zookeeper AdminServer占用的8080 端口就释放出来了。

Linux中查看、分析8080端口占用情况,以及对应的解决方案

相关推荐