栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

golang eshook 自定义elastic client 使用https连接es

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

golang eshook 自定义elastic client 使用https连接es

前情提要

因为es从http升级到https了,所以需要修改golang程序里的log钩子,在新建elastic client时设置为https

官方给出的方法见:https://github.com/olivere/elastic/issues/738

elastic包使用的是默认的httpclient 需要传入自定义的httpclient

主要是设置http.client的 Transport 字段

代码实现
func newEsHook(cc cfg) *esHook {
	// https
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr} // 自定义transport
	//
	es, err := elastic.NewClient(
		elastic.SetHttpClient(client), // 设置上面自己的client
		elastic.SetURL(cc.EsAddrs...),
		elastic.SetBasicAuth(cc.EsUser, cc.EsPassword),
		elastic.SetSniff(false),
		elastic.SetHealthcheckInterval(15*time.Second),
		elastic.SetErrorLog(log.New(os.Stderr, "ES:", log.LstdFlags)),
	)
	if err != nil {
		log.Println("failed to create Elastic V6 Client: ", err)
	}
	return &esHook{client: es, cmd: strings.Join(os.Args, " ")}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985947.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号