private async Task RegisterService()
        {
            var _node_id = this.Client.getSessionId().ToString();

            var list = this._contracts.Invoke().Select(x => new AddressModel()
            {
                Url              = x.Url,
                ServiceNodeName  = ServiceManageHelper.ParseServiceName(x.Contract),
                EndpointNodeName = ServiceManageHelper.EndpointNodeName(_node_id),
            }).ToList();

            var now = DateTime.Now;

            foreach (var m in list)
            {
                m.UpdateTime = now;

                var service_path = this._base_path + "/" + m.ServiceNodeName;
                await this.Client.EnsurePath(service_path);

                var path = service_path + "/" + m.EndpointNodeName;
                var data = this._serializer.Serialize(m);
                if (await this.Client.ExistAsync_(path))
                {
                    await this.Client.SetDataAsync_(path, data);
                }
                else
                {
                    //创建临时节点,服务端下线自动删除
                    await this.Client.CreateNode_(path, CreateMode.EPHEMERAL, data);
                }
            }
        }
Ejemplo n.º 2
0
        private async Task RegisterService()
        {
            var list = new List <AddressModel>();

            foreach (var m in ServiceHostManager.GetContractInfo())
            {
                var model = new AddressModel()
                {
                    Url              = m.url,
                    ServiceNodeName  = ServiceManageHelper.ParseServiceName(m.contract),
                    EndpointNodeName = ServiceManageHelper.EndpointNodeName(this._node_id),
                };
                list.Add(model);
            }

            foreach (var m in list)
            {
                var service_path = this._base_path + "/" + m.ServiceNodeName;
                await this.Client.EnsurePath(service_path);

                var path = service_path + "/" + m.EndpointNodeName;
                var data = this._serializer.Serialize(m);
                if (await this.Client.ExistAsync_(path))
                {
                    await this.Client.SetDataAsync_(path, data);
                }
                else
                {
                    //创建临时节点,服务端下线自动删除
                    await this.Client.CreateNode_(path, CreateMode.EPHEMERAL, data);
                }
            }
        }
Ejemplo n.º 3
0
        public AddressModel Resolve <T>()
        {
            var name = ServiceManageHelper.ParseServiceName <T>();
            var list = this._endpoints.Where(x => x.ServiceNodeName == name).ToList();

            if (ValidateHelper.IsPlumpList(list))
            {
                var theone = this._ran.Choice(list);
                //
                //根据权重选择
                //this._ran.ChoiceByWeight(list, x => x.Weight);
                Console.WriteLine($"选择了地址:{theone.Url}");
                return(theone);
            }
            return(null);
        }
        public AddressModel Resolve <T>(TimeSpan?timeout = null)
        {
            var name = ServiceManageHelper.ParseServiceName <T>();
            var list = this.AllService(timeout: timeout).Where(x => x.ServiceNodeName == name).ToList();

            if (ValidateHelper.IsPlumpList(list))
            {
                //这里用thread local比较好,一个线程共享一个随机对象
                lock (this._ran)
                {
                    var theone = this._ran.Choice(list) ??
                                 throw new Exception("server information is empty");
                    //根据权重选择
                    //this._ran.ChoiceByWeight(list, x => x.Weight);
                    this.OnServerSelected?.Invoke(theone);
                    return(theone);
                }
            }
            return(null);
        }
Ejemplo n.º 5
0
 protected Policy RetryAsync() => ServiceManageHelper.RetryAsyncPolicy();
Ejemplo n.º 6
0
 protected Policy Retry() => ServiceManageHelper.RetryPolicy();