nats.go 469 B

12345678910111213141516171819202122
  1. package options
  2. import (
  3. "github.com/nats-io/nats.go"
  4. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/log"
  5. )
  6. type NatsOption struct {
  7. Enable bool `json:"enable" mapstructure:"enable"`
  8. Url string `json:"url" mapstructure:"url"`
  9. }
  10. func InitNats(natsOption NatsOption) *nats.Conn {
  11. if !natsOption.Enable {
  12. return nil
  13. }
  14. conn, err := nats.Connect("nats://" + natsOption.Url)
  15. if err != nil {
  16. log.Panicf("初始化NATS失败:%s", err)
  17. }
  18. return conn
  19. }