master
parent
ba81e00cb3
commit
720a6c7949
|
@ -10,7 +10,6 @@ import com.lyh.common.aliyun.config.AliConfig;
|
|||
import com.lyh.common.aliyun.model.InstanceSpecification;
|
||||
import com.lyh.common.redis.service.RedisService;
|
||||
import com.lyh.common.utils.user.UserUtil;
|
||||
import com.lyh.common.domain.InstancesInformation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -18,7 +17,6 @@ import org.springframework.stereotype.Component;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
|
@ -277,8 +275,6 @@ public class AliYunEcsService {
|
|||
* @Param: [instanceIds] i-uf6chlqotgoc9h173alu
|
||||
**/
|
||||
public List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> queryInstancesInformation(String instanceIds) {
|
||||
//初始化计数器
|
||||
AtomicInteger count = new AtomicInteger();
|
||||
|
||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||
.setRegionId(aliConfig.getRegionId())
|
||||
|
@ -301,50 +297,6 @@ public class AliYunEcsService {
|
|||
//
|
||||
instances = bodyInstances.getInstance();
|
||||
|
||||
//新建List,用于redis存储实例信息
|
||||
ArrayList<InstancesInformation> instancesInformations = new ArrayList<>();
|
||||
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance item : instances) {
|
||||
log.info("查询第{" + count.get() + 1 + "}个实例的ID:" + item.getInstanceId());
|
||||
log.info("名称:" + item.getInstanceName());
|
||||
log.info("地域ID:" + item.getRegionId());
|
||||
log.info("状态:" + item.getStatus());
|
||||
log.info("类型:" + item.getInstanceType());
|
||||
log.info("CPU核心数:" + item.getCpu());
|
||||
log.info("内存大小:" + item.getMemory() + "MB");
|
||||
log.info("磁盘大小:" + item.getLocalStorageCapacity() + "G");
|
||||
log.info("操作系统:" + item.getOSName());
|
||||
log.info("网络类型:" + item.getInstanceNetworkType());
|
||||
log.info("公网出带宽值:" + item.getInternetMaxBandwidthOut() + "Mbit/s");
|
||||
log.info("公网入带宽值:" + item.getInternetMaxBandwidthIn() + "Mbit/s");
|
||||
log.info("公网IP:" + UserUtil.removeBrackets(item.getPublicIpAddress().getIpAddress().toString()));
|
||||
log.info("私网IP:" + UserUtil.removeBrackets(item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString()));
|
||||
log.info("专有网络VPCID:" + item.getVpcAttributes().getVpcId());
|
||||
log.info("安全组ID:" + UserUtil.removeBrackets(item.getSecurityGroupIds().getSecurityGroupId().toString()));
|
||||
log.info("创建时间:" + item.getCreationTime());
|
||||
log.info("到期时间:" + item.getExpiredTime());
|
||||
log.info("是否可以回收:" + (item.getRecyclable() ? "是" : "否") + "\n\n");
|
||||
|
||||
//存入集合
|
||||
instancesInformations.add(
|
||||
new InstancesInformation(
|
||||
item.getInstanceId(),
|
||||
item.getInstanceName(),
|
||||
item.getStatus(),
|
||||
UserUtil.removeBrackets(item.getPublicIpAddress().getIpAddress().toString()),
|
||||
UserUtil.removeBrackets(item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString()),
|
||||
item.getCreationTime(),
|
||||
item.getExpiredTime(),
|
||||
item.getRecyclable()
|
||||
)
|
||||
);
|
||||
|
||||
count.getAndIncrement();
|
||||
}
|
||||
|
||||
//缓存数据
|
||||
redisService.deleteObject("服务器信息:");
|
||||
redisService.setCacheList("服务器信息:", instancesInformations);
|
||||
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
|
|
|
@ -3,12 +3,13 @@ package com.lyh.service.impl;
|
|||
import com.alibaba.fastjson2.JSON;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.lyh.common.aliyun.service.AliYunEcsService;
|
||||
import com.lyh.common.redis.service.RedisService;
|
||||
import com.lyh.common.utils.mqtt.MqttUtil;
|
||||
import com.lyh.common.utils.user.UserUtil;
|
||||
import com.lyh.common.domain.InstancesInformation;
|
||||
import com.lyh.common.domain.IpAndLoadCount;
|
||||
import com.lyh.common.domain.IpAndWeight;
|
||||
import com.lyh.common.domain.resp.Result;
|
||||
import com.lyh.common.redis.service.RedisService;
|
||||
import com.lyh.common.utils.mqtt.MqttUtil;
|
||||
import com.lyh.common.utils.user.UserUtil;
|
||||
import com.lyh.service.LoadCenterService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -38,18 +39,6 @@ public class LoadCenterServiceImpl implements LoadCenterService {
|
|||
private AliYunEcsService aliYunEcsService;
|
||||
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/17 20:31
|
||||
* @Description: 定时刷新实例公网IP列表缓存
|
||||
* @Param: []
|
||||
* @Return: void
|
||||
**/
|
||||
@Scheduled(cron = "0/2 * * * * ?")
|
||||
public void refreshEcsIPListCache() {
|
||||
getEcsIPList();
|
||||
}
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/15 21:49
|
||||
|
@ -97,10 +86,16 @@ public class LoadCenterServiceImpl implements LoadCenterService {
|
|||
* @Return: List<String>
|
||||
**/
|
||||
@PostConstruct
|
||||
@Scheduled(cron = "0/2 * * * * ?")
|
||||
public void getEcsIPList() {
|
||||
int count = 0;
|
||||
|
||||
//存IP的List
|
||||
ArrayList<String> ecsIPList = new ArrayList<>();
|
||||
|
||||
//新建List,用于redis存储实例信息
|
||||
ArrayList<InstancesInformation> instancesInformations = new ArrayList<>();
|
||||
|
||||
try {
|
||||
//获取上海区的实例ID列表
|
||||
List<String> ecsIDList = aliYunEcsService.getIDList();
|
||||
|
@ -114,17 +109,59 @@ public class LoadCenterServiceImpl implements LoadCenterService {
|
|||
|
||||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> response = aliYunEcsService.queryInstancesInformation(ids);
|
||||
|
||||
response.forEach(item -> {
|
||||
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance item : response) {
|
||||
log.info("查询第{" + count + "}个实例的ID:" + item.getInstanceId());
|
||||
log.info("名称:" + item.getInstanceName());
|
||||
log.info("地域ID:" + item.getRegionId());
|
||||
log.info("状态:" + item.getStatus());
|
||||
log.info("类型:" + item.getInstanceType());
|
||||
log.info("CPU核心数:" + item.getCpu());
|
||||
log.info("内存大小:" + item.getMemory() + "MB");
|
||||
log.info("磁盘大小:" + item.getLocalStorageCapacity() + "G");
|
||||
log.info("操作系统:" + item.getOSName());
|
||||
log.info("网络类型:" + item.getInstanceNetworkType());
|
||||
log.info("公网出带宽值:" + item.getInternetMaxBandwidthOut() + "Mbit/s");
|
||||
log.info("公网入带宽值:" + item.getInternetMaxBandwidthIn() + "Mbit/s");
|
||||
log.info("公网IP:" + UserUtil.removeBrackets(item.getPublicIpAddress().getIpAddress().toString()));
|
||||
log.info("私网IP:" + UserUtil.removeBrackets(item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString()));
|
||||
log.info("专有网络VPCID:" + item.getVpcAttributes().getVpcId());
|
||||
log.info("安全组ID:" + UserUtil.removeBrackets(item.getSecurityGroupIds().getSecurityGroupId().toString()));
|
||||
log.info("创建时间:" + item.getCreationTime());
|
||||
log.info("到期时间:" + item.getExpiredTime());
|
||||
log.info("是否可以回收:" + (item.getRecyclable() ? "是" : "否") + "\n\n");
|
||||
|
||||
//存入集合
|
||||
instancesInformations.add(
|
||||
new InstancesInformation(
|
||||
item.getInstanceId(),
|
||||
item.getInstanceName(),
|
||||
item.getStatus(),
|
||||
UserUtil.removeBrackets(item.getPublicIpAddress().getIpAddress().toString()),
|
||||
UserUtil.removeBrackets(item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString()),
|
||||
item.getCreationTime(),
|
||||
item.getExpiredTime(),
|
||||
item.getRecyclable()
|
||||
)
|
||||
);
|
||||
|
||||
//获取添加的实例的公网ip
|
||||
String ip = UserUtil.removeBrackets(item.getPublicIpAddress().getIpAddress().toString());
|
||||
ecsIPList.add(ip);
|
||||
});
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取实例ID列表失败:{}", e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
//缓存数据
|
||||
redis.deleteObject("服务器信息:");
|
||||
redis.setCacheList("服务器信息:", instancesInformations);
|
||||
|
||||
//将IP列表存入redis
|
||||
redis.deleteObject("实例IP列表:");
|
||||
redis.setCacheList("实例IP列表:", ecsIPList);
|
||||
|
|
Loading…
Reference in New Issue