parent
4d1cb943ac
commit
3857e3a875
|
@ -156,13 +156,13 @@ public class RedisService {
|
||||||
/**
|
/**
|
||||||
* 缓存Set
|
* 缓存Set
|
||||||
*
|
*
|
||||||
* @param key 缓存键值
|
* @param key 缓存键值
|
||||||
* @param setValue 缓存的数据
|
* @param setValue 缓存的数据
|
||||||
* @return 缓存数据的对象
|
* @return 缓存数据的对象
|
||||||
*/
|
*/
|
||||||
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final T setValue) {
|
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final T setValue) {
|
||||||
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
|
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
|
||||||
setOperation.add(setValue);
|
setOperation.add(setValue);
|
||||||
return setOperation;
|
return setOperation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -272,8 +272,6 @@ public class RedisService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取Zset集合大小
|
* 获取Zset集合大小
|
||||||
*
|
*
|
||||||
|
@ -310,6 +308,7 @@ public class RedisService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 范围查询集合携带分数
|
* 范围查询集合携带分数
|
||||||
|
*
|
||||||
* @param zkey
|
* @param zkey
|
||||||
* @param start
|
* @param start
|
||||||
* @param end
|
* @param end
|
||||||
|
@ -329,46 +328,50 @@ public class RedisService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改对应键值分数,delta为正数则增加,为负数则减少
|
* 修改对应键值分数,delta为正数则增加,为负数则减少
|
||||||
|
*
|
||||||
* @param zkey
|
* @param zkey
|
||||||
* @param value
|
* @param value
|
||||||
* @param delta
|
* @param delta
|
||||||
* @param <T>
|
* @param <T>
|
||||||
*/
|
*/
|
||||||
public <T> Double incrementScore(final String zkey, final T value, final Double delta){
|
public <T> Double incrementScore(final String zkey, final T value, final Double delta) {
|
||||||
return redisTemplate.opsForZSet().incrementScore(zkey,value,delta);
|
return redisTemplate.opsForZSet().incrementScore(zkey, value, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除元素
|
* 删除元素
|
||||||
|
*
|
||||||
* @param zkey
|
* @param zkey
|
||||||
* @param values
|
* @param values
|
||||||
* @return
|
|
||||||
* @param <T>
|
* @param <T>
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
public <T> Long removeCacheZsetBatch(final String zkey,final T... values){
|
public <T> Long removeCacheZsetBatch(final String zkey, final T... values) {
|
||||||
return redisTemplate.opsForZSet().remove(zkey,values);
|
return redisTemplate.opsForZSet().remove(zkey, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 排序并限制结果集大小,按降序返回有序集合的部分元素,支持指定索引范围。
|
* 排序并限制结果集大小,按降序返回有序集合的部分元素,支持指定索引范围。
|
||||||
|
*
|
||||||
* @param zkey
|
* @param zkey
|
||||||
* @param start
|
* @param start
|
||||||
* @param end
|
* @param end
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<Double> reverseRange(final String zkey,final Long start,final Long end){
|
public Set<Double> reverseRange(final String zkey, final Long start, final Long end) {
|
||||||
return redisTemplate.opsForZSet().reverseRange(zkey,start,end);
|
return redisTemplate.opsForZSet().reverseRange(zkey, start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 按分数范围查询
|
* 按分数范围查询
|
||||||
|
*
|
||||||
* @param zkey
|
* @param zkey
|
||||||
* @param min
|
* @param min
|
||||||
* @param max
|
* @param max
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public Set<Double> rangeByScore(final String zkey,final Double min,final Double max){
|
public Set<Double> rangeByScore(final String zkey, final Double min, final Double max) {
|
||||||
return redisTemplate.opsForZSet().rangeByScore(zkey,min,max);
|
return redisTemplate.opsForZSet().rangeByScore(zkey, min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -377,7 +380,7 @@ public class RedisService {
|
||||||
* @param zkey
|
* @param zkey
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public ZSetOperations.TypedTuple getCacheZsetMin(final String zkey){
|
public ZSetOperations.TypedTuple getCacheZsetMin(final String zkey) {
|
||||||
ZSetOperations.TypedTuple typedTuple = redisTemplate.opsForZSet().popMin(zkey);
|
ZSetOperations.TypedTuple typedTuple = redisTemplate.opsForZSet().popMin(zkey);
|
||||||
return typedTuple;
|
return typedTuple;
|
||||||
}
|
}
|
||||||
|
@ -393,13 +396,37 @@ public class RedisService {
|
||||||
return redisTemplate.keys(pattern);
|
return redisTemplate.keys(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteCacheSet(String key) {
|
public void deleteCacheSet(final String key) {
|
||||||
SetOperations setOperations = redisTemplate.opsForSet();
|
SetOperations setOperations = redisTemplate.opsForSet();
|
||||||
setOperations.remove(key);
|
setOperations.remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> void deleteCacheSetValue(String key, T value) {
|
public <T> void deleteCacheSetValue(final String key, final T value) {
|
||||||
SetOperations setOperations = redisTemplate.opsForSet();
|
SetOperations setOperations = redisTemplate.opsForSet();
|
||||||
setOperations.remove(key,value);
|
setOperations.remove(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 根据下标获取List中value
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: key ,index 下标
|
||||||
|
* @return: value
|
||||||
|
**/
|
||||||
|
public <T> T getcacheListValue(final String key, final Long index) {
|
||||||
|
ListOperations listOperations = redisTemplate.opsForList();
|
||||||
|
|
||||||
|
return (T) listOperations.index(key, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 增加String类型中缓存值
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: key,number
|
||||||
|
* @return: Long 增加后的值
|
||||||
|
**/
|
||||||
|
public Long increment(final String key,final Long number) {
|
||||||
|
ValueOperations valueOperations = redisTemplate.opsForValue();
|
||||||
|
return valueOperations.increment(key,number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.zhilian.online.Timer;
|
||||||
|
|
||||||
|
import com.zhilian.online.uitls.AliyunOpenAPIUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.Timer
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-17 21:53
|
||||||
|
* @Description: 收集节点监视者
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class LoadObserver {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阿里云api接口类
|
||||||
|
*/
|
||||||
|
@Autowired
|
||||||
|
private AliyunOpenAPIUtils aliyunOpenAPIUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监视负载量是否需要扩容
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "0/10 * * * * ?")
|
||||||
|
public void addInstance(){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监视负载量是否需要缩容
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "0/10 * * * * ?")
|
||||||
|
public void ReduceInstance(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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.req.EcsCreateReq;
|
||||||
import com.zhilian.online.uitls.AliyunOpenAPIUtils;
|
import com.zhilian.online.uitls.AliyunOpenAPIUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -11,6 +12,7 @@ import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import com.aliyun.ecs20140526.models.RunInstancesRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @version:
|
* @version:
|
||||||
|
@ -23,16 +25,41 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@EnableMyFeignClients
|
@EnableMyFeignClients
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class ZhiLianOnlineApplication implements ApplicationRunner{
|
public class ZhiLianOnlineApplication implements ApplicationRunner {
|
||||||
@Autowired
|
@Autowired
|
||||||
private AliyunOpenAPIUtils aliyunOpenAPIUtils;
|
private AliyunOpenAPIUtils aliyunOpenAPIUtils;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(ZhiLianOnlineApplication.class,args);
|
SpringApplication.run(ZhiLianOnlineApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
log.info("项目启动,调用创建实例方法");
|
log.info("项目启动,调用创建实例方法");
|
||||||
// aliyunOpenAPIUtils.createInstance();
|
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk();
|
||||||
|
systemDisk = systemDisk.setSize("20").setCategory("cloud_efficiency");
|
||||||
|
EcsCreateReq ecs = EcsCreateReq.builder()
|
||||||
|
.regionId("cn-zhangjiakou")
|
||||||
|
.imageId("m-8vbfx0e48cekro0f13bx")
|
||||||
|
.instanceType("ecs.t6-c1m1.large")
|
||||||
|
.securityGroupId("sg-8vb49jd1c1lsa3akwo02")
|
||||||
|
.vSwitchId("vsw-8vb0krtyfdmb27nhcmzc2")
|
||||||
|
.instanceName("gather-node")
|
||||||
|
.internetMaxBandwidthIn(80)
|
||||||
|
.internetMaxBandwidthOut(10)
|
||||||
|
.uniqueSuffix(true)
|
||||||
|
.password("142730.Ly")
|
||||||
|
.zoneId("cn-zhangjiakou-c")
|
||||||
|
.internetChargeType("PayByTraffic")
|
||||||
|
.systemDiskSize("20")
|
||||||
|
.systemDickCategory("cloud_efficiency")
|
||||||
|
.amount(1)
|
||||||
|
.minAmount(1)
|
||||||
|
.periodUnit("Week")
|
||||||
|
.period(1)
|
||||||
|
.instanceChargeType("PostPaid")
|
||||||
|
.privateIpAddress("10.10.27.1")
|
||||||
|
.build();
|
||||||
|
aliyunOpenAPIUtils.createInstance(ecs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ public class OnlineConstants {
|
||||||
/**
|
/**
|
||||||
* 申请注册令牌缓存前缀
|
* 申请注册令牌缓存前缀
|
||||||
*/
|
*/
|
||||||
public static final String ONLINE_TOKEN_PREFIX = "online_token:";
|
public static final String NODE_TOKEN_PREFIX = "node_token:";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 申请令牌过期时间
|
* 申请令牌过期时间
|
||||||
|
@ -26,12 +26,27 @@ public class OnlineConstants {
|
||||||
/**
|
/**
|
||||||
* 节点负载前缀
|
* 节点负载前缀
|
||||||
*/
|
*/
|
||||||
public static final String GATHER_LOAD_CONTROL = "gather_info_control";
|
public static final String GATHER_LOAD_PREFIX = "gather:load:";
|
||||||
/**
|
/**
|
||||||
* 节点负载信息
|
* 在线节点前缀
|
||||||
*/
|
*/
|
||||||
public static final String ONLINE_VEHICLE = "online_vehicle:";
|
public static final String ONLINE_GATHER_PREFIX = "online_vehicle:";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* http请求前缀
|
||||||
|
*/
|
||||||
|
public static final String HTTP_PREFIX = "http://";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https请求前缀
|
||||||
|
*/
|
||||||
|
public static final String HTTPS_PREFIX = "https://";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fluxMQ请求地址
|
||||||
|
*/
|
||||||
|
public static final String FLUXMQ_INFO_URL = ":8080/public/cluster";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
package com.zhilian.online.consumer;
|
package com.zhilian.online.consumer;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.zhilian.common.core.constant.Constants;
|
import com.zhilian.common.core.constant.Constants;
|
||||||
import com.zhilian.common.redis.service.RedisService;
|
import com.zhilian.common.redis.service.RedisService;
|
||||||
import com.zhilian.online.config.RabbitConfig;
|
|
||||||
import com.zhilian.online.constans.OnlineConstants;
|
import com.zhilian.online.constans.OnlineConstants;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@ -54,8 +51,8 @@ public class DeadQueueConsumer {
|
||||||
if (Constants.FAIL == responseCode){
|
if (Constants.FAIL == responseCode){
|
||||||
log.error("节点{}上线失败",gatherMsg);
|
log.error("节点{}上线失败",gatherMsg);
|
||||||
//上线失败需要将该节点的负载均衡缓存删除
|
//上线失败需要将该节点的负载均衡缓存删除
|
||||||
if (redisService.hasKey(OnlineConstants.ONLINE_TOKEN_PREFIX + "")){
|
if (redisService.hasKey(OnlineConstants.NODE_TOKEN_PREFIX + "")){
|
||||||
redisService.removeCacheZsetBatch(OnlineConstants.ONLINE_TOKEN_PREFIX + "");
|
redisService.removeCacheZsetBatch(OnlineConstants.NODE_TOKEN_PREFIX + "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
package com.zhilian.online.controller;
|
|
||||||
|
|
||||||
import com.zhilian.common.core.domain.Result;
|
|
||||||
|
|
||||||
import com.zhilian.online.service.OnlineGatherService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @version:
|
|
||||||
* @Author: LiYuan
|
|
||||||
* @description: 车辆收集节点控制层
|
|
||||||
* @date: 2024/3/29 20:46
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/gather")
|
|
||||||
public class OnlineGatherController {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 车辆收集节点
|
|
||||||
*/
|
|
||||||
@Autowired
|
|
||||||
private OnlineGatherService onlineGatherService;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -40,14 +40,14 @@ public class OnlineLoadCenterController extends BaseController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 节点申请注解令牌接口, 仅限内网访问
|
* @description: 节点申请注解令牌接口, 仅限内网访问
|
||||||
* @author: LiY
|
* @author: LiYuan
|
||||||
* @param: vehicle
|
* @param: vehicle
|
||||||
* @return: Result<OnlineAccount>
|
* @return: Result<OnlineAccount>
|
||||||
**/
|
**/
|
||||||
@GetMapping("/applyForReg")
|
@GetMapping("/applyForReg")
|
||||||
public Result<String> applyForReg() {
|
public Result<String> applyForReg(String clusterId) {
|
||||||
log.info("申请注册令牌");
|
log.info("节点{}申请注册令牌",clusterId);
|
||||||
return onlineLoadCenterService.applyForReg();
|
return onlineLoadCenterService.applyForReg(clusterId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,49 +0,0 @@
|
||||||
package com.zhilian.online.controller;/**
|
|
||||||
* @version:
|
|
||||||
* @Author: LiYuan
|
|
||||||
* @description:
|
|
||||||
* @date: 2024/3/30 11:18
|
|
||||||
*/
|
|
||||||
|
|
||||||
import com.zhilian.common.core.domain.Result;
|
|
||||||
import com.zhilian.online.domain.model.MqttServerModel;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @BelongsProject: smart-cloud-server
|
|
||||||
* @BelongsPackage: com.zhilian.online.controller
|
|
||||||
* @Author: LiYuan
|
|
||||||
* @CreateTime: 2024-03-30 11:18
|
|
||||||
* @Description: 新增车辆上线验证控制器
|
|
||||||
* @Version: 1.0
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/verify")
|
|
||||||
public class OnlineVerifyController {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取配置文件中的mqtt服务地址
|
|
||||||
*/
|
|
||||||
@Value("${mqtt.server.broker}")
|
|
||||||
private String broker;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取配置文件中的订阅主题
|
|
||||||
*/
|
|
||||||
@Value("${mqtt.server.topic}")
|
|
||||||
private String topic;
|
|
||||||
|
|
||||||
@PostMapping("/vehicleConnection")
|
|
||||||
public Result<MqttServerModel> vehicleConnection() {
|
|
||||||
return Result.success(
|
|
||||||
MqttServerModel.builder()
|
|
||||||
.broker(broker)
|
|
||||||
.topic(topic)
|
|
||||||
.build()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.zhilian.online.domain.model;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.domain.model
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-18 19:47
|
||||||
|
* @Description: 收集节点信息
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
|
@Builder
|
||||||
|
public class GatewayNodeInfo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 节点id
|
||||||
|
*/
|
||||||
|
private String nodeId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公网IP
|
||||||
|
*/
|
||||||
|
private String publicIpAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 内网IP
|
||||||
|
*/
|
||||||
|
private String privateIpAddress;
|
||||||
|
|
||||||
|
}
|
|
@ -36,12 +36,12 @@ public class EcsCreateReq {
|
||||||
/**
|
/**
|
||||||
* 镜像ID
|
* 镜像ID
|
||||||
*/
|
*/
|
||||||
private String ImageId;
|
private String imageId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例规格
|
* 实例规格
|
||||||
*/
|
*/
|
||||||
private String InstanceType;
|
private String instanceType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 安全组ID
|
* 安全组ID
|
||||||
|
@ -85,37 +85,37 @@ public class EcsCreateReq {
|
||||||
private String zoneId;
|
private String zoneId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 网络计费类型
|
||||||
*/
|
*/
|
||||||
private String internetChargeType;
|
private String internetChargeType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 购买数量
|
||||||
*/
|
*/
|
||||||
private Integer amount;
|
private Integer amount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 最小购买数量
|
||||||
*/
|
*/
|
||||||
private Integer minAmount;
|
private Integer minAmount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 购买时长单位
|
||||||
*/
|
*/
|
||||||
private String periodUnit;
|
private String periodUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 购买时长
|
||||||
*/
|
*/
|
||||||
private Integer period;
|
private Integer period;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 实例付费类型
|
||||||
*/
|
*/
|
||||||
private String instanceChargeType;
|
private String instanceChargeType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 私网IP
|
||||||
*/
|
*/
|
||||||
private String privateIpAddress;
|
private String privateIpAddress;
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ import javax.validation.constraints.NotBlank;
|
||||||
* @BelongsPackage: com.zhilian.online.domain.req
|
* @BelongsPackage: com.zhilian.online.domain.req
|
||||||
* @Author: LiYuan
|
* @Author: LiYuan
|
||||||
* @CreateTime: 2024-04-12 15:37
|
* @CreateTime: 2024-04-12 15:37
|
||||||
* @Description: 调用阿里云openAPI查询实例请求实体类
|
* @Description: 阿里云openAPI查询实例请求
|
||||||
* @Version: 1.0
|
* @Version: 1.0
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.zhilian.online.load.abs;
|
||||||
|
|
||||||
|
import com.zhilian.common.redis.service.RedisService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.load.abs
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-18 19:09
|
||||||
|
* @Description: 网关负载均衡缓存抽象类
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public abstract class GatewayCacheAbs<K> {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public RedisService redisService;
|
||||||
|
|
||||||
|
public abstract String getPre();
|
||||||
|
|
||||||
|
public String encode(K key) {
|
||||||
|
return getPre() + key;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.zhilian.online.load.cache;
|
||||||
|
|
||||||
|
import com.zhilian.common.redis.service.RedisService;
|
||||||
|
import com.zhilian.online.load.abs.GatewayCacheAbs;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.load.cache
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-18 19:11
|
||||||
|
* @Description: 网关负载均衡节点缓存
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GatewayLoadNodeCache extends GatewayCacheAbs<String> {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private final static String gatewayLoadNodeKey = "node";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPre() {
|
||||||
|
return "gateway:load:";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 存负载节点
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: nodeList
|
||||||
|
* @return: void
|
||||||
|
**/
|
||||||
|
public void put(List<String> nodeList) {
|
||||||
|
redisService.deleteObject(encode(gatewayLoadNodeKey));
|
||||||
|
redisService.setCacheList(encode(gatewayLoadNodeKey), nodeList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 获取所有负载节点
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: null
|
||||||
|
* @return: List<String>
|
||||||
|
**/
|
||||||
|
public List<String> get() {
|
||||||
|
return redisService.getCacheList(encode(gatewayLoadNodeKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 通过下标获取节点
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: index 下标
|
||||||
|
* @return: String node 节点
|
||||||
|
**/
|
||||||
|
public String getByIndex(Long index){
|
||||||
|
if (null == index || index > 100){
|
||||||
|
throw new RuntimeException("下标违法,不在[0-100]内");
|
||||||
|
}
|
||||||
|
return redisService.getcacheListValue(encode(gatewayLoadNodeKey),index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
package com.zhilian.online.load.cache;
|
||||||
|
|
||||||
|
import com.zhilian.online.load.abs.GatewayCacheAbs;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.load.cache
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-18 19:34
|
||||||
|
* @Description: 网关负载自增序列
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GatewayLoadSeriesCache extends GatewayCacheAbs<String> {
|
||||||
|
|
||||||
|
private final static String gatewayLoadSeriesKey = "series";
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPre() {
|
||||||
|
return "gateway:load:";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 创建Bean后初始化自增序列
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param:
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
redisService.setCacheObject(encode(gatewayLoadSeriesKey), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 获取自增序列的值
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param:
|
||||||
|
* @return: Long
|
||||||
|
**/
|
||||||
|
public Long incrementAndGet() {
|
||||||
|
return redisService.increment(encode(gatewayLoadSeriesKey), 1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 获取当前序列的值
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param:
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
public Long get() {
|
||||||
|
return redisService.getCacheObject(encode(gatewayLoadSeriesKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 重置序列中的值
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param:
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
public void reset() {
|
||||||
|
this.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
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.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.load.cache
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-18 19:44
|
||||||
|
* @Description: 网关负载中心节点数据缓存 String key + nodeInfo 对象
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GatewayNodeCache extends GatewayCacheAbs<String > {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPre() {
|
||||||
|
return "gateway:node:info";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 增加节点数据
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: GatewayNodeInfo 节点数据
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
public void put(GatewayNodeInfo gatewayNodeInfo){
|
||||||
|
redisService.setCacheObject(encode(gatewayNodeInfo.getNodeId()),gatewayNodeInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 获取节点缓存数据
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: nodeId 节点Id
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
public GatewayNodeInfo get(String nodeId){
|
||||||
|
return redisService.getCacheObject(encode(nodeId));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 获取所有节点数据
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param:
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
public List<GatewayNodeInfo> get(){
|
||||||
|
return redisService.getCacheObject(encode(getPre()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 删除网关节点
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param:
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
public void remove(String nodeId){
|
||||||
|
redisService.deleteObject(encode(nodeId));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
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,56 @@
|
||||||
|
package com.zhilian.online.load.cache;
|
||||||
|
|
||||||
|
import com.zhilian.online.load.abs.GatewayCacheAbs;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.load.cache
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-18 19:58
|
||||||
|
* @Description: 车辆上线数据
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GatewayVehicleNodeCaChe extends GatewayCacheAbs<String> {
|
||||||
|
|
||||||
|
private final static String gatewayCarBusinessKey = "business:";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPre() {
|
||||||
|
return "gateway:car:";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 存放车辆数据
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: String vin, String nodeId
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
public void put(String vin, String nodeId) {
|
||||||
|
redisService.setCacheObject(encode(gatewayCarBusinessKey) + vin,nodeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description:删除车辆数据
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: String vin
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
public void remove(String vin){
|
||||||
|
redisService.deleteObject(encode(gatewayCarBusinessKey) + vin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 获取车辆连接数据
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: String vin
|
||||||
|
* @return:
|
||||||
|
**/
|
||||||
|
public String get(String vin){
|
||||||
|
return redisService.getCacheObject(encode(gatewayCarBusinessKey) + vin);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,17 +0,0 @@
|
||||||
package com.zhilian.online.mapper;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @version:
|
|
||||||
* @Author: LiYuan
|
|
||||||
* @description: 车辆收集节点数据层
|
|
||||||
* @date: 2024/3/29 20:48
|
|
||||||
*/
|
|
||||||
@Mapper
|
|
||||||
public interface OnlineGatherMapper{
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -17,7 +17,7 @@ public interface OnlineLoadCenterService{
|
||||||
* @param: vehicle
|
* @param: vehicle
|
||||||
* @return: Result<OnlineAccount>
|
* @return: Result<OnlineAccount>
|
||||||
**/
|
**/
|
||||||
Result<String> applyForReg();
|
Result<String> applyForReg(String clusterId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 车辆使用申请的一次性账户连接收集节点
|
* @description: 车辆使用申请的一次性账户连接收集节点
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
package com.zhilian.online.service.impl;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
||||||
import com.zhilian.common.core.domain.Result;
|
|
||||||
import com.zhilian.online.mapper.OnlineGatherMapper;
|
|
||||||
import com.zhilian.online.service.OnlineGatherService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @version:
|
|
||||||
* @Author: LiYuan
|
|
||||||
* @description: 车辆收集节点服务层
|
|
||||||
* @date: 2024/3/29 20:48
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
public class OnlineGatherServiceImpl implements OnlineGatherService {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private OnlineGatherMapper onlineGatherMapper;
|
|
||||||
|
|
||||||
}
|
|
|
@ -9,6 +9,7 @@ import com.zhilian.online.constans.OnlineConstants;
|
||||||
import com.zhilian.online.domain.ApifoxModel;
|
import com.zhilian.online.domain.ApifoxModel;
|
||||||
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 lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -47,6 +48,12 @@ public class OnlineLoadCenterServiceImpl implements OnlineLoadCenterService{
|
||||||
@Autowired
|
@Autowired
|
||||||
private RabbitTemplate rabbitTemplate;
|
private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MQTT服务
|
||||||
|
*/
|
||||||
|
@Autowired
|
||||||
|
private MqttUtil mqttUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 车辆申请注解令牌接口, 仅限内网访问
|
* @description: 车辆申请注解令牌接口, 仅限内网访问
|
||||||
* @author: LiYuan
|
* @author: LiYuan
|
||||||
|
@ -54,13 +61,13 @@ public class OnlineLoadCenterServiceImpl implements OnlineLoadCenterService{
|
||||||
* @return: Result<OnlineAccount>
|
* @return: Result<OnlineAccount>
|
||||||
**/
|
**/
|
||||||
@Override
|
@Override
|
||||||
public Result<String> applyForReg() {
|
public Result<String> applyForReg(String clusterId) {
|
||||||
|
|
||||||
//生成一次性令牌
|
//生成一次性令牌
|
||||||
String token = IdUtils.fastSimpleUUID();
|
String token = IdUtils.fastSimpleUUID();
|
||||||
|
|
||||||
//将令牌信息缓存到Redis中
|
//将令牌信息缓存到Redis中
|
||||||
redisService.setCacheObject(OnlineConstants.ONLINE_TOKEN_PREFIX + token,token, OnlineConstants.ONLINE_TOKEN_EXPIRE, TimeUnit.SECONDS);
|
redisService.setCacheObject(OnlineConstants.NODE_TOKEN_PREFIX + clusterId,token, OnlineConstants.ONLINE_TOKEN_EXPIRE, TimeUnit.SECONDS);
|
||||||
|
|
||||||
//将令牌信息返回客户端
|
//将令牌信息返回客户端
|
||||||
return Result.success(token);
|
return Result.success(token);
|
||||||
|
@ -76,16 +83,17 @@ public class OnlineLoadCenterServiceImpl implements OnlineLoadCenterService{
|
||||||
@Override
|
@Override
|
||||||
public Result regGather(ApifoxModel apifoxModel) {
|
public Result regGather(ApifoxModel apifoxModel) {
|
||||||
//判断登录令牌是否过期,一致
|
//判断登录令牌是否过期,一致
|
||||||
if (!redisService.hasKey(OnlineConstants.ONLINE_TOKEN_PREFIX + apifoxModel.getToken())) {
|
if (!redisService.hasKey(OnlineConstants.NODE_TOKEN_PREFIX + apifoxModel.getClusterId())) {
|
||||||
return Result.error("令牌已过期");
|
return Result.error("令牌已过期");
|
||||||
}
|
}
|
||||||
String token = redisService.getCacheObject(OnlineConstants.ONLINE_TOKEN_PREFIX + apifoxModel.getToken());
|
String token = redisService.getCacheObject(OnlineConstants.NODE_TOKEN_PREFIX + apifoxModel.getClusterId());
|
||||||
if (!token.equals(apifoxModel.getToken())){
|
if (!token.equals(apifoxModel.getToken())){
|
||||||
return Result.error("令牌错误");
|
return Result.error("令牌错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
//为该节点创建负载均衡缓存
|
//为该节点创建Zset负载均衡缓存,将该节点加入List在线节点缓存
|
||||||
redisService.setCacheZsetValue(OnlineConstants.GATHER_LOAD_CONTROL + apifoxModel.getClusterId(), JSON.toJSONString(apifoxModel), 0.0);
|
redisService.setCacheZsetValue(OnlineConstants.GATHER_LOAD_PREFIX, apifoxModel.getClusterId(), 0.0);
|
||||||
|
|
||||||
|
|
||||||
//向RabbitMQ||RocketMQ发送30s延迟消息,确保后续节点上线
|
//向RabbitMQ||RocketMQ发送30s延迟消息,确保后续节点上线
|
||||||
rabbitTemplate.convertAndSend(RabbitConfig.DELAY_EXCHANGE_NAME, RabbitConfig.DELAY_ROUTING_KEY, JSON.toJSONString(apifoxModel));
|
rabbitTemplate.convertAndSend(RabbitConfig.DELAY_EXCHANGE_NAME, RabbitConfig.DELAY_ROUTING_KEY, JSON.toJSONString(apifoxModel));
|
||||||
|
@ -110,10 +118,10 @@ public class OnlineLoadCenterServiceImpl implements OnlineLoadCenterService{
|
||||||
throw new RuntimeException("车辆未登记");
|
throw new RuntimeException("车辆未登记");
|
||||||
}
|
}
|
||||||
//获取负载最少的车辆进行链接
|
//获取负载最少的车辆进行链接
|
||||||
ZSetOperations.TypedTuple cacheZsetMin = redisService.getCacheZsetMin(OnlineConstants.GATHER_LOAD_CONTROL);
|
ZSetOperations.TypedTuple cacheZsetMin = redisService.getCacheZsetMin(OnlineConstants.GATHER_LOAD_PREFIX);
|
||||||
|
|
||||||
//存放节点车辆信息
|
//存放节点车辆信息
|
||||||
redisService.setCacheObject(OnlineConstants.ONLINE_VEHICLE+vin,1);
|
redisService.setCacheObject(OnlineConstants.GATHER_LOAD_PREFIX+vin,1);
|
||||||
//发送延迟队列确定车辆上线
|
//发送延迟队列确定车辆上线
|
||||||
rabbitTemplate.convertAndSend(RabbitConfig.DELAY_EXCHANGE_FOR_CAR, RabbitConfig.DELAY_ROUTING_FOR_CAR,vin);
|
rabbitTemplate.convertAndSend(RabbitConfig.DELAY_EXCHANGE_FOR_CAR, RabbitConfig.DELAY_ROUTING_FOR_CAR,vin);
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,14 @@ package com.zhilian.online.uitls;
|
||||||
import com.aliyun.ecs20140526.models.*;
|
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.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;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import com.aliyun.ecs20140526.Client;
|
import com.aliyun.ecs20140526.Client;
|
||||||
|
@ -35,6 +37,9 @@ public class AliyunOpenAPIUtils {
|
||||||
@Qualifier(value = "AliyunClient")
|
@Qualifier(value = "AliyunClient")
|
||||||
private Client aliyunClient;
|
private Client aliyunClient;
|
||||||
|
|
||||||
|
@Value("aliyun.ecs.region-id")
|
||||||
|
private String regionId;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询实例方法
|
* 查询实例方法
|
||||||
|
@ -76,20 +81,25 @@ public class AliyunOpenAPIUtils {
|
||||||
* @param:
|
* @param:
|
||||||
* @return: void
|
* @return: void
|
||||||
**/
|
**/
|
||||||
public void createInstance(EcsCreateReq ecsCreateReq) {
|
public List<String> createInstance(EcsCreateReq ecsCreateReq) {
|
||||||
//创建实例请求
|
//创建实例请求
|
||||||
RunInstancesRequest runInstancesRequest = makeRunInstancesRequest(ecsCreateReq);
|
RunInstancesRequest runInstancesRequest = makeRunInstancesRequest(ecsCreateReq);
|
||||||
|
|
||||||
RuntimeOptions runtime = new RuntimeOptions();
|
RuntimeOptions runtime = new RuntimeOptions();
|
||||||
|
|
||||||
|
List<String> instanceIds = new ArrayList<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 复制代码运行请自行打印 API 的返回值
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
RunInstancesResponse res = aliyunClient.runInstancesWithOptions(runInstancesRequest, runtime);
|
RunInstancesResponse res = aliyunClient.runInstancesWithOptions(runInstancesRequest, runtime);
|
||||||
|
|
||||||
//打印执行结果
|
//打印执行结果
|
||||||
RunInstancesResponseBody body = res.getBody();
|
RunInstancesResponseBody body = res.getBody();
|
||||||
|
instanceIds = body.getInstanceIdSets().getInstanceIdSet();
|
||||||
log.info("执行结果:[{}],请求ID:[{}],创建实例ID:[{}]",
|
log.info("执行结果:[{}],请求ID:[{}],创建实例ID:[{}]",
|
||||||
200 == res.getStatusCode() ? "创建成功" : "创建失败",
|
200 == res.getStatusCode() ? "创建成功" : "创建失败",
|
||||||
body.getRequestId(), body.getInstanceIdSets().getInstanceIdSet());
|
body.getRequestId(), instanceIds);
|
||||||
|
|
||||||
} catch (TeaException error) {
|
} catch (TeaException error) {
|
||||||
log.error("code:[{}],message:[{}],data:[{}]",
|
log.error("code:[{}],message:[{}],data:[{}]",
|
||||||
error.getCode(), error.getMessage(), error.getData());
|
error.getCode(), error.getMessage(), error.getData());
|
||||||
|
@ -98,6 +108,38 @@ public class AliyunOpenAPIUtils {
|
||||||
log.error("code:[{}],message:[{}],data:[{}]",
|
log.error("code:[{}],message:[{}],data:[{}]",
|
||||||
error.getCode(), error.getMessage(), error.getData());
|
error.getCode(), error.getMessage(), error.getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return instanceIds;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result deleteInstance(String instanceId) {
|
||||||
|
Result result = new Result<>();
|
||||||
|
DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest()
|
||||||
|
.setRegionId(regionId)
|
||||||
|
.setDryRun(false)
|
||||||
|
.setForce(true)
|
||||||
|
.setTerminateSubscription(false)
|
||||||
|
.setInstanceId(java.util.Arrays.asList(
|
||||||
|
instanceId
|
||||||
|
));
|
||||||
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
DeleteInstancesResponse deleteInstancesResponse = aliyunClient.deleteInstancesWithOptions(deleteInstancesRequest, runtime);
|
||||||
|
Integer statusCode = deleteInstancesResponse.getStatusCode();
|
||||||
|
DeleteInstancesResponseBody body = deleteInstancesResponse.getBody();
|
||||||
|
log.info(200 == statusCode ? "删除成功" : "删除失败");
|
||||||
|
result.setCode(statusCode);
|
||||||
|
result.setData(body);
|
||||||
|
result.setMsg(200 == statusCode ? "删除成功" : "删除失败");
|
||||||
|
} catch (TeaException error) {
|
||||||
|
log.error("code:[{}],message:[{}],data:[{}]", error.getCode(), error.getMessage(), error.getData());
|
||||||
|
} catch (Exception _error) {
|
||||||
|
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||||
|
log.error("code:[{}],message:[{}],data:[{}]", error.getCode(), error.getMessage(), error.getData());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -109,7 +151,7 @@ public class AliyunOpenAPIUtils {
|
||||||
**/
|
**/
|
||||||
public DescribeInstancesRequest setQueryReq(EcsQueryReq ecsQueryReq) {
|
public DescribeInstancesRequest setQueryReq(EcsQueryReq ecsQueryReq) {
|
||||||
return new DescribeInstancesRequest()
|
return new DescribeInstancesRequest()
|
||||||
.setRegionId(ecsQueryReq.getRegionId())
|
.setRegionId(regionId)
|
||||||
.setInstanceName(ecsQueryReq.getInstanceName())
|
.setInstanceName(ecsQueryReq.getInstanceName())
|
||||||
.setPageNumber(ecsQueryReq.getPageNumber())
|
.setPageNumber(ecsQueryReq.getPageNumber())
|
||||||
.setPageSize(ecsQueryReq.getPageSize())
|
.setPageSize(ecsQueryReq.getPageSize())
|
||||||
|
@ -141,7 +183,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(item.getRegionId())
|
.regionId(regionId)
|
||||||
.status(item.getStatus())
|
.status(item.getStatus())
|
||||||
.instanceType(item.getInstanceType())
|
.instanceType(item.getInstanceType())
|
||||||
.cpu(item.getCpu())
|
.cpu(item.getCpu())
|
||||||
|
@ -165,14 +207,21 @@ public class AliyunOpenAPIUtils {
|
||||||
return ecsInstances;
|
return ecsInstances;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RunInstancesRequest makeRunInstancesRequest(EcsCreateReq ecsCreateReq){
|
/**
|
||||||
|
* 构建阿里云创建实例请求
|
||||||
|
*
|
||||||
|
* @param: ecsCreateReq
|
||||||
|
* @return: RunInstancesRequest
|
||||||
|
*/
|
||||||
|
public RunInstancesRequest makeRunInstancesRequest(EcsCreateReq ecsCreateReq) {
|
||||||
|
|
||||||
//构建磁盘配置
|
//构建磁盘配置
|
||||||
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk()
|
RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||||
.setSize(ecsCreateReq.getSystemDiskSize())
|
.setSize(ecsCreateReq.getSystemDiskSize())
|
||||||
.setCategory(ecsCreateReq.getSystemDickCategory());
|
.setCategory(ecsCreateReq.getSystemDickCategory());
|
||||||
//构建创建实例请求
|
//构建创建实例请求
|
||||||
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
|
||||||
.setRegionId(ecsCreateReq.getRegionId())
|
.setRegionId(regionId)
|
||||||
.setImageId(ecsCreateReq.getImageId())
|
.setImageId(ecsCreateReq.getImageId())
|
||||||
.setInstanceType(ecsCreateReq.getInstanceType())
|
.setInstanceType(ecsCreateReq.getInstanceType())
|
||||||
.setSecurityGroupId(ecsCreateReq.getSecurityGroupId())
|
.setSecurityGroupId(ecsCreateReq.getSecurityGroupId())
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.zhilian.online.uitls;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.zhilian.online.constans.OnlineConstants;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.test.web.servlet.RequestBuilder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: smart-cloud-server
|
||||||
|
* @BelongsPackage: com.zhilian.online.uitls
|
||||||
|
* @Author: LiYuan
|
||||||
|
* @CreateTime: 2024-04-17 21:41
|
||||||
|
* @Description: mqtt工具类
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class MqttUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 通过ip地址调用mqtt服务获取fluxmq的负载量
|
||||||
|
* @author: LiYuan
|
||||||
|
* @param: ipAddress
|
||||||
|
* @return: Integer
|
||||||
|
**/
|
||||||
|
public Integer getLoadInfoByIp(String ipAddress) {
|
||||||
|
|
||||||
|
int num = 0;
|
||||||
|
|
||||||
|
String URL = OnlineConstants.HTTP_PREFIX + ipAddress + OnlineConstants.FLUXMQ_INFO_URL;
|
||||||
|
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
Request build = new Request.Builder()
|
||||||
|
.url(URL)
|
||||||
|
.get()
|
||||||
|
.addHeader("User-Agent","Apifox/1.0.0(https://apifox.com)")
|
||||||
|
.addHeader("AccessToken","")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Response response = client.newCall(build).execute();
|
||||||
|
JSONArray jsonArray = JSONArray.parseArray(response.body().string());
|
||||||
|
JSONObject jsonObject = jsonArray.getJSONObject(0);
|
||||||
|
|
||||||
|
//获取mqttInfo对象的值
|
||||||
|
JSONObject mqttInfo = jsonObject.getJSONObject("mqttInfo");
|
||||||
|
|
||||||
|
//获取fluxmq链接数
|
||||||
|
num = mqttInfo.getIntValue("connectSize");
|
||||||
|
|
||||||
|
log.info("{}的fluxMq链接数为:{}",ipAddress,num);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return num;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE mapper
|
|
||||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|
||||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
|
||||||
<mapper namespace="com.zhilian.online.mapper.OnlineGatherMapper">
|
|
||||||
|
|
||||||
</mapper>
|
|
Loading…
Reference in New Issue