elastic.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package utils
  2. import (
  3. "github.com/olivere/elastic/v7"
  4. "log"
  5. "os"
  6. "time"
  7. )
  8. var Client *elastic.Client
  9. const (
  10. ES_URL = "http://es-cn-nif227b580019rgw6.public.elasticsearch.aliyuncs.com:9200" //<1>
  11. ES_USERNAME = "elastic" //<2>
  12. ES_PASSWORD = "hongze@2021" //<3>
  13. )
  14. func init() {
  15. errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
  16. file := ""
  17. if RunMode == "release" {
  18. //file = `/data/rdlucklog/hongze_cygx/eslog.log`
  19. file = `./rdlucklog/eslog.log`
  20. } else {
  21. file = `./rdlucklog/eslog.log`
  22. }
  23. logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
  24. client, err := elastic.NewClient(
  25. elastic.SetURL(ES_URL),
  26. elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
  27. elastic.SetTraceLog(log.New(logFile, "ES-TRACE: ", 0)),
  28. elastic.SetSniff(false), elastic.SetErrorLog(errorlog))
  29. //elastic.SetSniff(false))
  30. Client = client
  31. if err != nil {
  32. go SendAlarmMsg("ElasticSearch连接失败", 2)
  33. go SendEmail("ElasticSearch连接失败"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), EmailSendToUsers)
  34. }
  35. return
  36. }