我正在运行一个地址为127.0.0.1:5000的Web应用程序,并且正在使用Prometheus的python客户端库。我在他们的start_http_server(8000)
中使用示例中的docs来公开该端口上的指标。该应用程序运行,但我得到[Errno 48] Address already in use
和localhost:8000当我尝试点击它时没有连接任何东西。
如果我无法从一个Web应用程序启动两个服务器,那么我应该将哪个端口传递到start_http_server()
以公开指标?
在我启动应用程序之前,任何端口上都没有运行任何内容。
其他一些过程正在使用端口(8000)。要终止在端口(8000)上运行的进程,只需找到进程的process_id [pid]。
lsof -i :8000
这将显示端口8000上运行的进程,如下所示:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 21271 hashed 3u IPv4 1430288 0t0 TCP *:8000 (LISTEN)
您可以使用kill命令终止进程,如下所示:
sudo kill -9 21271
如果使用相同的命令杀死进程,请重新检查
lsof -i :8000
标准杆上应该没有任何东西。
这主要是因为您在端口8000上重新启动服务器。
为了解决这个问题,我创建了一个函数,它可以在确保以前的服务器或端口可以使用之后创建服务器。
你可以看看https://github.com/prometheus/client_python/issues/155,这里的情况也是如此
端口8000不需要在其上运行Web服务器,因为它已经在使用中。使用您的OS命令行查找正在使用该端口然后将其终止的进程。如果某个服务也在运行,导致它再次生成,请禁用该过程。
一个更简单的解决方案是使用另一个端口而不是8000。
编辑:看起来它是普罗米修斯的一个错误。 Github Issue
你不能在同一个线程上运行两个http服务器。我不知道为什么,但prometheus_client的实现不会在单独的线程上运行服务器。我的解决方案是
import logging.config
import os
import connexion
from multiprocessing.pool import ThreadPool
from prometheus_client import start_http_server
app = connexion.App(__name__, specification_dir='./')
app.add_api('swagger.yml')
# If we're running in stand alone mode, run the application
if __name__ == '__main__':
pool = ThreadPool(1)
pool.apply_async(start_http_server, (8000, )) # start prometheus in a different thread
app.run(host='0.0.0.0', port=5000, debug=True) . # start my server