示例#1
0
 private static async Task SendAsync(INacosNamingClient client, NacosAspNetCoreOptions options, Uri uri, ILogger logger)
 {
     try
     {
         // send heart beat will register instance
         await client.SendHeartbeatAsync(new SendHeartbeatRequest
         {
             Ephemeral   = false,
             ServiceName = options.ServiceName,
             GroupName   = options.GroupName,
             BeatInfo    = new BeatInfo
             {
                 ip          = uri.Host,
                 port        = uri.Port,
                 serviceName = options.ServiceName,
                 scheduled   = true,
                 weight      = options.Weight,
                 cluster     = options.ClusterName,
             },
         });
     }
     catch (Exception ex)
     {
         logger.LogWarning(ex, "Send heart beat to Nacos error");
     }
 }
示例#2
0
        public static Uri GetUri(IFeatureCollection features, NacosAspNetCoreOptions config)
        {
            var port = config.Port <= 0 ? 80 : config.Port;

            // 1. config
            if (!string.IsNullOrWhiteSpace(config.Ip))
            {
                // it seems that nacos don't return the scheme
                // so here use http only.
                return(new Uri($"http://{config.Ip}:{port}"));
            }

            var address = string.Empty;

            // 2. IServerAddressesFeature
            if (features != null)
            {
                var addresses = features.Get <IServerAddressesFeature>();
                address = addresses?.Addresses?.FirstOrDefault();

                if (address != null)
                {
                    var url = ReplaceAddress(address, config.PreferredNetworks);
                    return(new Uri(url));
                }
            }

            // 3. ASPNETCORE_URLS
            address = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
            if (!string.IsNullOrWhiteSpace(address))
            {
                var url = ReplaceAddress(address, config.PreferredNetworks);
                return(new Uri(url));
            }

            // 4. --urls
            var cmdArgs = Environment.GetCommandLineArgs();

            if (cmdArgs != null && cmdArgs.Any())
            {
                var cmd = cmdArgs.FirstOrDefault(x => x.StartsWith("--urls", StringComparison.OrdinalIgnoreCase));

                if (!string.IsNullOrWhiteSpace(cmd))
                {
                    address = cmd.Split('=')[1];

                    var url = ReplaceAddress(address, config.PreferredNetworks);
                    return(new Uri(url));
                }
            }

            // 5. current ip address third
            address = $"http://{GetCurrentIp(config.PreferredNetworks)}:{port}";

            return(new Uri(address));
        }
示例#3
0
 public StatusReportBgTask(
     ILoggerFactory loggerFactory,
     INacosNamingClient client,
     IServer server,
     IOptionsMonitor <NacosAspNetCoreOptions> optionsAccs)
 {
     _logger   = loggerFactory.CreateLogger <StatusReportBgTask>();
     _client   = client;
     _options  = optionsAccs.CurrentValue;
     _features = server.Features;
 }
示例#4
0
        public static IEnumerable <Uri> GetUri(IFeatureCollection features, NacosAspNetCoreOptions config)
        {
            var splitChars = new char[] { ',', ';' };
            var port       = config.Port <= 0 ? 80 : config.Port;

            // 1. config
            if (!string.IsNullOrWhiteSpace(config.Ip))
            {
                // it seems that nacos don't return the scheme
                // so here use http only.
                return(new List <Uri> {
                    new Uri($"http://{config.Ip}:{port}")
                });
            }

            // 1.1. Ip is null && Port has value
            if (string.IsNullOrWhiteSpace(config.Ip) && port != 80)
            {
                return(new List <Uri> {
                    new Uri($"http://{GetCurrentIp(config.PreferredNetworks)}:{port}")
                });
            }

            var address = string.Empty;

            // 2. IServerAddressesFeature
            if (features != null)
            {
                var addresses         = features.Get <IServerAddressesFeature>();
                var addressCollection = addresses?.Addresses;

                if (addressCollection != null && addressCollection.Any())
                {
                    var uris = new List <Uri>();
                    foreach (var item in addressCollection)
                    {
                        var url = ReplaceAddress(item, config.PreferredNetworks);
                        uris.Add(new Uri(url));
                    }

                    return(uris);
                }
            }

            // 3. ASPNETCORE_URLS
            address = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
            if (!string.IsNullOrWhiteSpace(address))
            {
                var url = ReplaceAddress(address, config.PreferredNetworks);

                return(url.Split(splitChars).Select(x => new Uri(x)));
            }

            // 4. --urls
            var cmdArgs = Environment.GetCommandLineArgs();

            if (cmdArgs != null && cmdArgs.Any())
            {
                var cmd = cmdArgs.FirstOrDefault(x => x.StartsWith("--urls", StringComparison.OrdinalIgnoreCase));

                if (!string.IsNullOrWhiteSpace(cmd))
                {
                    address = cmd.Split('=')[1];

                    var url = ReplaceAddress(address, config.PreferredNetworks);

                    return(url.Split(splitChars).Select(x => new Uri(x)));
                }
            }

            // 5. current ip address third
            address = $"http://{GetCurrentIp(config.PreferredNetworks)}:{port}";

            return(new List <Uri> {
                new Uri(address)
            });
        }