You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2KB

  1. package discover
  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/tal-tech/go-zero/core/logx"
  8. "os"
  9. "strconv"
  10. )
  11. func Register(consulHost, consulPort, svcHost, svcPort, svrId, svrName 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. logx.Error("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: svrId,
  35. Name: "prometheus-" + svrName,
  36. Address: svcHost,
  37. Port: port,
  38. Tags: []string{svrName, "JobRiskBizServices"},
  39. Check: &check,
  40. }
  41. // 执行注册
  42. registar = consul.NewRegistrar(client, &reg, logger)
  43. return
  44. }