From 50ac15a8f7dc1a9e7354057c370c26b86b70cfc4 Mon Sep 17 00:00:00 2001 From: ayi <2294931964@qq.com> Date: Mon, 2 Feb 2026 15:54:44 +0800 Subject: [PATCH] =?UTF-8?q?nacos=E5=8A=A8=E6=80=81=E5=8A=A0=E8=BD=BDbean?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- xiaoyi-auth/pom.xml | 13 ++++++- .../xiaoyishu/auth/alarm/AlarmConfig.java | 39 +++++++++++++++++++ .../xiaoyishu/auth/alarm/AlarmInterface.java | 20 ++++++++++ .../auth/alarm/impl/MailAlarmHelper.java | 31 +++++++++++++++ .../auth/alarm/impl/SmsAlarmHelper.java | 28 +++++++++++++ .../auth/constant/XiaoyiAuthConstants.java | 3 ++ .../auth/controller/TestController.java | 11 ++++++ .../auth/controller/UserController.java | 4 +- .../main/resources/config/application-dev.yml | 24 ++++++------ .../src/main/resources/config/application.yml | 5 ++- .../src/main/resources/config/bootstrap.yml | 14 +++++++ 12 files changed, 176 insertions(+), 18 deletions(-) create mode 100644 xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/AlarmConfig.java create mode 100644 xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/AlarmInterface.java create mode 100644 xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/impl/MailAlarmHelper.java create mode 100644 xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/impl/SmsAlarmHelper.java create mode 100644 xiaoyi-auth/src/main/resources/config/bootstrap.yml diff --git a/pom.xml b/pom.xml index 59dfda7..b42ac1b 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,7 @@ 2.16.1 8.0.29 3.5.5 - 1.2.21 + 1.2.27 1.5.0 1.18 1.38.0 diff --git a/xiaoyi-auth/pom.xml b/xiaoyi-auth/pom.xml index 4f89a34..4012b40 100644 --- a/xiaoyi-auth/pom.xml +++ b/xiaoyi-auth/pom.xml @@ -98,9 +98,18 @@ + + + + - com.alibaba.boot - nacos-config-spring-boot-starter + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + org.springframework.cloud + spring-cloud-starter-bootstrap diff --git a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/AlarmConfig.java b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/AlarmConfig.java new file mode 100644 index 0000000..dfc8526 --- /dev/null +++ b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/AlarmConfig.java @@ -0,0 +1,39 @@ +package top.crushtj.xiaoyishu.auth.alarm; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import top.crushtj.xiaoyishu.auth.alarm.impl.MailAlarmHelper; +import top.crushtj.xiaoyishu.auth.alarm.impl.SmsAlarmHelper; +import top.crushtj.xiaoyishu.auth.constant.XiaoyiAuthConstants; + +/** + * @author ayi + * @version V1.0 + * @title AlarmConfig + * @date 2026/2/2 15:37 + * @description 告警配置类 + */ + +@Configuration +@RefreshScope +public class AlarmConfig { + + @Value("${alarm.type}") + private String alarmType; + + @Bean + @RefreshScope + public AlarmInterface alarmHelper() { + // 根据配置文件中的告警类型,初始化选择不同的告警实现类 + if (StringUtils.equals(XiaoyiAuthConstants.ALARM_TYPE_SMS, alarmType)) { + return new SmsAlarmHelper(); + } else if (StringUtils.equals(XiaoyiAuthConstants.ALARM_TYPE_MAIL, alarmType)) { + return new MailAlarmHelper(); + } else { + throw new IllegalArgumentException("错误的告警类型..."); + } + } +} diff --git a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/AlarmInterface.java b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/AlarmInterface.java new file mode 100644 index 0000000..463dcbe --- /dev/null +++ b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/AlarmInterface.java @@ -0,0 +1,20 @@ +package top.crushtj.xiaoyishu.auth.alarm; + +/** + * @author ayi + * @version V1.0 + * @title AlarmInterface + * @date 2026/2/2 15:37 + * @description 告警接口 + */ + +public interface AlarmInterface { + + /** + * 发送告警信息 + * + * @param message 告警信息 + * @return 发送结果 + */ + boolean send(String message); +} diff --git a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/impl/MailAlarmHelper.java b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/impl/MailAlarmHelper.java new file mode 100644 index 0000000..6494bf7 --- /dev/null +++ b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/impl/MailAlarmHelper.java @@ -0,0 +1,31 @@ +package top.crushtj.xiaoyishu.auth.alarm.impl; + +import lombok.extern.slf4j.Slf4j; +import top.crushtj.xiaoyishu.auth.alarm.AlarmInterface; + +/** + * @author ayi + * @version V1.0 + * @title MailAlarmHelper + * @date 2026/2/2 15:38 + * @description 邮件告警通知类 + */ + +@Slf4j +public class MailAlarmHelper implements AlarmInterface { + + /** + * 发送告警信息 + * + * @param message 告警信息 + * @return 发送结果 + */ + @Override + public boolean send(String message) { + log.info("==> 【邮件告警】:{}", message); + + // 业务逻辑... + + return true; + } +} diff --git a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/impl/SmsAlarmHelper.java b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/impl/SmsAlarmHelper.java new file mode 100644 index 0000000..43609b8 --- /dev/null +++ b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/alarm/impl/SmsAlarmHelper.java @@ -0,0 +1,28 @@ +package top.crushtj.xiaoyishu.auth.alarm.impl; + +import lombok.extern.slf4j.Slf4j; +import top.crushtj.xiaoyishu.auth.alarm.AlarmInterface; + +/** + * @author ayi + * @version V1.0 + * @title SmsAlarmHelper + * @date 2026/2/2 15:39 + * @description 短信告警通知类 + */ + +@Slf4j +public class SmsAlarmHelper implements AlarmInterface { + + /** + * 发送告警信息 + * + * @param message 告警信息 + * @return 发送结果 + */ + @Override + public boolean send(String message) { + log.info("==> 【短信告警】:{}", message); + return false; + } +} diff --git a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/constant/XiaoyiAuthConstants.java b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/constant/XiaoyiAuthConstants.java index 72e6bd9..275f356 100644 --- a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/constant/XiaoyiAuthConstants.java +++ b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/constant/XiaoyiAuthConstants.java @@ -11,4 +11,7 @@ package top.crushtj.xiaoyishu.auth.constant; public class XiaoyiAuthConstants { public static final String NICK_NAME_PREFIX = "咿呀_"; public static final Long XIAOYI_ID_INITIAL_VALUE = 1000000L; + + public static final String ALARM_TYPE_SMS = "sms"; + public static final String ALARM_TYPE_MAIL = "mail"; } diff --git a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/controller/TestController.java b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/controller/TestController.java index c4d38c1..75c9831 100644 --- a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/controller/TestController.java +++ b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/controller/TestController.java @@ -1,9 +1,11 @@ package top.crushtj.xiaoyishu.auth.controller; import com.alibaba.nacos.api.config.annotation.NacosValue; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; +import top.crushtj.xiaoyishu.auth.alarm.AlarmInterface; @Slf4j @RestController @@ -12,8 +14,17 @@ public class TestController { @NacosValue(value = "${rate-limit.api.limit}",autoRefreshed = true) private Integer limit; + @Resource + private AlarmInterface alarm; + @GetMapping("/test") public String test() { return "当前限流阈值为: " + limit; } + + @GetMapping("/alarm") + public String sendAlarm() { + alarm.send("系统出错啦,犬小哈这个月绩效没了,速度上线解决问题!"); + return "alarm success"; + } } diff --git a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/controller/UserController.java b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/controller/UserController.java index ca64f25..ab73e5e 100644 --- a/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/controller/UserController.java +++ b/xiaoyi-auth/src/main/java/top/crushtj/xiaoyishu/auth/controller/UserController.java @@ -12,13 +12,13 @@ import top.crushtj.framework.common.response.Response; import top.crushtj.xiaoyishu.auth.model.vo.user.UserLoginReqVO; import top.crushtj.xiaoyishu.auth.service.UserService; - /** +/** * @author ayi * @version V1.0 * @title UserController * @date 2026-01-18 20:40:21 * @description 用户表(User)表控制层 -*/ + */ @Slf4j @RestController diff --git a/xiaoyi-auth/src/main/resources/config/application-dev.yml b/xiaoyi-auth/src/main/resources/config/application-dev.yml index 1419495..c38711e 100644 --- a/xiaoyi-auth/src/main/resources/config/application-dev.yml +++ b/xiaoyi-auth/src/main/resources/config/application-dev.yml @@ -83,18 +83,18 @@ jasypt: string-output-type: base64 provider-name: SunJCE iv-generator-classname: org.jasypt.iv.RandomIvGenerator -nacos: - config: # Nacos 配置中心 - access-key: # 身份验证 - secret-key: # 身份验证 - data-id: xiaoyishu-auth # 指定要加载的配置数据的 Data Id - group: DEFAULT_GROUP # 指定配置数据所属的组 - type: yaml # 指定配置数据的格式 - server-addr: http://127.0.0.1:8848/ # 指定 Nacos 配置中心的服务器地址 - auto-refresh: true # 是否自动刷新配置 - remote-first: true # 是否优先使用远程配置 - bootstrap: - enable: true # 启动时,预热配置 +#nacos: +# config: # Nacos 配置中心 +# access-key: # 身份验证 +# secret-key: # 身份验证 +# data-id: xiaoyishu-auth # 指定要加载的配置数据的 Data Id +# group: DEFAULT_GROUP # 指定配置数据所属的组 +# type: yaml # 指定配置数据的格式 +# server-addr: http://127.0.0.1:8848/ # 指定 Nacos 配置中心的服务器地址 +# auto-refresh: true # 是否自动刷新配置 +# remote-first: true # 是否优先使用远程配置 +# bootstrap: +# enable: true # 启动时,预热配置 rate-limit: api: diff --git a/xiaoyi-auth/src/main/resources/config/application.yml b/xiaoyi-auth/src/main/resources/config/application.yml index 3061882..7104fe1 100644 --- a/xiaoyi-auth/src/main/resources/config/application.yml +++ b/xiaoyi-auth/src/main/resources/config/application.yml @@ -22,4 +22,7 @@ sa-token: # token 风格(默认可取值:uuid、simple-uuid、random-32、random-64、random-128、tik) token-style: uuid # 是否输出操作日志 - is-log: true \ No newline at end of file + is-log: true + +alarm: + type: sms # 告警方式,sms-短信,email-邮件 \ No newline at end of file diff --git a/xiaoyi-auth/src/main/resources/config/bootstrap.yml b/xiaoyi-auth/src/main/resources/config/bootstrap.yml new file mode 100644 index 0000000..44138d6 --- /dev/null +++ b/xiaoyi-auth/src/main/resources/config/bootstrap.yml @@ -0,0 +1,14 @@ +spring: + application: + name: xiaoyishu-auth # 应用名称 + profiles: + active: dev # 默认激活 dev 本地开发环境 + cloud: + nacos: + config: + server-addr: http://127.0.0.1:8848 # 指定 Nacos 配置中心的服务器地址 + prefix: ${spring.application.name} # 配置 Data Id 前缀,这里使用应用名称作为前缀 + group: DEFAULT_GROUP # 所属组 + namespace: public # 命名空间 + file-extension: yaml # 配置文件格式 + refresh-enabled: true # 是否开启动态刷新