功能:从配置加载消息中心客户端设置
This commit is contained in:
+16
-8
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 中的值
|
||||
Reference in New Issue
Block a user