using Grpc.Extension.Abstract; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; namespace Grpc.Extension.Client.LoadBalancer { /// /// 轮询负载 /// public class RoundLoadBalancer : ILoadBalancer { private ConcurrentDictionary _serviceInvokeIndexs = new ConcurrentDictionary(); /// /// 轮询获取Endpoint /// /// /// /// public string SelectEndpoint(string serviceName, List endpoints) { endpoints = endpoints.OrderBy(q => q).ToList(); var index = _serviceInvokeIndexs.GetOrAdd(serviceName, 0); if (index >= endpoints.Count) { index = _serviceInvokeIndexs.AddOrUpdate(serviceName, 0, (k, v) => 0); } _serviceInvokeIndexs.AddOrUpdate(serviceName, index, (k, v) => v + 1); return endpoints.ElementAt(index); } } }