diff --git a/doc/sql/easy_retry_mysql.sql b/doc/sql/easy_retry_mysql.sql index d9179e91..7f29e1ce 100644 --- a/doc/sql/easy_retry_mysql.sql +++ b/doc/sql/easy_retry_mysql.sql @@ -29,6 +29,7 @@ CREATE TABLE `group_config` `namespace_id` varchar(64) NOT NULL DEFAULT '764d604ec6fc45f68cd92514c40e9e1a' COMMENT '命名空间id', `group_name` varchar(64) NOT NULL DEFAULT '' COMMENT '组名称', `description` varchar(256) NOT NULL DEFAULT '' COMMENT '组描述', + `token` varchar(256) NOT NULL DEFAULT 'ER_cKqBTPzCsWA3VyuCfFoccmuIEGXjr5KT' COMMENT 'token', `group_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT '组状态 0、未启用 1、启用', `version` int(11) NOT NULL COMMENT '版本号', `group_partition` int(11) NOT NULL COMMENT '分区', diff --git a/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/config/EasyRetryProperties.java b/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/config/EasyRetryProperties.java index 1944eeba..d1803b03 100644 --- a/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/config/EasyRetryProperties.java +++ b/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/config/EasyRetryProperties.java @@ -34,6 +34,12 @@ public class EasyRetryProperties { */ private String group; + /** + * 令牌 + * 若不填则默认为 SystemConstants::DEFAULT_TOKEN + */ + private String token; + /** * 指定客户端IP,默认取本地IP */ diff --git a/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/netty/NettyChannel.java b/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/netty/NettyChannel.java index 22130f79..002c5a3c 100644 --- a/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/netty/NettyChannel.java +++ b/easy-retry-client/easy-retry-client-common/src/main/java/com/aizuda/easy/retry/client/common/netty/NettyChannel.java @@ -182,6 +182,8 @@ public class NettyChannel { .set(HeadersEnum.HOST.getKey(), serverConfig.getHost()) .set(HeadersEnum.NAMESPACE.getKey(), Optional.ofNullable(easyRetryProperties.getNamespace()).orElse( SystemConstants.DEFAULT_NAMESPACE)) + .set(HeadersEnum.TOKEN.getKey(), Optional.ofNullable(easyRetryProperties.getNamespace()).orElse( + SystemConstants.DEFAULT_TOKEN)) ; //发送数据 diff --git a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/constant/SystemConstants.java b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/constant/SystemConstants.java index a2a8d755..e7bb4aeb 100644 --- a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/constant/SystemConstants.java +++ b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/constant/SystemConstants.java @@ -98,6 +98,11 @@ public interface SystemConstants { */ String DEFAULT_NAMESPACE = "764d604ec6fc45f68cd92514c40e9e1a"; + /** + * 默认Token + */ + String DEFAULT_TOKEN = "ER_cKqBTPzCsWA3VyuCfFoccmuIEGXjr5KT"; + /** * AT 所有人 */ diff --git a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/enums/HeadersEnum.java b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/enums/HeadersEnum.java index ab76d27d..b42bff69 100644 --- a/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/enums/HeadersEnum.java +++ b/easy-retry-common/easy-retry-common-core/src/main/java/com/aizuda/easy/retry/common/core/enums/HeadersEnum.java @@ -18,6 +18,7 @@ public enum HeadersEnum { VERSION("version"), HOST("Host"), NAMESPACE("namespace"), + TOKEN("token"), ; private final String key; diff --git a/easy-retry-datasource/easy-retry-datasource-template/src/main/java/com/aizuda/easy/retry/template/datasource/persistence/po/GroupConfig.java b/easy-retry-datasource/easy-retry-datasource-template/src/main/java/com/aizuda/easy/retry/template/datasource/persistence/po/GroupConfig.java index cbea07ab..ec88d6d2 100644 --- a/easy-retry-datasource/easy-retry-datasource-template/src/main/java/com/aizuda/easy/retry/template/datasource/persistence/po/GroupConfig.java +++ b/easy-retry-datasource/easy-retry-datasource-template/src/main/java/com/aizuda/easy/retry/template/datasource/persistence/po/GroupConfig.java @@ -33,6 +33,8 @@ public class GroupConfig implements Serializable { private Integer bucketIndex; + private String token; + private String description; private LocalDateTime createDt; diff --git a/easy-retry-server/easy-retry-server-common/src/main/java/com/aizuda/easy/retry/server/common/cache/CacheConsumerGroup.java b/easy-retry-server/easy-retry-server-common/src/main/java/com/aizuda/easy/retry/server/common/cache/CacheConsumerGroup.java index 0ce4c838..1b639016 100644 --- a/easy-retry-server/easy-retry-server-common/src/main/java/com/aizuda/easy/retry/server/common/cache/CacheConsumerGroup.java +++ b/easy-retry-server/easy-retry-server-common/src/main/java/com/aizuda/easy/retry/server/common/cache/CacheConsumerGroup.java @@ -39,12 +39,11 @@ public class CacheConsumerGroup implements Lifecycle { * @return 缓存对象 */ public static synchronized void addOrUpdate(String groupName, String namespaceId) { -// EasyRetryLog.LOCAL.info("add consumer cache. groupName:[{}]", groupName); CACHE.put(groupName, namespaceId); } public static void remove(String groupName) { - EasyRetryLog.LOCAL.info("Remove consumer cache. groupName:[{}]", groupName); + EasyRetryLog.LOCAL.debug("Remove consumer cache. groupName:[{}]", groupName); CACHE.invalidate(groupName); } @@ -54,7 +53,7 @@ public class CacheConsumerGroup implements Lifecycle { @Override public void start() { - EasyRetryLog.LOCAL.info("CacheRegisterTable start"); + EasyRetryLog.LOCAL.info("CacheRegisterTable start"); CACHE = CacheBuilder.newBuilder() // 设置并发级别为cpu核心数 .concurrencyLevel(Runtime.getRuntime().availableProcessors()) @@ -65,7 +64,7 @@ public class CacheConsumerGroup implements Lifecycle { @Override public void close() { - EasyRetryLog.LOCAL.info("CacheRegisterTable stop"); + EasyRetryLog.LOCAL.info("CacheRegisterTable stop"); CACHE.invalidateAll(); } } diff --git a/easy-retry-server/easy-retry-server-common/src/main/java/com/aizuda/easy/retry/server/common/cache/CacheToken.java b/easy-retry-server/easy-retry-server-common/src/main/java/com/aizuda/easy/retry/server/common/cache/CacheToken.java new file mode 100644 index 00000000..011171a3 --- /dev/null +++ b/easy-retry-server/easy-retry-server-common/src/main/java/com/aizuda/easy/retry/server/common/cache/CacheToken.java @@ -0,0 +1,61 @@ +package com.aizuda.easy.retry.server.common.cache; + +import cn.hutool.core.lang.Pair; +import com.aizuda.easy.retry.common.core.constant.SystemConstants; +import com.aizuda.easy.retry.common.core.context.SpringContext; +import com.aizuda.easy.retry.common.log.EasyRetryLog; +import com.aizuda.easy.retry.server.common.Lifecycle; +import com.aizuda.easy.retry.template.datasource.access.AccessTemplate; +import com.aizuda.easy.retry.template.datasource.persistence.po.GroupConfig; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + +import java.util.Objects; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +/** + * @author xiaowoniu + * @date 2024-03-29 23:15:26 + * @since 3.2.0 + */ +public class CacheToken implements Lifecycle { + + private static Cache , String/*Token*/> CACHE; + + public static void add(String groupName, String namespaceId, String token) { + CACHE.put(Pair.of(groupName, namespaceId), token); + } + + public static String get(String groupName, String namespaceId) throws ExecutionException { + return CACHE.get(Pair.of(groupName, namespaceId), () -> { + AccessTemplate template = SpringContext.getBean(AccessTemplate.class); + GroupConfig config = template.getGroupConfigAccess().getGroupConfigByGroupName(groupName, namespaceId); + if (Objects.isNull(config)) { + return SystemConstants.DEFAULT_TOKEN; + } + + String token = config.getToken(); + add(groupName, namespaceId, token); + return token; + }); + } + + @Override + public void start() { + EasyRetryLog.LOCAL.info("CacheToken start"); + CACHE = CacheBuilder.newBuilder() + // 设置并发级别为cpu核心数 + .concurrencyLevel(Runtime.getRuntime().availableProcessors()) + // 若当前节点不在消费次组,则自动到期删除 + .expireAfterWrite(60, TimeUnit.SECONDS) + .build(); + } + + @Override + public void close() { + EasyRetryLog.LOCAL.info("CacheToken stop"); + CACHE.invalidateAll(); + } +} diff --git a/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/model/request/GroupConfigRequestVO.java b/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/model/request/GroupConfigRequestVO.java index fc593353..31e32d3a 100644 --- a/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/model/request/GroupConfigRequestVO.java +++ b/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/model/request/GroupConfigRequestVO.java @@ -23,6 +23,11 @@ public class GroupConfigRequestVO { @NotNull(message = "组状态不能为空") private Integer groupStatus; + /** + * 令牌 + */ + private String token; + /** * 描述 */ diff --git a/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/model/response/GroupConfigResponseVO.java b/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/model/response/GroupConfigResponseVO.java index 42d748cd..79cc40ef 100644 --- a/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/model/response/GroupConfigResponseVO.java +++ b/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/model/response/GroupConfigResponseVO.java @@ -36,6 +36,8 @@ public class GroupConfigResponseVO { private List onlinePodList; + private String token; + private LocalDateTime createDt; private LocalDateTime updateDt; diff --git a/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/service/impl/GroupConfigServiceImpl.java b/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/service/impl/GroupConfigServiceImpl.java index 37678aee..506d97b0 100644 --- a/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/service/impl/GroupConfigServiceImpl.java +++ b/easy-retry-server/easy-retry-server-web/src/main/java/com/aizuda/easy/retry/server/web/service/impl/GroupConfigServiceImpl.java @@ -78,8 +78,6 @@ public class GroupConfigServiceImpl implements GroupConfigService { private JdbcTemplate jdbcTemplate; @Autowired private MybatisPlusProperties mybatisPlusProperties; - @Autowired - private RetryTaskMapper retryTaskMapper; @Override @Transactional @@ -137,6 +135,7 @@ public class GroupConfigServiceImpl implements GroupConfigService { groupConfig.setDescription(Optional.ofNullable(groupConfigRequestVO.getDescription()).orElse(StrUtil.EMPTY)); // 使用@TableField(value = "version", update= "%s+1") 进行更新version, 这里必须初始化一个值 groupConfig.setVersion(1); + groupConfig.setToken(null); Assert.isTrue(systemProperties.getTotalPartition() > groupConfigRequestVO.getGroupPartition(), () -> new EasyRetryServerException("分区超过最大分区. [{}]", systemProperties.getTotalPartition() - 1)); Assert.isTrue(groupConfigRequestVO.getGroupPartition() >= 0, @@ -226,6 +225,7 @@ public class GroupConfigServiceImpl implements GroupConfigService { groupConfig.setVersion(1); groupConfig.setNamespaceId(systemUser.getNamespaceId()); groupConfig.setGroupName(groupConfigRequestVO.getGroupName()); + groupConfig.setToken(groupConfigRequestVO.getToken()); groupConfig.setDescription(Optional.ofNullable(groupConfigRequestVO.getDescription()).orElse(StrUtil.EMPTY)); if (Objects.isNull(groupConfigRequestVO.getGroupPartition())) { groupConfig.setGroupPartition( diff --git a/frontend/src/views/config/GroupInfo.vue b/frontend/src/views/config/GroupInfo.vue index 90abd7e8..a449308c 100644 --- a/frontend/src/views/config/GroupInfo.vue +++ b/frontend/src/views/config/GroupInfo.vue @@ -19,6 +19,9 @@ {{ groupInfo.description }} + + {{ groupInfo.token }} + {{ item }} diff --git a/frontend/src/views/config/basicConfigForm/GroupForm.vue b/frontend/src/views/config/basicConfigForm/GroupForm.vue index 268a3278..8da09f09 100644 --- a/frontend/src/views/config/basicConfigForm/GroupForm.vue +++ b/frontend/src/views/config/basicConfigForm/GroupForm.vue @@ -56,6 +56,22 @@ + + + + + + + + + @@ -224,7 +240,7 @@ export default { new Promise((resolve) => { setTimeout(resolve, 100) }).then(() => { - const formData = pick(data, ['id', 'groupName', 'groupStatus', 'description', 'groupPartition', 'idGeneratorMode', 'initScene']) + const formData = pick(data, ['id', 'groupName', 'groupStatus', 'description', 'groupPartition', 'idGeneratorMode', 'initScene', 'token']) formData.groupStatus = formData.groupStatus.toString() formData.idGeneratorMode = formData.idGeneratorMode.toString() formData.initScene = formData.initScene.toString() @@ -232,6 +248,20 @@ export default { form.setFieldsValue(formData) }) + }, + getToken () { + const { form } = this + const token = this.generatePassword(32) + form.setFieldsValue({ token: token }) + }, + generatePassword (length) { + const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' + let password = 'ER_' + for (let i = 0; i < length; i++) { + const randomNumber = Math.floor(Math.random() * chars.length) + password += chars.substring(randomNumber, randomNumber + 1) + } + return password } } }