1、优雅启动

1.1、说明

优雅启动的目的是为了保证在应用完全启动成功之后再接收到外部请求,否则在应用未完全准备好之前接收请求,则无法正常处理请求,会报错。

1.2、使用场景

在分布式应用中,经常会根据实际需要对应用服务集群进行扩容和缩容,而进行扩容时,即新启动加入集群的应用,应该在应用彻底启动完成后才能接收请求。

1.3、实现方案

消费方服务是从注册中心获取到提供方服务的信息,从而向提供方发起的请求。所以只需要保证在提供方应用未彻底启动之前,消费方不会从注册中心获取到提供方的信息,就不会向未启动成功的提供方发出请求了。
在应用启动完成之前,不要向Nacos-Server注册自身的信息,在应用启动完成之后,再向Nacos-Server注册。
Nacos-Client实现了SpringCloud提供的注册中心的接口,从而实现了在Springboot启动时自动注册。具体注册逻辑,可以参考此文:https://www.yuque.com/davi/pwc2vg/hdwoti
Nacos-Client在Springboot启动时自动向Server端注册时,是通过这个类:com.alibaba.cloud.nacos.registry.NacosServiceRegistry中的register方法来注册的。所以我们可以通过自定义实现一个CustomNacosServiceRegistry继承NacosServiceRegistry并且替换它,重写register方法,即可实现不自动注册。
通过自定义的CustomNacosServiceRegistry来组织它自动注册之后,还需要在应用启动完成之后再手动将应用注册到Nacos-Server。我们可以通过启动一个轮询线程NacosRegisterTask,这个轮询线程将会在应用启动期间不停的轮询应用的状态,如果应用的状态处于启动完成、可注册,那么该线程就会把应用注册到Nacos-Server。

  1. import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
  2. import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
  3. import com.alibaba.nacos.api.naming.NamingService;
  4. import com.alibaba.nacos.api.naming.pojo.Instance;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.cloud.client.serviceregistry.Registration;
  8. public class CustomNacosServiceRegistry extends NacosServiceRegistry {
  9. private static final Logger log = LoggerFactory.getLogger(CustomNacosServiceRegistry.class);
  10. private final NacosDiscoveryProperties nacosDiscoveryProperties;
  11. private final NamingService namingService;
  12. public CustomNacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
  13. super(nacosDiscoveryProperties);
  14. this.nacosDiscoveryProperties = nacosDiscoveryProperties;
  15. this.namingService = nacosDiscoveryProperties.namingServiceInstance();
  16. }
  17. @Override
  18. public void register(Registration registration) {
  19. if (StringUtils.isEmpty(registration.getServiceId())) {
  20. log.warn("No service to register for nacos client...");
  21. return;
  22. }
  23. String serviceId = registration.getServiceId();
  24. Instance instance = getNacosInstanceFromRegistration(registration);
  25. //将实例的健康状态设置为false,注册到nacos的实例就不会接受请求
  26. instance.setEnabled(false);
  27. try {
  28. namingService.registerInstance(serviceId, instance);
  29. log.warn("The system is not ready now, relegate to register thread to do register on nacos. ");
  30. //创建轮训任务
  31. new NacosRegisterTask(instance, namingService, serviceId).start();
  32. } catch (Exception e) {
  33. log.error("nacos registry, {} register failed...{},", serviceId,
  34. registration.toString(), e);
  35. }
  36. }
  37. private Instance getNacosInstanceFromRegistration(Registration registration) {
  38. Instance instance = new Instance();
  39. instance.setIp(registration.getHost());
  40. instance.setPort(registration.getPort());
  41. instance.setWeight(nacosDiscoveryProperties.getWeight());
  42. instance.setClusterName(nacosDiscoveryProperties.getClusterName());
  43. instance.setMetadata(registration.getMetadata());
  44. return instance;
  45. }
  46. }

