ASP.NET Core+Consul+Ocleot 出错
一、错误: Index was out of range.……
1.1 Consul服务注入正常
public static void RegisterConsul()
{
ConsulClient client = new ConsulClient(options =>
{
options.Address = new Uri("http://localhost:8500");
options.Datacenter = "dc1";
});
client.Agent.ServiceRegister(new AgentServiceRegistration()
{
ID = "ApiService1",
Name = "Api",
Address="http://localhost:5002/api/values",
Tags = new string[] { "1" },
Check = new AgentServiceCheck()
{
Interval = TimeSpan.FromSeconds(10),
HTTP = $"http://localhost:5002/api/values",
Timeout = TimeSpan.FromSeconds(20),
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(60)
}
});
}
2.2 Ocelot报错: Index was out of range.……
2.3 找出错误:原来是注入方式错误,更改注入方式
public static void RegisterConsul()
{
ConsulClient client = new ConsulClient(options =>
{
options.Address = new Uri("http://localhost:8500");
options.Datacenter = "dc1";
});
client.Agent.ServiceRegister(new AgentServiceRegistration()
{
ID = "ApiService1",
Name = "Api",
Address = "localhost",
Port =5002,
Tags = new string[] { "1" },
Check = new AgentServiceCheck()
{
Interval = TimeSpan.FromSeconds(10),
HTTP = $"http://localhost:5002/api/values",
Timeout = TimeSpan.FromSeconds(20),
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(60)
}
});
}
三、Ocelot网关终于正常访问