parent
3857e3a875
commit
a3059cbbf7
|
@ -4,6 +4,7 @@ import com.zhilian.common.security.annotation.EnableCustomConfig;
|
||||||
import com.zhilian.common.security.annotation.EnableMyFeignClients;
|
import com.zhilian.common.security.annotation.EnableMyFeignClients;
|
||||||
import com.zhilian.common.swagger.annotation.EnableCustomSwagger2;
|
import com.zhilian.common.swagger.annotation.EnableCustomSwagger2;
|
||||||
import com.zhilian.online.controller.OnlineLoadCenterController;
|
import com.zhilian.online.controller.OnlineLoadCenterController;
|
||||||
|
import com.zhilian.online.domain.EcsInstance;
|
||||||
import com.zhilian.online.domain.req.EcsCreateReq;
|
import com.zhilian.online.domain.req.EcsCreateReq;
|
||||||
import com.zhilian.online.uitls.AliyunOpenAPIUtils;
|
import com.zhilian.online.uitls.AliyunOpenAPIUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -14,6 +15,8 @@ import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import com.aliyun.ecs20140526.models.RunInstancesRequest;
|
import com.aliyun.ecs20140526.models.RunInstancesRequest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @version:
|
* @version:
|
||||||
* @Author: LiYuan
|
* @Author: LiYuan
|
||||||
|
@ -58,8 +61,11 @@ public class ZhiLianOnlineApplication implements ApplicationRunner {
|
||||||
.periodUnit("Week")
|
.periodUnit("Week")
|
||||||
.period(1)
|
.period(1)
|
||||||
.instanceChargeType("PostPaid")
|
.instanceChargeType("PostPaid")
|
||||||
.privateIpAddress("10.10.27.1")
|
// .privateIpAddress("10.10.27.1")
|
||||||
.build();
|
.build();
|
||||||
aliyunOpenAPIUtils.createInstance(ecs);
|
List<String> instance = aliyunOpenAPIUtils.createInstance(ecs);
|
||||||
|
|
||||||
|
List<EcsInstance> ecsInstances = aliyunOpenAPIUtils.queryEcsInstances(instance);
|
||||||
|
log.info("创建的实例信息为{}" , ecsInstances);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.zhilian.online.config;
|
||||||
|
|
||||||
import com.aliyun.ecs20140526.Client;
|
import com.aliyun.ecs20140526.Client;
|
||||||
import com.aliyun.teaopenapi.models.Config;
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
@ -21,6 +22,7 @@ import com.aliyun.teaopenapi.models.Config;
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties("aliyun.ecs")
|
@ConfigurationProperties("aliyun.ecs")
|
||||||
|
@Data
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class AliyunEcsConfig {
|
public class AliyunEcsConfig {
|
||||||
|
|
||||||
|
@ -39,12 +41,17 @@ public class AliyunEcsConfig {
|
||||||
*/
|
*/
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 地域
|
||||||
|
*/
|
||||||
|
private String regionId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用AK&SK初始化账号Client
|
* 使用AK&SK初始化账号Client
|
||||||
* @return Client
|
* @return Client
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Bean("AliyunClient")
|
@Bean()
|
||||||
public Client createClient() throws Exception {
|
public Client createClient() throws Exception {
|
||||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||||
|
@ -52,7 +59,8 @@ public class AliyunEcsConfig {
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
.setAccessKeyId(accessKeyId)
|
.setAccessKeyId(accessKeyId)
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
.setAccessKeySecret(accessKeySecret);
|
.setAccessKeySecret(accessKeySecret)
|
||||||
|
.setRegionId(regionId);
|
||||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||||
config.endpoint = endpoint;
|
config.endpoint = endpoint;
|
||||||
Client aliyunClient = new Client(config);
|
Client aliyunClient = new Client(config);
|
||||||
|
|
|
@ -100,7 +100,7 @@ public class MqttxConfig {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@PostConstruct
|
// @PostConstruct
|
||||||
public void initMqtt() {
|
public void initMqtt() {
|
||||||
log.info("mqttx连接中......");
|
log.info("mqttx连接中......");
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.zhilian.online.domain.req;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @BelongsProject: smart-cloud-server
|
* @BelongsProject: smart-cloud-server
|
||||||
|
@ -55,7 +56,7 @@ public class EcsQueryReq {
|
||||||
/**
|
/**
|
||||||
* 实例ID ["i-bp67acfmxazb4p****", "i-bp67acfmxazb4p****", … "i-bp67acfmxazb4p****"]
|
* 实例ID ["i-bp67acfmxazb4p****", "i-bp67acfmxazb4p****", … "i-bp67acfmxazb4p****"]
|
||||||
*/
|
*/
|
||||||
private String instanceIds;
|
private List<String> instanceIds;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 页码
|
* 页码
|
||||||
|
|
|
@ -12,7 +12,7 @@ import java.util.List;
|
||||||
* @BelongsPackage: com.zhilian.online.load.cache
|
* @BelongsPackage: com.zhilian.online.load.cache
|
||||||
* @Author: LiYuan
|
* @Author: LiYuan
|
||||||
* @CreateTime: 2024-04-18 19:11
|
* @CreateTime: 2024-04-18 19:11
|
||||||
* @Description: 网关负载均衡节点缓存
|
* @Description: 加权轮询的100个节点IP 为之后车辆上线取用
|
||||||
* @Version: 1.0
|
* @Version: 1.0
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
|
@ -28,7 +28,7 @@ public class GatewayLoadNodeCache extends GatewayCacheAbs<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 存负载节点
|
* @description: 存放
|
||||||
* @author: LiYuan
|
* @author: LiYuan
|
||||||
* @param: nodeList
|
* @param: nodeList
|
||||||
* @return: void
|
* @return: void
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.zhilian.online.load.cache;
|
||||||
|
|
||||||
|
import com.zhilian.online.load.abs.GatewayCacheAbs;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.load.cache
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-19 19:42
|
||||||
|
* @Description: 节点负载的车辆数据操作
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public class GatewayNodeCarsCache extends GatewayCacheAbs<String> {
|
||||||
|
@Override
|
||||||
|
public String getPre() {
|
||||||
|
return "gateway:node:cars";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 车辆上线添加数据
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: String nodeId, String vin
|
||||||
|
* @return: void
|
||||||
|
**/
|
||||||
|
public void put(String nodeId, String vin) {
|
||||||
|
redisService.setCacheSet(encode(nodeId), vin);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 根据节点获取其负载车辆vin列表
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: String nodeId
|
||||||
|
* @return: void
|
||||||
|
**/
|
||||||
|
public List<String> get(String nodeId) {
|
||||||
|
Set<String> res = redisService.getCacheSet(encode(nodeId));
|
||||||
|
return res.stream().map(String::valueOf).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 删除下线车辆
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: String nodeId, String vin
|
||||||
|
* @return: void
|
||||||
|
**/
|
||||||
|
public void remove(String nodeId, String vin) {
|
||||||
|
redisService.deleteCacheSetValue(encode(nodeId), vin);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -11,16 +11,16 @@ import java.util.List;
|
||||||
* @BelongsPackage: com.zhilian.online.load.cache
|
* @BelongsPackage: com.zhilian.online.load.cache
|
||||||
* @Author: LiYuan
|
* @Author: LiYuan
|
||||||
* @CreateTime: 2024-04-18 19:44
|
* @CreateTime: 2024-04-18 19:44
|
||||||
* @Description: 网关负载中心节点数据缓存 String key + nodeInfo 对象
|
* @Description: 网关负载中心节点数据缓存 String key(节点ID) + 节点 对象
|
||||||
* @Version: 1.0
|
* @Version: 1.0
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class GatewayNodeCache extends GatewayCacheAbs<String > {
|
public class GatewayNodeInfoCache extends GatewayCacheAbs<String > {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPre() {
|
public String getPre() {
|
||||||
return "gateway:node:info";
|
return "gateway:node:info:";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -1,41 +0,0 @@
|
||||||
package com.zhilian.online.load.cache;
|
|
||||||
|
|
||||||
import com.zhilian.online.load.abs.GatewayCacheAbs;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @BelongsProject: smart-cloud-server
|
|
||||||
* @BelongsPackage: com.zhilian.online.load.cache
|
|
||||||
* @Author: LiYuan
|
|
||||||
* @CreateTime: 2024-04-18 20:03
|
|
||||||
* @Description: 收集节点负载量数据 Zset : 车辆连接数
|
|
||||||
* @Version: 1.0
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class GatewayNodeLoadInfoCache extends GatewayCacheAbs<String > {
|
|
||||||
|
|
||||||
private final static String gatewayZset = "node-connects:";
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getPre() {
|
|
||||||
return "gateway:Zset:";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @description: 获取所有Zset集合
|
|
||||||
* @author: LiYuan
|
|
||||||
* @param:
|
|
||||||
* @return:
|
|
||||||
**/
|
|
||||||
public Map<Object,Double> get(){
|
|
||||||
return redisService.getCacheObject(encode(gatewayZset));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.zhilian.online.load.cache;
|
||||||
|
|
||||||
|
import com.zhilian.online.domain.model.GatewayNodeInfo;
|
||||||
|
import com.zhilian.online.load.abs.GatewayCacheAbs;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.load.cache
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-18 20:03
|
||||||
|
* @Description: 节点权重数据
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GatewayNodeWeightCache extends GatewayCacheAbs<String > {
|
||||||
|
|
||||||
|
private final static String gatewayNodeLoadKey = "weight";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPre() {
|
||||||
|
return "gateway:node:";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 获取所有Zset集合
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: void
|
||||||
|
* @return: Map<Object,Double>
|
||||||
|
**/
|
||||||
|
public Map<Object,Double> get(){
|
||||||
|
return redisService.getCacheObject(encode(gatewayNodeLoadKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 获取指定节点分数
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: gatewayNodeInfo
|
||||||
|
* @return: score
|
||||||
|
**/
|
||||||
|
public Double get(GatewayNodeInfo gatewayNodeInfo){
|
||||||
|
return redisService.getCacheZsetScore(encode(gatewayNodeLoadKey),gatewayNodeInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void put(GatewayNodeInfo gatewayNodeInfo){
|
||||||
|
redisService.setCacheZsetValue(encode(gatewayNodeLoadKey),gatewayNodeInfo,0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void increment(GatewayNodeInfo gatewayNodeInfo,Double score){
|
||||||
|
redisService.incrementScore(encode(gatewayNodeLoadKey),gatewayNodeInfo,score);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -8,13 +8,12 @@ import org.springframework.stereotype.Component;
|
||||||
* @BelongsPackage: com.zhilian.online.load.cache
|
* @BelongsPackage: com.zhilian.online.load.cache
|
||||||
* @Author: LiYuan
|
* @Author: LiYuan
|
||||||
* @CreateTime: 2024-04-18 19:58
|
* @CreateTime: 2024-04-18 19:58
|
||||||
* @Description: 车辆上线数据
|
* @Description: 车辆连接数据
|
||||||
* @Version: 1.0
|
* @Version: 1.0
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class GatewayVehicleNodeCaChe extends GatewayCacheAbs<String> {
|
public class GatewayVehicleNodeCaChe extends GatewayCacheAbs<String> {
|
||||||
|
|
||||||
private final static String gatewayCarBusinessKey = "business:";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getPre() {
|
public String getPre() {
|
||||||
|
@ -28,7 +27,7 @@ public class GatewayVehicleNodeCaChe extends GatewayCacheAbs<String> {
|
||||||
* @return:
|
* @return:
|
||||||
**/
|
**/
|
||||||
public void put(String vin, String nodeId) {
|
public void put(String vin, String nodeId) {
|
||||||
redisService.setCacheObject(encode(gatewayCarBusinessKey) + vin,nodeId);
|
redisService.setCacheObject(encode(vin),nodeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,7 +38,7 @@ public class GatewayVehicleNodeCaChe extends GatewayCacheAbs<String> {
|
||||||
* @return:
|
* @return:
|
||||||
**/
|
**/
|
||||||
public void remove(String vin){
|
public void remove(String vin){
|
||||||
redisService.deleteObject(encode(gatewayCarBusinessKey) + vin);
|
redisService.deleteObject(encode(vin));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,7 +48,7 @@ public class GatewayVehicleNodeCaChe extends GatewayCacheAbs<String> {
|
||||||
* @return:
|
* @return:
|
||||||
**/
|
**/
|
||||||
public String get(String vin){
|
public String get(String vin){
|
||||||
return redisService.getCacheObject(encode(gatewayCarBusinessKey) + vin);
|
return redisService.getCacheObject(encode(vin));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import com.zhilian.common.redis.service.RedisService;
|
||||||
import com.zhilian.online.config.RabbitConfig;
|
import com.zhilian.online.config.RabbitConfig;
|
||||||
import com.zhilian.online.constans.OnlineConstants;
|
import com.zhilian.online.constans.OnlineConstants;
|
||||||
import com.zhilian.online.domain.ApifoxModel;
|
import com.zhilian.online.domain.ApifoxModel;
|
||||||
|
import com.zhilian.online.load.cache.GatewayNodeInfoCache;
|
||||||
import com.zhilian.online.mapper.OnlineLoadCenterMapper;
|
import com.zhilian.online.mapper.OnlineLoadCenterMapper;
|
||||||
import com.zhilian.online.service.OnlineLoadCenterService;
|
import com.zhilian.online.service.OnlineLoadCenterService;
|
||||||
import com.zhilian.online.uitls.MqttUtil;
|
import com.zhilian.online.uitls.MqttUtil;
|
||||||
|
@ -54,8 +55,12 @@ public class OnlineLoadCenterServiceImpl implements OnlineLoadCenterService{
|
||||||
@Autowired
|
@Autowired
|
||||||
private MqttUtil mqttUtil;
|
private MqttUtil mqttUtil;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private GatewayNodeInfoCache gatewayNodeInfoCache;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 车辆申请注解令牌接口, 仅限内网访问
|
* @description: 申请注解令牌接口, 仅限内网访问
|
||||||
* @author: LiYuan
|
* @author: LiYuan
|
||||||
* @param: vehicle
|
* @param: vehicle
|
||||||
* @return: Result<OnlineAccount>
|
* @return: Result<OnlineAccount>
|
||||||
|
@ -74,6 +79,7 @@ public class OnlineLoadCenterServiceImpl implements OnlineLoadCenterService{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 节点上线
|
* @description: 节点上线
|
||||||
* @author: LiYuan
|
* @author: LiYuan
|
||||||
|
@ -128,6 +134,10 @@ public class OnlineLoadCenterServiceImpl implements OnlineLoadCenterService{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description:车辆下线接口
|
* @description:车辆下线接口
|
||||||
* @author: LiYuan
|
* @author: LiYuan
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.aliyun.ecs20140526.models.*;
|
||||||
import com.aliyun.tea.TeaException;
|
import com.aliyun.tea.TeaException;
|
||||||
import com.aliyun.teautil.models.RuntimeOptions;
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
import com.zhilian.common.core.domain.Result;
|
import com.zhilian.common.core.domain.Result;
|
||||||
|
import com.zhilian.online.config.AliyunEcsConfig;
|
||||||
import com.zhilian.online.domain.EcsInstance;
|
import com.zhilian.online.domain.EcsInstance;
|
||||||
import com.zhilian.online.domain.req.EcsCreateReq;
|
import com.zhilian.online.domain.req.EcsCreateReq;
|
||||||
import com.zhilian.online.domain.req.EcsQueryReq;
|
import com.zhilian.online.domain.req.EcsQueryReq;
|
||||||
|
@ -16,6 +17,7 @@ import org.springframework.validation.annotation.Validated;
|
||||||
import com.aliyun.ecs20140526.Client;
|
import com.aliyun.ecs20140526.Client;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,11 +36,11 @@ public class AliyunOpenAPIUtils {
|
||||||
* 阿里云ECS客户端
|
* 阿里云ECS客户端
|
||||||
*/
|
*/
|
||||||
@Autowired
|
@Autowired
|
||||||
@Qualifier(value = "AliyunClient")
|
|
||||||
private Client aliyunClient;
|
private Client aliyunClient;
|
||||||
|
|
||||||
@Value("aliyun.ecs.region-id")
|
@Autowired
|
||||||
private String regionId;
|
private AliyunEcsConfig aliyunEcsConfig;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,8 +49,8 @@ public class AliyunOpenAPIUtils {
|
||||||
* @param: ecsQueryReq
|
* @param: ecsQueryReq
|
||||||
* @return: List<EcsInstance>
|
* @return: List<EcsInstance>
|
||||||
*/
|
*/
|
||||||
public List<EcsInstance> queryEcsInstances(@Validated EcsQueryReq ecsQueryReq) {
|
public List<EcsInstance> queryEcsInstances(EcsQueryReq ecsQueryReq) {
|
||||||
log.info("开始创建实例");
|
log.info("开始查询实例{}",ecsQueryReq.getInstanceIds());
|
||||||
List<EcsInstance> ecsInstances = new ArrayList<>();
|
List<EcsInstance> ecsInstances = new ArrayList<>();
|
||||||
DescribeInstancesResponse describeInstancesResponse = null;
|
DescribeInstancesResponse describeInstancesResponse = null;
|
||||||
|
|
||||||
|
@ -75,6 +77,77 @@ public class AliyunOpenAPIUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询实例方法
|
||||||
|
*
|
||||||
|
* @param: ecsQueryReq
|
||||||
|
* @return: List<EcsInstance>
|
||||||
|
*/
|
||||||
|
public List<EcsInstance> queryEcsInstancesByPrivateIp(List<String> privateIpAddress) {
|
||||||
|
log.info("开始查询实例{}",privateIpAddress);
|
||||||
|
List<EcsInstance> ecsInstances = new ArrayList<>();
|
||||||
|
DescribeInstancesResponse describeInstancesResponse = null;
|
||||||
|
|
||||||
|
//将请求参数写入查询请求中
|
||||||
|
String ipaddress = parseIpListToString(privateIpAddress);
|
||||||
|
DescribeInstancesRequest describeInstancesRequest = setQueryReq(ipaddress);
|
||||||
|
|
||||||
|
RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
describeInstancesResponse = aliyunClient.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||||
|
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||||
|
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = body.getInstances();
|
||||||
|
|
||||||
|
ecsInstances = takeQueryInstances(instances);
|
||||||
|
|
||||||
|
} catch (TeaException error) {
|
||||||
|
log.error("code:[{}],message:[{}],data:[{}]", error.getCode(), error.getMessage(), error.getData());
|
||||||
|
} catch (Exception aError) {
|
||||||
|
TeaException error = new TeaException(aError.getMessage(), aError);
|
||||||
|
log.error("code:[{}],message:[{}],data:[{}]", error.getCode(), error.getMessage(), error.getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
return ecsInstances;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询实例方法
|
||||||
|
*
|
||||||
|
* @param: instanceIds
|
||||||
|
* @return: List<EcsInstance>
|
||||||
|
*/
|
||||||
|
public List<EcsInstance> queryEcsInstances(List<String> instanceIds) {
|
||||||
|
log.info("开始查询实例{}",instanceIds);
|
||||||
|
List<EcsInstance> ecsInstances = new ArrayList<>();
|
||||||
|
DescribeInstancesResponse describeInstancesResponse = null;
|
||||||
|
|
||||||
|
//将请求参数写入查询请求中
|
||||||
|
DescribeInstancesRequest describeInstancesRequest = setQueryReq(instanceIds);
|
||||||
|
|
||||||
|
RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
describeInstancesResponse = aliyunClient.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||||
|
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||||
|
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = body.getInstances();
|
||||||
|
|
||||||
|
ecsInstances = takeQueryInstances(instances);
|
||||||
|
|
||||||
|
} catch (TeaException error) {
|
||||||
|
log.error("code:[{}],message:[{}],data:[{}]", error.getCode(), error.getMessage(), error.getData());
|
||||||
|
} catch (Exception aError) {
|
||||||
|
TeaException error = new TeaException(aError.getMessage(), aError);
|
||||||
|
log.error("code:[{}],message:[{}],data:[{}]", error.getCode(), error.getMessage(), error.getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
return ecsInstances;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 创建阿里云实例
|
* @description: 创建阿里云实例
|
||||||
* @author: LiYuan
|
* @author: LiYuan
|
||||||
|
@ -109,20 +182,21 @@ public class AliyunOpenAPIUtils {
|
||||||
error.getCode(), error.getMessage(), error.getData());
|
error.getCode(), error.getMessage(), error.getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
return instanceIds;
|
|
||||||
|
|
||||||
|
// List<EcsInstance> ecsInstances = queryEcsInstances(instanceIds);
|
||||||
|
// putNodeOfCreatedCache(ecsInstances);
|
||||||
|
|
||||||
|
return instanceIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result deleteInstance(String instanceId) {
|
public Result deleteInstance(String instanceId) {
|
||||||
Result result = new Result<>();
|
Result result = new Result<>();
|
||||||
DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest()
|
DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest()
|
||||||
.setRegionId(regionId)
|
.setRegionId(aliyunEcsConfig.getRegionId())
|
||||||
.setDryRun(false)
|
.setDryRun(false)
|
||||||
.setForce(true)
|
.setForce(true)
|
||||||
.setTerminateSubscription(false)
|
.setTerminateSubscription(false)
|
||||||
.setInstanceId(java.util.Arrays.asList(
|
.setInstanceId(Arrays.asList(instanceId));
|
||||||
instanceId
|
|
||||||
));
|
|
||||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
try {
|
try {
|
||||||
// 复制代码运行请自行打印 API 的返回值
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
@ -150,23 +224,87 @@ public class AliyunOpenAPIUtils {
|
||||||
* @return: DescribeInstancesRequest
|
* @return: DescribeInstancesRequest
|
||||||
**/
|
**/
|
||||||
public DescribeInstancesRequest setQueryReq(EcsQueryReq ecsQueryReq) {
|
public DescribeInstancesRequest setQueryReq(EcsQueryReq ecsQueryReq) {
|
||||||
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
|
stringBuffer.append("[");
|
||||||
|
ecsQueryReq.getInstanceIds().forEach(item ->{
|
||||||
|
stringBuffer.append("\"" + item + "\",");
|
||||||
|
});
|
||||||
|
stringBuffer.deleteCharAt(stringBuffer.length()-1);
|
||||||
|
stringBuffer.append("]");
|
||||||
return new DescribeInstancesRequest()
|
return new DescribeInstancesRequest()
|
||||||
.setRegionId(regionId)
|
.setRegionId(aliyunEcsConfig.getRegionId())
|
||||||
.setInstanceName(ecsQueryReq.getInstanceName())
|
.setInstanceIds(stringBuffer.toString());
|
||||||
.setPageNumber(ecsQueryReq.getPageNumber())
|
// .setInstanceName(ecsQueryReq.getInstanceName())
|
||||||
.setPageSize(ecsQueryReq.getPageSize())
|
// .setPageNumber(ecsQueryReq.getPageNumber())
|
||||||
.setVpcId(ecsQueryReq.getVpcId())
|
// .setPageSize(ecsQueryReq.getPageSize())
|
||||||
.setVSwitchId(ecsQueryReq.getVSwitchId())
|
// .setVpcId(ecsQueryReq.getVpcId())
|
||||||
.setZoneId(ecsQueryReq.getZoneId())
|
// .setVSwitchId(ecsQueryReq.getVSwitchId())
|
||||||
.setInstanceNetworkType(ecsQueryReq.getInstanceNetworkType())
|
// .setZoneId(ecsQueryReq.getZoneId())
|
||||||
.setSecurityGroupId(ecsQueryReq.getSecurityGroupId())
|
// .setInstanceNetworkType(ecsQueryReq.getInstanceNetworkType())
|
||||||
.setInstanceIds(ecsQueryReq.getInstanceIds())
|
// .setSecurityGroupId(ecsQueryReq.getSecurityGroupId())
|
||||||
.setPrivateIpAddresses(ecsQueryReq.getPrivateIpAddresses())
|
// .setPrivateIpAddresses(ecsQueryReq.getPrivateIpAddresses())
|
||||||
.setPublicIpAddresses(ecsQueryReq.getPublicIpAddresses())
|
// .setPublicIpAddresses(ecsQueryReq.getPublicIpAddresses())
|
||||||
.setInstanceName(ecsQueryReq.getInstanceName())
|
// .setInstanceName(ecsQueryReq.getInstanceName())
|
||||||
.setImageId(ecsQueryReq.getImageId())
|
// .setImageId(ecsQueryReq.getImageId())
|
||||||
.setInstanceType(ecsQueryReq.getInstanceType())
|
// .setInstanceType(ecsQueryReq.getInstanceType())
|
||||||
.setStatus(ecsQueryReq.getStatus());
|
// .setStatus(ecsQueryReq.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 将请求参数写入查询请求中
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: EcsQueryReq
|
||||||
|
* @return: DescribeInstancesRequest
|
||||||
|
**/
|
||||||
|
public DescribeInstancesRequest setQueryReq(String ipAddresses) {
|
||||||
|
return new DescribeInstancesRequest()
|
||||||
|
.setRegionId(aliyunEcsConfig.getRegionId())
|
||||||
|
.setPrivateIpAddresses(ipAddresses);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String parseIpListToString(List<String> ipAddress){
|
||||||
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
|
stringBuffer.append("[");
|
||||||
|
ipAddress.forEach(item ->{
|
||||||
|
stringBuffer.append("\"" + item + "\",");
|
||||||
|
});
|
||||||
|
stringBuffer.deleteCharAt(stringBuffer.length()-1);
|
||||||
|
stringBuffer.append("]");
|
||||||
|
return stringBuffer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 将请求参数写入查询请求中
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: EcsQueryReq
|
||||||
|
* @return: DescribeInstancesRequest
|
||||||
|
**/
|
||||||
|
public DescribeInstancesRequest setQueryReq(List<String> instances) {
|
||||||
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
|
stringBuffer.append("[");
|
||||||
|
instances.forEach(item ->{
|
||||||
|
stringBuffer.append("\"" + item + "\",");
|
||||||
|
});
|
||||||
|
stringBuffer.deleteCharAt(stringBuffer.length()-1);
|
||||||
|
stringBuffer.append("]");
|
||||||
|
return new DescribeInstancesRequest()
|
||||||
|
.setRegionId(aliyunEcsConfig.getRegionId())
|
||||||
|
.setInstanceIds(stringBuffer.toString());
|
||||||
|
// .setInstanceName(ecsQueryReq.getInstanceName())
|
||||||
|
// .setPageNumber(ecsQueryReq.getPageNumber())
|
||||||
|
// .setPageSize(ecsQueryReq.getPageSize())
|
||||||
|
// .setVpcId(ecsQueryReq.getVpcId())
|
||||||
|
// .setVSwitchId(ecsQueryReq.getVSwitchId())
|
||||||
|
// .setZoneId(ecsQueryReq.getZoneId())
|
||||||
|
// .setInstanceNetworkType(ecsQueryReq.getInstanceNetworkType())
|
||||||
|
// .setSecurityGroupId(ecsQueryReq.getSecurityGroupId())
|
||||||
|
// .setPrivateIpAddresses(ecsQueryReq.getPrivateIpAddresses())
|
||||||
|
// .setPublicIpAddresses(ecsQueryReq.getPublicIpAddresses())
|
||||||
|
// .setInstanceName(ecsQueryReq.getInstanceName())
|
||||||
|
// .setImageId(ecsQueryReq.getImageId())
|
||||||
|
// .setInstanceType(ecsQueryReq.getInstanceType())
|
||||||
|
// .setStatus(ecsQueryReq.getStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -183,7 +321,7 @@ public class AliyunOpenAPIUtils {
|
||||||
EcsInstance ecsInstance = EcsInstance.builder()
|
EcsInstance ecsInstance = EcsInstance.builder()
|
||||||
.instanceId(item.getInstanceId())
|
.instanceId(item.getInstanceId())
|
||||||
.instanceName(item.getInstanceName())
|
.instanceName(item.getInstanceName())
|
||||||
.regionId(regionId)
|
.regionId(aliyunEcsConfig.getRegionId())
|
||||||
.status(item.getStatus())
|
.status(item.getStatus())
|
||||||
.instanceType(item.getInstanceType())
|
.instanceType(item.getInstanceType())
|
||||||
.cpu(item.getCpu())
|
.cpu(item.getCpu())
|
||||||
|
@ -221,7 +359,7 @@ public class AliyunOpenAPIUtils {
|
||||||
.setCategory(ecsCreateReq.getSystemDickCategory());
|
.setCategory(ecsCreateReq.getSystemDickCategory());
|
||||||
//构建创建实例请求
|
//构建创建实例请求
|
||||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||||
.setRegionId(regionId)
|
.setRegionId(aliyunEcsConfig.getRegionId())
|
||||||
.setImageId(ecsCreateReq.getImageId())
|
.setImageId(ecsCreateReq.getImageId())
|
||||||
.setInstanceType(ecsCreateReq.getInstanceType())
|
.setInstanceType(ecsCreateReq.getInstanceType())
|
||||||
.setSecurityGroupId(ecsCreateReq.getSecurityGroupId())
|
.setSecurityGroupId(ecsCreateReq.getSecurityGroupId())
|
||||||
|
@ -243,4 +381,6 @@ public class AliyunOpenAPIUtils {
|
||||||
return runInstancesRequest;
|
return runInstancesRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue