Java应用在docker环境配置容器健康检查 (3)

执行以下命令,用新建的java应用镜像创建容器,该容器会将test文件夹映射到容器的/app/depend文件夹:

docker run --rm \ --name=java-health-check \ -p 8080:8080 \ -v /Users/zhaoqin/temp/201910/20:/app/depend \ bolingcavalry/springboot-app-docker-health-check:0.0.1-SNAPSHOT

控制台可见以下输出,表明健康检查接口已经被调用:

2019-10-20 14:16:34.875 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-10-20 14:16:34.876 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-10-20 14:16:34.892 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 16 ms 2019-10-20 14:16:34.959 INFO 1 --- [nio-8080-exec-1] pringbootAppDockerHealthCheckApplication : service is healthy 2019-10-20 14:16:40.159 INFO 1 --- [nio-8080-exec-2] pringbootAppDockerHealthCheckApplication : service is healthy 2019-10-20 14:16:45.356 INFO 1 --- [nio-8080-exec-4] pringbootAppDockerHealthCheckApplication : service is healthy 2019-10-20 14:16:50.580 INFO 1 --- [nio-8080-exec-6] pringbootAppDockerHealthCheckApplication : service is healthy

执行命令docker ps查看容器状态,可见已经是healthy:

(base) zhaoqindeMBP:20 zhaoqin$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 51572d2488fb bolingcavalry/springboot-app-docker-health-check:0.0.1-SNAPSHOT "java -Xms1g -Xmx1g …" About a minute ago Up About a minute (healthy) 0.0.0.0:8080->8080/tcp java-health-check

删除宿主机上的/Users/zhaoqin/temp/201910/20/abc.txt,相当于容器内的abc.txt文件被删除,此时控制台可见健康检查接口在被调用时发现文件不存在,已返回了403错误码:

019-10-20 14:22:37.490 ERROR 1 --- [nio-8080-exec-7] pringbootAppDockerHealthCheckApplication : service is unhealthy

健康检查接口被连续10次调用后,再执行命令docker ps查看容器状态,可见已经是unhealthy:

(base) zhaoqindeMBP:20 zhaoqin$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 51572d2488fb bolingcavalry/springboot-app-docker-health-check:0.0.1-SNAPSHOT "java -Xms1g -Xmx1g …" 7 minutes ago Up 7 minutes (unhealthy) 0.0.0.0:8080->8080/tcp java-health-check

至此,Java应用在docker环境配置容器健康检查的实战就完成了,希望您在给自己的应用添加健康检查时,此文能给您一些参考。

欢迎关注公众号:程序员欣宸

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfxdw.html