private static async Task StartService(string host, int port) { //服务信息 var serviceInfo = new ServiceInformation() { Host = host, Port = port, Description = "我的小服务, 老火了", Key = "Foo.Services", Name = "我的小服务", ServiceType = ServiceType.Grpc, }; //启动服务 Server service = new Server { Services = { Greeter.BindService(new GreeterImpl()) }, Ports = { new ServerPort(serviceInfo.Host, port, ServerCredentials.Insecure) } }; service.Start(); using (IZookeeperClient zon = await ServiceBus.Register(serviceInfo)) { Console.WriteLine("Greeter server listening on port " + port); Console.WriteLine("Press any key to stop the server..."); Console.ReadKey(); service.ShutdownAsync().Wait(); } }
internal async Task HandleNodeDataChange(IZookeeperClient client, NodeDataChangeArgs args) { var eventType = args.Type; byte[] nodeData = null; if (args.CurrentData != null && args.CurrentData.Any()) { nodeData = args.CurrentData.ToArray(); } switch (eventType) { case Watcher.Event.EventType.NodeDeleted: var serviceId = Path.Split("/").Last(); _serviceRouteCache.RemoveCache(serviceId); break; case Watcher.Event.EventType.NodeCreated: Check.NotNullOrEmpty(nodeData, nameof(nodeData)); var createdJsonString = nodeData.GetString(); var createdServiceRouteDescriptor = _serializer.Deserialize <ServiceRouteDescriptor>(createdJsonString); _serviceRouteCache.UpdateCache(createdServiceRouteDescriptor); break; case Watcher.Event.EventType.NodeDataChanged: Check.NotNullOrEmpty(nodeData, nameof(nodeData)); var updateJsonString = nodeData.GetString(); var updateServiceRouteDescriptor = _serializer.Deserialize <ServiceRouteDescriptor>(updateJsonString); _serviceRouteCache.UpdateCache(updateServiceRouteDescriptor); break; } }
/// <summary> /// 构造函数 /// </summary> /// <param name="zookeeperConfig">Zookeeper配置</param> /// <param name="loggerFactory">日志工厂</param> /// <param name="serviceProxyCreator">服务代理构造器</param> /// <param name="nodeClientManager">NodeClient管理器</param> public ServiceSubscriber(ZookeeperConfig zookeeperConfig, ILoggerFactory loggerFactory, IServiceProxyCreator serviceProxyCreator, INodeClientManager nodeClientManager) { BasePath = zookeeperConfig.BasePath; logger = loggerFactory.CreateLogger <ServiceSubscriber>(); this.serviceProxyCreator = serviceProxyCreator; this.nodeClientManager = nodeClientManager; ZooKeeper.LogLevel = System.Diagnostics.TraceLevel.Error; try { client = new ZookeeperClient(new ZookeeperClientOptions() { ConnectionString = zookeeperConfig.ConnectionString, ConnectionTimeout = zookeeperConfig.ConnectionTimeout, OperatingTimeout = zookeeperConfig.OperatingTimeout, EnableEphemeralNodeRestore = true, ReadOnly = zookeeperConfig.ReadOnly, SessionId = zookeeperConfig.SessionId, SessionPasswd = zookeeperConfig.SessionPasswd, SessionTimeout = zookeeperConfig.SessionTimeout }); } catch (Exception ex) { logger.LogError(ex, $"Connect zookeeper failed. ConnectionString={zookeeperConfig.ConnectionString}"); throw ex; } logger.LogDebug($"Connect zookeeper success. ConnectionString={zookeeperConfig.ConnectionString}"); }
private async Task CreateSubdirectory(IZookeeperClient zooKeeperClient, string path) { if (await zooKeeperClient.ExistsAsync(path)) { return; } if (_logger.IsEnabled(LogLevel.Information)) { _logger.LogInformation($"节点{path}不存在,将进行创建。"); } var childrens = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var nodePath = "/"; foreach (var children in childrens) { nodePath += children; if (!await zooKeeperClient.ExistsAsync(nodePath)) { await zooKeeperClient.CreateAsync(nodePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } nodePath += "/"; } }
public KafkaTopic(IZookeeperConnection zkConnect, string name) { _zkConnect = zkConnect; _name = name; _zkClient = zkConnect.CreateClient(); _producer = _zkClient.CreateProducer <string, KafkaMessage>(); }
public ZookeeperListenerAgentStartup(IOptions <ZookeeperListenerOptions> options, IExtractCommand extractCommand) { _extractCommand = extractCommand; _options = options.Value; _client = new ZookeeperClient(_options); ListenerPath = $"/Bucket.Listener/{_options.ListenerKey}"; }
public ZookeeperClientTests() { _client = new ZookeeperClient(new ZookeeperClientOptions("127.0.0.1:2181") { SessionTimeout = TimeSpan.FromSeconds(20), OperatingTimeout = TimeSpan.FromSeconds(30) }); }
public ZKCacheDependency(ZookeeperClientOptions option) { if (option == null || string.IsNullOrWhiteSpace(option.ConnectionString)) { throw new ArgumentException("无效的zookeeper配置参数,请检查配置!"); } _zkClient = new ZookeeperClient(option); }
private static IZookeeperClient GetZooKeeper() { if (_zooKeeper == null) { _zooKeeper = new ZookeeperClient(new ZookeeperClientOptions("localhost:2181")); } return(_zooKeeper); }
public ZookeeperClientTests() { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); _client = new Client(new ZookeeperClientOptions("localhost:32776,localhost:32775,localhost:32774") { SessionTimeout = TimeSpan.FromSeconds(20), OperatingTimeout = TimeSpan.FromSeconds(30) }); }
public async Task SubscribeChildrenChange(IZookeeperClient client, NodeChildrenChangeArgs args) { var currentChildrens = args.CurrentChildrens; foreach (var child in currentChildrens) { await _zookeeperServiceRouteManager.CreateSubscribeDataChange(client, child); } }
public ZookeeperClientTests() { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); _client = new ZookeeperClient(new ZookeeperClientOptions("172.18.20.132:2181") { SessionTimeout = TimeSpan.FromSeconds(20), OperatingTimeout = TimeSpan.FromSeconds(30) }); }
private static async Task CheckOrCreateRoot(IZookeeperClient zk) { bool exists = await zk.ExistsAsync(Root); if (!exists) { await zk.CreatePersistentAsync(Root, GetData("this is service register root")); } }
public DataChangeWatcher(IZookeeperClient keeper, string path, Action <DataWatchContext> action) { Context = new DataWatchContext() { Client = keeper, Path = path }; _action = action; }
/// <summary> /// 构造函数 /// </summary> /// <param name="identity">当前锁的唯一标识</param> public ZKLockProvider(string identity) : base(identity) { ZKConfig = ZKConfig.New(); _zkClient = new ZookeeperClient(ZKConfig.ConnStr); if (_zkClient.ExistsAsync(NodeName).Result == false) { _zkClient.CreateRecursiveAsync(NodeName, ZKConfig.LockZKValue.Bytes()); } }
public static async Task <bool> StrictExistsAsync(this IZookeeperClient zookeeperClient, string path) { try { return(await zookeeperClient.ExistsAsync(path)); } catch (Exception ex) { return(false); } }
public KafkaTopic(IZookeeperConnection zkConnect, string name, ProducerConfig producerConfig = null) { if (producerConfig == null) { producerConfig = ProducerConfig.GetDefaultConfig(); } _zkConnect = zkConnect; _name = name; _zkClient = zkConnect.CreateClient(); _producer = _zkClient.CreateProducer <string, KafkaMessage>(producerConfig); }
/// <summary> /// Disposing unused resources. /// </summary> /// <param name="disposing">Whether need to dispose the resources.</param> protected virtual void Dispose(bool disposing) { if (disposing) { if (this.ZookeeperClient != null) { this.ZookeeperClient.Dispose(); } this.ZookeeperClient = null; } }
/// <summary> /// 构造函数 /// </summary> /// <param name="connectionString">ZK地址,多个以“,”分割</param> /// <param name="SessionTimeout">会话超时时间(Second)</param> public ZooKeeperClient(string connectionString, double SessionTimeout = 20) { client = new ZookeeperClient(new ZookeeperClientOptions(connectionString) { BasePath = "/", //default value ConnectionTimeout = TimeSpan.FromSeconds(10), //default value SessionTimeout = TimeSpan.FromSeconds(SessionTimeout), //default value OperatingTimeout = TimeSpan.FromSeconds(60), //default value ReadOnly = false, //default value SessionId = 0, //default value SessionPasswd = null, //default value EnableEphemeralNodeRestore = true //default value }); }
public ZookeeperRegistry(IZookeeperClient zkClient) { client = zkClient; zooKeeper = client.ZooKeeper; client.SubscribeStatusChange((self, args) => { if (self.ZooKeeper == zooKeeper) { return(Task.CompletedTask); } zooKeeper = self.ZooKeeper; return(Recover()); }); }
public ZookeeperProvider(ZookeeperSource source) { _source = source; _client = new ZookeeperClient(new ZookeeperClientOptions(source.ConnectionString) { BasePath = "/", ConnectionTimeout = TimeSpan.FromSeconds(10), SessionTimeout = TimeSpan.FromMilliseconds(source.SessionTimeout), OperatingTimeout = TimeSpan.FromSeconds(60), ReadOnly = false, SessionId = 0, SessionPasswd = null, EnableEphemeralNodeRestore = true }); }
/// <summary> /// Create Zookeeper client from config. /// </summary> /// <returns>Zookeeper client</returns> private IZookeeperClient GetZookeeperClient() { if (_zookeeperClient == null) { lock (SyncObject) { if (_zookeeperClient == null) { _zookeeperClient = ZookeeperFactory.CreateClient(this._currentEnvironment.Location); } } } return(_zookeeperClient); }
private async Task <bool> SetRouteAsync(ServiceRouteDescriptor route, IZookeeperClient zooKeeperClient) { try { bool isSetRoute = false; _logger.LogDebug($"准备添加{route.ServiceDescriptor.Id}服务路由。"); var zooKeeperClients = await _zookeeperClientProvider.GetZooKeeperClients(); await CreateSubdirectory(zooKeeperClient, _configInfo.RoutePath); var path = _configInfo.RoutePath; if (!path.EndsWith("/")) { path += "/"; } var nodePath = $"{path}{route.ServiceDescriptor.Id}"; var nodeData = _serializer.Serialize(route); _logger.LogDebug($"服务路由内容为:{Encoding.UTF8.GetString(nodeData)}。"); if (!nodeWatchers.ContainsKey(nodePath)) { var watcher = nodeWatchers.GetOrAdd(nodePath, f => new NodeMonitorWatcher(path, async(oldData, newData) => await NodeChange(oldData, newData))); await zooKeeperClient.SubscribeDataChange(nodePath, watcher.HandleNodeDataChange); } if (!await zooKeeperClient.ExistsAsync(nodePath)) { _logger.LogDebug($"节点:{nodePath}不存在将进行创建。"); await zooKeeperClient.CreateAsync(nodePath, nodeData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { var onlineData = (await zooKeeperClient.GetDataAsync(nodePath)).ToArray(); if (!DataEquals(nodeData, onlineData)) { await zooKeeperClient.SetDataAsync(nodePath, nodeData); _logger.LogDebug($"{nodePath}节点的缓存的服务路由与服务注册中心不一致,路由数据已被更新。"); isSetRoute = true; } } return(isSetRoute); } catch (Exception ex) { _logger.LogError($"{route.ServiceDescriptor.Id}服务的路由注册失败,原因:{ex.Message}"); return(false); } }
public static async Task CreateRecursiveAsync(this IZookeeperClient client, string path, byte[] data, List <ACL> acls, CreateMode createMode) { try { await client.CreateAsync(path, data, acls, createMode); } catch (KeeperException.NodeExistsException) { } catch (KeeperException.NoNodeException) { var parentDir = path.Substring(0, path.LastIndexOf('/')); await CreateRecursiveAsync(client, parentDir, null, acls, CreateMode.PERSISTENT); await client.CreateAsync(path, data, acls, createMode); } }
public static async Task <IZookeeperClient> Register(ServiceInformation service) { IZookeeperClient zk = GetZooKeeper(); await CheckOrCreateRoot(zk); string servicesPath = $"{Root}/{service.Key}"; //todo 先获取后追加 if (!await zk.ExistsAsync(servicesPath)) { await zk.CreatePersistentAsync(servicesPath, GetData(string.Empty)); } string serviceNodePaht = $"{servicesPath}/{service.Host}:{service.Port}"; await zk.CreateEphemeralAsync(serviceNodePaht, GetData(service)); return(zk); }
/// <summary> /// 递归创建该节点下的所有子节点和该节点本身。 /// </summary> /// <param name="client">ZooKeeper客户端。</param> /// <param name="path">节点路径。</param> /// <param name="getNodeData">获取当前被创建节点数据的委托。</param> /// <param name="getNodeAcls">获取当前被创建节点权限的委托。</param> public static async Task CreateRecursiveAsync(this IZookeeperClient client, string path, Func <string, byte[]> getNodeData, Func <string, List <ACL> > getNodeAcls) { var data = getNodeData(path); var acls = getNodeAcls(path); try { await client.CreateAsync(path, data, acls, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException) { } catch (KeeperException.NoNodeException) { var parentDir = path.Substring(0, path.LastIndexOf('/')); await CreateRecursiveAsync(client, parentDir, getNodeData, getNodeAcls); await client.CreateAsync(path, data, acls, CreateMode.PERSISTENT); } }
//public void SetCurrentData(byte[] currentData) //{ // _currentData = currentData; //} internal async Task HandleNodeDataChange(IZookeeperClient client, NodeDataChangeArgs args) { Watcher.Event.EventType eventType = args.Type; var nodeData = new byte[0]; if (args.CurrentData != null && args.CurrentData.Any()) { nodeData = args.CurrentData.ToArray(); } switch (eventType) { case Watcher.Event.EventType.NodeCreated: _action(new byte[0], nodeData); _currentData = nodeData; break; case Watcher.Event.EventType.NodeDataChanged: _action(_currentData, nodeData); _currentData = nodeData; break; } }
internal async Task HandleChildrenChange(IZookeeperClient client, NodeChildrenChangeArgs args) { Watcher.Event.EventType eventType = args.Type; var path = args.Path; var watcher = new ChildrenMonitorWatcher(path, _action); switch (eventType) { case Watcher.Event.EventType.NodeCreated: await client.SubscribeChildrenChange(path, watcher.HandleChildrenChange); break; case Watcher.Event.EventType.NodeDataChanged: try { var currentChildrens = new string[0]; if (args.CurrentChildrens != null && args.CurrentChildrens.Any()) { currentChildrens = args.CurrentChildrens.ToArray(); } _action(_currentData, currentChildrens); watcher.SetCurrentData(currentChildrens); } catch (KeeperException.NoNodeException) { _action(_currentData, new string[0]); watcher.SetCurrentData(new string[0]); } break; case Watcher.Event.EventType.NodeDeleted: _action(_currentData, new string[0]); watcher.SetCurrentData(new string[0]); break; } }
/// <summary> /// 递归删除该节点下的所有子节点和该节点本身。 /// </summary> /// <param name="client">ZooKeeper客户端。</param> /// <param name="path">节点路径。</param> /// <returns>如果成功则返回true,false。</returns> public static async Task <bool> DeleteRecursiveAsync(this IZookeeperClient client, string path) { IEnumerable <string> children; try { children = await client.GetChildrenAsync(path); } catch (KeeperException.NoNodeException) { return(true); } foreach (var subPath in children) { if (!await client.DeleteRecursiveAsync(path + "/" + subPath)) { return(false); } } await client.DeleteAsync(path); return(true); }
/// <summary> /// 等待直到zk连接成功。 /// </summary> /// <param name="client">zk客户端。</param> /// <param name="timeout">最长等待时间。</param> /// <returns>如果成功则返回true,否则返回false。</returns> public static bool WaitUntilConnected(this IZookeeperClient client, TimeSpan timeout) { return(client.WaitForKeeperState(Watcher.Event.KeeperState.SyncConnected, timeout)); }