如何正常关闭docker容器中tomcat中的servlts? [英] How to gracefully shutdown servelts in tomcat in docker container?

查看:301
本文介绍了如何正常关闭docker容器中tomcat中的servlts?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我发现了什么

What I found out so far:

  • "docker stop"发送SIGTERM来处理容器中的ID 1.
  • 容器中的进程ID 1是运行tomcat的Java进程.*)
  • 是的,tomcat本身会正常关闭,但servlet不会这样做.
  • Servlet即使在处理reguest(!!)的过程中,也会在2秒后被杀死

*)旁注: 尽管我们的容器入口点是["/opt/tomcat/bin/catalina.sh","run"],但是 在catalina.sh中,通过bash buildin"exec"命令启动Java进程, 因此Java进程替换了 shell进程,从而成为新的进程ID 1. (我可以通过exec在运行的容器中对此进行验证,并在其中进行"ps aux".) 顺便说一句,我正在使用tomcat 7.0.88.

*) Side note: Although our container entrypoint is [ "/opt/tomcat/bin/catalina.sh", "run" ], but in catalina.sh the java process is started via the bash buildin "exec" command, and therefore the java process replaces the shell process and hereby becomes the new process id 1. (I can verify this by exec into the running container and do a "ps aux" in there.) Btw, I am using tomcat 7.0.88.

我发现默认情况下有关tomcat进行宽容关闭的语句( http: //tomcat.10.x6.nabble.com/Graceful-Shutdown-td5020523.html -任何正在进行的连接都将完成"),但我只能看到从docker发送到的SIGTERM Java进程几乎不会停止正在进行的请求执行.

I found statements about tomcat doing gracefull shutdown by default (http://tomcat.10.x6.nabble.com/Graceful-Shutdown-td5020523.html - "any in-progress connections will complete"), but all I can see is that the SIGTERM which is sent from docker to the java process results in hardly stopping the ongoing execution of a request.

我写了一个小小的servlet来测试这种行为:

I wrote a little rest servlet to test this behaviour:

import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.core.Response.Status;

@Path("/")
public class SlowServerRes
{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("test1")
    public Response test1(@QueryParam("sleep") final int sleepDurationSec)
    {
        long received = System.currentTimeMillis();
        System.out.println("+++++++++++++++++++++ received request at " + received);

        for (int i=1; i <= sleepDurationSec; i++) {         
            System.out.println("  ++++ Sleeping for 1 sec ("+i+")");
            try { Thread.sleep(1000); }
            catch (InterruptedException e) { 
                System.out.println("   Sleep was interrupted at second " + i + " ... ignoring/continue sleeping."); 
            }
        }

        long finished = System.currentTimeMillis();
        String result = "received: " + received + "  finished: " + finished;
        System.out.println("+++++++++++++++++++++ " + result);
        Response response = Response.status(Status.OK).entity(result).build();
        return response;
    }
}

经过严格的谷歌搜索后,我终于看到了这个帖子: http://grokbase.com/t/tomcat/users/113nayv5kx/tomcat-6-graceful-shutdown

After intensive googling I finally came across this posting: http://grokbase.com/t/tomcat/users/113nayv5kx/tomcat-6-graceful-shutdown

因此,给tomcat的宽限期不会作为servlet的宽限期传播. 我想知道天气是否有意义,但事实就是如此. 因此,使servlet能够正确结束其正在进行的请求的唯一方法是更改 "unloadDelay"( https://tomcat.apache.org/tomcat -7.0-doc/config/context.html ).

So the grace period that is given to tomcat is NOT propagated as a grace period for the servlets. I wonder weather this makes much sense, but it looks like this is the fact. So the only way to give servlets the possibility to properly end their ongoing requests is to change the "unloadDelay" (https://tomcat.apache.org/tomcat-7.0-doc/config/context.html).

但是,我没有在tomcat配置文件中找到正确的位置来定义非默认的unloadDelay.如果这很重要,我主要关心的是球衣Servlet(org.glassfish.jersey.servlet.ServletContainer).

However, I did not find the right place in the tomcat config files to define a non-default unloadDelay. In case this matters, my main concern is about jersey servlets (org.glassfish.jersey.servlet.ServletContainer).

或者也许还有其他可能性,我现在还看不到?

Or maybe there are other possibilities, which I don't see by now?

(我在标签列表中添加了kubernetes,因为这可能是对kubernets尤其重要的问题,因为它经常重新定位(docker stop-> SIGTERM)容器,只是为了保持负载平衡.)

(I added kubernetes to the tags list, because this may be of major concern especially for kubernets, as it relocates (docker stop->SIGTERM) containers quite often, just to keep the load balanced.)

推荐答案

现在我在这里找到了答案: https://stackoverflow. com/a/11154770/2081279

Now I found the answer here: https://stackoverflow.com/a/11154770/2081279

它在

<Context path="/myapp" unloadDelay="10000"/> 

但仅带大写字母"C"的文字.

but only with upper letter "C"ontext.

这篇关于如何正常关闭docker容器中tomcat中的servlts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