consul服务注册与发现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Consul;
namespace consulTest
{
class Program
{
static void Main(string[] args)
{
//注册Consul
string ip = "127.0.0.1";
string port = "8080";
string serviceName = "MsgService";
string serviceId = serviceName + Guid.NewGuid();
using (var consulClient = new ConsulClient(ConsulConfig))
{
var agentServer = new AgentServiceRegistration
{
Address = ip,
Port = Convert.ToInt32(port),
ID = serviceId,
Name = serviceName,
Check = new AgentServiceCheck
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),
HTTP = $"http://www.baidu.com",
Interval = TimeSpan.FromSeconds(10),
Timeout = TimeSpan.FromSeconds(5),
},
};
consulClient.Agent.ServiceRegister(agentServer).Wait();
Console.WriteLine("服务注册成功");
var serverDic = consulClient.Agent.Services().Result.Response;
foreach (var s in serverDic.Values)
{
Console.WriteLine($"ID={s.ID},Service={s.Service},Addr={s.Address},Port={s.Port}");
}
Console.ReadLine();
}
}
//Consul 配置委托
private static void ConsulConfig(ConsulClientConfiguration config)
{
config.Address = new Uri("http://127.0.0.1:8500"); //Demo硬编码Consul的地址
config.Datacenter = "dc1";
}
}
}
转载于//www.cnblogs.com/huangzelin/p/10610078.html
还没有评论,来说两句吧...