Пример #1
0
        /// <summary>
        /// 向远端注册服务信息
        /// </summary>
        public void RegisterServer(IServiceProvider provider)
        {
            //注册服务
            var servers  = provider.GetServices <DevelopBase.Discovery.IServer>();
            var Register = servers.First(c => c.GetType().GetInterfaces().Contains(DiscoveryMap[BaseServerInfo.DiscoveryInfo.ProtocolType].BaseInterface));

            Register.SetServer(BaseServerInfo.DiscoveryInfo.Connstring, BaseServerInfo.DiscoveryInfo.TimeOut);
            //转换本地配置为远端配置
            foreach (var localInfo in Appinfos)
            {
                foreach (var SupportProtocolType in localInfo.SupportProtocolTypes)
                {
                    var remoteInfo = new DPBase.RemoteAppInfo()
                    {
                        AppName      = localInfo.AppName,
                        ProtocolType = SupportProtocolType,
                        HostName     = BaseServerInfo.HostName,
                        Infos        = BaseServerInfo.Infos,
                        HostAddr     = ProtocolInfos.First(c => c.ProtocolType == SupportProtocolType).HostAddr,
                        HostPort     = ProtocolInfos.First(c => c.ProtocolType == SupportProtocolType).HostPort,
                    };
                    lock (Registered)
                    {
                        var key = $"{remoteInfo.AppName}-{remoteInfo.HostName}-{remoteInfo.ProtocolType}";
                        Registered.Add(key, Newtonsoft.Json.JsonConvert.SerializeObject(remoteInfo));
                        Register.Register(key, Registered[key]);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 向远端注册服务信息
        /// </summary>
        public void UpdateServer(IServiceProvider provider, DPBase.BaseServerInfo NewInfos)
        {
            //移除旧注册信息
            var servers  = provider.GetServices <DevelopBase.Discovery.IServer>();
            var Register = servers.First(c => c.GetType().GetInterfaces().Contains(DiscoveryMap[BaseServerInfo.DiscoveryInfo.ProtocolType].BaseInterface));

            //重置连接将删除以前全部数据
            Register.SetServer(BaseServerInfo.DiscoveryInfo.Connstring);
            lock (Registered)
            {
                Registered.Clear();
            }
            //更新本地缓存信息
            Appinfos = provider.GetService <HashSet <DPBase.Appinfo> >();
            if (Appinfos == null)
            {
                Appinfos = new HashSet <DPBase.Appinfo>();
            }
            foreach (var item in NewInfos.Appinfos)
            {
                lock (Appinfos)
                {
                    if (!Appinfos.Any(c => c.AppName == item.AppName))
                    {
                        Appinfos.Add(item);
                    }
                }
            }
            ProtocolInfos  = NewInfos.ProtocolInfos;
            BaseServerInfo = NewInfos;
            //重新注册信息
            foreach (var localInfo in Appinfos)
            {
                foreach (var SupportProtocolType in localInfo.SupportProtocolTypes)
                {
                    var remoteInfo = new DPBase.RemoteAppInfo()
                    {
                        AppName      = localInfo.AppName,
                        ProtocolType = SupportProtocolType,
                        HostName     = BaseServerInfo.HostName,
                        Infos        = BaseServerInfo.Infos,
                        HostAddr     = ProtocolInfos.First(c => c.ProtocolType == SupportProtocolType).HostAddr,
                        HostPort     = ProtocolInfos.First(c => c.ProtocolType == SupportProtocolType).HostPort,
                    };
                    lock (Registered)
                    {
                        var key = $"{remoteInfo.AppName}-{remoteInfo.HostName}-{remoteInfo.ProtocolType}";
                        Registered.Add(key, Newtonsoft.Json.JsonConvert.SerializeObject(remoteInfo));
                        Register.Register(key, Registered[key]);
                    }
                }
            }
        }