micro.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package toolkit
  2. import (
  3. "time"
  4. "github.com/go-kit/kit/log"
  5. consulsd "github.com/go-kit/kit/sd/consul"
  6. consulapi "github.com/hashicorp/consul/api"
  7. )
  8. // ServiceOptions server options
  9. type ServiceOptions struct {
  10. // Address is the address of the Consul server
  11. Address string
  12. // Scheme is the URI scheme for the Consul server
  13. Scheme string
  14. // Datacenter to use. If not provided, the default agent datacenter is used.
  15. Datacenter string
  16. // Transport is the Transport to use for the http client.
  17. //Transport *http.Transport
  18. // HttpClient is the client to use. Default will be
  19. // used if not provided.
  20. //HttpClient *http.Client
  21. // HttpAuth is the auth info to use for http access.
  22. //HttpAuth *HttpBasicAuth
  23. // WaitTime limits how long a Watch will block. If not provided,
  24. // the agent default values will be used.
  25. WaitTime time.Duration
  26. // Token is used to provide a per-request ACL token
  27. // which overrides the agent's default token.
  28. Token string
  29. //TLSConfig TLSConfig
  30. }
  31. // NewConsulInstancer consul Instancer
  32. func NewConsulInstancer(options ServiceOptions, name string, tags []string, passingOnly bool, logger log.Logger) (instancer *consulsd.Instancer, err error) {
  33. var client consulsd.Client
  34. config := consulapi.DefaultConfig()
  35. config.Address = options.Address
  36. config.Scheme = options.Scheme
  37. config.Datacenter = options.Datacenter
  38. config.WaitTime = options.WaitTime
  39. config.Token = options.Token
  40. consulClient, err := consulapi.NewClient(config)
  41. if err != nil {
  42. //logger.Log("err", err)
  43. return
  44. }
  45. client = consulsd.NewClient(consulClient)
  46. instancer = consulsd.NewInstancer(client, logger, name, tags, passingOnly)
  47. return
  48. }