コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConsulServiceRegistrar"/> class.
 /// </summary>
 /// <param name="registry">the Consul service registry to use when doing registrations</param>
 /// <param name="optionsMonitor">configuration options to use</param>
 /// <param name="registration">the registration to register with Consul</param>
 /// <param name="logger">optional logger</param>
 public NacosServiceRegistrar(INacosServiceRegistry registry, IOptionsMonitor <NacosDiscoveryOptions> optionsMonitor, INacosRegistration registration, ILogger <NacosServiceRegistrar> logger = null)
 {
     _registry       = registry ?? throw new ArgumentNullException(nameof(registry));
     _optionsMonitor = optionsMonitor ?? throw new ArgumentNullException(nameof(optionsMonitor));
     Registration    = registration ?? throw new ArgumentNullException(nameof(registration));
     _logger         = logger;
 }
コード例 #2
0
        /// <inheritdoc/>
        public async Task <object> GetStatusAsync(INacosRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }


            var request = new ListInstancesRequest()
            {
                ServiceName = registration.ServiceId,
                Clusters    = registration.ClusterName
            };
            var result = await _nacosClient.ListInstancesAsync(request);

            //ListInstancesResult result = Task.Run(async () =>
            //{
            //    return
            //}).Result;
            foreach (Host check in result.Hosts)
            {
                if (check.Ip.Equals(registration.Host))
                {
                    if (check.Healthy == false)
                    {
                        return(OUT_OF_SERVICE);
                    }
                }
            }

            return(UP);
        }
コード例 #3
0
        public S GetStatus <S>(INacosRegistration registration) where S : class
        {
            var result = Task.Run(async() =>
            {
                return(await GetStatusAsync(registration));
            }).Result;

            return((S)result);
        }
コード例 #4
0
 public ThisServiceInstance(INacosRegistration registration)
 {
     ServiceId = registration.ServiceId;
     Host      = registration.Host;
     IsSecure  = registration.IsSecure;
     Port      = registration.Port;
     Metadata  = registration.Metadata;
     Uri       = registration.Uri;
 }
コード例 #5
0
        public Task DeregisterAsync(INacosRegistration registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            return(DeregisterAsyncInternal(registration));
        }
コード例 #6
0
        public void Deregister(INacosRegistration registration)
        {
            RemoveInstanceRequest request = new RemoveInstanceRequest()
            {
                Ip          = registration.Host,
                Port        = registration.Port,
                ServiceName = registration.ServiceId
            };

            _nacosClient.RemoveInstanceAsync(request);
        }
コード例 #7
0
        public void SetStatus(INacosRegistration registration, string status)
        {
            ModifyInstanceHealthStatusRequest request = new ModifyInstanceHealthStatusRequest()
            {
                Ip          = registration.Host,
                Port        = registration.Port,
                ServiceName = registration.ServiceId,
                Healthy     = registration.Healthy,
                NamespaceId = registration.Namespace
            };

            _nacosClient.ModifyInstanceHealthStatusAsync(request);
        }
コード例 #8
0
        public void Register(INacosRegistration registration)
        {
            RegisterInstanceRequest request = new RegisterInstanceRequest()
            {
                ClusterName = registration.ClusterName,
                Ephemeral   = registration.Ephemeral,
                ServiceName = registration.ServiceId,
                Enable      = registration.Enable,
                GroupName   = registration.GroupName,
                Ip          = registration.Host,
                Port        = registration.Port,
                NamespaceId = registration.Namespace
            };

            _nacosClient.RegisterInstanceAsync(request);
        }
コード例 #9
0
        private async Task <bool> ResistryAsync(INacosRegistration registration)
        {
            RegisterInstanceRequest request = new RegisterInstanceRequest()
            {
                ClusterName = registration.ClusterName,
                Ephemeral   = registration.Ephemeral,
                ServiceName = registration.ServiceId,
                Enable      = registration.Enable,
                GroupName   = registration.GroupName,
                Ip          = registration.Host,
                Port        = registration.Port,
                NamespaceId = registration.Namespace
            };

            return(await _nacosClient.RegisterInstanceAsync(request));
        }
コード例 #10
0
        private async Task DeregisterAsyncInternal(INacosRegistration registration)
        {
            if (Options.IsHeartBeatEnabled && _scheduler != null)
            {
                _scheduler.Remove(registration.ServiceId);
            }

            _logger?.LogInformation("Deregistering service with consul {instanceId} ", registration.ServiceId);

            ModifyInstanceHealthStatusRequest request = new ModifyInstanceHealthStatusRequest()
            {
                Ip          = registration.Host,
                Port        = registration.Port,
                ServiceName = registration.ServiceId,
                Healthy     = registration.Healthy,
                NamespaceId = registration.Namespace
            };
            await _nacosClient.ModifyInstanceHealthStatusAsync(request);
        }
コード例 #11
0
        private async Task RegisterAsyncInternal(INacosRegistration registration)
        {
            _logger?.LogInformation("Registering service with nacos {serviceId} ", registration.ServiceId);

            try
            {
                await ResistryAsync(registration).ConfigureAwait(false);

                if (Options.IsHeartBeatEnabled && _scheduler != null)
                {
                    _scheduler.Add(registration.ServiceId);
                }
            }
            catch (Exception e)
            {
                if (Options.FailFast)
                {
                    _logger?.LogError(e, "Error registering service with consul {serviceId} ", registration.ServiceId);
                    throw;
                }

                _logger?.LogWarning(e, "Failfast is false. Error registering service with consul {serviceId} ", registration.ServiceId);
            }
        }
コード例 #12
0
 public void SetStatus(INacosRegistration registration, string status)
 {
     DeregisterAsync(registration).GetAwaiter().GetResult();
 }
コード例 #13
0
 public void Register(INacosRegistration registration)
 {
     RegisterAsync(registration).GetAwaiter().GetResult();
 }