当前位置:   article > 正文

springboot + nginx 项目无感知发版

springboot + nginx 项目无感知发版

要点

  1. springboot项目

  2. spring-boot-starter-actuator插件

  3. 定制化tomcat关闭回调

  4. nginx 负载均衡(至少两台机器)

代码实现

1、引入pom

  1. <dependencies>
  2. <!--集成springmvc框架并实现自动配置 -->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-web</artifactId>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-actuator</artifactId>
  10. </dependency>
  11. </dependencies>

2、定制化tomcat回调

CustomShutdown

  1. /**
  2. * @author lixiaoyi
  3. * @description 定制shutdown 关闭逻辑
  4. * 1、 先暂停所有请求
  5. * 2、 等待现有线程处理完
  6. * 3、 关闭线程
  7. * @date 2021/7/22
  8. **/
  9. @Slf4j
  10. public class CustomShutdown implements TomcatConnectorCustomizer, ApplicationListener<ContextClosedEvent> {
  11. private static final int TIME_OUT = 30;
  12. private volatile Connector connector;
  13. @Override
  14. public void customize(Connector connector) {
  15. this.connector = connector;
  16. }
  17. @Override
  18. public void onApplicationEvent(ContextClosedEvent event) {
  19. /* 暂停所有请求 */
  20. this.connector.pause();
  21. /* 获取tomcat的线程池 */
  22. Executor executor = this.connector.getProtocolHandler().getExecutor();
  23. if (executor instanceof ThreadPoolExecutor) {
  24. try {
  25. ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
  26. /* 关闭线程 (等待线程处理完之后)*/
  27. threadPoolExecutor.shutdown();
  28. if (!threadPoolExecutor.awaitTermination(TIME_OUT, TimeUnit.SECONDS)) {
  29. log.warn("当前应用等待超过最大时长{}秒,将强制关闭", TIME_OUT);
  30. /* Try shutDown Now*/
  31. threadPoolExecutor.shutdownNow();
  32. if (!threadPoolExecutor.awaitTermination(TIME_OUT, TimeUnit.SECONDS)) {
  33. log.error("强制关闭失败", TIME_OUT);
  34. }
  35. }
  36. } catch (InterruptedException e) {
  37. Thread.currentThread().interrupt();
  38. }
  39. }
  40. }
  41. }

ShutdownConfig

  1. @Configuration
  2. public class ShutdownConfig {
  3. @Bean
  4. public CustomShutdown customShutdown() {
  5. return new CustomShutdown();
  6. }
  7. @Bean
  8. public ConfigurableServletWebServerFactory webServerFactory(final CustomShutdown customShutdown) {
  9. TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
  10. tomcatServletWebServerFactory.addConnectorCustomizers(customShutdown);
  11. return tomcatServletWebServerFactory;
  12. }
  13. }

测试例子

  1. @RestController
  2. public class TestController {
  3. @RequestMapping("/test")
  4. public String test() throws InterruptedException {
  5. System.out.println("test");
  6. Thread.sleep(30000);
  7. String s = System.currentTimeMillis()+"";
  8. System.out.println("返回结果:"+s);
  9. return s;
  10. }
  11. @RequestMapping("/test1")
  12. public String test1() {
  13. System.out.println("test1");
  14. return "test1";
  15. }
  16. }

3、nginx配置

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. sendfile on;
  5. keepalive_timeout 65;
  6. #gzip on;
  7. upstream backend {
  8. server localhost:8888;
  9. server localhost:8889;
  10. }
  11. server {
  12. listen 80;
  13. server_name localhost;
  14. #charset koi8-r;
  15. #access_log logs/host.access.log main;
  16. location / {
  17. proxy_pass http://backend;
  18. }
  19. #error_page 404 /404.html;
  20. error_page 500 502 503 504 /50x.html;
  21. location = /50x.html {
  22. root html;
  23. }
  24. }
  25. }

操作流程

  1. 业务请求: http://localhost/test   30s 返回结果

  2. 注释 nginx 配置  server localhost:8888;

  3. 刷新nginx配置  nginx -s reload

  4. 关闭应用 http://localhost:8888/actuator/shutdown

  5. 重新启动关闭应用

  6. 启动完成 重复 2 3 4 5 步骤,第二步注释8889的机器 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/644450
推荐阅读
相关标签
  

闽ICP备14008679号