Skip to content

Commit 57b736d

Browse files
committed
feat: 为RPC配置类增加Nacos动态配置加载与热更新功能
1 parent cec938a commit 57b736d

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

rpc-core/src/main/java/com/xiaoyu/rpc/core/config/RpcConfig.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
import java.io.InputStream;
88
import java.util.Map;
9+
import java.util.Properties;
10+
import java.util.concurrent.Executor;
11+
12+
import com.alibaba.nacos.api.NacosFactory;
13+
import com.alibaba.nacos.api.config.ConfigService;
14+
import com.alibaba.nacos.api.config.listener.Listener;
915

1016
/**
1117
* RPC配置类 - 从YAML文件读取配置
@@ -133,6 +139,111 @@ private void loadConfig() {
133139
this.protocol = protocolStr;
134140
log.info("检测到 System Property 覆盖协议: {}", this.protocol);
135141
}
142+
143+
// 加载Nacos配置并注册监听
144+
if ("nacos".equalsIgnoreCase(this.registryType)) {
145+
loadNacosConfig();
146+
}
147+
}
148+
149+
/**
150+
* 从Nacos配置中心加载配置,并注册监听以实现热切换
151+
*/
152+
private void loadNacosConfig() {
153+
try {
154+
// Nacos 配置参数
155+
String serverAddr = this.registryAddress != null && !this.registryAddress.isEmpty() ? this.registryAddress
156+
: "127.0.0.1:8848";
157+
String dataId = "rpc-config.yaml";
158+
String group = "DEFAULT_GROUP";
159+
160+
Properties properties = new Properties();
161+
properties.put("serverAddr", serverAddr);
162+
163+
ConfigService configService = NacosFactory.createConfigService(properties);
164+
165+
// 首次获取配置
166+
String configInfo = configService.getConfig(dataId, group, 5000);
167+
if (configInfo != null && !configInfo.isEmpty()) {
168+
log.info("从Nacos加载配置文件成功!\n{}", configInfo);
169+
parseYamlConfigString(configInfo);
170+
} else {
171+
log.info("Nacos中不存在配置 dataId={}, 将使用本地配置", dataId);
172+
}
173+
174+
// 添加监听器,实现热切换
175+
configService.addListener(dataId, group, new Listener() {
176+
@Override
177+
public void receiveConfigInfo(String configInfo) {
178+
log.info("检测到Nacos配置更新!\n{}", configInfo);
179+
if (configInfo != null && !configInfo.isEmpty()) {
180+
parseYamlConfigString(configInfo);
181+
}
182+
}
183+
184+
@Override
185+
public Executor getExecutor() {
186+
return null;
187+
}
188+
});
189+
190+
log.info("已注册Nacos配置监听器 dataId={}, group={}", dataId, group);
191+
} catch (Exception e) {
192+
log.error("加载Nacos配置失败,继续使用本地配置", e);
193+
}
194+
}
195+
196+
/**
197+
* 解析 YAML 格式的字符串并更新配置属性
198+
*/
199+
private void parseYamlConfigString(String yamlString) {
200+
Yaml yaml = new Yaml();
201+
try {
202+
Map<String, Object> config = yaml.load(yamlString);
203+
if (config != null && config.containsKey("rpc")) {
204+
@SuppressWarnings("unchecked")
205+
Map<String, Object> rpcConfig = (Map<String, Object>) config.get("rpc");
206+
updateConfigFields(rpcConfig);
207+
}
208+
} catch (Exception e) {
209+
log.error("解析Nacos配置字符串失败", e);
210+
}
211+
}
212+
213+
/**
214+
* 根据 Map 更新自身字段
215+
*/
216+
private void updateConfigFields(Map<String, Object> rpcConfig) {
217+
if (rpcConfig.containsKey("serializer"))
218+
this.serializerType = (String) rpcConfig.get("serializer");
219+
if (rpcConfig.containsKey("server-port"))
220+
this.serverPort = (Integer) rpcConfig.get("server-port");
221+
if (rpcConfig.containsKey("server-host"))
222+
this.serverHost = (String) rpcConfig.get("server-host");
223+
if (rpcConfig.containsKey("protocol"))
224+
this.protocol = (String) rpcConfig.get("protocol");
225+
if (rpcConfig.containsKey("registry-address"))
226+
this.registryAddress = (String) rpcConfig.get("registry-address");
227+
if (rpcConfig.containsKey("registry"))
228+
this.registryType = (String) rpcConfig.get("registry");
229+
if (rpcConfig.containsKey("proxy"))
230+
this.proxyType = (String) rpcConfig.get("proxy");
231+
if (rpcConfig.containsKey("load-balancer"))
232+
this.loadBalancer = (String) rpcConfig.get("load-balancer");
233+
if (rpcConfig.containsKey("transport"))
234+
this.transport = (String) rpcConfig.get("transport");
235+
if (rpcConfig.containsKey("max-message-size"))
236+
this.maxMessageSize = (Integer) rpcConfig.get("max-message-size");
237+
if (rpcConfig.containsKey("worker-threads"))
238+
this.workerThreads = (Integer) rpcConfig.get("worker-threads");
239+
if (rpcConfig.containsKey("boss-threads"))
240+
this.bossThreads = (Integer) rpcConfig.get("boss-threads");
241+
if (rpcConfig.containsKey("max-connections"))
242+
this.maxConnections = (Integer) rpcConfig.get("max-connections");
243+
244+
log.info("配置更新完毕: 序列化方式={}, 服务器={}:{},使用的协议={}, 注册中心={}, 代理方式={}, 负载均衡={}, 传输层={}, 最大报文={}",
245+
serializerType, serverHost, serverPort, protocol, registryAddress, proxyType, loadBalancer,
246+
transport, maxMessageSize);
136247
}
137248

138249
/**

0 commit comments

Comments
 (0)