Beispiel #1
0
        public void HealthyCheck(NetClient netclient, GatewayCommand registerCmd)
        {
            this.NetClient   = netclient;
            ServiceInfo      = registerCmd.Content.FromJson <RegisterServiceInfo>();
            ServiceInfo.Host = ((IPEndPoint)NetClient.Socket.RemoteEndPoint).Address.ToString();
            if (string.IsNullOrEmpty(ServiceInfo.ServiceAddress))
            {
                ServiceInfo.ServiceAddress = ServiceInfo.Host;
            }

            _gatewayReferee.AddMicroService(ServiceInfo);

            NetClient.WriteServiceData(new InvokeResult {
                Success = true
            });
            lock (_Gateway.OnlineMicroServices)
            {
                _Gateway.OnlineMicroServices.Add(this);
            }
            SystemEventCenter.OnMicroServiceOnline(this.ServiceInfo);

            Task.Run(() => {
                _ServiceProviderAllocator.ServiceInfoChanged(_Gateway.GetAllServiceProviders());
            });
            _Logger?.LogInformation($"微服务{this.ServiceInfo.ServiceNames.ToJsonString()} {this.ServiceInfo.Host}:{this.ServiceInfo.Port}注册");

            checkState();
        }
Beispiel #2
0
        public void RemoveMicroService(RegisterServiceInfo service)
        {
            if (_refereeAddress == null)
            {
                return;
            }

            Task.Run(() =>
            {
                try
                {
                    using (NetClient client = new NetClient(_refereeAddress.Address, _refereeAddress.Port))
                    {
                        client.WriteServiceData(new GatewayCommand
                        {
                            Type    = CommandType.UnRegisterSerivce,
                            Content = new RegisterServiceLocation
                            {
                                Host = service.Host,
                                Port = service.Port
                            }.ToJsonString()
                        });
                        var cmd = client.ReadServiceObject <InvokeResult>();
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, ex.Message);
                }
            });
        }
Beispiel #3
0
 public static void OnMicroServiceUploadLockedKeyCompleted(RegisterServiceInfo serviceInfo)
 {
     if (MicroServiceUploadLockedKeyCompleted != null)
     {
         MicroServiceUploadLockedKeyCompleted(null, serviceInfo);
     }
 }
Beispiel #4
0
 public static void OnMicroServiceOnffline(RegisterServiceInfo serviceInfo)
 {
     if (MicroServiceOnffline != null)
     {
         MicroServiceOnffline(null, serviceInfo);
     }
 }
Beispiel #5
0
        public void SetServicePerformanceInfo(RegisterServiceInfo from, PerformanceInfo performanceInfo)
        {
            var item = _serviceInfos.FirstOrDefault(m => m.ServiceInfo.Host == from.Host && m.ServiceInfo.Port == from.Port);

            if (item != null)
            {
                Interlocked.Exchange(ref item.RequestQuantity, performanceInfo.RequestQuantity.GetValueOrDefault());
                item.CpuUsage = performanceInfo.CpuUsage.GetValueOrDefault();
                item.Usage    = item.RequestQuantity / (decimal)item.ServiceInfo.MaxThread;
            }
        }
Beispiel #6
0
        public PerformanceInfo GetPerformanceInfo(RegisterServiceInfo from)
        {
            var item = _serviceInfos.FirstOrDefault(m => m.ServiceInfo.Host == from.Host && m.ServiceInfo.Port == from.Port);

            if (item != null)
            {
                return(new PerformanceInfo {
                    RequestQuantity = item.RequestQuantity,
                    CpuUsage = item.CpuUsage
                });
            }
            return(null);
        }
Beispiel #7
0
        public bool UnLock(string key, RegisterServiceInfo service)
        {
            if (IsReady == false)
            {
                throw new Exception("lock key is not ready");
            }

            if (_cache.TryGetValue(key, out KeyObject keyObj))
            {
                if (service == null || keyObj.Locker == service.ServiceId)
                {
                    return(_cache.TryRemove(key, out keyObj));
                }
            }
            return(false);
        }
Beispiel #8
0
        public bool TryLock(string key, RegisterServiceInfo locker, bool checkReady = true)
        {
            if (checkReady && IsReady == false)
            {
                throw new Exception("lock key is not ready");
            }

            KeyObject keyObj = null;

            while (true)
            {
                if (_cache.TryGetValue(key, out keyObj) == false)
                {
                    if (_cache.TryAdd(key, new KeyObject()
                    {
                        Key = key,
                        Locker = locker.ServiceId
                    }))
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    if (keyObj.Locker == locker.ServiceId)
                    {
                        keyObj.RemoveTime = null;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
        }
Beispiel #9
0
 public void AddMicroService(RegisterServiceInfo service)
 {
     if (_refereeAddress == null)
     {
         return;
     }
     using (NetClient client = new NetClient(_refereeAddress))
     {
         client.WriteServiceData(new GatewayCommand
         {
             Type    = CommandType.RegisterSerivce,
             Content = new RegisterServiceLocation {
                 Host = service.Host,
                 Port = service.Port
             }.ToJsonString()
         });
         var cmd = client.ReadServiceObject <InvokeResult>();
         if (cmd.Success == false)
         {
             throw new Exception("not master");
         }
     }
 }
Beispiel #10
0
 private void SystemEventCenter_MicroServiceUploadLockedKeyCompleted(object sender, RegisterServiceInfo e)
 {
     _logger?.LogInformation($"{e.Host}:{e.Port} UploadLockedKeyCompleted");
     _waitServiceList?.TryRemove($"{e.Host}:{e.Port}", out RegisterServiceLocation o);
 }