当前位置:网站首页>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进行
其中学序列化,编码不会
边栏推荐
- It can't be true? Nailing has been downloaded. Don't you know you can play like this?
- 93.(leaflet篇)leaflet态势标绘-进攻方向修改
- 实验二 YUV
- 算法面试高频题解指南【一】
- Networkx visualizes graphs
- 大咖訪談 | 開源社區裏各種奇怪的現狀——夜天之書陳梓立tison
- 多商户系统的直播功能用过吗?用过的朋友扣个 666!
- Spark疑难杂症排查-Premature EOF: no length prefix available
- WPS data splitting
- SQL报错盲注实例分析
猜你喜欢

Niuke Xiaobai month race 53

php可不可以拆分数组

Detailed analysis of the 110th blog of the fledgling Xiao Li in stm32__ NVIC_ Setprioritygrouping (uint32_t prioritygroup) function

多商户系统的直播功能用过吗?用过的朋友扣个 666!

Experiment III LZW

Worthington:来自酵母的己糖激酶的特性及其它参数说明

实验二 YUV

Hcip --- BGP comprehensive experiment

Expérience II Yuv

技术干货 | 基于MindSpore详解Perplexity语言模型评价指标
随机推荐
组蛋白研究丨Worthington小牛胸腺组蛋白的特征及文献参考
如何高效安装MindSpore的GPU版本
This is not a true sense of the meta universe, which should have its own distinctive characteristics and unique development logic
C voice realizes TCP client and TCP server, and QT call test
实验三 LZW
树以及二叉树的常用性质以及遍历
[day 31] given an integer n, find the base and exponent of each prime factor | basic theorem of arithmetic
ProSci LAG3抗体:改善体外研究,助力癌症免疫治疗
使用路由协议配置 IPv6 over IP手动隧道
Example analysis of SQL error reporting and blind injection
笔者认为,元宇宙与互联网的发展逻辑在某种成都上是截然不同的
学习总结 | 真实记录 MindSpore 两日集训营能带给你什么(一)!
剑指Offer | 旋转数组的最小数字
深入浅出地理解STM32中的中断系统——从原理到简单工程示例——保姆级教程
Simulate not all endpoints registered exceptions and Solutions
General, special and hidden attributes of the file (instance generates animation)
技术干货 | 数据处理好难?来看MindSpore提供的解决思路!
数据库基础及安装
How to implement WebService service on the c/s side of C #
2022年中国软件产品全国巡回展即将启航