gpt4 book ai didi

python - Errno 111:使用Python脚本连接到Elasticsearch时,连接被拒绝

转载 作者:行者123 更新时间:2023-12-02 19:46:07 25 4
gpt4 key购买 nike

我遇到了一个 jetty 化的Elasticsearch集群问题,我想使用一个简单的python索引脚本进行连接。
在执行docker-compose之后,它也会启动集群,以及kibana,但是当它尝试通过python脚本连接到ES时,出现以下错误:

elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f1c04e0c0d0>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f1c04e0c0d0>: Failed to establish a new connection: [Errno 111] Connection refused)
我可以通过浏览器访问ES,如果我在pycharm中执行脚本,也可以定期编制索引。因此,它与docker有关,但是我找不到问题的根源。
这是我的dockerfile:
    version: '3.4'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1
container_name: es01
environment:
#- discovery.type=single-node needed?
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- xpack.security.enabled=false
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- elastic

es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data02:/usr/share/elasticsearch/data
networks:
- elastic

es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.1
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data03:/usr/share/elasticsearch/data
networks:
- elastic

kib01:
image: docker.elastic.co/kibana/kibana:7.8.1
container_name: kib01
depends_on:
- es01
- es02
- es03
ports:
- 5601:5601
environment:
ELASTICSEARCH_URL: http://es01:9200
ELASTICSEARCH_HOSTS: http://es01:9200
networks:
- elastic

web:
build: .
ports:
- 8000:8000
depends_on:
- es01
- es02
- es03
networks:
- elastic


volumes:
data01:
driver: local
data02:
driver: local
data03:
driver: local

networks:
elastic:
driver: bridge
和python脚本:
from elasticsearch import Elasticsearch, helpers
import sys, json, os
import requests

es = Elasticsearch([{'host': '127.0.0.1', 'port': 9200}])

def load_json(directory):
" Use a generator, no need to load all in memory"
for filename in os.listdir(directory):
if filename.endswith('.json'):
filename = "JSON/" + filename
with open(filename,'r') as open_file:
yield json.load(open_file)

if __name__ == '__main__':
helpers.bulk(es, load_json("JSON"), index='urteile')
从容器外部对本地主机进行 curl ,从容器内部不进行,并给出以下错误:
docker 文件中的curl命令还给了我一个被拒绝的连接。这是我得到的错误:
mapping_1  | * TCP_NODELAY set
mapping_1 | * connect to 127.0.0.1 port 9200 failed: Connection refused
mapping_1 | * Trying ::1...
mapping_1 | * TCP_NODELAY set
mapping_1 | * Immediate connect fail for ::1: Address not available
mapping_1 | * Trying ::1...
mapping_1 | * TCP_NODELAY set
mapping_1 | * Immediate connect fail for ::1: Address not available
mapping_1 | * Failed to connect to localhost port 9200: Connection refused
mapping_1 | * Closing connection 0
mapping_1 | curl: (7) Failed to connect to localhost port 9200: Connection refused
有人知道这可能是什么问题吗?
编辑:
好的,这样使用es01而不是localhost的注释会有所帮助,因为当插入“客户端”服务时,它将成功ping Elasticsearch。仍然-python应用程序告诉我连接被拒绝...

最佳答案

您正在使用本地主机作为python代码中的ES群集地址。 ES在单独的容器上运行,因此您需要使用那些主机名。您可以使用docker compose yaml文件中声明的服务名称(es01,es02等)代替127.0.0.1

关于python - Errno 111:使用Python脚本连接到Elasticsearch时,连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63248601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com