From 62c7dd3e3079bdb3b333c88632845111f434bb84 Mon Sep 17 00:00:00 2001 From: binking338 Date: Mon, 12 Aug 2024 12:41:46 +0800 Subject: [PATCH] =?UTF-8?q?SnowflakeId=E9=85=8D=E7=BD=AE=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultSnowflakeWorkerIdDispatcher.java | 5 +++++ .../snowflake/SnowflakeWorkerIdDispatcher.java | 10 ++++++++-- .../distributed/SnowflakeAutoConfiguration.java | 13 ++++++++++++- .../distributed/configure/SnowflakeProperties.java | 1 + 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/ddd-distributed-idgenerator-snowflake/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/snowflake/DefaultSnowflakeWorkerIdDispatcher.java b/ddd-distributed-idgenerator-snowflake/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/snowflake/DefaultSnowflakeWorkerIdDispatcher.java index a50b6e9..bd55998 100644 --- a/ddd-distributed-idgenerator-snowflake/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/snowflake/DefaultSnowflakeWorkerIdDispatcher.java +++ b/ddd-distributed-idgenerator-snowflake/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/snowflake/DefaultSnowflakeWorkerIdDispatcher.java @@ -32,4 +32,9 @@ public void release() { public boolean pong() { return true; } + + @Override + public void remind(){ + + } } diff --git a/ddd-distributed-idgenerator-snowflake/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/snowflake/SnowflakeWorkerIdDispatcher.java b/ddd-distributed-idgenerator-snowflake/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/snowflake/SnowflakeWorkerIdDispatcher.java index a80be01..6748848 100644 --- a/ddd-distributed-idgenerator-snowflake/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/snowflake/SnowflakeWorkerIdDispatcher.java +++ b/ddd-distributed-idgenerator-snowflake/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/snowflake/SnowflakeWorkerIdDispatcher.java @@ -8,7 +8,7 @@ */ public interface SnowflakeWorkerIdDispatcher { /** - * 获取 + * 获取WorkerId占用 * @param workerId 指定workerId * @param datacenterId 指定datacenterId * @return @@ -16,14 +16,20 @@ public interface SnowflakeWorkerIdDispatcher { long acquire(Long workerId, Long datacenterId); /** - * 释放占用 + * 释放WorkerId占用 * @return */ void release(); /** * 心跳上报 + * 如果长期失联,需通知运维介入 * @return */ boolean pong(); + + /** + * 心跳失败累计到一定次数,提醒运维或相关人员,以便介入处理 + */ + void remind(); } diff --git a/starter/ddd-starter-jpa-rocketmq/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/SnowflakeAutoConfiguration.java b/starter/ddd-starter-jpa-rocketmq/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/SnowflakeAutoConfiguration.java index e79e51c..c2b4ac2 100644 --- a/starter/ddd-starter-jpa-rocketmq/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/SnowflakeAutoConfiguration.java +++ b/starter/ddd-starter-jpa-rocketmq/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/SnowflakeAutoConfiguration.java @@ -21,6 +21,9 @@ @RequiredArgsConstructor @Slf4j public class SnowflakeAutoConfiguration { + private final SnowflakeProperties properties; + + int pongContinuousErrorCount = 0; SnowflakeWorkerIdDispatcher snowflakeWorkerIdDispatcher; @Bean @@ -34,6 +37,7 @@ public SnowflakeIdGenerator snowflakeIdGenerator(SnowflakeWorkerIdDispatcher sno @Bean @ConditionalOnMissingBean(SnowflakeWorkerIdDispatcher.class) public DefaultSnowflakeWorkerIdDispatcher defaultSnowflakeWorkerIdDispatcher(SnowflakeProperties properties) { + log.warn("默认调度器需通过手工配置完成WorkerId、DatacenterId分发,有重复分配风险!!!请根据项目实际情况自行实现SnowflakeWorkerIdDispatcher。"); DefaultSnowflakeWorkerIdDispatcher dispatcher = new DefaultSnowflakeWorkerIdDispatcher( properties.getWorkerId() == null ? 0 : properties.getWorkerId().longValue(), properties.getDatacenterId() == null ? 0 : properties.getDatacenterId().longValue() @@ -44,8 +48,15 @@ public DefaultSnowflakeWorkerIdDispatcher defaultSnowflakeWorkerIdDispatcher(Sno @Scheduled(cron = "0 */1 * * * ?") public void pong() { - if (!snowflakeWorkerIdDispatcher.pong()) { + if (snowflakeWorkerIdDispatcher.pong()) { + log.info("SnowflakeWorkerIdDispatcher 心跳上报成功"); + pongContinuousErrorCount = 0; + } else { log.error("SnowflakeWorkerIdDispatcher 心跳上报失败"); + pongContinuousErrorCount ++; + if(pongContinuousErrorCount > properties.getMaxPongContinuousErrorCount()){ + snowflakeWorkerIdDispatcher.remind(); + } } } diff --git a/starter/ddd-starter-jpa-rocketmq/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/configure/SnowflakeProperties.java b/starter/ddd-starter-jpa-rocketmq/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/configure/SnowflakeProperties.java index 25ed008..15cc027 100644 --- a/starter/ddd-starter-jpa-rocketmq/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/configure/SnowflakeProperties.java +++ b/starter/ddd-starter-jpa-rocketmq/src/main/java/org/netcorepal/cap4j/ddd/application/distributed/configure/SnowflakeProperties.java @@ -16,4 +16,5 @@ public class SnowflakeProperties { Long workerId = null; Long datacenterId = null; + int maxPongContinuousErrorCount = 10; }