Beispiel #1
0
        public override Task <GetMicroReply> GetMicro(GetMicroRequest request, ServerCallContext context)
        {
            var reply = new GetMicroReply();

            reply.Micros.AddRange(Distribute.GetMicro(request.Request));
            return(Task.FromResult(reply));
        }
Beispiel #2
0
        /// <summary>
        /// 更新服务缓存
        /// </summary>
        internal static void UpdateCache(string channel)
        {
            #region 到DNS中心取服务信息
            try
            {
                if (channel.Equals("cron:"))
                {
                    RefreshServiceMd5();
                    channel = ServiceMd5;
                }
                DateTime        now     = DateTime.Now; //获取缓存时间
                GetMicroRequest request = new GetMicroRequest();
                request.Request = channel;
                var microList = _client.GetMicro(request: request, 30000.GetCallOptions()).Micros.ToList();

                #region Micro +添加到缓存

                if (microList != null && microList.Count > 0)
                {
                    var microCaches = new List <MicroCache>();
                    microList.ForEach(m =>
                    {
                        microCaches.Add(new MicroCache()
                        {
                            LasTime = now,
                            Mi      = m,
                            Tags    = m.Name.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Substring(0, t.Length - 7)).ToList()
                        });
                    });
                    _microCaches = microCaches;

                    #region  步服务到连接池
                    var scs = new List <ServiceConfig>();
                    _microCaches.ForEach(mc =>
                    {
                        if (!scs.Exists(s => s.Host == mc.Mi.Ip && s.Port == mc.Mi.Port))
                        {
                            scs.Add(new ServiceConfig()
                            {
                                Host    = mc.Mi.Ip,
                                Port    = mc.Mi.Port,
                                Timeout = mc.Mi.Timeout
                            });
                        }
                    });
                    GrpcFactory.Synchronization(scs);

                    #endregion
                }
                else
                {
                    _microCaches.Clear();
                    GrpcFactory.Synchronization(new List <ServiceConfig>());
                }

                #endregion
            }
            catch
            {
                try
                {
                    _channel.ShutdownAsync();
                    // return (null, FailMessage($"负载中心连接失败!"));
                    _channel = new Channel($"{SettingService.Local.IpAddress}:{SettingService.Local.Port}", ChannelCredentials.Insecure);
                    _client  = new BrokerCenter.BrokerCenterClient(_channel);
                }
                catch
                {
                    // ignored
                }
            }
            #endregion
        }