-
改造java应用,提供/getstate接口服务,根据业务的实际情况决定当前应用是否健康,健康时返回码为200,不健康时返回码为403;
-
编译构建应用并且生成docker镜像;
-
验证;
制作基础镜像
- 创建名为Dockerfile的文件,内容如下:
Docker file from bolingcavalry # VERSION 0.0.1
Author: bolingcavalry
#基础镜像
FROM openjdk:8-jdk-stretch
#作者
MAINTAINER BolingCavalry zq2599@gmail.com
#健康检查参数设置,每5秒检查一次,接口超时时间2秒,连续10次返回1就判定该容器不健康
HEALTHCHECK --interval=5s --timeout=2s --retries=10 \
CMD curl --silent --fail localhost:8080/getstate || exit 1
由上述可见Dockerfile的内容非常简单,选定自身的基础镜像为openjdk:8-jdk-stretch,再配置好健康检查参数:
| 参数名 | 作用 |
| — | — |
| health-cmd | 指定命令在容器内执行,用于检查容器健康状态 |
| health-interval | 每次健康检查的间隔时间,默认30秒 |
| health-retries | 假设该值为3,表示若连续三次检测的返回结果都是不健康,就判定该容器不健康,默认值为3 |
| health-timeout | 超时时间,默认30秒 |
- 在Dockerfile文件所在目录执行命令docker build -t bolingcavalry/jdk8-healthcheck:0.0.1 .(最后那个点号不要漏掉),控制台输出如下,提示镜像构建成功:
(base) zhaoqindeMacBook-Pro:springboot-app-docker-health-check zhaoqin$ docker build -t bolingcavalry/jdk8-healthcheck:0.0.1 .
Sending build context to Docker daemon 217.6kB
Step 1/3 : FROM openjdk:8-jdk-stretch
8-jdk-stretch: Pulling from library/openjdk
9a0b0ce99936: Already exists
db3b6004c61a: Already exists
f8f075920295: Already exists
6ef14aff1139: Already exists
962785d3b7f9: Already exists
631589572f9b: Already exists
c55a0c6f4c7b: Already exists
Digest: sha256:8bce852e5ccd41b17bf9704c0047f962f891bdde3e401678a52d14a628defa49
Status: Downloaded newer image for openjdk:8-jdk-stretch
—> 57c2c2d2643d
Step 2/3 : MAINTAINER BolingCavalry zq2599@gmail.com
—> Running in 270f78efa617
Removing intermediate container 270f78efa617
—> 01b5df83611d
Step 3/3 : HEALTHCHECK --interval=5s --timeout=2s --retries=10 CMD curl --silent --fail localhost:8080/getstate || exit 1
—> Running in 7cdd08b9ca22
Removing intermediate container 7cdd08b9ca22
—> 9dd7ffb22df4
Successfully built 9dd7ffb22df4
Successfully tagged bolingcavalry/jdk8-healthcheck:0.0.1
-
此时宿主机上已经有了名为bolingcavalry/jdk8-healthcheck:0.0.1的镜像,该镜像带有容器健康检查的参数配置,以此作为基础镜像来构建的其他镜像都集成了健康检查的特性;
-
如果您已经在hub.docker.com上注册过,就可以用docker login命令登录,然后执行以下命令将本地镜像推送到hub.docker.com给更多人使用:
docker push bolingcavalry/jdk8-healthcheck:0.0.1
改造Java应用
本次实战的目标是让Java应用支持docker的容器健康检查功能,接下来一起创建这个Java应用:
- 这是个基于maven构建的springboot工程,pom.xml内容如下:
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.0.RELEASE
com.bolingcavalry
springboot-app-docker-health-check
0.0.1-SNAPSHOT
springboot-app-docker-health-check
Demo project for Spring Boot
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
commons-io
commons-io
2.5
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
com.google.cloud.tools
jib-maven-plugin
1.7.0
bolingcavalry/jdk8-healthcheck:0.0.1
bolingcavalry/ p r o j e c t . a r t i f a c t I d : {project.artifactId}: project.artifactId:{project.version}
-Xms1g
-Xmx1g
8080
true
上述pom.xml有以下几处需要注意:
a. 使用jib插件来将当前工程构建成docker镜像;
b. 基础镜像是前面构建的bolingcavalry/jdk8-healthcheck:0.0.1,以此为基础镜像的镜像都带有健康检查功能;
- 主要功能类是SpringbootAppDockerHealthCheckApplication.java:
package com.bolingcavalry.springbootappdockerhealthcheck;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframew
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
ork.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.util.List;
@SpringBootApplication
@RestController
@Slf4j
public class SpringbootAppDockerHealthCheckApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAppDockerHealthCheckApplication.class, args);
}
/**
-
读取本地文本文件的内容并返回
-
@return
*/
private String getLocalFileContent() {
String content = null;
try{
InputStream is = new FileInputStream("/app/depend/abc.txt");
List lines = IOUtils.readLines(is, “UTF-8”);
if(null!=lines && lines.size()>0){
content = lines.get(0);
}
} catch (FileNotFoundException e) {
log.error(“local file not found”, e);
} catch (IOException e) {
log.error(“io exception”, e);
}
return content;
}
/**
-
对外提供的http服务,读取本地的txt文件将内容返回,
-
如果读取不到内容返回码为403
-
@return
*/
@RequestMapping(value = “/hello”, method = RequestMethod.GET)
public ResponseEntity hello(){
String localFileContent = getLocalFileContent();
if(StringUtils.isEmpty(localFileContent)) {
log.error(“hello service error”);
return ResponseEntity.status(403).build();
} else {
log.info(“hello service success”);
return ResponseEntity.status(200).body(localFileContent);
}
}
/**
-
该http服务返回当前应用是否正常,
-
如果能从本地txt文件成功读取内容,当前应用就算正常,返回码为200,
-
如果无法从本地txt文件成功读取内容,当前应用就算异常,返回码为403
-
@return
*/
@RequestMapping(value = “/getstate”, method = RequestMethod.GET)
public ResponseEntity getstate(){