7优化
parent
e70a315159
commit
ba81e00cb3
|
@ -10,7 +10,7 @@ import com.lyh.common.aliyun.config.AliConfig;
|
||||||
import com.lyh.common.aliyun.model.InstanceSpecification;
|
import com.lyh.common.aliyun.model.InstanceSpecification;
|
||||||
import com.lyh.common.redis.service.RedisService;
|
import com.lyh.common.redis.service.RedisService;
|
||||||
import com.lyh.common.utils.user.UserUtil;
|
import com.lyh.common.utils.user.UserUtil;
|
||||||
import com.lyh.domain.InstancesInformation;
|
import com.lyh.common.domain.InstancesInformation;
|
||||||
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.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.lyh.domain;
|
package com.lyh.common.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.lyh.domain;
|
package com.lyh.common.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.lyh.domain;
|
package com.lyh.common.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.lyh.domain;
|
package com.lyh.common.domain;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.lyh.domain.resp;
|
package com.lyh.common.domain.resp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回状态码
|
* 返回状态码
|
|
@ -1,4 +1,4 @@
|
||||||
package com.lyh.domain.resp;
|
package com.lyh.common.domain.resp;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.lyh.controller;
|
package com.lyh.controller;
|
||||||
|
|
||||||
import com.lyh.domain.resp.Result;
|
import com.lyh.common.domain.resp.Result;
|
||||||
import com.lyh.service.LoadCenterService;
|
import com.lyh.service.LoadCenterService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.lyh.gateway.cache;
|
||||||
|
|
||||||
|
import com.lyh.gateway.cache.abs.GatewayCacheAbs;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: LoadCenter
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/18
|
||||||
|
* @Description: 网关负载节点缓存
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GatewayLoadNodeCache extends GatewayCacheAbs<String> {
|
||||||
|
private final static String gatewayLoadNodeKey = "node";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPre() {
|
||||||
|
return "gateway:load:";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Description: 存负载集合
|
||||||
|
* @Date: 2024/4/18 16:12
|
||||||
|
* @Param: [nodeList]
|
||||||
|
* @Return: void
|
||||||
|
**/
|
||||||
|
public void put(List<String> nodeList) {
|
||||||
|
redisService.deleteObject(encode(gatewayLoadNodeKey));
|
||||||
|
redisService.setCacheList(encode(gatewayLoadNodeKey), nodeList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Description: 获取所有负载节点
|
||||||
|
* @Date: 2024/4/18 16:12
|
||||||
|
* @Param: []
|
||||||
|
* @Return: java.util.List<java.lang.String>
|
||||||
|
**/
|
||||||
|
public List<String> get() {
|
||||||
|
return redisService.getCacheList(encode(gatewayLoadNodeKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Description: 通过下标获取节点
|
||||||
|
* @Date: 2024/4/18 16:13
|
||||||
|
* @Param: []
|
||||||
|
* @Return: java.lang.String
|
||||||
|
**/
|
||||||
|
public String getByIndex(Long index) {
|
||||||
|
if (null == index || 100 < index) {
|
||||||
|
throw new RuntimeException("下标违法,0-100");
|
||||||
|
}
|
||||||
|
return redisService.getCacheList(encode(gatewayLoadNodeKey), index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.lyh.gateway.cache;
|
||||||
|
|
||||||
|
import com.lyh.gateway.cache.abs.GatewayCacheAbs;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: LoadCenter
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/18
|
||||||
|
* @Description: 网关负载序列
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class GatewayLoadSeriesCache extends GatewayCacheAbs<String> {
|
||||||
|
//redis Key
|
||||||
|
private final static String gatewayLoadSeriesKey = "series";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPre() {
|
||||||
|
return "gateway:load:";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Date: 2024/4/18 15:35
|
||||||
|
* @Description: bean创建完成后执行方法
|
||||||
|
* @Param: []
|
||||||
|
* @Return: void
|
||||||
|
**/
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
redisService.setCacheObject(encode(gatewayLoadSeriesKey), 0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Description: 获取当前序列值
|
||||||
|
* @Date: 2024/4/18 15:56
|
||||||
|
* @Param: []
|
||||||
|
* @Return: java.lang.Long
|
||||||
|
**/
|
||||||
|
public Long get() {
|
||||||
|
return redisService.getCacheObject(encode(gatewayLoadSeriesKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Description: 获取自增序列值
|
||||||
|
* @Date: 2024/4/18 15:38
|
||||||
|
* @Param: []
|
||||||
|
* @Return: 自增后的值
|
||||||
|
**/
|
||||||
|
public Long incrementAndGet() {
|
||||||
|
return redisService.increment(encode(gatewayLoadSeriesKey), 1L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Description: 重置缓存
|
||||||
|
* @Date: 2024/4/18 15:57
|
||||||
|
* @Param: []
|
||||||
|
* @Return: void
|
||||||
|
**/
|
||||||
|
public void reset() {
|
||||||
|
|
||||||
|
this.init();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.lyh.gateway.cache;
|
||||||
|
|
||||||
|
import com.lyh.gateway.cache.abs.GatewayCacheAbs;
|
||||||
|
import com.lyh.gateway.mode.GatewayNodeInfo;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: LoadCenter
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/18
|
||||||
|
* @Description: 网关节点缓存
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class GatewayNodeCache extends GatewayCacheAbs<String> {
|
||||||
|
//redis Key前缀
|
||||||
|
@Override
|
||||||
|
public String getPre() {
|
||||||
|
return "gateway:node:info:";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @Date: 2024/4/18 15:16
|
||||||
|
* @Description: 增加缓存数据
|
||||||
|
* @Param: 节点信息
|
||||||
|
* @Return: void
|
||||||
|
**/
|
||||||
|
public void put(GatewayNodeInfo gatewayNodeInfo) {
|
||||||
|
redisService.setCacheObject(encode(gatewayNodeInfo.getNodeId()), gatewayNodeInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @Date: 2024/4/18 15:19
|
||||||
|
* @Description: 获取缓存数据
|
||||||
|
* @Param: 节点id
|
||||||
|
* @Return: 节点信息
|
||||||
|
**/
|
||||||
|
public GatewayNodeInfo get(String nodeId) {
|
||||||
|
return redisService.getCacheObject(encode(nodeId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @Date: 2024/4/18 15:19
|
||||||
|
* @Description: 删除缓存数据
|
||||||
|
* @Param: 节点id
|
||||||
|
* @Return: void
|
||||||
|
**/
|
||||||
|
public void remove(String nodeId) {
|
||||||
|
redisService.deleteObject(encode(nodeId));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.lyh.gateway.cache.abs;
|
||||||
|
|
||||||
|
import com.lyh.common.redis.service.RedisService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: LoadCenter
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/18
|
||||||
|
* @Description: 缓存抽象类
|
||||||
|
*/
|
||||||
|
|
||||||
|
public abstract class GatewayCacheAbs<K> {
|
||||||
|
@Autowired
|
||||||
|
public RedisService redisService;
|
||||||
|
|
||||||
|
public abstract String getPre();
|
||||||
|
|
||||||
|
public String encode(K key) {
|
||||||
|
return getPre() + key;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.lyh.gateway.mode;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ProjectName: LoadCenter
|
||||||
|
* @Author: LiuYunHu
|
||||||
|
* @CreateTime: 2024/4/18
|
||||||
|
* @Description: 网关节点信息
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class GatewayNodeInfo {
|
||||||
|
/*
|
||||||
|
* 节点id
|
||||||
|
* */
|
||||||
|
private String nodeId;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 公网IP
|
||||||
|
* */
|
||||||
|
private String publicIpAddress;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 内网IP
|
||||||
|
* */
|
||||||
|
private String privateIpAddress;
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.lyh.service;
|
package com.lyh.service;
|
||||||
|
|
||||||
import com.lyh.domain.resp.Result;
|
import com.lyh.common.domain.resp.Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ProjectName: LoadCenter
|
* @ProjectName: LoadCenter
|
||||||
|
|
|
@ -6,9 +6,9 @@ import com.lyh.common.aliyun.service.AliYunEcsService;
|
||||||
import com.lyh.common.redis.service.RedisService;
|
import com.lyh.common.redis.service.RedisService;
|
||||||
import com.lyh.common.utils.mqtt.MqttUtil;
|
import com.lyh.common.utils.mqtt.MqttUtil;
|
||||||
import com.lyh.common.utils.user.UserUtil;
|
import com.lyh.common.utils.user.UserUtil;
|
||||||
import com.lyh.domain.IpAndLoadCount;
|
import com.lyh.common.domain.IpAndLoadCount;
|
||||||
import com.lyh.domain.IpAndWeight;
|
import com.lyh.common.domain.IpAndWeight;
|
||||||
import com.lyh.domain.resp.Result;
|
import com.lyh.common.domain.resp.Result;
|
||||||
import com.lyh.service.LoadCenterService;
|
import com.lyh.service.LoadCenterService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
|
@ -4,7 +4,6 @@ server:
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
redis:
|
redis:
|
||||||
# host: 39.103.132.68
|
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: 6379
|
port: 6379
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ public class Test {
|
||||||
**/
|
**/
|
||||||
@org.junit.jupiter.api.Test
|
@org.junit.jupiter.api.Test
|
||||||
public void releaseInstances() throws Exception {
|
public void releaseInstances() throws Exception {
|
||||||
aliYunEcsService.releaseInstances("i-uf6dmmscd3b64m0jqs9f");
|
aliYunEcsService.releaseInstances("i-uf65dwndeh5u4cmpl1ff,i-uf68ar6qiqmgetp67a5q");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue