#region 调用---负载均衡
{
string url = "http://{域名}/api/users/get";
Uri uri = new Uri(url);
string groupName = uri.Host;
using (ConsulClient client = new ConsulClient(c =>
{
c.Address = new Uri("http://localhost:8500/");
c.Datacenter = "dc1";
}))
{
var dictionary = client.Agent.Services().Result.Response;
var list = dictionary.Where(k => k.Value.Service.Equals(groupName, StringComparison.OrdinalIgnoreCase));
KeyValuePair<string, AgentService> keyValuePair = new KeyValuePair<string, AgentService>();
{
List<KeyValuePair<string, AgentService>> pairsList = new List<KeyValuePair<string, AgentService>>();
foreach (var pair in list)
{
int count = int.Parse(pair.Value.Tags?[0]);
for (int i = 0; i < count; i++)
{
pairsList.Add(pair);
}
}
keyValuePair = pairsList.ToArray()[new Random(iSeed++).Next(0, pairsList.Count())];
}
resultUrl = $"{uri.Scheme}://{keyValuePair.Value.Address}:{keyValuePair.Value.Port}{uri.PathAndQuery}";
string result = WebApiHelperExtend.InvokeApi(resultUrl);
userList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Users>>(result);
}
}
#endregion