elastic.go 1.2 KB

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