[微服务] 微服务之web容器优雅升级方案

导读:本篇文章讲解 [微服务] 微服务之web容器优雅升级方案,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

原文链接:https://www.jianshu.com/p/b5fd0f3285e7
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        HealthCheckController.status = ContainerStatus.YELLOW;
        logger.info("睡眠10s,等待tengine踢出当前web服务");
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("准备关闭容器,先关闭线程!");
        this.connector.pause();
        Executor executor = this.connector.getProtocolHandler().getExecutor();

        if (executor instanceof ThreadPoolExecutor) {
            try {
                ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
                threadPoolExecutor.shutdown();

                if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) {

                    logger.warn("Tomcat thread pool did not shut down gracefully within " + waitTime
                            + " seconds. Proceeding with forceful shutdown");
                }
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

在这里插入图片描述

public class Test {
    private static Logger logger = LoggerFactory.getLogger(Test.class);
    private static String url = "http://www.maple.td/check";

    public static void main(String[] args) {
        System.setProperty("java.net.preferIPv4Stack", "true");
        ExecutorService service = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 2; i++) {
            service.execute(() -> {
                try {
                    execute();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    private static void execute() throws InterruptedException {
        int i = 0;
        while (true) {
            Thread.sleep(10);
            i++;
            try {
                List<NameValuePair> pairs = new ArrayList<>(4);
                ResponseResult postResult = null;
                postResult = poolPost(url, pairs);
                String response = postResult.getContent();
                //利于展示,根据路由到的web服务ip对response以不同颜色区分打印
                String sub = response.substring(response.indexOf("[") + 1, response.indexOf("]"));
                //api1
                if (s.equals("172.22.0.2")) {
                    System.err.println("response: " + response);
                } else {
                    logger.info("response: " + response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

private static ResponseResult poolPost(String uri, List<NameValuePair> arguments) throws Exception {
      // 通过 httpclient 或者其他方式请求指定url,这里省略代码
}

在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/73868.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!