Skip to content

Commit

Permalink
修改服务状态为健康/异常;新增一个删除按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
kklldog committed Apr 5, 2022
1 parent a757a8e commit 30d0afe
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion AgileConfig.Server.Apisite/Controllers/ReportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public async Task<IActionResult> ServiceCount()
{
var services = await _serviceInfoService.GetAllServiceInfoAsync();
var serviceCount = services.Count;
var serviceOnCount = services.Count(x => x.Alive == ServiceAlive.Online);
var serviceOnCount = services.Count(x => x.Status == ServiceStatus.Healthy);
return Json(new
{
serviceCount,
Expand Down
8 changes: 4 additions & 4 deletions AgileConfig.Server.Apisite/Controllers/ServiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task<IActionResult> Add([FromBody] ServiceInfoVM model)
});
}

public async Task<IActionResult> Search(string serviceName, string serviceId, ServiceAlive? alive,
public async Task<IActionResult> Search(string serviceName, string serviceId, ServiceStatus? status,
string sortField, string ascOrDesc,
int current = 1, int pageSize = 20)
{
Expand All @@ -89,9 +89,9 @@ public async Task<IActionResult> Search(string serviceName, string serviceId, Se
{
query = query.Where(x => x.ServiceId.Contains(serviceId)).ToList();
}
if (alive.HasValue)
if (status.HasValue)
{
query = query.Where(x => x.Alive == alive).ToList();
query = query.Where(x => x.Status == status).ToList();
}

query = query.OrderByDescending(x => x.RegisterTime).ToList();
Expand Down Expand Up @@ -127,7 +127,7 @@ public async Task<IActionResult> Search(string serviceName, string serviceId, Se
serviceVMs.Add(new ServiceInfoVM()
{
Id = service.Id,
Alive = service.Alive,
Status = service.Status,
ServiceId = service.ServiceId,
ServiceName = service.ServiceName,
Ip = service.Ip,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ServiceInfoVM

public List<string> MetaData { get; set; } = new List<string>();

public ServiceAlive Status { get; set; }
public ServiceStatus Status { get; set; }
}

public class QueryServiceInfoResultVM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public async Task<List<ServiceInfoVM>> AllServices()
Ip = serviceInfo.Ip,
Port = serviceInfo.Port,
MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData),
Status = serviceInfo.Alive
Status = serviceInfo.Status
};
vms.Add(vm);
}
Expand All @@ -157,7 +157,7 @@ public async Task<List<ServiceInfoVM>> OnlineServices()
Ip = serviceInfo.Ip,
Port = serviceInfo.Port,
MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData),
Status = serviceInfo.Alive
Status = serviceInfo.Status
};
vms.Add(vm);
}
Expand All @@ -179,7 +179,7 @@ public async Task<List<ServiceInfoVM>> OfflineServices()
Ip = serviceInfo.Ip,
Port = serviceInfo.Port,
MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData),
Status = serviceInfo.Alive
Status = serviceInfo.Status
};
vms.Add(vm);
}
Expand Down
2 changes: 1 addition & 1 deletion AgileConfig.Server.Apisite/Models/ServiceInfoVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ServiceInfoVM

public string MetaData { get; set; }

public ServiceAlive Alive { get; set; }
public ServiceStatus Status { get; set; }

public DateTime? RegisterTime { get; set; }

