feature(web): 优化 cloud 服务相关信息获取

This commit is contained in:
2023-05-05 15:13:07 +08:00
parent f7c3b6f0be
commit 0d3d9049f7
4 changed files with 148 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
package com.lanyuanxiaoyao.service.web;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
@@ -8,7 +9,9 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;
import org.springframework.scheduling.annotation.EnableAsync;
/**

View File

@@ -1,7 +1,9 @@
package com.lanyuanxiaoyao.service.web.controller;
import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse;
import com.lanyuanxiaoyao.service.web.entity.CloudServiceVO;
import com.netflix.appinfo.InstanceInfo;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Maps;
@@ -9,6 +11,7 @@ import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -33,15 +36,32 @@ public class CloudController extends BaseController {
private ImmutableList<CloudServiceVO> serviceVOS(Function<CloudServiceVO.Service, String> groupByField) {
return Lists.immutable.ofAll(client.getServices())
.flatCollect(client::getInstances)
.collect(instance -> new CloudServiceVO.Service(
instance.getInstanceId(),
instance.getServiceId(),
instance.getHost(),
instance.getPort(),
instance.isSecure(),
instance.getUri().toString(),
Maps.immutable.ofAll(instance.getMetadata())
))
.collect(instance -> {
CloudServiceVO.Service service = new CloudServiceVO.Service(
instance.getInstanceId(),
instance.getServiceId(),
instance.getHost(),
instance.getPort(),
instance.isSecure(),
instance.getUri().toString(),
Maps.immutable.ofAll(instance.getMetadata())
);
if (instance instanceof EurekaServiceInstance) {
EurekaServiceInstance eurekaServiceInstance = (EurekaServiceInstance) instance;
InstanceInfo info = eurekaServiceInstance.getInstanceInfo();
service.setActionType(info.getActionType().name());
service.setStatus(info.getStatus().name());
service.setUpdateTime(info.getLastUpdatedTimestamp());
if (ObjectUtil.isNotNull(info.getLeaseInfo())) {
service.setDuration(info.getLeaseInfo().getDurationInSecs());
service.setRegistrationTime(info.getLeaseInfo().getRegistrationTimestamp());
service.setLastRenewalTime(info.getLeaseInfo().getRenewalTimestamp());
service.setEvictionTime(info.getLeaseInfo().getEvictionTimestamp());
service.setServiceUpTime(info.getLeaseInfo().getServiceUpTimestamp());
}
}
return service;
})
.groupBy(groupByField)
.toMap()
.collectValues((serviceId, services) -> new CloudServiceVO(serviceId, services.toList().toImmutable()))

View File

@@ -44,6 +44,15 @@ public class CloudServiceVO {
private final String url;
private final ImmutableMap<String, String> metadata;
private String actionType;
private String status;
private Long updateTime;
private Integer duration;
private Long registrationTime;
private Long lastRenewalTime;
private Long evictionTime;
private Long serviceUpTime;
public Service(String instanceId, String serviceId, String host, Integer port, Boolean secure, String url, ImmutableMap<String, String> metadata) {
this.instanceId = instanceId;
this.serviceId = serviceId;
@@ -82,6 +91,70 @@ public class CloudServiceVO {
return metadata;
}
public String getActionType() {
return actionType;
}
public void setActionType(String actionType) {
this.actionType = actionType;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
public Long getRegistrationTime() {
return registrationTime;
}
public void setRegistrationTime(Long registrationTime) {
this.registrationTime = registrationTime;
}
public Long getLastRenewalTime() {
return lastRenewalTime;
}
public void setLastRenewalTime(Long lastRenewalTime) {
this.lastRenewalTime = lastRenewalTime;
}
public Long getEvictionTime() {
return evictionTime;
}
public void setEvictionTime(Long evictionTime) {
this.evictionTime = evictionTime;
}
public Long getServiceUpTime() {
return serviceUpTime;
}
public void setServiceUpTime(Long serviceUpTime) {
this.serviceUpTime = serviceUpTime;
}
@Override
public String toString() {
return "Service{" +
@@ -92,6 +165,9 @@ public class CloudServiceVO {
", secure=" + secure +
", url='" + url + '\'' +
", metadata=" + metadata +
", actionType='" + actionType + '\'' +
", status='" + status + '\'' +
", updateTime='" + updateTime + '\'' +
'}';
}
}

View File

@@ -1,3 +1,10 @@
function time(field) {
return {
type: 'tpl',
tpl: `\${${field}|date:YYYY-MM-DD HH\\:mm\\:ss:x}`
}
}
function cloudCrud(title, path) {
return {
type: 'crud',
@@ -13,11 +20,41 @@ function cloudCrud(title, path) {
columns: [
{name: 'name', label: '名称'},
{
name: 'serviceId',
label: '服务',
width: 200,
name: 'status',
label: '状态',
align: 'center',
width: 60,
},
{
name: 'serviceUpTime',
label: '启动时间',
...time('serviceUpTime'),
align: 'center',
width: 140,
},
{
name: 'registrationTime',
label: '注册时间',
...time('registrationTime'),
align: 'center',
width: 140,
},
{
name: 'lastRenewalTime',
label: '上次更新时间',
...time('lastRenewalTime'),
align: 'center',
width: 140,
},
{
name: 'evictionTime',
label: '驱逐时间',
...time('evictionTime'),
align: 'center',
width: 140,
},
{name: 'url', label: '地址', width: 200},
{name: 'serviceId', label: '服务', width: 180, fixed: 'right'},
]
}
}