nacos动态加载bean

This commit is contained in:
2026-02-02 15:54:44 +08:00
parent 872be51082
commit 50ac15a8f7
12 changed files with 176 additions and 18 deletions
+1 -1
View File
@@ -41,7 +41,7 @@
<jackson.version>2.16.1</jackson.version>
<mysql-connector-java.version>8.0.29</mysql-connector-java.version>
<mybatis-plus.version>3.5.5</mybatis-plus.version>
<druid.version>1.2.21</druid.version>
<druid.version>1.2.27</druid.version>
<flatten-maven-plugin.version>1.5.0</flatten-maven-plugin.version>
<jansi.version>1.18</jansi.version>
<sa-token.version>1.38.0</sa-token.version>
+11 -2
View File
@@ -98,9 +98,18 @@
</dependency>
<!-- Nacos 配置中心 -->
<!-- <dependency> -->
<!-- <groupId>com.alibaba.boot</groupId> -->
<!-- <artifactId>nacos-config-spring-boot-starter</artifactId> -->
<!-- </dependency> -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
@@ -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("错误的告警类型...");
}
}
}
@@ -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);
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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";
}
@@ -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";
}
}
@@ -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
@@ -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:
@@ -22,4 +22,7 @@ sa-token:
# token 风格(默认可取值:uuid、simple-uuid、random-32、random-64、random-128、tik
token-style: uuid
# 是否输出操作日志
is-log: true
is-log: true
alarm:
type: sms # 告警方式,sms-短信,email-邮件
@@ -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 # 是否开启动态刷新