求,生产者能动态的获取序列化器选择配置
有没有学了rpc框架的鱼友,帮帮忙,给给思路
在最新的rpc框架教程里,有这么一栏。生产者和消费者的序列化选择都写在了配置文件里。

消费者对应配置写在.properties文件里,它会通过
//拿到序列化器
final Serializer serializer = SerializerFactory.getInstance(RpcAplication.getRpcConfig().getSerializer());
得到一个单例的对象RpcConfig,从而拿到与配置文件相应的序列化器。
但我在想,生产者怎么能将配置写死呢,不应该根据消费者选择的序列器来动态更换自己的序列化器吗。
如下是rpcconfig初始化代码:
public class RpcAplication {
private static volatile RpcConfig rpcConfig;
/**
* 框架初始化
*/
public static void init(RpcConfig newRpcConfig) {
rpcConfig = newRpcConfig;
log.info("RpcConfig init :{}", rpcConfig);
}
/**
* 初始化
*/
public static void init(){
RpcConfig newRpcConfig;
try {
newRpcConfig = ConfigUtils.loadCoonfig( RpcConstant.DEFAULT_CONFIG_PREFIX,RpcConfig.class);//读取.properties文件映射属性到newRpcConfig
} catch (Exception e) {
//配置失败使用默认值
log.error("RpcConfig init error:{}",e);
newRpcConfig = new RpcConfig();
}
init(newRpcConfig);
}
/**
* 获得 单例对象 采用双检索模式
* @return
*/
public static RpcConfig getRpcConfig() {
if (rpcConfig == null) {
synchronized (RpcAplication.class) {
if (rpcConfig == null) {
init();
}
}
}
return rpcConfig;
}
}
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
内容推荐
又又又又又完结,本来是点赞系统之前就在写rpc,点赞系统出来后就搁置了,现在终于完结
4
求助一下,未初始化的List为什么值是[]而不是null,做RPC项目时遇到一个很奇怪的问题,在做注册中心本地缓存时,服务缓存private List serviceCache;没有做任何初始化,但第一次执行readCache方法时,serviceCache的值不是null,而是[],自己排查过程:1,类没有使用@Data注解2,成员变量私有,唯一对外提供修改的方法为writeCache3,在方法
3
myrpc 学习笔记-008 重试机制
4
myrpc 学习笔记-009 容错机制
4
myrpc 学习笔记-0010 启动机制和注解驱动(完结)
5
作者分享
有没有做完 rpc项目 第二章 全局加载配置 扩展 的鱼友,教教我怎么实现监听配置文件的变更和自动更新呗😀
这是我的代码:
public class ConfigFileWatcher {
private Props props;
public ConfigFileWatcher(String configFileName) throws IOException, URISyntaxException {
//加载配置文件
this.props = new Props(configFileName);
//启动配置文件监听
startConfigFileWatcher(props, configFileName);
}
private static void startConfigFileWatcher(Props props, String filePath) throws URISyntaxException {
//todo 获取资源文件的URL 要适配linux
URL resourceUrl = ConfigFileWatcher.class.getResource("/"+filePath);
if (resourceUrl == null) {
return;
}
// 将URL转换为Path
Path path = null;
try {
path = Paths.get(resourceUrl.toURI()).getParent();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
if (path == null) {
System.err.println("无法获取资源文件的父目录: " + resourceUrl);
return;
}
// 输出父目录
System.out.println("资源文件的父目录: " + path);
// 创建一个简单的监听器
Watcher watcher = new SimpleWatcher() {
@Override
public void onModify(WatchEvent<?> event, Path currentPath) {
if (currentPath.endsWith(filePath)) {
Console.log("配置文件已修改,重新加载...");
// 重新加载配置文件
props.autoLoad(true);
}
}
};
try{
// 创建并启动监控器
WatchMonitor watchMonitor = WatchMonitor.create(path);
watchMonitor.setWatcher(watcher);
watchMonitor.start();
}catch (Exception e) {
Console.error(e, "无法启动配置文件监听器: {}", path);
}
}
}
在ConfigUtils里
//加载文件
ConfigFileWatcher watcher = new ConfigFileWatcher(application.toString());
return watcher.getProps().toBean(clazz,Prefix);
应该是我理解不够,还是没做出来😜,求各路大佬指点和分享
3
我已通关码神挑战,一起来玩呀~https://1024.codefather.cn/
6
1
oj判题系统-vue中async和await是否失效问题
4
有鱼友了解对日开发吗,找的java实习岗!在沈阳东软,实习工资2000包住宿,今天得到邮件实习通知,目前就这一个实习,还在犹豫!
12
补作业:伙伴匹配系统,解决某个sql语句错误的方法
9
