From 772d61ef4c7516a8a1f2df8b069df1d0bd0985fb Mon Sep 17 00:00:00 2001 From: YuanYh <2972788632@qq.com> Date: Thu, 18 Apr 2024 22:20:06 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 35 ++ .idea/.gitignore | 8 + .idea/encodings.xml | 8 + .idea/misc.xml | 14 + .idea/uiDesigner.xml | 124 +++++++ .idea/vcs.xml | 6 + pom.xml | 299 +++++++++++++++++ src/main/java/com/yyh/GatewayLoadCenter.java | 17 + .../java/com/yyh/common/domain/Result.java | 110 +++++++ .../FastJson2JsonRedisSerializer.java | 51 +++ .../common/redis/configure/RedisConfig.java | 43 +++ .../redis/configure/RedisListenerConfig.java | 33 ++ .../common/redis/service/RedisService.java | 306 ++++++++++++++++++ .../com/yyh/controller/GatewayController.java | 29 ++ .../gateway/cache/GatewayLoadNodeCache.java | 48 +++ .../gateway/cache/GatewayLoadSeriesCache.java | 55 ++++ .../yyh/gateway/cache/GatewayNodeCache.java | 58 ++++ .../gateway/cache/GatewayNodeScoreCache.java | 10 + .../yyh/gateway/cache/GatewayNodeSetVin.java | 10 + .../cache/GatewayVehicleLineNodeCache.java | 10 + .../gateway/cache/abs/GatewayCacheAbs.java | 27 ++ .../yyh/gateway/model/GatewayNodeInfo.java | 28 ++ .../com/yyh/service/GatewayLoadService.java | 13 + .../service/impl/GatewayLoadServiceImpl.java | 43 +++ 24 files changed, 1385 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/com/yyh/GatewayLoadCenter.java create mode 100644 src/main/java/com/yyh/common/domain/Result.java create mode 100644 src/main/java/com/yyh/common/redis/configure/FastJson2JsonRedisSerializer.java create mode 100644 src/main/java/com/yyh/common/redis/configure/RedisConfig.java create mode 100644 src/main/java/com/yyh/common/redis/configure/RedisListenerConfig.java create mode 100644 src/main/java/com/yyh/common/redis/service/RedisService.java create mode 100644 src/main/java/com/yyh/controller/GatewayController.java create mode 100644 src/main/java/com/yyh/gateway/cache/GatewayLoadNodeCache.java create mode 100644 src/main/java/com/yyh/gateway/cache/GatewayLoadSeriesCache.java create mode 100644 src/main/java/com/yyh/gateway/cache/GatewayNodeCache.java create mode 100644 src/main/java/com/yyh/gateway/cache/GatewayNodeScoreCache.java create mode 100644 src/main/java/com/yyh/gateway/cache/GatewayNodeSetVin.java create mode 100644 src/main/java/com/yyh/gateway/cache/GatewayVehicleLineNodeCache.java create mode 100644 src/main/java/com/yyh/gateway/cache/abs/GatewayCacheAbs.java create mode 100644 src/main/java/com/yyh/gateway/model/GatewayNodeInfo.java create mode 100644 src/main/java/com/yyh/service/GatewayLoadService.java create mode 100644 src/main/java/com/yyh/service/impl/GatewayLoadServiceImpl.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e403e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..63574ec --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e2044d1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..37614f4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,299 @@ + + + 4.0.0 + + com.yyh + GatewayLoadCenter + 1.0-SNAPSHOT + jar + + + 17 + 17 + UTF-8 + 2.7.13 + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.projectlombok + lombok + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.47 + + + + + + + + com.aliyun + ecs20140526 + 3.1.2 + + + com.aliyun + tea-openapi + 0.3.2 + + + com.aliyun + tea-util + 0.2.21 + + + com.aliyun + darabonba-number + 0.0.3 + + + com.aliyun + tea-console + 0.0.1 + + + com.aliyun + darabonba-env + 0.1.1 + + + com.aliyun + tea + 1.1.14 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + com.aliyun + darabonba-string + 0.0.3 + + + + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.3 + true + + sonatype-nexus-staging + https://s01.oss.sonatype.org/ + true + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.1.1 + + UTF-8 + none + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + org.apache.maven.plugins + maven-assembly-plugin + 2.4.1 + + + jar-with-dependencies + + + + true + com.aliyun.sample.Sample + + + + + + make-assembly + package + + single + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/yyh/GatewayLoadCenter.java b/src/main/java/com/yyh/GatewayLoadCenter.java new file mode 100644 index 0000000..e36d2d3 --- /dev/null +++ b/src/main/java/com/yyh/GatewayLoadCenter.java @@ -0,0 +1,17 @@ +package com.yyh; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @ClassName GatewayLoadCenter + * @Description TODO + * @Author YuanYongH + * @Date 2024/4/18 15:34 + */ +@SpringBootApplication +public class GatewayLoadCenter { + public static void main(String[] args) { + SpringApplication.run(GatewayLoadCenter.class, args); + } +} diff --git a/src/main/java/com/yyh/common/domain/Result.java b/src/main/java/com/yyh/common/domain/Result.java new file mode 100644 index 0000000..7357f76 --- /dev/null +++ b/src/main/java/com/yyh/common/domain/Result.java @@ -0,0 +1,110 @@ +package com.yyh.common.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 响应信息主体 + * + * @author zhilian + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class Result implements Serializable { + /** + * 成功 + */ + public static final int SUCCESS = 200; + /** + * 失败 + */ + public static final int FAIL = 500; + /** + * 警告 + */ + public static final int WARN = 501; + + private static final long serialVersionUID = 1L; + private int code; + + private String msg; + + private T data; + + public static Result success () { + return restResult(null, SUCCESS, null); + } + + public static Result success (T data) { + return restResult(data, SUCCESS, null); + } + + public static Result success (T data, String msg) { + return restResult(data, SUCCESS, msg); + } + + public static Result error () { + return restResult(null, FAIL, null); + } + + public static Result error (String msg) { + return restResult(null, FAIL, msg); + } + + public static Result error (T data) { + return restResult(data, FAIL, null); + } + + public static Result error (T data, String msg) { + return restResult(data, FAIL, msg); + } + + public static Result error (int code, String msg) { + return restResult(null, code, msg); + } + + + + public static Result warn () { + return restResult(null, WARN, null); + } + + public static Result warn (String msg) { + return restResult(null, WARN, msg); + } + + public static Result warn (T data) { + return restResult(data, WARN, null); + } + + public static Result warn (T data, String msg) { + return restResult(data, WARN, msg); + } + + public static Result warn (int code, String msg) { + return restResult(null, code, msg); + } + + private static Result restResult (T data, int code, String msg) { + return Result.builder() + .code(code) + .data(data) + .msg(msg) + .build(); + } + + public static Boolean isError (Result ret) { + return !isSuccess(ret); + } + + public static Boolean isSuccess (Result ret) { + return Result.SUCCESS == ret.getCode(); + } + +} diff --git a/src/main/java/com/yyh/common/redis/configure/FastJson2JsonRedisSerializer.java b/src/main/java/com/yyh/common/redis/configure/FastJson2JsonRedisSerializer.java new file mode 100644 index 0000000..f3cf49b --- /dev/null +++ b/src/main/java/com/yyh/common/redis/configure/FastJson2JsonRedisSerializer.java @@ -0,0 +1,51 @@ +package com.yyh.common.redis.configure; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.filter.Filter; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; + +import java.nio.charset.Charset; + +/** + * Redis使用FastJson序列化 + * + * @author couplet + */ +public class FastJson2JsonRedisSerializer implements RedisSerializer { + public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); + + /** + * 自动识别json对象白名单配置(仅允许解析的包名,范围越小越安全) + */ + public static final String[] JSON_WHITELIST_STR = {"org.springframework", "com.couplet"}; + + static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(JSON_WHITELIST_STR); + + private Class clazz; + + public FastJson2JsonRedisSerializer (Class clazz) { + super(); + this.clazz = clazz; + } + + @Override + public byte[] serialize (T t) throws SerializationException { + if (t == null) { + return new byte[0]; + } + return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET); + } + + @Override + public T deserialize (byte[] bytes) throws SerializationException { + if (bytes == null || bytes.length <= 0) { + return null; + } + String str = new String(bytes, DEFAULT_CHARSET); + + return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER); + } +} diff --git a/src/main/java/com/yyh/common/redis/configure/RedisConfig.java b/src/main/java/com/yyh/common/redis/configure/RedisConfig.java new file mode 100644 index 0000000..67e98f6 --- /dev/null +++ b/src/main/java/com/yyh/common/redis/configure/RedisConfig.java @@ -0,0 +1,43 @@ +package com.yyh.common.redis.configure; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * @ClassName RedisConfig + * @Description TODO + * @Author YuanYongH + * @Date 2024/4/18 15:36 + */ +@Configuration +@EnableCaching +@AutoConfigureBefore(RedisAutoConfiguration.class) +public class RedisConfig extends CachingConfigurerSupport { + + @Bean + @SuppressWarnings(value = {"unchecked", "rawtypes" }) + public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){ + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(connectionFactory); + + FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer<>(Object.class); + + //使用StringRedisSerializer来序列和反序列化redis的key值 + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(serializer); + + //Hash的key也采用StringRedisSerializer的序列化方式 + template.setHashKeySerializer(new StringRedisSerializer()); + template.setHashKeySerializer(serializer); + + template.afterPropertiesSet(); + return template; + } +} diff --git a/src/main/java/com/yyh/common/redis/configure/RedisListenerConfig.java b/src/main/java/com/yyh/common/redis/configure/RedisListenerConfig.java new file mode 100644 index 0000000..bcfd096 --- /dev/null +++ b/src/main/java/com/yyh/common/redis/configure/RedisListenerConfig.java @@ -0,0 +1,33 @@ +package com.yyh.common.redis.configure; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; + +/** + * @Author: YY + * @Date: 2024/4/18 + * @Description: redis监听配置 + */ +@Configuration +public class RedisListenerConfig { + + @Bean + RedisMessageListenerContainer listenerContainer(RedisConnectionFactory redisConnectionFactory) { + + RedisMessageListenerContainer container = new RedisMessageListenerContainer(); + container.setConnectionFactory(redisConnectionFactory); + return container; + } + + @Bean + KeyExpirationEventMessageListener redisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { + return new KeyExpirationEventMessageListener(listenerContainer); + } + + + + +} diff --git a/src/main/java/com/yyh/common/redis/service/RedisService.java b/src/main/java/com/yyh/common/redis/service/RedisService.java new file mode 100644 index 0000000..f72a67b --- /dev/null +++ b/src/main/java/com/yyh/common/redis/service/RedisService.java @@ -0,0 +1,306 @@ +package com.yyh.common.redis.service; + +import org.springframework.data.redis.core.BoundSetOperations; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.*; +import java.util.concurrent.TimeUnit; + +/** + * spring redis 工具类 + * + * @author couplet + **/ +@SuppressWarnings(value = {"unchecked", "rawtypes"}) +@Component +public class RedisService { + @Resource + public RedisTemplate redisTemplate; + + + // ... 其他已有方法 ... + + /** + * 向指定集合中添加值,如果值不存在则添加并返回true,否则返回false。 + * + * @param setKey 集合键名 + * @param number 要添加的值 + * @return true表示值已成功添加(之前不存在),false表示值已存在 + */ + public boolean addToSetIfNotExists(String setKey, Long number) { + return redisTemplate.opsForSet().add(setKey, number) == 1; + } + + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + */ + public void setCacheObject(final String key, final T value) { + redisTemplate.opsForValue().set(key, value); + } + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + * @param timeout 时间 + * @param timeUnit 时间颗粒度 + */ + public void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) { + redisTemplate.opsForValue().set(key, value, timeout, timeUnit); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * @return true=设置成功;false=设置失败 + */ + public boolean expire(final String key, final long timeout) { + return expire(key, timeout, TimeUnit.SECONDS); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * @param unit 时间单位 + * @return true=设置成功;false=设置失败 + */ + public boolean expire(final String key, final long timeout, final TimeUnit unit) { + return redisTemplate.expire(key, timeout, unit); + } + + /** + * 获取有效时间 + * + * @param key Redis键 + * @return 有效时间 + */ + public long getExpire(final String key) { + return redisTemplate.getExpire(key); + } + + /** + * 判断 key是否存在 + * + * @param key 键 + * @return true 存在 false不存在 + */ + public Boolean hasKey(String key) { + return redisTemplate.hasKey(key); + } + + /** + * 获得缓存的基本对象。 + * + * @param key 缓存键值 + * @return 缓存键值对应的数据 + */ + public T getCacheObject(final String key) { + ValueOperations operation = redisTemplate.opsForValue(); + return operation.get(key); + } + + /** + * 删除单个对象 + * + * @param key + */ + public boolean deleteObject(final String key) { + return redisTemplate.delete(key); + } + + /** + * 删除集合对象 + * + * @param collection 多个对象 + * @return + */ + public boolean deleteObject(final Collection collection) { + return redisTemplate.delete(collection) > 0; + } + + /** + * 缓存List数据 + * + * @param key 缓存的键值 + * @param dataList 待缓存的List数据 + * @return 缓存的对象 + */ + public long setCacheList(final String key, final List dataList) { + Long count = redisTemplate.opsForList().rightPushAll(key, dataList); + return count == null ? 0 : count; + } + + /** + * 获得缓存的list对象 + * + * @param key 缓存的键值 + * @return 缓存键值对应的数据 + */ + public List getCacheList(final String key) { + return redisTemplate.opsForList().range(key, 0, -1); + } + + /** + * 缓存Set + * + * @param key 缓存键值 + * @param dataSet 缓存的数据 + * @return 缓存数据的对象 + */ + public BoundSetOperations setCacheSet(final String key, final Set dataSet) { + BoundSetOperations setOperation = redisTemplate.boundSetOps(key); + Iterator it = dataSet.iterator(); + while (it.hasNext()) { + setOperation.add(it.next()); + } + return setOperation; + } + + /** + * 缓存Set + * + * @param key 缓存键值 + * @param setValue 缓存的数据 + * @return 缓存数据的对象 + */ + public BoundSetOperations setCacheSet(final String key, final T setValue) { + BoundSetOperations setOperation = redisTemplate.boundSetOps(key); + setOperation.add(setValue); + return setOperation; + } + + + /* + * 缓存Zset + * + * @param key 缓存键值 + * @param setValue 缓存的数据 + * @return 缓存数据的对象 + **/ + public void setCacheZset(final String key, final T setValue) { + HashSet set = new HashSet<>(); + set.add((String) setValue); + Long add = redisTemplate.opsForZSet().add(key, set); + + } + + /** + * 缓存Set + * + * @param key 缓存键值 + * @param setValue 缓存的数据 + * @return 缓存数据的对象 + */ + public void deleteSet(String key, String setValue) { + + BoundSetOperations setOperations = redisTemplate.boundSetOps(key); + setOperations.remove(setValue); + } + + /** + * 获得缓存的set + * + * @param key + * @return + */ + public Set getCacheSet(final String key) { + return redisTemplate.opsForSet().members(key); + } + + + + /** + * 缓存Map + * + * @param key + * @param dataMap + */ + public void setCacheMap(final String key, final Map dataMap) { + if (dataMap != null) { + redisTemplate.opsForHash().putAll(key, dataMap); + } + } + + /** + * 获得缓存的Map + * + * @param key + * @return + */ + public Map getCacheMap(final String key) { + return redisTemplate.opsForHash().entries(key); + } + + /** + * 往Hash中存入数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @param value 值 + */ + public void setCacheMapValue(final String key, final String hKey, final T value) { + redisTemplate.opsForHash().put(key, hKey, value); + } + + /** + * 获取Hash中的数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @return Hash中的对象 + */ + public T getCacheMapValue(final String key, final String hKey) { + HashOperations opsForHash = redisTemplate.opsForHash(); + return opsForHash.get(key, hKey); + } + + /** + * 获取多个Hash中的数据 + * + * @param key Redis键 + * @param hKeys Hash键集合 + * @return Hash对象集合 + */ + public List getMultiCacheMapValue(final String key, final Collection hKeys) { + return redisTemplate.opsForHash().multiGet(key, hKeys); + } + + /** + * 删除Hash中的某条数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @return 是否成功 + */ + public boolean deleteCacheMapValue(final String key, final String hKey) { + return redisTemplate.opsForHash().delete(key, hKey) > 0; + } + + /** + * 获得缓存的基本对象列表 + * + * @param pattern 字符串前缀 + * @return 对象列表 + */ + public Collection keys(final String pattern) { + return redisTemplate.keys(pattern); + } + + + public String getCacheListValue(String encode, Long index) { + return null; + } +} diff --git a/src/main/java/com/yyh/controller/GatewayController.java b/src/main/java/com/yyh/controller/GatewayController.java new file mode 100644 index 0000000..f5e16c5 --- /dev/null +++ b/src/main/java/com/yyh/controller/GatewayController.java @@ -0,0 +1,29 @@ +package com.yyh.controller; + +import com.yyh.common.domain.Result; +import com.yyh.service.GatewayLoadService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @ClassName GatewayController + * @Description 网关控制层 + * @Author YuanYongH + * @Date 2024/4/18 19:59 + */ +@RestController +@RequestMapping("/gateway") +public class GatewayController { + + @Autowired + private GatewayLoadService gatewayLoadService; + + + @GetMapping("/load/node") + public Result loadNode(){ + return Result.success(gatewayLoadService.loadNode()); + } +} diff --git a/src/main/java/com/yyh/gateway/cache/GatewayLoadNodeCache.java b/src/main/java/com/yyh/gateway/cache/GatewayLoadNodeCache.java new file mode 100644 index 0000000..db73920 --- /dev/null +++ b/src/main/java/com/yyh/gateway/cache/GatewayLoadNodeCache.java @@ -0,0 +1,48 @@ +package com.yyh.gateway.cache; + +import com.yyh.gateway.cache.abs.GatewayCacheAbs; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @ClassName GatewayLoadNodeCache + * @Description 网关负载节点缓存 + * @Author YuanYongH + * @Date 2024/4/18 19:38 + */ +@Component +public class GatewayLoadNodeCache extends GatewayCacheAbs { + + private final static String gatewayLoadNodeKey = "node"; + + @Override + public String getPre(){ + return "gateway:load:"; + } + /** + * @Description 负载权重集合 + * @Date 2024/4/18 + * @param nodeList + **/ + public void put(List nodeList){ + redisService.deleteObject(encode(gatewayLoadNodeKey)); + redisService.setCacheList(encode(gatewayLoadNodeKey), nodeList); + } + /** + * @Description 获取所有负载节点 + * @Date 2024/4/18 + * @return java.util.List + **/ + + public List get(){ + return redisService.getCacheList(gatewayLoadNodeKey); + } + + public String getBydIndex(Long index){ + if (index == null || index > 100){ + throw new RuntimeException("下标违法,0-100"); + } + return redisService.getCacheListValue(encode(gatewayLoadNodeKey),index); + } +} diff --git a/src/main/java/com/yyh/gateway/cache/GatewayLoadSeriesCache.java b/src/main/java/com/yyh/gateway/cache/GatewayLoadSeriesCache.java new file mode 100644 index 0000000..58973ed --- /dev/null +++ b/src/main/java/com/yyh/gateway/cache/GatewayLoadSeriesCache.java @@ -0,0 +1,55 @@ +package com.yyh.gateway.cache; + +import com.yyh.common.redis.service.RedisService; +import com.yyh.gateway.cache.abs.GatewayCacheAbs; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +/** + * @ClassName GatewayLoadSeriesCache + * @Description 网关负载序列 + * @Author YuanYongH + * @Date 2024/4/18 16:13 + */ +@Component +public class GatewayLoadSeriesCache extends GatewayCacheAbs { + + private final static String gatewayLoadSeriesKey = "series"; + + + @Override + public String getPre(){ + return "gateway:load:"; + } + + + + //bean创建完成之后执行方法 + @PostConstruct + public void init(){ + redisService.setCacheObject(encode(gatewayLoadSeriesKey),0L); + } + + /** + * @Description 获取当前序列值 + * @Date 2024/4/18 + * @return java.lang.Long + **/ + public Long get(){ + return redisService.getCacheObject(encode(gatewayLoadSeriesKey)); + } + + /** + * @Date 2024/4/18 + * @return 获取自增序列值 + **/ + public boolean incrementAndGet(){ + return redisService.addToSetIfNotExists(encode(gatewayLoadSeriesKey),1L); + } + + + public void reset(){ + this.init(); + } +} diff --git a/src/main/java/com/yyh/gateway/cache/GatewayNodeCache.java b/src/main/java/com/yyh/gateway/cache/GatewayNodeCache.java new file mode 100644 index 0000000..58a32b9 --- /dev/null +++ b/src/main/java/com/yyh/gateway/cache/GatewayNodeCache.java @@ -0,0 +1,58 @@ +package com.yyh.gateway.cache; + +import com.yyh.common.redis.service.RedisService; +import com.yyh.gateway.cache.abs.GatewayCacheAbs; +import com.yyh.gateway.model.GatewayNodeInfo; +import org.springframework.stereotype.Component; + +/** + * @ClassName GatewayNodeCache + * @Description 网关节点缓存 + * @Author YuanYongH + * @Date 2024/4/18 16:06 + */ +@Component +public class GatewayNodeCache extends GatewayCacheAbs { + + + @Override + public String getPre(){ + return "gateway:node:info:"; + } + + + private final RedisService redisService; + + //redis缓存 + public GatewayNodeCache(RedisService redisService) { + this.redisService = redisService; + } + + /** + * @Description + * @Date 2024/4/18 添加缓存数据 + * @param gatewayNodeInfo 节点信息 + **/ + public void add(GatewayNodeInfo gatewayNodeInfo){ + redisService.setCacheObject(encode(gatewayNodeInfo.getNodeId()),gatewayNodeInfo); + } + + /** + * @Description + * @Date 2024/4/18 获取缓存数据 + * @param nodeId 节点ID + * @return com.yyh.gateway.model.GatewayNodeInfo + **/ + public GatewayNodeInfo get(String nodeId){ + return redisService.getCacheObject(encode(nodeId)); + } + + /** + * @Description + * @Date 2024/4/18 删除网关节点 + * @param nodeId 节点ID + **/ + public void remove(String nodeId){ + redisService.deleteObject(encode(nodeId)); + } +} diff --git a/src/main/java/com/yyh/gateway/cache/GatewayNodeScoreCache.java b/src/main/java/com/yyh/gateway/cache/GatewayNodeScoreCache.java new file mode 100644 index 0000000..0d36628 --- /dev/null +++ b/src/main/java/com/yyh/gateway/cache/GatewayNodeScoreCache.java @@ -0,0 +1,10 @@ +package com.yyh.gateway.cache; + +/** + * @ClassName GatewayNodeScoreCache + * @Description 网关节点分数 + * @Author YuanYongH + * @Date 2024/4/18 22:06 + */ +public class GatewayNodeScoreCache { +} diff --git a/src/main/java/com/yyh/gateway/cache/GatewayNodeSetVin.java b/src/main/java/com/yyh/gateway/cache/GatewayNodeSetVin.java new file mode 100644 index 0000000..a2525b9 --- /dev/null +++ b/src/main/java/com/yyh/gateway/cache/GatewayNodeSetVin.java @@ -0,0 +1,10 @@ +package com.yyh.gateway.cache; + +/** + * @ClassName GatewayNodeSetVin + * @Description 网关节点存储VIN + * @Author YuanYongH + * @Date 2024/4/18 22:09 + */ +public class GatewayNodeSetVin { +} diff --git a/src/main/java/com/yyh/gateway/cache/GatewayVehicleLineNodeCache.java b/src/main/java/com/yyh/gateway/cache/GatewayVehicleLineNodeCache.java new file mode 100644 index 0000000..fa948bd --- /dev/null +++ b/src/main/java/com/yyh/gateway/cache/GatewayVehicleLineNodeCache.java @@ -0,0 +1,10 @@ +package com.yyh.gateway.cache; + +/** + * @ClassName GatewayVehicleLineNodeCache + * @Description 网关车辆对应网关节点 + * @Author YuanYongH + * @Date 2024/4/18 22:07 + */ +public class GatewayVehicleLineNodeCache { +} diff --git a/src/main/java/com/yyh/gateway/cache/abs/GatewayCacheAbs.java b/src/main/java/com/yyh/gateway/cache/abs/GatewayCacheAbs.java new file mode 100644 index 0000000..5eca2a9 --- /dev/null +++ b/src/main/java/com/yyh/gateway/cache/abs/GatewayCacheAbs.java @@ -0,0 +1,27 @@ +package com.yyh.gateway.cache.abs; + +import com.yyh.common.redis.service.RedisService; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.servlet.http.PushBuilder; + +/** + * @ClassName GatewayCacheAbs + * @Description 缓存抽象类 + * @Author YuanYongH + * @Date 2024/4/18 19:39 + */ +public abstract class GatewayCacheAbs { + + @Autowired + public RedisService redisService; + + public abstract String getPre(); + + public String encode(K key){ + return getPre() + key; + } + + + +} diff --git a/src/main/java/com/yyh/gateway/model/GatewayNodeInfo.java b/src/main/java/com/yyh/gateway/model/GatewayNodeInfo.java new file mode 100644 index 0000000..654707c --- /dev/null +++ b/src/main/java/com/yyh/gateway/model/GatewayNodeInfo.java @@ -0,0 +1,28 @@ +package com.yyh.gateway.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @ClassName GatewayNodeInfo + * @Description TODO + * @Author YuanYongH + * @Date 2024/4/18 16:05 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class GatewayNodeInfo { + + //节点ID + private String nodeId; + + //公网IP + private String publicIdAddress; + + //内网IP + private String privateIdAddress; +} diff --git a/src/main/java/com/yyh/service/GatewayLoadService.java b/src/main/java/com/yyh/service/GatewayLoadService.java new file mode 100644 index 0000000..354fcb0 --- /dev/null +++ b/src/main/java/com/yyh/service/GatewayLoadService.java @@ -0,0 +1,13 @@ +package com.yyh.service; + +/** + * @ClassName GatewayLoadService + * @Description 网关负载业务 + * @Author YuanYongH + * @Date 2024/4/18 20:00 + */ +public interface GatewayLoadService { + + //返回负载节点 + String loadNode(); +} diff --git a/src/main/java/com/yyh/service/impl/GatewayLoadServiceImpl.java b/src/main/java/com/yyh/service/impl/GatewayLoadServiceImpl.java new file mode 100644 index 0000000..661d427 --- /dev/null +++ b/src/main/java/com/yyh/service/impl/GatewayLoadServiceImpl.java @@ -0,0 +1,43 @@ +package com.yyh.service.impl; + +import com.yyh.gateway.cache.GatewayLoadNodeCache; +import com.yyh.gateway.cache.GatewayLoadSeriesCache; +import com.yyh.gateway.cache.GatewayNodeCache; +import com.yyh.gateway.model.GatewayNodeInfo; +import com.yyh.service.GatewayLoadService; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +/** + * @ClassName GatewayLoadServiceImpl + * @Description 负载实现层 + * @Author YuanYongH + * @Date 2024/4/18 20:00 + */ +@Service +@AllArgsConstructor +public class GatewayLoadServiceImpl implements GatewayLoadService { + + private final Long nodeLength = 100L; + + //负载信息 + private final GatewayLoadNodeCache gatewayLoadNodeCache; + //负载序列 + private final GatewayLoadSeriesCache gatewayLoadSeriesCache; + //节点信息 + private final GatewayNodeCache gatewayNodeCache; + + /** + * @Description 返回负载节点 + * @Date 2024/4/18 + * @return java.lang.String + **/ + @Override + public String loadNode() { + boolean seriesLoad = gatewayLoadSeriesCache.incrementAndGet(); + Long seriesLoadIndex = seriesLoad ? nodeLength : gatewayLoadSeriesCache.get(); + String loadNodeId = gatewayLoadNodeCache.getBydIndex(seriesLoadIndex); + GatewayNodeInfo gatewayNodeInfo = gatewayNodeCache.get(loadNodeId); + return gatewayNodeInfo.getPublicIdAddress(); + } +}