注: go-es包使用的是https://github.com/olivere/elastic

当es配置了ssl,但是证书是自签证书,那可能在操作初始化es-client时候会报这样错误

certificate signed by unknown authority

这个问题我在issue中找到了答案

可以通过配置httpclient来忽略ssl验证

最终改造结果如下

1
2
3
4
var client *elastic.Client
var host = "https://192.168.56.103:9200"
var userName = "elastic"
var passWord = "xiaowu"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var err error
httpClient := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
client, err = elastic.NewClient(
elastic.SetHttpClient(httpClient),
elastic.SetURL(host),
elastic.SetScheme("https"),
elastic.SetSniff(false),
elastic.SetBasicAuth(userName,passWord),
)
if err != nil {
panic(err)
}
_, _, err = client.Ping(host).Do(context.Background())
if err != nil {
panic(err)
}
_, err = client.ElasticsearchVersion(host)
if err != nil {
panic(err)
}