Technology Sharing

DangerWind-RPC-framework---3. Server shutdown

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

When a machine goes offline, there are many problems: How to remove it from the registration center? How to clean up and release resources? The client also uses the local cache when pulling the service list. How to update the local cache in time?

The graceful offline of the server machine requires the use of ShutdownHook, which is equivalent to adding a shutdown hook. This hook is a thread that is called when the JVM is shut down (that is, when the program ends) to clean up resources and shut down gracefully.

  1. public void clearAll() {
  2. log.info("addShutdownHook for clearAll");
  3. // 添加了一个关闭钩子,这个钩子是一个线程,它在JVM关闭时(即程序结束时)被调用,清理资源,优雅下机
  4. Runtime.getRuntime().addShutdownHook(new Thread(() -> {
  5. try {
  6. InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), NettyRpcServer.PORT);
  7. CuratorUtils.clearRegistry(CuratorUtils.getZkClient(), inetSocketAddress);
  8. } catch (UnknownHostException ignored) {
  9. }
  10. // 操作完整、优雅,便于释放连接资源,便于自定义清理逻辑
  11. ThreadPoolFactoryUtil.shutDownAllThreadPool();
  12. }));
  13. }

In the hook thread, you need to write the logic for deleting nodes from the registration center, as shown below:

  1. // RPC Server端 本机所注册服务的缓存
  2. private static final Set<String> REGISTERED_PATH_SET = ConcurrentHashMap.newKeySet();
  3. public static void clearRegistry(CuratorFramework zkClient, InetSocketAddress inetSocketAddress) {
  4. REGISTERED_PATH_SET.stream().parallel().forEach(p -> {
  5. try {
  6. // 是本机在ZK注册的节点
  7. if (p.endsWith(inetSocketAddress.toString())) {
  8. // 根据路径名删除节点
  9. zkClient.delete().forPath(p);
  10. }
  11. } catch (Exception e) {
  12. log.error("clear registry for path [{}] fail", p);
  13. }
  14. });
  15. log.info("All registered services on the server are cleared:[{}]", REGISTERED_PATH_SET.toString());
  16. }

After deleting the node in the registry center ZK, you need to release the thread pool resources:

  1. public static void shutDownAllThreadPool() {
  2. log.info("call shutDownAllThreadPool method");
  3. THREAD_POOLS.entrySet().parallelStream().forEach(entry -> {
  4. ExecutorService executorService = entry.getValue();
  5. // 停止接收新的任务,但已提交的任务会继续执行
  6. executorService.shutdown();
  7. log.info("shut down thread pool [{}] [{}]", entry.getKey(), executorService.isTerminated());
  8. try {
  9. // 等待线程池中的任务在指定的时间内完成。如果在指定时间内线程池未能终止,会抛出 InterruptedException
  10. executorService.awaitTermination(10, TimeUnit.SECONDS);
  11. } catch (InterruptedException e) {
  12. log.error("Thread pool never terminated");
  13. // 指定时间内线程池未能终止,立即停止所有正在执行的任务
  14. executorService.shutdownNow();
  15. }
  16. });
  17. }

Customizing the logic of closing the thread pool can more elegantly release thread pool resources. It can stop accepting new tasks, continue to execute submitted tasks within a certain waiting time range, and forcefully terminate the thread pool if the waiting time is exceeded.