Пример #1
0
        public int Register()
        {
            if (_serverManager.MyServer != null)
            {
                throw new Exception("已注册过,不能重试注册");
            }
            Ts_Servers Server = new Ts_Servers()
            {
                LastHeartTime = DateTime.Now,
            };
            int    result     = 0;
            string ServerName = Dns.GetHostName();

            try
            {
                IPAddress[] ServerIPs = Dns.GetHostAddresses(ServerName);

                var tempserver = GetServerId(ServerName);
                if (tempserver != null)
                {
                    if (!tempserver.IsEnable)
                    {
                        return(-1);
                    }
                    result    = _ormServers.Update(Server, w => w.Id == tempserver.Id);
                    Server.Id = tempserver.Id;
                }
                else
                {
                    Server.IsEnable   = true;
                    Server.ServerName = ServerName;
                    Server.IsMain     = false;
                    Server.ServerIP   = ServerIPs.Select <IPAddress, string>(x => x.ToString()).ToJson();
                    result            = (int)_ormServers.Add(Server);
                    Server.Id         = result;
                }
                if (result <= 0)
                {
                    log.Fatal(string.Format("Server:{0},服务器注册失败,系统将无法正常运行", ServerName));
                    throw new Exception(string.Format("Server:{0},服务器注册失败,系统将无法正常运行", ServerName));
                }
                _serverManager.MyServer = new Ts_Servers {
                    Id         = Server.Id,
                    ServerName = ServerName
                };

                _serverManager.OnServerCountChange += _serverManager_OnServerCountChange;
                _serverManager.OnDeadServer        += _serverManager_OnDeadServer;
                _serverManager.StartUp(Heart, QueryUseServerAction);
                return(Server.Id);
            }
            catch (Exception ex)
            {
                log.Fatal(string.Format("Server:{0},服务器注册异常,系统将无法正常运行", ServerName), ex);
                throw new Exception(string.Format("Server:{0},服务器注册异常,系统将无法正常运行", ServerName), ex);
            }
        }
Пример #2
0
        public bool Heart(int ServerId)
        {
            Ts_Servers Server = new Ts_Servers()
            {
                LastHeartTime = DateTime.Now,
            };

            try
            {
                return(_ormServers.Update(Server, w => w.Id == ServerId) > 0);
            }
            catch (Exception ex) {
                log.Fatal(string.Format("Server:{0},服务器心跳异常", _serverManager.MyServer.ServerName), ex);
                return(false);
            }
        }