如何用自定义的CustomNacosServiceRegistry替换原本的NacosServiceRegistry呢?原本的NacosServiceRegistry是会注册到Spring容器中的,那么我们可以将自定义的CustomNacosServiceRegistry也注册到容器中,并且设置优先级高于原本的NacosServiceRegistry,这样Springboot启动时获取注册中心实现的Bean时就会优先获取到我们自定义的CustomNacosServiceRegistry。

  1. import cn.sunline.adp.boot.cedar.handler.CustomNacosServiceRegistry;
  2. import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
  3. import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
  4. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Primary;
  8. @ConditionalOnProperty(name="spring.cloud.nacos.discovery.enabled",havingValue ="true",matchIfMissing=false)
  9. @Configuration
  10. public class NacosRegistryConfig {
  11. //设置一个标识位,标识应用当前的状态是否可以注册
  12. private boolean isRegistryNow=false;
  13. public boolean isRegistryNow() {
  14. return isRegistryNow;
  15. }
  16. //在应用启动完成之后,通过该set方法将标识位置为true
  17. public void setRegistryNow(boolean isRegistryNow) {
  18. this.isRegistryNow = isRegistryNow;
  19. }
  20. @Bean("NacosServiceRegistry") //指定BeanName,防止与原本的NacosServiceRegistry冲突
  21. @Primary //通过该注解可以设置Bean的优先级
  22. public NacosServiceRegistry nacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
  23. return new CustomNacosServiceRegistry(nacosDiscoveryProperties);
  24. }
  25. }
  1. package cn.sunline.adp.boot.cedar.handler;
  2. import cn.sunline.adp.boot.configuration.NacosRegistryConfig;
  3. import cn.sunline.adp.cedar.base.logging.SysLog;
  4. import cn.sunline.adp.cedar.base.logging.SysLogUtil;
  5. import cn.sunline.adp.core.GlobalContext;
  6. import cn.sunline.adp.core.util.SpringUtils;
  7. import com.alibaba.nacos.api.exception.NacosException;
  8. import com.alibaba.nacos.api.naming.NamingService;
  9. import com.alibaba.nacos.api.naming.pojo.Instance;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. public class NacosRegisterTask {
  13. private static final Logger log = LoggerFactory.getLogger(NacosRegisterTask.class);
  14. private Instance instance;
  15. private final NamingService namingService;
  16. private String serviceId;
  17. public NacosOnline(Instance instance, NamingService namingService, String serviceId) {
  18. this.instance=instance;
  19. this.namingService=namingService;
  20. this.serviceId=serviceId;
  21. }
  22. public void start() {
  23. //启动轮训线程
  24. new NacosRegistryThread().start();
  25. }
  26. class NacosRegistryThread extends Thread{
  27. //从Spring容器中获取设置的应用状态标识位
  28. NacosRegistryConfig nacosConfig= SpringUtils.getBean(NacosRegistryConfig.class);
  29. @Override
  30. public void run() {
  31. try {
  32. //如果应用当前的状态是false,则自旋1秒
  33. while(!nacosConfig.isRegistryNow()){
  34. TimeUnit.SECOND.sleep(1);
  35. }
  36. //将实例的健康状态设置为true
  37. instance.setEnabled(true);
  38. namingService.registerInstance(serviceId, instance);
  39. log.info("The system is ready now, register to nacos success.");
  40. } catch (NacosException e) {
  41. e.printStackTrace();
  42. } catch (InterruptedException e) {
  43. e.printStackTrace();
  44. }
  45. String status=nacosConfig.isRegistryNow()==true?" online":" offline";
  46. log.info("nacos registry, {} {}:{} register "+status+" finished", serviceId,
  47. instance.getIp(), instance.getPort());
  48. }
  49. }

注意:一定要在系统启动完成之后,将系统状态标识位置为true,否则轮训线程会一直轮训,且不会注册到Nacos-server

2、优雅停机

2.1、说明

优雅停机的目的是为了在应用停机下线时,也应该保证该应用中所有的服务请求都正确执行完毕。

2.2、使用场景

在分布式应用中,经常会根据实际需要对应用服务集群进行扩容和缩容,而进行缩容时,即准备要停机下线的应用,也应该保证该应用中所有的服务请求都正确执行完毕。

2.3、实现方案

首先要明确一个前提,在Linux系统中,关闭一个JVM进程,通常是通过kill命令,而kill命令常用的有kill -9和kill -15。kill -9和kill -15这两种命令的区别,这里就不在赘述,如果使用kill -9命令来停机,那就不用考虑什么优雅停机了,所以我们在生产环境中是禁止使用kill -9命令停机的。而使用kill -15命令时,JVM就不会马上关闭,而是先做一些准备工作,并且提供了一个钩子函数:Runtime.getRuntime().addShutdownHook(this.shutdownHook)来给用户做一些关闭前的准备工作。
Spring就是通过这个钩子函数来接收关闭容器的信号的。Spring在启动过程中,在刷新容器refreshContext时,向JVM注册了这个钩子。

当应用准备停机下线时,该应用中很有可能还有正在处理中的服务,必须要保证这些正在处理中的服务能够正常处理完成之后,应用才停机。并且,当应用准备下线停机时,就不要再从外部接收新的请求了,所以需要应用主动的从Nacos中下线,这样就不会有新的请求发进来了。
总的来说,优雅停机就是两个步骤。第一步:主动从注册中心下线。第二步:将应用中正在处理的服务执行完毕再停机即可。
Spring容器在准备关闭之前,会发布一个事件:ContextClosedEvent。我们可以通过监听这个事件来知道什么时候要关闭应用了。
第一步:主动从注册中心下线。Nacos实现了SpringCloud提供的统一服务注册的接口:org.springframework.cloud.client.serviceregistry.ServiceRegistry。我们可以通过从Spring容器中获取该ServiceRegistry的Bean,且调用其deregister方法即可实现主动下线。而Nacos在消费方还有一个本地缓存的机制,消费方从Nacos中获取到服务信息之后,会在本地保存一个缓存,并定期刷新该缓存,当我们的提供方服务主动从nacos下线之后,还需要等待一段时间,直到所有消费方都刷新本地缓存。
第二步:等待应用中正在处理的服务执行完毕。这个步骤需要考虑到具体的web容器了,SpringBoot内置了几种常用的,比如Tomcat、Jetty、Undertown。这几种不通的web容器,具体停机的实现方式不同。这里仅给出这三种常用容器的停机策略,看代码。

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.context.event.ContextClosedEvent;
  3. import org.springframework.context.event.SmartApplicationListener;
  4. import org.springframework.core.Ordered;
  5. import org.apache.catalina.connector.Connector;
  6. import org.apache.catalina.startup.Tomcat;
  7. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  8. import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
  9. import org.springframework.boot.web.context.WebServerInitializedEvent;
  10. import org.springframework.boot.web.embedded.jetty.JettyWebServer;
  11. import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
  12. import org.springframework.boot.web.embedded.undertow.UndertowServletWebServer;
  13. import org.springframework.boot.web.server.WebServer;
  14. import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
  15. import org.springframework.cloud.client.serviceregistry.Registration;
  16. import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
  17. import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration;
  18. import org.springframework.context.ApplicationContext;
  19. import org.springframework.context.ApplicationEvent;
  20. import org.springframework.util.ReflectionUtils;
  21. import io.micrometer.core.instrument.MeterRegistry;
  22. import io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics;
  23. import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
  24. import io.undertow.Undertow;
  25. import io.undertow.server.ConnectorStatistics;
  26. import java.lang.reflect.Field;
  27. import java.util.List;
  28. import java.util.concurrent.Executor;
  29. import java.util.concurrent.ExecutorService;
  30. import java.util.concurrent.ThreadPoolExecutor;
  31. import java.util.concurrent.TimeUnit;
  32. import java.util.concurrent.atomic.AtomicInteger;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. /**
  36. * 自定义监听器控制服务注册
  37. *
  38. *
  39. */
  40. public class GracefulRegistryInstanceListener implements SmartApplicationListener {
  41. private static final Logger log = LoggerFactory.getLogger(GracefulRegistryInstanceListener.class);
  42. @Override
  43. public boolean supportsSourceType(Class<?> sourceType) {
  44. return true;
  45. }
  46. @Override
  47. public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
  48. return WebServerInitializedEvent.class.isAssignableFrom(eventType)
  49. || ContextClosedEvent.class.isAssignableFrom(eventType);
  50. }
  51. private WebServerInitializedEvent event;
  52. private final int waitTime = 30;
  53. private ApplicationContext context;
  54. private WebServer webServer;
  55. @Override
  56. public void onApplicationEvent(ApplicationEvent event) {
  57. //在Springboot启动时获取到当前使用的web容器
  58. if (event instanceof WebServerInitializedEvent) {
  59. onWebServerInitializedEvent((WebServerInitializedEvent) event);
  60. } else if(event instanceof ContextClosedEvent){
  61. //监听容器关闭时发布的ContextClosedEvent事件
  62. onContextClosedEvent((ContextClosedEvent) event);
  63. }
  64. }
  65. @Override
  66. public int getOrder() {
  67. return Ordered.HIGHEST_PRECEDENCE;
  68. }
  69. private void onWebServerInitializedEvent(WebServerInitializedEvent event) {
  70. ApplicationContext context = event.getApplicationContext();
  71. if (context instanceof ConfigurableWebServerApplicationContext) {
  72. this.context = context;
  73. this.event = (WebServerInitializedEvent) event;
  74. this.webServer = ((WebServerInitializedEvent) event).getWebServer();
  75. }
  76. }
  77. private void onContextClosedEvent(ContextClosedEvent event) {
  78. if(webServer == null) {
  79. return;
  80. }
  81. //第一步:服务主动下线
  82. ServiceRegistry serviceRegistry=context.getBean(ServiceRegistry.class);
  83. serviceRegistry.deregister(context.getBean(Registration.class));
  84. //休眠1min,等待消费方刷新本地缓存
  85. try{
  86. TimeUnit.SECOND.sleep(60);
  87. }cache(Exception e){
  88. }
  89. //第二步:等待容器中正在处理的服务执行完毕,再关闭容器
  90. if (webServer instanceof TomcatWebServer) {
  91. TomcatWebServer tomcatWebServer = (TomcatWebServer) webServer;
  92. Tomcat tomcat = tomcatWebServer.getTomcat();
  93. Connector connector = tomcat.getConnector();
  94. connector.pause();
  95. Executor executor = connector.getProtocolHandler().getExecutor();
  96. if (executor instanceof ThreadPoolExecutor) {
  97. try {
  98. ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
  99. threadPoolExecutor.shutdown();
  100. if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {
  101. LOGGER.info(
  102. "Tomcat thread pool did not shut down gracefully within {} seconds. Proceeding with forceful shutdown.",
  103. waitTime);
  104. }
  105. } catch (InterruptedException ex) {
  106. Thread.currentThread().interrupt();
  107. }
  108. }
  109. } else if (webServer instanceof JettyWebServer) {
  110. webServer.stop();
  111. } else if (webServer instanceof UndertowServletWebServer) {
  112. UndertowShutdownWrapper gracefulShutdownWrapper = SpringUtils.getBean(UndertowShutdownWrapper.class);
  113. gracefulShutdownWrapper.getGracefulShutdownHandler().shutdown();
  114. UndertowServletWebServer undertowWebserver = (UndertowServletWebServer) webServer;
  115. try {
  116. Field field = undertowWebserver.getClass().getDeclaredField("undertow");
  117. field.setAccessible(true);
  118. Undertow undertow = (Undertow) field.get(undertowWebserver);
  119. List<Undertow.ListenerInfo> listenerInfo = undertow.getListenerInfo();
  120. Undertow.ListenerInfo listener = listenerInfo.get(0);
  121. ConnectorStatistics connectorStatistics = listener.getConnectorStatistics();
  122. long beginTime = System.currentTimeMillis();
  123. while (connectorStatistics.getActiveConnections() > 0) {
  124. long nowTime = System.currentTimeMillis();
  125. if ((nowTime - beginTime) > 30 * 1000) {
  126. break;
  127. }
  128. Thread.sleep(50);
  129. }
  130. } catch (Exception e) {
  131. // TODO: handle exception
  132. }
  133. }
  134. }
  135. }