From 23d794b26127f6759ec8db30d99f1259ab18169f Mon Sep 17 00:00:00 2001 From: xiaowang Date: Tue, 24 Mar 2026 14:35:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9A=E4=BB=8E=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8A=A0=E8=BD=BD=E6=B6=88=E6=81=AF=E4=B8=AD=E5=BF=83?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/MessageHubClientServiceImpl.java | 24 ++++++---- .../src/main/resources/application.yaml | 6 +++ ...-Core-MessageHub客户端改为读取Nacos配置.md | 46 +++++++++++++++++++ 3 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 docs/changes/2026-03-24-YumiNail-Core-MessageHub客户端改为读取Nacos配置.md diff --git a/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/MessageHubClientServiceImpl.java b/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/MessageHubClientServiceImpl.java index 7ee357d5..4362c3a8 100644 --- a/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/MessageHubClientServiceImpl.java +++ b/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/MessageHubClientServiceImpl.java @@ -4,9 +4,11 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.xiaowang.yuminailcore.service.MessageHubClientService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageBuilder; import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Service; import java.nio.charset.StandardCharsets; @@ -20,16 +22,22 @@ import java.util.UUID; */ @Slf4j @Service +@RefreshScope @RequiredArgsConstructor public class MessageHubClientServiceImpl implements MessageHubClientService { - private static final String SMS_QUEUE = "smsQueue"; - private static final String SOURCE_SYSTEM = "yuminail"; - private static final String VERIFY_TEMPLATE_CODE = "SPUG0001"; - private final RabbitTemplate rabbitTemplate; private final ObjectMapper objectMapper; + @Value("${messagehub.client.sms-queue:smsQueue}") + private String smsQueue; + + @Value("${messagehub.client.source-system:yuminail}") + private String sourceSystem; + + @Value("${messagehub.client.verify-template-code:SPUG0001}") + private String verifyTemplateCode; + @Override public void sendSmsVerificationCode(String phone, String code) { try { @@ -37,8 +45,8 @@ public class MessageHubClientServiceImpl implements MessageHubClientService { messageBody.put("receiver", phone); messageBody.put("channelType", "SMS"); messageBody.put("msgType", "VERIFICATION"); - messageBody.put("templateCode", VERIFY_TEMPLATE_CODE); - messageBody.put("sourceSystem", SOURCE_SYSTEM); + messageBody.put("templateCode", verifyTemplateCode); + messageBody.put("sourceSystem", sourceSystem); messageBody.put("uniqueId", UUID.randomUUID().toString()); messageBody.put("sendMethod", "SPUG"); @@ -56,9 +64,9 @@ public class MessageHubClientServiceImpl implements MessageHubClientService { .setMessageId(UUID.randomUUID().toString()) .build(); - rabbitTemplate.convertAndSend(SMS_QUEUE, message); + rabbitTemplate.convertAndSend(smsQueue, message); log.info("短信验证码消息已发送到队列,手机号: {}, sourceSystem: {}, templateCode: {}", - phone, SOURCE_SYSTEM, VERIFY_TEMPLATE_CODE); + phone, sourceSystem, verifyTemplateCode); } catch (Exception exception) { log.error("发送短信验证码失败", exception); throw new RuntimeException("发送短信验证码失败: " + exception.getMessage(), exception); diff --git a/YumiNail-Core/src/main/resources/application.yaml b/YumiNail-Core/src/main/resources/application.yaml index f586c0a1..d4eb15d4 100644 --- a/YumiNail-Core/src/main/resources/application.yaml +++ b/YumiNail-Core/src/main/resources/application.yaml @@ -20,3 +20,9 @@ server: mybatis: configuration: map-underscore-to-camel-case: true + +messagehub: + client: + sms-queue: smsQueue + source-system: yuminail + verify-template-code: SPUG0001 diff --git a/docs/changes/2026-03-24-YumiNail-Core-MessageHub客户端改为读取Nacos配置.md b/docs/changes/2026-03-24-YumiNail-Core-MessageHub客户端改为读取Nacos配置.md new file mode 100644 index 00000000..6495441a --- /dev/null +++ b/docs/changes/2026-03-24-YumiNail-Core-MessageHub客户端改为读取Nacos配置.md @@ -0,0 +1,46 @@ +# 2026-03-24 YumiNail-Core MessageHub 客户端改为读取 Nacos 配置 + +## 本次目标 + +将 `MessageHubClientServiceImpl` 中写死的短信队列、来源系统标识、验证码模板编码改为通过配置项读取,便于在 `YumiNail-Dev` 命名空间下由 Nacos 统一维护。 + +## 主要改动 + +- `MessageHubClientServiceImpl` 不再使用硬编码常量: + - `smsQueue` + - `yuminail` + - `SPUG0001` +- 改为读取以下配置项: + - `messagehub.client.sms-queue` + - `messagehub.client.source-system` + - `messagehub.client.verify-template-code` +- 为消息客户端增加 `@RefreshScope`,便于配置刷新后重新注入 +- 在 [application.yaml](/E:/code/xiaowang/YumiNail-Core/src/main/resources/application.yaml) 中增加同名默认值,作为本地和未下发 Nacos 时的兜底配置 + +## 涉及文件 + +- [MessageHubClientServiceImpl.java](/E:/code/xiaowang/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/MessageHubClientServiceImpl.java) +- [application.yaml](/E:/code/xiaowang/YumiNail-Core/src/main/resources/application.yaml) + +## Nacos 配置项 + +建议在 `YumiNail-Dev` 命名空间对应的 `yuminail-core.yaml` 中维护: + +```yaml +messagehub: + client: + sms-queue: smsQueue + source-system: yuminail + verify-template-code: SPUG0001 +``` + +## 验证情况 + +- 执行 `mvn -q -DskipTests compile` +- 编译通过 +- 本次无 SQL 变更 + +## 说明 + +- 当前实现保留了默认值兜底,因此即使 Nacos 暂未补齐这 3 个键,服务也仍可启动 +- 一旦 Nacos 下发同名配置,会优先使用 Nacos 中的值