Beispiel #1
0
        public async Task Regist(IEnumerable <Assembly> assemblyList, ServiceAddress serverAddress)
        {
            using (var client = CreateClient())
            {
                foreach (var ass in assemblyList)
                {
                    var assName = ass.GetName();
                    var service = new AgentServiceRegistration
                    {
                        ID   = $"{ass.GetName().Name}_{serverAddress}".Md5(),
                        Name = assName.Name,
                        Tags = new[] { $"{Consts.Mode}" },
                        EnableTagOverride = true,
                        Address           = serverAddress.Address(),
                        Port = serverAddress.Port,
                        Meta = new Dictionary <string, string>
                        {
                            { "serverAddress", serverAddress.ToJson() }
                        }
                    };
                    _services.Add(service.ID);
                    var result = await client.Agent.ServiceRegister(service);

                    if (result.StatusCode != HttpStatusCode.OK)
                    {
                        _logger.Warn(
                            $"服务注册失败 [{assName.Name},{serverAddress}]:{result.StatusCode},{result.RequestTime}");
                    }
                    else
                    {
                        _logger.Info($"服务注册成功 [{assName.Name},{serverAddress}]");
                    }
                }
            }
        }
Beispiel #2
0
        public override async Task Regist(IEnumerable <Assembly> assemblyList, ServiceAddress serverAddress)
        {
            foreach (var assembly in assemblyList)
            {
                var serviceName = assembly.ServiceName();
                var meta        = new Dictionary <string, string>
                {
                    { KeyService, serverAddress.ToJson() },
                    { KeyMode, $"{Constants.Mode}" },
                    { KeyVersion, assembly.GetName().Version.ToString() }
                };
                var request = new CreateInstanceRequest
                {
                    serviceName = serviceName,
                    namespaceId = _config.Tenant,
                    groupName   = _config.Group,
                    clusterName = string.Empty,
                    ip          = serverAddress.IpAddress,
                    port        = serverAddress.Port,
                    weight      = serverAddress.Weight,
                    Meta        = meta,
                    enabled     = true,
                    healthy     = true,
                    ephemeral   = false
                };
                try
                {
                    var result = await _client.CreateInstance(request);

                    if (result == "ok")
                    {
                        _services.Add(new RemoveInstanceRequest
                        {
                            namespaceId = request.namespaceId,
                            serviceName = request.serviceName,
                            groupName   = request.groupName,
                            clusterName = request.clusterName,
                            ip          = request.ip,
                            port        = request.port,
                            ephemeral   = request.ephemeral
                        });
                        _logger.LogInformation($"服务注册成功 [{serviceName},{serverAddress}]");
                        //发送心跳包
                        _listenerHelper.AddServiceBeat(request, t => { }, 15);
                    }
                    else
                    {
                        throw new SpearException($"注册实例失败,{result}");
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(
                        $"服务注册失败 [{serviceName},{serverAddress}]:{ex.Message}");
                }
            }
        }