Expand Down
10 changes: 5 additions & 5 deletions AgileConfig.Server.Data.Entity/ServiceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace AgileConfig.Server.Data.Entity
{
public enum ServiceAlive
public enum ServiceStatus
{
Offline = 0,
Online = 1
Unhealthy = 0,
Healthy = 1
}

public enum RegisterWay
Expand Down Expand Up @@ -46,8 +46,8 @@ public class ServiceInfo
[Column(Name = "meta_data", StringLength = 2000)]
public string MetaData { get; set; }

[Column(Name = "alive")]
public ServiceAlive Alive { get; set; }
[Column(Name = "status")]
public ServiceStatus Status { get; set; }

[Column(Name = "register_time")]
public DateTime? RegisterTime { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion AgileConfig.Server.IService/IServiceInfoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IServiceInfoService: IDisposable

Task<bool> RemoveAsync(string id);

Task UpdateServiceStatus(ServiceInfo service, ServiceAlive status);
Task UpdateServiceStatus(ServiceInfo service, ServiceStatus status);
Task<List<ServiceInfo>> GetAllServiceInfoAsync();

Task<List<ServiceInfo>> GetOnlineServiceInfoAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void Register()
using var serviceInfoService = NewServiceInfoService();
var service = await serviceInfoService.GetByUniqueIdAsync(id);
if (service != null && !string.IsNullOrWhiteSpace(service.AlarmUrl) &&
service.Alive == ServiceAlive.Offline)
service.Status == ServiceStatus.Unhealthy)
{
//如果是下线发送通知
_ = SendServiceOfflineMessageAsync(service);
Expand All @@ -121,7 +121,7 @@ private async Task SendServiceOfflineMessageAsync(ServiceInfo service)
service.ServiceId,
service.ServiceName,
Time = DateTime.Now,
Status = ServiceAlive.Offline.ToString(),
Status = ServiceStatus.Unhealthy.ToString(),
Message = "服务已经下线"
};

Expand Down
10 changes: 5 additions & 5 deletions AgileConfig.Server.Service/RegisterCenterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task<string> RegisterAsync(ServiceInfo serviceInfo)
if (oldEntity != null)
{
oldEntity.RegisterTime = DateTime.Now;
oldEntity.Alive = ServiceAlive.Online;
oldEntity.Status = ServiceStatus.Healthy;
oldEntity.LastHeartBeat = DateTime.Now;
oldEntity.ServiceName = serviceInfo.ServiceName;
oldEntity.Ip = serviceInfo.Ip;
Expand All @@ -65,7 +65,7 @@ public async Task<string> RegisterAsync(ServiceInfo serviceInfo)

serviceInfo.RegisterTime = DateTime.Now;
serviceInfo.LastHeartBeat = DateTime.Now;
serviceInfo.Alive = ServiceAlive.Online;
serviceInfo.Status = ServiceStatus.Healthy;
serviceInfo.Id = Guid.NewGuid().ToString("n");

_dbContext.ServiceInfo.Add(serviceInfo);
Expand Down Expand Up @@ -151,14 +151,14 @@ public async Task<bool> ReceiveHeartbeatAsync(string serviceUniqueId)
}
else
{
var oldStatus = entity.Alive;
entity.Alive = ServiceAlive.Online;
var oldStatus = entity.Status;
entity.Status = ServiceStatus.Healthy;
entity.LastHeartBeat = DateTime.Now;
await _dbContext.UpdateAsync(entity);

await _dbContext.SaveChangesAsync();

if (oldStatus != ServiceAlive.Online)
if (oldStatus != ServiceStatus.Healthy)
{
_serviceInfoService.ClearCache();
dynamic param = new ExpandoObject();
Expand Down
8 changes: 4 additions & 4 deletions AgileConfig.Server.Service/ServiceHealthCheckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public Task StartCheckAsync()
if ((DateTime.Now - lstHeartBeat.Value).TotalSeconds > UnhealthInterval)
{
//客户端主动心跳模式:超过 UnhealthInterval 没有心跳,则认为服务不可用
if (service.Alive == ServiceAlive.Online)
if (service.Status == ServiceStatus.Healthy)
{
await _serviceInfoService.UpdateServiceStatus(service, ServiceAlive.Offline);
await _serviceInfoService.UpdateServiceStatus(service, ServiceStatus.Unhealthy);
}
}

Expand All @@ -164,15 +164,15 @@ public Task StartCheckAsync()
if (string.IsNullOrWhiteSpace(service.CheckUrl))
{
//CheckUrl不填,直接认为下线
await _serviceInfoService.UpdateServiceStatus(service, ServiceAlive.Offline);
await _serviceInfoService.UpdateServiceStatus(service, ServiceStatus.Unhealthy);
continue;
}

_ = Task.Run(async () =>
{
var result = await CheckAService(service);
await _serviceInfoService.UpdateServiceStatus(service,
result ? ServiceAlive.Online : ServiceAlive.Offline);
result ? ServiceStatus.Healthy : ServiceStatus.Unhealthy);
});
}
}
Expand Down
16 changes: 8 additions & 8 deletions AgileConfig.Server.Service/ServiceInfoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public async Task<List<ServiceInfo>> GetAllServiceInfoAsync()

public async Task<List<ServiceInfo>> GetOnlineServiceInfoAsync()
{
var services = await FreeSQL.Instance.Select<ServiceInfo>().Where(x => x.Alive == ServiceAlive.Online)
var services = await FreeSQL.Instance.Select<ServiceInfo>().Where(x => x.Status == ServiceStatus.Healthy)
.ToListAsync();

return services;
}

public async Task<List<ServiceInfo>> GetOfflineServiceInfoAsync()
{
var services = await FreeSQL.Instance.Select<ServiceInfo>().Where(x => x.Alive == ServiceAlive.Offline)
var services = await FreeSQL.Instance.Select<ServiceInfo>().Where(x => x.Status == ServiceStatus.Unhealthy)
.ToListAsync();

return services;
Expand Down Expand Up @@ -107,7 +107,7 @@ private string GenerateMD5(List<ServiceInfo> services)
}

sb.Append(
$"{serviceInfo.ServiceId}&{serviceInfo.ServiceName}&{serviceInfo.Ip}&{serviceInfo.Port}&{(int)serviceInfo.Alive}&{metaDataStr}&");
$"{serviceInfo.ServiceId}&{serviceInfo.ServiceName}&{serviceInfo.Ip}&{serviceInfo.Port}&{(int)serviceInfo.Status}&{metaDataStr}&");
}

var txt = sb.ToString();
Expand All @@ -122,22 +122,22 @@ public void ClearCache()
}
}

public async Task UpdateServiceStatus(ServiceInfo service, ServiceAlive status)
public async Task UpdateServiceStatus(ServiceInfo service, ServiceStatus status)
{
var id = service.Id;
var oldStatus = service.Alive;
var oldStatus = service.Status;

if (status == ServiceAlive.Offline)
if (status == ServiceStatus.Unhealthy)
{
await FreeSQL.Instance.Update<ServiceInfo>()
.Set(x => x.Alive, status)
.Set(x => x.Status, status)
.Where(x => x.Id == id)
.ExecuteAffrowsAsync();
}
else
{
await FreeSQL.Instance.Update<ServiceInfo>()
.Set(x => x.Alive, status)
.Set(x => x.Status, status)
.Set(x => x.LastHeartBeat, DateTime.Now)
.Where(x => x.Id == id)
.ExecuteAffrowsAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const services: React.FC = () => {
},
{
title: '状态',
dataIndex: 'alive',
dataIndex: 'status',
valueEnum: {
0: {
text: '异常',
Expand Down Expand Up @@ -148,14 +148,14 @@ const services: React.FC = () => {
toolBarRender={()=>
[
<Button key="button" icon={<PlusOutlined />} type="primary" onClick={() => { setCreateModalVisible(true) }}>
手工注册
注册
</Button>
]
}
/>
<ModalForm
formRef={addFormRef}
title='手工注册服务'
title='注册服务'
visible={createModalVisible}
onVisibleChange={setCreateModalVisible}
onFinish={
Expand Down

0 comments on commit 30d0afe

Please sign in to comment.