当前位置:网站首页>RPC-BDY(5)-服务自动注销、负载均衡
RPC-BDY(5)-服务自动注销、负载均衡
2022-07-22 22:22:00 【Hash..】
RPC-BDY(5)
-2022.7.22
一、负载均衡
1.loadBalancer接口
public interface LoadBalancer {
Instance select(List<Instance> instances);
}
2.RandomLoadBalancer
public class RandomLoadBalancer implements LoadBalancer {
@Override
public Instance select(List<Instance> instances) {
return instances.get(new Random().nextInt(instances.size()));
}
}
3.在NacosServiceRegistry的lookupService中使用随机负载均衡
public InetSocketAddress lookupService(String serviceName) {
try {
List<Instance> instances = NacosUtil.getAllInstance(serviceName);
Instance instance = loadBalancer.select(instances);
return new InetSocketAddress(instance.getIp(), instance.getPort());
} catch (NacosException e) {
logger.error("获取服务时有错误发生:", e);
}
return null;
}
二、服务自动注销
思路,在关闭服务线程时,使用钩子函数,这个钩子函数会在 JVM 关闭之前被调用
1.在NacosUtils类中写入clearRegistry方法
public static void clearRegistry() {
if(!serviceNames.isEmpty() && address != null) {
String host = address.getHostName();
int port = address.getPort();
Iterator<String> iterator = serviceNames.iterator();
while(iterator.hasNext()) {
String serviceName = iterator.next();
try {
namingService.deregisterInstance(serviceName, host, port);
} catch (NacosException e) {
logger.error("注销服务 {} 失败", serviceName, e);
}
}
}
}
2.新建ShutdownHook钩子类
public class ShutdownHook {
private static final Logger logger = LoggerFactory.getLogger(ShutdownHook.class);
private final ExecutorService threadPool = ThreadPoolFactory.createDefaultThreadPool("shutdown-hook");
private static final ShutdownHook shutdownHook = new ShutdownHook();
public static ShutdownHook getShutdownHook() {
return shutdownHook;
}
public void addClearAllHook() {
logger.info("关闭后将自动注销所有服务");
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
NacosUtil.clearRegistry();
threadPool.shutdown();
}));
}
}
3.在 RpcServer 启动之前,只需要调用 addClearAllHook,就可以注册这个钩子了
ChannelFuture future = serverBootstrap.bind(host, port).sync();
ShutdownHook.getShutdownHook().addClearAllHook();
future.channel().closeFuture().sync();
三、知识
钩子函数
总结
从开始的RPC协议写起
有客户端,服务端
然后使用TCP传输
改用Netty的NIO进行
其中学序列化,编码不会
边栏推荐
- networkx 对图进行可视化
- matlab simulink 水能和同步电机发电
- Arduino中断实现上升沿检测,并执行其他函数
- The boss asked me to do an IP territorial function and an open source library!
- Go 语言的 RSA 用秘钥解析JSEncrypt.js 这个库加密的密文失败
- 方正中期是什么级别的期货公司?开户安全可靠吗?
- 【读书笔记->统计学】12-01 置信区间的构建-置信区间概念简介
- MySQL消息队列表结构
- Experiment 5 JPEG
- Worthington哺乳动物乳酸脱氢酶研究——特点及测定方案
猜你喜欢

Experiment 7 H.264 file analysis

MySQL消息队列表结构

SLAAC 无状态地址自动配置

Worthington纯化酶制剂助力新生儿心肌细胞分离系统研究

matlab simulink 水能和同步电机发电

技术干货 | 数据处理好难?来看MindSpore提供的解决思路!

PostgreSQL database master-slave deployment master database suspended restore master database

组蛋白研究丨Worthington小牛胸腺组蛋白的特征及文献参考

Spark疑难杂症排查-Premature EOF: no length prefix available

How to open the file in keil is the real path in the 109th blog of fledgling Xiao Li
随机推荐
MPLS VPN 跨域-optionB
Wechat applet project practice
pip更新一个package
Can PHP split arrays
WPS data splitting
Experiment 5 JPEG
实验三 LZW
实验四 DPCM
读书笔记->统计学】12-02 置信区间的构建-t分布概念简介
挖财和启牛都是干什么的开户安全吗?
Example analysis of SQL error reporting and blind injection
postgresql数据库主从部署 主库挂了重新还原主库
实验二 YUV
Reading notes - > statistics] construction of 12-02 confidence interval -t distribution concept introduction
U盘被格式化数据能恢复吗,U盘被格式化了怎样恢复
General, special and hidden attributes of the file (instance generates animation)
It can't be true? Nailing has been downloaded. Don't you know you can play like this?
如何用C语言实现简单职工信息管理系统
传统银行票据打印系统几个关键技术点简要分析
RequestContextHolder