Compare commits
No commits in common. "dff5ca8e8d3b463734cb497accce9ce75cca4f4e" and "2220c13e8d845c3eae922cd03fcec767478f2f3c" have entirely different histories.
dff5ca8e8d
...
2220c13e8d
|
@ -1,5 +1,4 @@
|
||||||
target/
|
target/
|
||||||
.idea
|
|
||||||
!.mvn/wrapper/maven-wrapper.jar
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
!**/src/main/**/target/
|
!**/src/main/**/target/
|
||||||
!**/src/test/**/target/
|
!**/src/test/**/target/
|
||||||
|
|
|
@ -8,7 +8,6 @@ import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
import com.yao.common.aliy.model.EcsSelectModel;
|
import com.yao.common.aliy.model.EcsSelectModel;
|
||||||
import com.yao.common.config.AlyConfigProperties;
|
import com.yao.common.config.AlyConfigProperties;
|
||||||
import com.yao.common.domain.aliy.InstanceInfo;
|
import com.yao.common.domain.aliy.InstanceInfo;
|
||||||
import com.yao.gateway.cache.GatewayNodeSetVinCache;
|
|
||||||
import com.yao.common.redis.service.RedisService;
|
import com.yao.common.redis.service.RedisService;
|
||||||
import com.yao.gateWay.cache.GatewayAliYunCache;
|
import com.yao.gateWay.cache.GatewayAliYunCache;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
@ -27,14 +26,16 @@ import java.util.List;
|
||||||
@Log4j2
|
@Log4j2
|
||||||
public class AliYunEcsService {
|
public class AliYunEcsService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
private final AlyConfigProperties alyConfigProperties;
|
private final AlyConfigProperties alyConfigProperties;
|
||||||
private final Client client;
|
private final Client client;
|
||||||
|
|
||||||
private final GatewayNodeSetVinCache gatewayNodeSetVinCache;
|
private final GatewayAliYunCache gatewayAliYunCache;
|
||||||
public AliYunEcsService(AlyConfigProperties alyConfigProperties, Client client, GatewayNodeSetVinCache gatewayNodeSetVinCache) {
|
public AliYunEcsService(AlyConfigProperties alyConfigProperties, Client client, GatewayAliYunCache gatewayAliYunCache) {
|
||||||
this.alyConfigProperties = alyConfigProperties;
|
this.alyConfigProperties = alyConfigProperties;
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.gatewayNodeSetVinCache = gatewayNodeSetVinCache;
|
this.gatewayAliYunCache = gatewayAliYunCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
//todo----------------------------------------------------以下是查询代码--------------------------------------------
|
//todo----------------------------------------------------以下是查询代码--------------------------------------------
|
||||||
|
@ -125,7 +126,7 @@ public class AliYunEcsService {
|
||||||
String publicIP = item.getPublicIpAddress().substring(1, item.getPublicIpAddress().length() - 1);
|
String publicIP = item.getPublicIpAddress().substring(1, item.getPublicIpAddress().length() - 1);
|
||||||
item.setPublicIpAddress(publicIP);
|
item.setPublicIpAddress(publicIP);
|
||||||
//存入数据
|
//存入数据
|
||||||
gatewayNodeSetVinCache.newInstance(item);
|
redisService.setCacheSet("new:real:column",item);
|
||||||
log.info("公网IP:" + item.getPublicIpAddress());
|
log.info("公网IP:" + item.getPublicIpAddress());
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
package com.yao.common.domain;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author: LiJiaYao
|
|
||||||
* @Date: 2024/4/15
|
|
||||||
* @Description:
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
@Builder
|
|
||||||
public class GateWayNodeInfo {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 节点id
|
|
||||||
*/
|
|
||||||
private String nodeId;
|
|
||||||
/**
|
|
||||||
* 权重值
|
|
||||||
*/
|
|
||||||
private Double score;
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
package com.yao.common.domain;
|
package com.yao.common.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@ -13,7 +12,6 @@ import lombok.NoArgsConstructor;
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Builder
|
|
||||||
public class WorkGateWayNode {
|
public class WorkGateWayNode {
|
||||||
|
|
||||||
private String nodeId;
|
private String nodeId;
|
||||||
|
|
|
@ -2,11 +2,12 @@ package com.yao.common.mqtt;
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.yao.gateway.cache.GatewayVehicleLineNodeCache;
|
import com.yao.common.redis.service.RedisService;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -20,11 +21,11 @@ import java.io.IOException;
|
||||||
@Log4j2
|
@Log4j2
|
||||||
public class MqttConnectService {
|
public class MqttConnectService {
|
||||||
|
|
||||||
private GatewayVehicleLineNodeCache gatewayVehicleLineNodeCache;
|
/**
|
||||||
|
* 调用redis分装好的方法
|
||||||
public MqttConnectService(GatewayVehicleLineNodeCache gatewayVehicleLineNodeCache) {
|
*/
|
||||||
this.gatewayVehicleLineNodeCache = gatewayVehicleLineNodeCache;
|
@Autowired
|
||||||
}
|
private RedisService redisService;
|
||||||
|
|
||||||
//todo-----------------------连接mqtt方法-------------------
|
//todo-----------------------连接mqtt方法-------------------
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ public class MqttConnectService {
|
||||||
String URL = "http://" + ip + ":8080/public/cluster";
|
String URL = "http://" + ip + ":8080/public/cluster";
|
||||||
OkHttpClient client = new OkHttpClient();
|
OkHttpClient client = new OkHttpClient();
|
||||||
Request request = new Request.Builder().url(URL).get().addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)").addHeader("Accesstoken", "").build();
|
Request request = new Request.Builder().url(URL).get().addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)").addHeader("Accesstoken", "").build();
|
||||||
gatewayVehicleLineNodeCache.address(ip);
|
redisService.setCacheSet("ECS", ip);
|
||||||
Response response = null;
|
Response response = null;
|
||||||
try {
|
try {
|
||||||
response = client.newCall(request).execute();
|
response = client.newCall(request).execute();
|
||||||
|
|
|
@ -151,6 +151,7 @@ public class RedisService {
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 获得缓存的list对象
|
* 获得缓存的list对象
|
||||||
|
*
|
||||||
* @param key 缓存的键值
|
* @param key 缓存的键值
|
||||||
* @return 缓存键值对应的数据
|
* @return 缓存键值对应的数据
|
||||||
*/
|
*/
|
||||||
|
@ -249,30 +250,10 @@ public class RedisService {
|
||||||
*
|
*
|
||||||
* @param key Redis键
|
* @param key Redis键
|
||||||
* @param zValue Hash键
|
* @param zValue Hash键
|
||||||
* @param score 值
|
* @param value 值
|
||||||
*/
|
*/
|
||||||
public <T> void setCacheZSet(final String key, final T zValue, final double score) {
|
public <T> void setCacheZSet(final String key, final T zValue, final double value) {
|
||||||
redisTemplate.opsForZSet().add(key,zValue,score);
|
redisTemplate.opsForZSet().add(key,zValue,value);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 取值
|
|
||||||
* @param key 键名
|
|
||||||
* @return 对象
|
|
||||||
* @param <T>
|
|
||||||
*/
|
|
||||||
public <T> Set<T> getCacheZSet(final String key) {
|
|
||||||
return redisTemplate.opsForZSet().range(key,0,-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取集合中是分数数据
|
|
||||||
* @param zSetKey
|
|
||||||
* @return
|
|
||||||
* @param <T>
|
|
||||||
*/
|
|
||||||
public <T> Set<ZSetOperations.TypedTuple<String>> getZSet(String zSetKey) {
|
|
||||||
return redisTemplate.opsForZSet().rangeWithScores(zSetKey,0,-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -346,6 +327,4 @@ public class RedisService {
|
||||||
return redisTemplate.opsForValue().increment(cursor,l);
|
return redisTemplate.opsForValue().increment(cursor,l);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.yao.gateway.cache;
|
package com.yao.gateWay.cache;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
|
@ -1,19 +1,20 @@
|
||||||
package com.yao.gateway.cache;
|
package com.yao.gateWay.cache;
|
||||||
|
|
||||||
import com.aliyun.teautil.Common;
|
|
||||||
import com.yao.common.domain.aliy.InstanceInfo;
|
import com.yao.common.domain.aliy.InstanceInfo;
|
||||||
import com.yao.gateway.cache.abs.GatewayNodeAbstract;
|
import com.yao.gateWay.cache.abs.GatewayNodeAbstract;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author: LiJiaYao
|
* @Author: LiJiaYao
|
||||||
* @Date: 2024/4/18
|
* @Date: 2024/4/18
|
||||||
* @Description: 网关节点存储vin详情
|
* @Description: 阿里云缓存
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class GatewayNodeSetVinCache extends GatewayNodeAbstract {
|
public class GatewayAliYunCache extends GatewayNodeAbstract {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 阿里云键
|
* 阿里云键
|
||||||
*/
|
*/
|
||||||
|
@ -32,16 +33,8 @@ public class GatewayNodeSetVinCache extends GatewayNodeAbstract {
|
||||||
* @param realKey 键名称
|
* @param realKey 键名称
|
||||||
* @return 相关实例数据值
|
* @return 相关实例数据值
|
||||||
*/
|
*/
|
||||||
public Set<InstanceInfo> get(){
|
public Set<InstanceInfo> get(String realKey){
|
||||||
return redisService.getCacheSet(realKey);
|
return redisService.getCacheSet(realKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除这个key其中的一个数据
|
|
||||||
* @param realKey new:real:column键
|
|
||||||
* @param info 对应的值
|
|
||||||
*/
|
|
||||||
public void remote(InstanceInfo info){
|
|
||||||
redisService.deleteCacheMapValue(realKey, Common.toJSONString(info));
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.yao.gateway.cache;
|
package com.yao.gateWay.cache;
|
||||||
|
|
||||||
import com.yao.gateway.cache.abs.GatewayNodeAbstract;
|
import com.yao.gateWay.cache.abs.GatewayNodeAbstract;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,6 +1,6 @@
|
||||||
package com.yao.gateway.cache;
|
package com.yao.gateWay.cache;
|
||||||
|
|
||||||
import com.yao.gateway.cache.abs.GatewayNodeAbstract;
|
import com.yao.gateWay.cache.abs.GatewayNodeAbstract;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
|
@ -1,6 +1,6 @@
|
||||||
package com.yao.gateway.cache;
|
package com.yao.gateWay.cache;
|
||||||
|
|
||||||
import com.yao.gateway.cache.abs.GatewayNodeAbstract;
|
import com.yao.gateWay.cache.abs.GatewayNodeAbstract;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.yao.gateWay.cache;
|
||||||
|
|
||||||
|
import com.yao.gateWay.cache.abs.GatewayNodeAbstract;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: LiJiaYao
|
||||||
|
* @Date: 2024/4/18
|
||||||
|
* @Description: 网关节点分数
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GatewayNodeScoreCache extends GatewayNodeAbstract {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.yao.gateWay.cache;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: LiJiaYao
|
||||||
|
* @Date: 2024/4/18
|
||||||
|
* @Description: 网关节点存储vin详情
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GatewayNodeSetVinCache {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.yao.gateWay.cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: LiJiaYao
|
||||||
|
* @Date: 2024/4/18
|
||||||
|
* @Description: 网关连接车俩
|
||||||
|
*/
|
||||||
|
public class GatewayVehicleLineNodeCache {
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.yao.gateway.cache.abs;
|
package com.yao.gateWay.cache.abs;
|
||||||
|
|
||||||
import com.yao.common.redis.service.RedisService;
|
import com.yao.common.redis.service.RedisService;
|
||||||
|
|
||||||
|
@ -14,4 +14,8 @@ public abstract class GatewayNodeAbstract {
|
||||||
*/
|
*/
|
||||||
public RedisService redisService;
|
public RedisService redisService;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,58 +0,0 @@
|
||||||
package com.yao.gateway.cache;
|
|
||||||
|
|
||||||
import com.yao.gateway.cache.abs.GatewayNodeAbstract;
|
|
||||||
import lombok.extern.log4j.Log4j2;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author: LiJiaYao
|
|
||||||
* @Date: 2024/4/18
|
|
||||||
* @Description: 网关算法缓存
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
@Log4j2
|
|
||||||
public class GatewayArithmeticCache extends GatewayNodeAbstract {
|
|
||||||
|
|
||||||
private static final String loadNode ="work:node:gateway";
|
|
||||||
private static final String cursor ="cursor";
|
|
||||||
/**
|
|
||||||
* 计数器
|
|
||||||
*/
|
|
||||||
public void count(){
|
|
||||||
redisService.setCacheObject(cursor, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 自增
|
|
||||||
* @return 把这个值自增
|
|
||||||
*/
|
|
||||||
public Long increment(){
|
|
||||||
return redisService.increment(cursor, 1L);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 删除loadNode的值
|
|
||||||
*/
|
|
||||||
public void remove(){
|
|
||||||
redisService.deleteObject(loadNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 存储数据
|
|
||||||
* @param loadNodeList 节点数据
|
|
||||||
*/
|
|
||||||
public void loadNode(List<String> loadNodeList){
|
|
||||||
redisService.setCacheList(loadNode, loadNodeList);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取list信息
|
|
||||||
* @param count 传入的数据
|
|
||||||
* @return nodeId
|
|
||||||
*/
|
|
||||||
public String cacheList(Long count){
|
|
||||||
return redisService.getCacheList(loadNode, count % 100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,74 +0,0 @@
|
||||||
package com.yao.gateway.cache;
|
|
||||||
|
|
||||||
import com.yao.common.domain.GateWayNodeInfo;
|
|
||||||
import com.yao.common.domain.WorkGateWayNode;
|
|
||||||
import com.yao.common.domain.aliy.InstanceInfo;
|
|
||||||
import com.yao.gateway.cache.abs.GatewayNodeAbstract;
|
|
||||||
import org.springframework.data.redis.core.ZSetOperations;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author: LiJiaYao
|
|
||||||
* @Date: 2024/4/18
|
|
||||||
* @Description: 网关节点分数
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class GatewayNodeScoreCache extends GatewayNodeAbstract {
|
|
||||||
|
|
||||||
private static final String zSetKey = "gateway:zSet:count";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 网关连接计数
|
|
||||||
*
|
|
||||||
* @param info 新增数据的信息
|
|
||||||
* @param count 网关连接的个数
|
|
||||||
*/
|
|
||||||
public void newCount(InstanceInfo info, Integer count) {
|
|
||||||
redisService.setCacheZSet(zSetKey, info, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 取出数据
|
|
||||||
*
|
|
||||||
* @return 取出的内容
|
|
||||||
*/
|
|
||||||
public Set<InstanceInfo> get() {
|
|
||||||
Set<InstanceInfo> cacheZSet = redisService.getCacheZSet(zSetKey);
|
|
||||||
|
|
||||||
return cacheZSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取他的内容
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public List<GateWayNodeInfo> getCacheZSet() {
|
|
||||||
Set<ZSetOperations.TypedTuple<String>> set = redisService.getZSet(zSetKey);
|
|
||||||
// Map<String, Double> nodeMap = set.stream()
|
|
||||||
// .collect(Collectors.toMap(
|
|
||||||
// ZSetOperations.TypedTuple::getValue,
|
|
||||||
// ZSetOperations.TypedTuple::getScore
|
|
||||||
// ));
|
|
||||||
return set.stream()
|
|
||||||
.map(zset->
|
|
||||||
GateWayNodeInfo.builder()
|
|
||||||
.nodeId(zset.getValue())
|
|
||||||
.score(zset.getScore())
|
|
||||||
.build()
|
|
||||||
).toList();
|
|
||||||
//算法:有没有超过百分比
|
|
||||||
// double sum = nodeMap.values().stream().mapToDouble(value -> value).sum();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除数据
|
|
||||||
*/
|
|
||||||
public void remove() {
|
|
||||||
redisService.deleteObject(zSetKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,44 +0,0 @@
|
||||||
package com.yao.gateway.cache;
|
|
||||||
|
|
||||||
import com.yao.gateway.cache.abs.GatewayNodeAbstract;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author: LiJiaYao
|
|
||||||
* @Date: 2024/4/18
|
|
||||||
* @Description: 网关连接车俩
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class GatewayVehicleLineNodeCache extends GatewayNodeAbstract {
|
|
||||||
|
|
||||||
private final static String reconnectCar = "reconnectCar";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 把数据存入数据
|
|
||||||
* @param ip ip地址
|
|
||||||
*/
|
|
||||||
public void save(String ip){
|
|
||||||
redisService.setCacheSet(reconnectCar,ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 存储ip地址数据
|
|
||||||
* @param ip ip地址
|
|
||||||
*/
|
|
||||||
public void address(String ip) {
|
|
||||||
redisService.setCacheSet("ECS", ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取存储ecs的ip
|
|
||||||
*/
|
|
||||||
public Set<String> getAddress() {
|
|
||||||
return redisService.getCacheSet("ECS");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -18,6 +18,7 @@ public class LoadController {
|
||||||
private LoadService loadService;
|
private LoadService loadService;
|
||||||
@PostMapping("/load")
|
@PostMapping("/load")
|
||||||
public Result<?> loadInfo(){
|
public Result<?> loadInfo(){
|
||||||
return Result.success(loadService.load());
|
String load = loadService.load();
|
||||||
|
return Result.success(load);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package com.yao.server.service.impl;
|
package com.yao.server.service.impl;
|
||||||
|
|
||||||
import com.yao.gateway.cache.GateWayNodeInfo;
|
import com.yao.gateWay.cache.GateWayNodeInfo;
|
||||||
import com.yao.gateway.cache.GatewayLoadNodeCache;
|
import com.yao.gateWay.cache.GatewayLoadNodeCache;
|
||||||
import com.yao.gateway.cache.GatewayLoadSeriesCache;
|
import com.yao.gateWay.cache.GatewayLoadSeriesCache;
|
||||||
import com.yao.gateway.cache.GatewayNodeCache;
|
import com.yao.gateWay.cache.GatewayNodeCache;
|
||||||
import com.yao.server.service.GatewayLoadService;
|
import com.yao.server.service.GatewayLoadService;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
|
@ -3,10 +3,10 @@ package com.yao.server.service.impl;
|
||||||
import com.yao.common.config.Constants;
|
import com.yao.common.config.Constants;
|
||||||
import com.yao.common.domain.WorkGateWayNode;
|
import com.yao.common.domain.WorkGateWayNode;
|
||||||
import com.yao.common.mqtt.MqttConnectService;
|
import com.yao.common.mqtt.MqttConnectService;
|
||||||
import com.yao.gateway.cache.GatewayArithmeticCache;
|
import com.yao.common.redis.service.RedisService;
|
||||||
import com.yao.gateway.cache.GatewayVehicleLineNodeCache;
|
|
||||||
import com.yao.server.service.LoadService;
|
import com.yao.server.service.LoadService;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
@ -21,22 +21,20 @@ import java.util.concurrent.CountDownLatch;
|
||||||
@Log4j2
|
@Log4j2
|
||||||
@Service
|
@Service
|
||||||
public class LoadServiceImpl implements LoadService {
|
public class LoadServiceImpl implements LoadService {
|
||||||
//网关算法缓存
|
|
||||||
private final GatewayArithmeticCache gatewayArithmeticCache;
|
@Autowired
|
||||||
// 网关连接车俩
|
private RedisService redisService;
|
||||||
private final GatewayVehicleLineNodeCache gatewayVehicleLineNodeCache;
|
|
||||||
private MqttConnectService mqttConnectService;
|
private MqttConnectService mqttConnectService;
|
||||||
|
|
||||||
public LoadServiceImpl(GatewayArithmeticCache gatewayArithmeticCache, GatewayVehicleLineNodeCache gatewayVehicleLineNodeCache, MqttConnectService mqttConnectService) {
|
public LoadServiceImpl(MqttConnectService mqttConnectService) {
|
||||||
this.gatewayArithmeticCache = gatewayArithmeticCache;
|
|
||||||
this.gatewayVehicleLineNodeCache = gatewayVehicleLineNodeCache;
|
|
||||||
this.mqttConnectService = mqttConnectService;
|
this.mqttConnectService = mqttConnectService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String load() {
|
public String load() {
|
||||||
//初始化序列
|
//初始化序列
|
||||||
gatewayArithmeticCache.count();
|
redisService.setCacheObject("cursor", 0);
|
||||||
ArrayList<WorkGateWayNode> nodeIdList = carWorkGatewayNode();
|
ArrayList<WorkGateWayNode> nodeIdList = carWorkGatewayNode();
|
||||||
//100
|
//100
|
||||||
List<String> loadNodeList = new ArrayList<>();
|
List<String> loadNodeList = new ArrayList<>();
|
||||||
|
@ -70,13 +68,13 @@ public class LoadServiceImpl implements LoadService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gatewayArithmeticCache.remove();
|
redisService.deleteObject("work:node:gateway");
|
||||||
gatewayArithmeticCache.loadNode(loadNodeList);
|
redisService.setCacheList("work:node:gateway", loadNodeList);
|
||||||
CountDownLatch countDownLatch = new CountDownLatch(300);
|
CountDownLatch countDownLatch = new CountDownLatch(300);
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
for (int i = 0; i < Constants.SUM; i++) {
|
for (int i = 0; i < Constants.SUM; i++) {
|
||||||
long cursor = gatewayArithmeticCache.increment();
|
long cursor = redisService.increment("cursor", 1L);
|
||||||
String cacheList = gatewayArithmeticCache.cacheList(cursor);
|
String cacheList = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||||
log.info(cursor + "---------" + cacheList);
|
log.info(cursor + "---------" + cacheList);
|
||||||
SitNode.sti(cacheList);
|
SitNode.sti(cacheList);
|
||||||
countDownLatch.countDown();
|
countDownLatch.countDown();
|
||||||
|
@ -85,8 +83,8 @@ public class LoadServiceImpl implements LoadService {
|
||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
for (int i = 0; i < Constants.SUM; i++) {
|
for (int i = 0; i < Constants.SUM; i++) {
|
||||||
long cursor = gatewayArithmeticCache.increment();
|
long cursor = redisService.increment("cursor", 1L);
|
||||||
String cacheList = gatewayArithmeticCache.cacheList(cursor);
|
String cacheList = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||||
log.info(cursor + "---------" + cacheList);
|
log.info(cursor + "---------" + cacheList);
|
||||||
SitNode.sti(cacheList);
|
SitNode.sti(cacheList);
|
||||||
countDownLatch.countDown();
|
countDownLatch.countDown();
|
||||||
|
@ -95,8 +93,8 @@ public class LoadServiceImpl implements LoadService {
|
||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
for (int i = 0; i < Constants.SUM; i++) {
|
for (int i = 0; i < Constants.SUM; i++) {
|
||||||
long cursor = gatewayArithmeticCache.increment();
|
Long cursor = redisService.increment("cursor", 1L);
|
||||||
String cacheList = gatewayArithmeticCache.cacheList(cursor);
|
String cacheList = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||||
log.info(cursor + "---------" + cacheList);
|
log.info(cursor + "---------" + cacheList);
|
||||||
SitNode.sti(cacheList);
|
SitNode.sti(cacheList);
|
||||||
countDownLatch.countDown();
|
countDownLatch.countDown();
|
||||||
|
@ -125,7 +123,7 @@ public class LoadServiceImpl implements LoadService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<WorkGateWayNode> carWorkGatewayNode() {
|
public ArrayList<WorkGateWayNode> carWorkGatewayNode() {
|
||||||
Set<String> ip = gatewayVehicleLineNodeCache.getAddress();
|
Set<String> ip = redisService.getCacheSet("ECS");
|
||||||
ArrayList<WorkGateWayNode> list = new ArrayList<>();
|
ArrayList<WorkGateWayNode> list = new ArrayList<>();
|
||||||
for (String s : ip) {
|
for (String s : ip) {
|
||||||
Integer connectSize = mqttConnectService.connectMqtt(s);
|
Integer connectSize = mqttConnectService.connectMqtt(s);
|
||||||
|
@ -172,4 +170,6 @@ class SitNode {
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package com.yao.server.test;
|
package com.yao.server.test;
|
||||||
|
|
||||||
|
import com.aliyun.ecs20140526.Client;
|
||||||
import com.aliyun.ecs20140526.models.DescribeAvailableResourceRequest;
|
import com.aliyun.ecs20140526.models.DescribeAvailableResourceRequest;
|
||||||
import com.aliyun.ecs20140526.models.DescribeAvailableResourceResponse;
|
import com.aliyun.ecs20140526.models.DescribeAvailableResourceResponse;
|
||||||
import com.aliyun.tea.TeaException;
|
import com.aliyun.tea.TeaException;
|
||||||
import com.aliyun.tea.TeaModel;
|
import com.aliyun.tea.TeaModel;
|
||||||
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author: LiJiaYao
|
* @Author: LiJiaYao
|
||||||
|
|
|
@ -1,18 +1,20 @@
|
||||||
package com.yao.server.timer;
|
package com.yao.server.timer;
|
||||||
|
|
||||||
|
import com.aliyun.teautil.Common;
|
||||||
import com.yao.common.aliy.AliYunEcsService;
|
import com.yao.common.aliy.AliYunEcsService;
|
||||||
import com.yao.common.domain.GateWayNodeInfo;
|
import com.yao.common.aliy.model.EcsSelectModel;
|
||||||
import com.yao.common.domain.aliy.InstanceInfo;
|
import com.yao.common.domain.aliy.InstanceInfo;
|
||||||
|
import com.yao.common.domain.aliy.InstanceRequest;
|
||||||
import com.yao.common.mqtt.MqttConnectService;
|
import com.yao.common.mqtt.MqttConnectService;
|
||||||
import com.yao.gateway.cache.GatewayNodeScoreCache;
|
import com.yao.common.redis.service.RedisService;
|
||||||
import com.yao.gateway.cache.GatewayNodeSetVinCache;
|
|
||||||
import com.yao.gateway.cache.GatewayVehicleLineNodeCache;
|
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.scheduling.annotation.EnableAsync;
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -25,61 +27,96 @@ import java.util.Set;
|
||||||
@EnableAsync //开启异步支持
|
@EnableAsync //开启异步支持
|
||||||
@Component
|
@Component
|
||||||
public class Timer {
|
public class Timer {
|
||||||
private final AliYunEcsService aliYunEcsService;
|
|
||||||
private final MqttConnectService mqttConnectService;
|
|
||||||
// 阿里云缓存
|
|
||||||
private final GatewayNodeSetVinCache gatewayNodeSetVinCache;
|
|
||||||
private final GatewayVehicleLineNodeCache gatewayVehicleLineNodeCache;
|
|
||||||
private final GatewayNodeScoreCache gatewayNodeScoreCache;
|
|
||||||
|
|
||||||
public Timer(AliYunEcsService aliYunEcsService, MqttConnectService mqttConnectService, GatewayNodeSetVinCache gatewayAliYunCache, GatewayVehicleLineNodeCache gatewayVehicleLineNodeCache, GatewayNodeScoreCache gatewayNodeScoreCache) {
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
private AliYunEcsService aliYunEcsService;
|
||||||
|
private MqttConnectService mqttConnectService;
|
||||||
|
|
||||||
|
public Timer(AliYunEcsService aliYunEcsService, MqttConnectService mqttConnectService) {
|
||||||
this.aliYunEcsService = aliYunEcsService;
|
this.aliYunEcsService = aliYunEcsService;
|
||||||
this.mqttConnectService = mqttConnectService;
|
this.mqttConnectService = mqttConnectService;
|
||||||
this.gatewayNodeSetVinCache = gatewayAliYunCache;
|
|
||||||
this.gatewayVehicleLineNodeCache = gatewayVehicleLineNodeCache;
|
|
||||||
this.gatewayNodeScoreCache = gatewayNodeScoreCache;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
@Scheduled(cron = "0/20 * * * * ?")
|
@Scheduled(cron = "0/20 * * * * ?")
|
||||||
public void timer() {
|
public void timer() {
|
||||||
// 记录开始时间
|
long startTime = System.currentTimeMillis(); // 记录开始时间
|
||||||
long startTime = System.currentTimeMillis();
|
long endTime = startTime + 10 * 60 * 1000; // 设置结束时间为10分钟后
|
||||||
// 设置结束时间为10分钟后
|
// redisService.deleteObject("new:real:column");
|
||||||
long endTime = startTime + 10 * 60 * 1000;
|
Set<InstanceInfo> instance = redisService.getCacheSet("new:real:column");
|
||||||
Set<InstanceInfo> instance = gatewayNodeSetVinCache.get();
|
// List<InstanceInfo> myFirstEcsInstance = null;
|
||||||
if (instance.isEmpty()) {
|
// try {
|
||||||
try {
|
// myFirstEcsInstance = aliYunEcsService.selectList(ecsSelectModelName("MyFirstEcsInstance"));
|
||||||
aliYunEcsService.startCreate();
|
// for (InstanceInfo instanceInfo : myFirstEcsInstance) {
|
||||||
} catch (Exception e) {
|
// String publicIpAddress = instanceInfo.getPublicIpAddress();
|
||||||
throw new RuntimeException(e);
|
// String instanceId = instanceInfo.getInstanceId();
|
||||||
}
|
// InstanceRequest instanceRequest = new InstanceRequest(publicIpAddress, instanceId);
|
||||||
}
|
// redisService.setCacheSet("new:real:column", instanceRequest);
|
||||||
for (InstanceInfo s : instance) {
|
// }
|
||||||
Integer connectSize = mqttConnectService.connectMqtt(s.getPublicIpAddress());
|
if (instance.isEmpty()) {
|
||||||
List<String> instanceId = null;
|
// if (myFirstEcsInstance.isEmpty()) {
|
||||||
String deleteInstanceId = null;
|
|
||||||
if (connectSize >= 79) {
|
|
||||||
//执行节点扩容
|
|
||||||
//返回实例的ID
|
|
||||||
if (!instanceId.isEmpty()) {
|
|
||||||
try {
|
try {
|
||||||
instanceId = aliYunEcsService.startCreate();
|
aliYunEcsService.startCreate();
|
||||||
log.info("扩容成功!");
|
|
||||||
log.info("扩容的节点id为:" + instanceId);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
for (InstanceInfo s : instance) {
|
||||||
|
Integer connectSize = mqttConnectService.connectMqtt(s.getPublicIpAddress());
|
||||||
|
List<String> instanceId = null;
|
||||||
|
String deleteInstanceId = null;
|
||||||
|
if (connectSize >= 80) {
|
||||||
|
//执行节点扩容
|
||||||
|
//返回实例的ID
|
||||||
|
if (!instanceId.isEmpty()) {
|
||||||
|
try {
|
||||||
|
instanceId = aliYunEcsService.startCreate();
|
||||||
|
log.info("扩容成功!");
|
||||||
|
log.info("扩容的节点id为:" + instanceId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (connectSize <= 20 && System.currentTimeMillis() < endTime) {
|
||||||
|
// if (!deleteInstanceId.isEmpty()) {
|
||||||
|
aliYunEcsService.delete(s.getInstanceId());
|
||||||
|
//删除实列以后再去把redis的值删除 再去通知重新上线
|
||||||
|
redisService.deleteCacheMapValue("new:real:column", Common.toJSONString(s));
|
||||||
|
redisService.setCacheSet("reconnectCar",s.getPublicIpAddress());
|
||||||
|
log.info("缩容成功!");
|
||||||
|
log.info("锁容的节点id为:" + deleteInstanceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (connectSize <= 20 && System.currentTimeMillis() < endTime) {
|
// } catch (Exception e) {
|
||||||
aliYunEcsService.delete(s.getInstanceId());
|
// throw new RuntimeException(e);
|
||||||
//删除实列以后再去把redis的值删除 再去通知重新上线
|
|
||||||
gatewayNodeSetVinCache.remote(s);
|
|
||||||
gatewayVehicleLineNodeCache.save(s.getPublicIpAddress());
|
|
||||||
log.info("缩容成功!");
|
|
||||||
log.info("锁容的节点id为:" + deleteInstanceId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* 查出来数据以后把值返回给要查的数据
|
||||||
|
*
|
||||||
|
* @param instance
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
// public EcsSelectModel ecsSelectModel(Set<InstanceInfo> instance) {
|
||||||
|
// List<String> instanceIdList = new ArrayList<>();
|
||||||
|
// for (InstanceInfo req : instance) {
|
||||||
|
// instanceIdList.add(req.getInstanceId());
|
||||||
|
// }
|
||||||
|
// EcsSelectModel ecsSelectModel = new EcsSelectModel();
|
||||||
|
// ecsSelectModel.setInstanceIdList(instanceIdList);
|
||||||
|
// return ecsSelectModel;
|
||||||
|
// }
|
||||||
|
// public EcsSelectModel ecsSelectModelName(String instanceName) {
|
||||||
|
// List<String> instanceIdList = new ArrayList<>();
|
||||||
|
// instanceIdList.add(instanceName);
|
||||||
|
// EcsSelectModel ecsSelectModel = new EcsSelectModel();
|
||||||
|
// ecsSelectModel.setInstanceIdList(instanceIdList);
|
||||||
|
// return ecsSelectModel;
|
||||||
|
// }
|
||||||
|
|
||||||
|
//}
|
||||||
|
|
|
@ -20,10 +20,13 @@ import java.util.concurrent.CountDownLatch;
|
||||||
@SpringBootTest(classes = APISpringBootApplication.class)
|
@SpringBootTest(classes = APISpringBootApplication.class)
|
||||||
public class LoadService1 {
|
public class LoadService1 {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisService redisService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void load() {
|
public void load() {
|
||||||
//初始化序列
|
//初始化序列
|
||||||
// redisService.setCacheObject("cursor",0);
|
redisService.setCacheObject("cursor",0);
|
||||||
|
|
||||||
List<WorkGateWayNode> nodeIdList = new ArrayList() {
|
List<WorkGateWayNode> nodeIdList = new ArrayList() {
|
||||||
{
|
{
|
||||||
|
@ -74,33 +77,33 @@ public class LoadService1 {
|
||||||
// for (int i = 0; i < 100; i++) {
|
// for (int i = 0; i < 100; i++) {
|
||||||
// loadNodeList.add(nodeIdList.get(i % nodeIdList.size()));
|
// loadNodeList.add(nodeIdList.get(i % nodeIdList.size()));
|
||||||
// }
|
// }
|
||||||
// redisService.deleteObject("work:node:gateway");
|
redisService.deleteObject("work:node:gateway");
|
||||||
// redisService.setCacheList("work:node:gateway",loadNodeList);
|
redisService.setCacheList("work:node:gateway",loadNodeList);
|
||||||
CountDownLatch countDownLatch = new CountDownLatch(300);
|
CountDownLatch countDownLatch = new CountDownLatch(300);
|
||||||
new Thread(()->{
|
new Thread(()->{
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
// Long cursor = redisService.increment("cursor", 1L);
|
Long cursor = redisService.increment("cursor", 1L);
|
||||||
// String cacheList = redisService.getCacheList("work:node:gateway", cursor % 100);
|
String cacheList = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||||
// log.info(cursor+"---------"+cacheList);
|
log.info(cursor+"---------"+cacheList);
|
||||||
// SitNode.sti(cacheList);
|
SitNode.sti(cacheList);
|
||||||
countDownLatch.countDown();
|
countDownLatch.countDown();
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
new Thread(()->{
|
new Thread(()->{
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
// Long cursor = redisService.increment("cursor", 1L);
|
Long cursor = redisService.increment("cursor", 1L);
|
||||||
// String cacheList = redisService.getCacheList("work:node:gateway", cursor % 100);
|
String cacheList = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||||
// log.info(cursor+"---------"+cacheList);
|
log.info(cursor+"---------"+cacheList);
|
||||||
// SitNode.sti(cacheList);
|
SitNode.sti(cacheList);
|
||||||
countDownLatch.countDown();
|
countDownLatch.countDown();
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
new Thread(()->{
|
new Thread(()->{
|
||||||
for (int i = 0; i < 100; i++) {
|
for (int i = 0; i < 100; i++) {
|
||||||
// Long cursor = redisService.increment("cursor", 1L);
|
Long cursor = redisService.increment("cursor", 1L);
|
||||||
// String cacheList = redisService.getCacheList("work:node:gateway", cursor % 100);
|
String cacheList = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||||
// log.info(cursor+"---------"+cacheList);
|
log.info(cursor+"---------"+cacheList);
|
||||||
// SitNode.sti(cacheList);
|
SitNode.sti(cacheList);
|
||||||
countDownLatch.countDown();
|
countDownLatch.countDown();
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
Loading…
Reference in New Issue