EsClient.go 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. package init_serve
  2. import (
  3. "github.com/olivere/elastic/v7"
  4. "hongze/hongze_yb/global"
  5. "log"
  6. "os"
  7. )
  8. func NewEsClient() {
  9. esConfig := global.CONFIG.EsClient
  10. client, err := elastic.NewClient(
  11. elastic.SetURL(esConfig.Endpoints), // 支持多个地址,用逗号分隔即可
  12. elastic.SetBasicAuth(esConfig.Username, esConfig.Password), // 基于http base auth 验证机制的账号密码
  13. elastic.SetSniff(false), // 是否应该定期检查集群
  14. elastic.SetHealthcheck(false), // 是否开启健康检查
  15. // elastic.SetInfoLog(newEsLog(esConfig.Log+"/es_info.log", "Info")),
  16. // elastic.SetErrorLog(newEsLog(esConfig.Log+"/es_error.log", "Error")),
  17. // elastic.SetTraceLog(newEsLog(esConfig.Log+"/es_trace.log", "trace")),
  18. )
  19. if err != nil {
  20. global.LOG.Error("newEsClient init error,err=", err)
  21. panic("newEsClient 初始化失败:" + err.Error())
  22. }
  23. //全局赋值redis链接
  24. global.EsClient = client
  25. }
  26. func newEsLog(file string, level string) *log.Logger {
  27. logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
  28. return log.New(logFile, "ES-"+level+": ", 0)
  29. }