6350777be35a82aeeb9688637d34c49dc717d64d.svn-base 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package main
  2. import (
  3. "github.com/go-kit/kit/log"
  4. "github.com/go-kit/kit/sd"
  5. "github.com/go-kit/kit/sd/consul"
  6. "github.com/hashicorp/consul/api"
  7. "github.com/pborman/uuid"
  8. "os"
  9. "strconv"
  10. )
  11. func Register(consulHost, consulPort, svcHost, svcPort string, logger log.Logger) (registar sd.Registrar) {
  12. // 创建Consul客户端连接
  13. var client consul.Client
  14. {
  15. consulCfg := api.DefaultConfig()
  16. consulCfg.Address = consulHost + ":" + consulPort
  17. consulClient, err := api.NewClient(consulCfg)
  18. if err != nil {
  19. logger.Log("create consul client error:", err)
  20. os.Exit(1)
  21. }
  22. client = consul.NewClient(consulClient)
  23. }
  24. // 设置Consul对服务健康检查的参数
  25. check := api.AgentServiceCheck{
  26. HTTP: "http://" + svcHost + ":" + svcPort + "/health",
  27. Interval: "10s",
  28. Timeout: "1s",
  29. Notes: "Consul check service health status.",
  30. }
  31. port, _ := strconv.Atoi(svcPort)
  32. //设置微服务想Consul的注册信息
  33. reg := api.AgentServiceRegistration{
  34. ID: "string-service" + uuid.New(),
  35. Name: "string-service",
  36. Address: svcHost,
  37. Port: port,
  38. Tags: []string{"string-service"},
  39. Check: &check,
  40. }
  41. // 执行注册
  42. registar = consul.NewRegistrar(client, &reg, logger)
  43. return
  44. }