consul服务注册与发现

男娘i 2022-01-06 09:23 404阅读 0赞
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Consul;
  7. namespace consulTest
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //注册Consul
  14. string ip = "127.0.0.1";
  15. string port = "8080";
  16. string serviceName = "MsgService";
  17. string serviceId = serviceName + Guid.NewGuid();
  18. using (var consulClient = new ConsulClient(ConsulConfig))
  19. {
  20. var agentServer = new AgentServiceRegistration
  21. {
  22. Address = ip,
  23. Port = Convert.ToInt32(port),
  24. ID = serviceId,
  25. Name = serviceName,
  26. Check = new AgentServiceCheck
  27. {
  28. DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),
  29. HTTP = $"http://www.baidu.com",
  30. Interval = TimeSpan.FromSeconds(10),
  31. Timeout = TimeSpan.FromSeconds(5),
  32. },
  33. };
  34. consulClient.Agent.ServiceRegister(agentServer).Wait();
  35. Console.WriteLine("服务注册成功");
  36. var serverDic = consulClient.Agent.Services().Result.Response;
  37. foreach (var s in serverDic.Values)
  38. {
  39. Console.WriteLine($"ID={s.ID},Service={s.Service},Addr={s.Address},Port={s.Port}");
  40. }
  41. Console.ReadLine();
  42. }
  43. }
  44. //Consul 配置委托
  45. private static void ConsulConfig(ConsulClientConfiguration config)
  46. {
  47. config.Address = new Uri("http://127.0.0.1:8500"); //Demo硬编码Consul的地址
  48. config.Datacenter = "dc1";
  49. }
  50. }
  51. }

转载于:https://www.cnblogs.com/huangzelin/p/10610078.html

发表评论

表情:
评论列表 (有 0 条评论,404人围观)

还没有评论,来说两句吧...

相关阅读