public string Register(string ServiceName, string SrvInfo) { try { Etcdserverpb.LeaseGrantRequest request = new Etcdserverpb.LeaseGrantRequest(); request.TTL = _locklease; var lease = _client.LeaseGrant(request); var newkey = Google.Protobuf.ByteString.CopyFromUtf8($"/EtcdDiscovery/{ServiceName}"); var newval = Google.Protobuf.ByteString.CopyFromUtf8(SrvInfo); _client.PutAsync(new Etcdserverpb.PutRequest() { Key = newkey, Value = newval, Lease = lease.ID }).Wait(); lock (watchers) { watchers.Add(new zkDiscoveryWatcher() { key = newkey, value = newval }); } return($"/EtcdDiscovery/{ServiceName}"); } catch (Exception ex) { throw ex; } }
public Server(string host, int port, int locklease = 10000, string username = "", string password = "", string caCert = "", string clientCert = "", string clientKey = "", bool publicRootCa = false) { _locklease = locklease / 1000; _client = new dotnet_etcd.EtcdClient(host, port, username, password, caCert, clientCert, clientKey, publicRootCa); //过期延期部分 Task.Run(() => { while (!IsDisposing) { Dictionary <Google.Protobuf.ByteString, Google.Protobuf.ByteString> lockers = null; lock (watchers) { lockers = watchers.ToDictionary(c => c.key, c => c.value); } if (IsDisposing) { return; } Etcdserverpb.LeaseGrantRequest request = new Etcdserverpb.LeaseGrantRequest(); request.TTL = _locklease; var lease = _client.LeaseGrant(request); foreach (var item in lockers) { _client.Put(new Etcdserverpb.PutRequest() { Key = item.Key, Value = item.Value, Lease = lease.ID }); } //休眠 TTL/2 时间,防止执行过程导致的node丢失 System.Threading.Thread.Sleep(_locklease * 500); } }); }
public Client(string host, int port, int locklease = 10, string username = "", string password = "", string caCert = "", string clientCert = "", string clientKey = "", bool publicRootCa = false) { _client = new dotnet_etcd.EtcdClient(host, port, username, password, caCert, clientCert, clientKey, publicRootCa); _locklease = locklease; //获取集群中全部客户端制作客户端列表 foreach (var item in _client.MemberList(new Etcdserverpb.MemberListRequest()).Members) { if (item.ClientURLs.Count > 0) { string[] baseurl = item.ClientURLs[0].Substring(item.ClientURLs[0].LastIndexOf("/") + 1).Split(':'); string clienthost = baseurl[0] == "0.0.0.0" ? host : baseurl[0]; int clientport = int.Parse(baseurl[1]); _clients.Add(new dotnet_etcd.EtcdClient(clienthost, clientport, username, password, caCert, clientCert, clientKey, publicRootCa)); } } //后台自延期部分 System.Threading.Tasks.Task.Run(() => { while (!IsDisposing) { //获取客户端全部node Dictionary <Google.Protobuf.ByteString, Google.Protobuf.ByteString> lockers = null; lock (_lockerList) { lockers = _lockerList .ToDictionary(c => Google.Protobuf.ByteString.CopyFromUtf8(c.Name) , c => Google.Protobuf.ByteString.CopyFromUtf8(c.Id.ToString())); } //生成lease Etcdserverpb.LeaseGrantRequest request = new Etcdserverpb.LeaseGrantRequest(); request.TTL = _locklease; var lease = _client.LeaseGrant(request); //更新全部node foreach (var item in lockers) { _client.Put(new Etcdserverpb.PutRequest() { Key = item.Key, Value = item.Value, Lease = lease.ID }); } //休眠 TTL/2 时间,防止执行过程导致的node丢失 System.Threading.Thread.Sleep(_locklease * 500); } }); }
public LockModel Lock(string lockname) { LockModel @lock = new LockModel(); Etcdserverpb.RangeResponse oldlockers = null; //use local cache speed up. lock (_lockerList) { var locallst = _lockerList.Where(c => c.Name == lockname).ToList(); if (locallst.Count > 0) { @lock.Result = LockResult.LockExists; @lock.Id = locallst.FirstOrDefault().Id; return(@lock); } } //confirm lock not exists. try { oldlockers = _client.GetAsync($"/locks/{lockname}").Result; } catch (Exception ex) { @lock.Id = 0; @lock.Result = LockResult.Fail; lock (_client) { _client = bestClient(_clients); } return(@lock); } //return lockid to locker . if (oldlockers?.Kvs.Count > 0) { @lock.Id = long.Parse(oldlockers.Kvs.FirstOrDefault().Value.ToStringUtf8()); @lock.Result = LockResult.LockExists; } else { //creat lock id TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); @lock.Id = (long)ts.TotalMilliseconds; @lock.Result = LockResult.Success; try { Etcdserverpb.LeaseGrantRequest request = new Etcdserverpb.LeaseGrantRequest(); request.TTL = _locklease; var lease = _client.LeaseGrant(request); Etcdserverpb.PutRequest put = new Etcdserverpb.PutRequest(); put.Key = Google.Protobuf.ByteString.CopyFromUtf8(($"/locks/{lockname}")); put.Value = Google.Protobuf.ByteString.CopyFromUtf8(@lock.Id.ToString()); put.Lease = lease.ID; _client.Put(put); //add to cache and srv . _client.Put($"/locks/{lockname}", @lock.Id.ToString()); lock (_lockerList) { _lockerList.Add(new ZkLockerWatcher() { Event = null, Id = @lock.Id, Name = lockname }); } } catch (Exception ex) { lock (_client) { _client = bestClient(_clients); } @lock.Result = LockResult.Fail; } } return(@lock); }