/// <summary> /// Build new beat information. /// </summary> /// <param name="serviceName">service name with group name, format: ${groupName}@@${serviceName}</param> /// <param name="instance">instance</param> /// <returns>new beat information</returns> public BeatInfo BuildBeatInfo(string serviceName, RegisterInstanceRequest instance) { BeatInfo beatInfo = new BeatInfo { serviceName = serviceName, ip = instance.Ip, port = instance.Port, cluster = instance.ClusterName, weight = instance.Weight, metadata = instance.Metadata, scheduled = false, // using the default value at first, the unit is ms. period = 5000 }; return(beatInfo); }
public async Task <bool> RegisterInstanceAsync(RegisterInstanceRequest request) { if (request == null) { throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "request param invalid"); } request.CheckParam(); if (request.Ephemeral.HasValue && request.Ephemeral.Value) { BeatInfo beatInfo = _beatReactor.BuildBeatInfo(request.ServiceName, request); await _beatReactor.AddBeatInfo(GetGroupedName(request.ServiceName, request.GroupName), beatInfo); } var responseMessage = await _proxy.ReqApiAsync(HttpMethod.Post, RequestPathValue.INSTANCE, null, request.ToDict(), _options.DefaultTimeOut); switch (responseMessage.StatusCode) { case System.Net.HttpStatusCode.OK: var result = await responseMessage.Content.ReadAsStringAsync(); if (result.Equals("ok", StringComparison.OrdinalIgnoreCase)) { return(true); } else { _logger.LogWarning($"[client.RegisterInstance] server return {result} "); return(false); } default: _logger.LogWarning($"[client.RegisterInstance] Register an instance to service failed {responseMessage.StatusCode.ToString()}"); throw new NacosException((int)responseMessage.StatusCode, $"Register an instance to service failed {responseMessage.StatusCode.ToString()}"); } }