elastic.go 747 B

123456789101112131415161718192021222324252627282930313233343536
  1. package utils
  2. import (
  3. "github.com/olivere/elastic/v7"
  4. "log"
  5. "os"
  6. )
  7. // EsClient es客户端
  8. var EsClient *elastic.Client
  9. func initEs() {
  10. var logInfo *log.Logger
  11. if RunMode == `debug` {
  12. logInfo = log.New(os.Stderr, "", log.LstdFlags)
  13. }
  14. clientOptionFuncList := []elastic.ClientOptionFunc{
  15. elastic.SetURL(ES_URL),
  16. elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
  17. elastic.SetSniff(false),
  18. }
  19. if logInfo != nil {
  20. clientOptionFuncList = append(clientOptionFuncList, elastic.SetTraceLog(logInfo))
  21. }
  22. client, err := elastic.NewClient(
  23. clientOptionFuncList...,
  24. )
  25. EsClient = client
  26. if err != nil {
  27. panic("ElasticSearch连接失败,err:" + err.Error())
  28. //go alarm_msg.SendAlarmMsg("ElasticSearch连接失败", 2)
  29. }
  30. return
  31. }