示例#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,
             },
             NameSpaceId = options.Namespace
         });
     }
     catch (Exception ex)
     {
         logger.LogWarning(ex, "Send heart beat to Nacos error");
     }
 }
        private static Uri GetUri(IApplicationBuilder app, NacosAspNetCoreOptions config)
        {
            Uri uri = null;

            var port = config.Port <= 0 ? 80 : config.Port;

            // config first
            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;

            // IServerAddressesFeature second
            if (app.Properties["server.Features"] is FeatureCollection features)
            {
                var addresses = features.Get <IServerAddressesFeature>();
                address = addresses?.Addresses?.First();

                if (address != null)
                {
                    if (address.Contains("*"))
                    {
                        var ip = GetCurrentIp();

                        address = address.Replace("*", ip);
                    }

                    uri = new Uri(address);
                    return(uri);
                }
            }

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

            uri = new Uri(address);
            return(uri);
        }