文档首页/ 云搜索服务 CSS/ 用户指南/ Elasticsearch/ 访问Elasticsearch集群/ 通过Go客户端接入Elasticsearch集群
更新时间:2026-01-09 GMT+08:00
分享

通过Go客户端接入Elasticsearch集群

在使用CSS服务的Elasticsearch集群时,开发者可能会使用Go语言进行数据查询和管理。本文将详细介绍通过Go语言访问Elasticsearch集群的配置说明。

前提条件

  • Elasticsearch集群处于可用状态。
  • 运行Go语言的服务器与Elasticsearch集群之间网络互通。
  • 根据集群的网络配置方式,获取集群的访问地址,操作指导请参见网络配置
  • 确认服务器已安装Elasticsearch Go客户端,操作指导请参见Elasticsearch Go Client

连接非安全集群

连接非安全集群,示例代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package main

import (
	"github.com/elastic/go-elasticsearch/v7"
	"log"
)

func main() {
	cfg := elasticsearch.Config{
		Addresses: []string{
			"http://HOST:9200/",
		},
	}

	es, _ := elasticsearch.NewClient(cfg)
	log.Println(es.Info())
}

其中,HOST为集群节点的内网IP地址。

连接HTTP协议的安全集群

连接使用HTTP协议的安全集群,示例代码如下:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package main

import (
	"github.com/elastic/go-elasticsearch/v7"
	"log"
)

func main() {
	cfg := elasticsearch.Config{
		Addresses: []string{
			"http://HOST:9200/",
		},
		Username: "USERNAME",
		Password: "PASSWORD",
	}

	es, _ := elasticsearch.NewClient(cfg)
	log.Println(es.Info())
}
表1 函数中的变量说明

参数

描述

HOST

集群访问地址,当存在多个IP地址时,中间用“,”隔开。

USERNAME

访问集群的用户名。

PASSWORD

用户名对应的密码。

连接HTTPS协议的安全集群(无证书)

在不加载安全证书的情况下连接使用HTTPS协议的安全集群,示例代码如下:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
	"crypto/tls"
	"github.com/elastic/go-elasticsearch/v7"
	"log"
	"net/http"
)

func main() {
	cfg := elasticsearch.Config{
		Addresses: []string{
			"https://HOST:9200/",
		},
		Username: "USERNAME",
		Password: "PASSWORD",
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		},
	}

	es, _ := elasticsearch.NewClient(cfg)
	log.Println(es.Info())
}
表2 函数中的变量说明

参数

描述

HOST

集群访问地址,当存在多个IP地址时,中间用“,”隔开。

USERNAME

访问集群的用户名。

PASSWORD

用户名对应的密码。

连接HTTPS协议的安全集群(加载证书)

在加载安全证书的情况下连接使用HTTPS协议的安全集群,示例代码如下:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main

import (
	"crypto/tls"
	"crypto/x509"
	"flag"
	"github.com/elastic/go-elasticsearch/v7"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"time"
)

func main() {
	insecure := flag.Bool("insecure-ssl", false, "Accept/Ignore all server SSL certificates")
	flag.Parse()

	// Get the SystemCertPool, continue with an empty pool on error
	rootCAs, _ := x509.SystemCertPool()
	if rootCAs == nil {
		rootCAs = x509.NewCertPool()
	}

	// Read in the cert file
	certs, err := ioutil.ReadFile("/tmp/CloudSearchService.cer")
	if err != nil {
		log.Fatalf("Failed to append %q to RootCAs: %v", "xxx", err)
	}

	// Append our cert to the system pool
	if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
		log.Println("No certs appended, using system certs only")
	}

	config := elasticsearch.Config{
		Addresses: []string{
			"https://HOST:9200/",
		},
		Username: "USERNAME",
		Password: "PASSWORD",
		Transport: &http.Transport{
			MaxIdleConnsPerHost:   10,
			ResponseHeaderTimeout: time.Second,
			DialContext: (&net.Dialer{
				Timeout:   30 * time.Second,
				KeepAlive: 30 * time.Second,
			}).DialContext,
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: *insecure,
				RootCAs:            rootCAs,
			},
		},
	}
	es, _ := elasticsearch.NewClient(config)
	log.Println(elasticsearch.Version)
	log.Println(es.Info())
}
表3 函数中的变量说明

参数

描述

HOST

集群访问地址,当存在多个IP地址时,中间用“,”隔开。

USERNAME

访问集群的用户名。

PASSWORD

用户名对应的密码。

运行代码

根据集群类型将以上适配的代码写入到“EsTest.gc”文件并存放到一个单独的目录,在该目录执行以下命令运行代码。

go env -w GO111MODULE=on
go env -w GOPROXY=https://repo.huaweicloud.com/repository/goproxy/ 
go env -w GONOSUMDB=*

go mod init test
go mod tidy
go run EsTest.go

当成功返回集群信息时,表示集群连接成功。

相关文档