golang 使用github.com/olivere/elastic包进行连接 远程 es 或者docker搭建的es时(只要golang所在主机与es所在主机不同),报错信息如下
es client connect failed:no active connection found: no Elasticsearch node available
检查es node 信息
curl -s -XGET 'http://localhost:9200/_nodes/http?pretty=true’
{
??"_nodes" : {
????"total" : 1,
????"successful" : 1,
????"failed" : 0
??},
??"cluster_name" : "docker-cluster",
??"nodes" : {
????"XSzTxWW9Q3OC9UWlfdncMw" : {
??????"name" : "XSzTxWW",
??????"transport_address" : "172.17.0.2:9300",
??????"host" : "172.17.0.2",
??????"ip" : "172.17.0.2",
??????"version" : "6.6.2",
??????"build_flavor" : "default",
??????"build_type" : "tar",
??????"build_hash" : "3bd3e59",
??????"roles" : [
????????"master",
????????"data",
????????"ingest"
??????],
??????"attributes" : {
????????"ml.machine_memory" : "2088206336",
????????"xpack.installed" : "true",
????????"ml.max_open_jobs" : "20",
????????"ml.enabled" : "true"
??????},
??????"http" : {
????????"bound_address" : [
??????????"0.0.0.0:9200"
????????],
????????"publish_address" : "172.17.0.2:9200",
????????"max_content_length_in_bytes" : 104857600
??????}
????}
??}
}
发现publish_address(172.17.0.2) 地址是一个内网地址,无法正常访问,所以 会报错上面的信息,无可用的node节点
解决方法(两种)
-
改变golang代码初始化client时的参数. client, err := elastic.NewClient(elastic.SetSniff(false),elastic.SetURL(host…)) 新增参数elastic.SetSniff(false), 用于关闭 Sniff
-
调整es node 的 publish_address 配置,新增network.publish_host: 127.0.0.1, 整体配置文件如下:
[root@e33229400bf1 config]# cat elasticsearch.yml
cluster.name: “docker-cluster”
network.host: 0.0.0.0
network.publish_host: 127.0.0.1 # 新增配置项