refactor: demo 目录拆分为 app-simple 与 plateform,同步更新 README 路径
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
target/
|
||||
src/main/webapp/
|
||||
.git/
|
||||
.DS_Store
|
||||
@@ -0,0 +1,3 @@
|
||||
target/
|
||||
.idea/
|
||||
*.iml
|
||||
@@ -0,0 +1,22 @@
|
||||
# 编译阶段:compile-maven3-jdk17(JDK 17 + Maven 3.9.9 + 阿里云 Maven 源)
|
||||
FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-maven3-jdk17 AS build
|
||||
WORKDIR /src
|
||||
|
||||
# 先只拷贝 POM 预热依赖层:源码改动不会让依赖缓存失效
|
||||
COPY pom.xml ./
|
||||
RUN mvn -B -q dependency:go-offline
|
||||
|
||||
COPY src ./src
|
||||
RUN mvn -B -q clean package -DskipTests \
|
||||
&& cp target/springboot-jar17.jar /src/app.jar
|
||||
|
||||
# 运行阶段:runtime-jdk17(仅 JDK,不含 Maven)
|
||||
FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:runtime-jdk17 AS runtime
|
||||
LABEL MAINTAINER="tianjun@odboy.cn"
|
||||
ENV TimeZone=Asia/Shanghai
|
||||
ENV APP_PORT=8084
|
||||
WORKDIR /home/admin
|
||||
COPY --from=build /src/app.jar ./
|
||||
EXPOSE 8084
|
||||
# exec 形式,保证 docker stop 的 SIGTERM 能被 JVM 收到,触发 Spring 优雅停机
|
||||
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75.0", "-Duser.timezone=Asia/Shanghai", "-jar", "/home/admin/app.jar"]
|
||||
@@ -0,0 +1,90 @@
|
||||
# springboot-jar17 示例
|
||||
|
||||
演示如何用 `compile-maven3-jdk17` + `runtime-jdk17` 两阶段构建并运行一个 Spring Boot 3 Web 应用,产物是**可执行 jar**(内嵌 Tomcat)。
|
||||
|
||||
| 阶段 | 镜像 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| 编译 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-maven3-jdk17` | JDK 17 + Maven 3.9.9 + 阿里云 Maven 源(见 `image/compile-maven3-jdk17`) |
|
||||
| 运行 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:runtime-jdk17` | 仅 JDK 17(见 `image/runtime-jdk17`) |
|
||||
|
||||
运行阶段只拷贝 `spring-boot-maven-plugin` 重打包后的 jar,镜像里不含 Maven、源码和 `~/.m2`。
|
||||
|
||||
### 示例内容
|
||||
|
||||
```
|
||||
springboot-jar17/
|
||||
├── pom.xml # Spring Boot 3.4.1,Java 17,parent 统一管版本
|
||||
├── src/main/java/cn/odboy/demo/
|
||||
│ ├── DemoApplication.java # @SpringBootApplication 入口
|
||||
│ ├── DemoController.java # @RestController:/ 、/api/info、/api/hello/{name}
|
||||
│ └── PageController.java # @Controller:/page 渲染 Thymeleaf 模板
|
||||
├── src/main/resources/
|
||||
│ ├── application.yml # 端口 8084、优雅停机、actuator 只开 health
|
||||
│ └── templates/index.html # /page 页面
|
||||
├── Dockerfile # 两阶段:compile-maven3-jdk17 -> runtime-jdk17
|
||||
├── build.sh # 构建镜像(PUSH=1 时同时推送)
|
||||
├── docker-compose.yml # 部署编排(端口 8084,固定 IP 172.30.1.20)
|
||||
├── startup.sh # docker-compose up -d
|
||||
└── shutdown.sh # docker-compose down
|
||||
```
|
||||
|
||||
### 构建与部署
|
||||
|
||||
```shell
|
||||
sh build.sh # docker build -t registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-springboot-jar17 .
|
||||
PUSH=1 sh build.sh # 构建并推送
|
||||
sh startup.sh # docker-compose -f docker-compose.yml -p springboot-jar17-demo up -d
|
||||
```
|
||||
|
||||
访问:
|
||||
|
||||
```shell
|
||||
curl http://127.0.0.1:8084/ # {"message":"springboot-jar17 is running","java":"17.0.x",...}
|
||||
curl http://127.0.0.1:8084/api/info # 同上,等价入口
|
||||
curl http://127.0.0.1:8084/api/hello/demo # {"message":"Hello, demo!","time":"..."}
|
||||
curl http://127.0.0.1:8084/actuator/health # {"status":"UP"}
|
||||
```
|
||||
|
||||
浏览器打开 http://127.0.0.1:8084/page 可看到渲染后的运行信息页面。
|
||||
|
||||
### 关键点
|
||||
|
||||
**依赖层缓存**:先只 `COPY pom.xml` 执行 `mvn dependency:go-offline`,再 `COPY src` 编译。只改源码时不会重新下载依赖;
|
||||
镜像源已在 `compile-maven3-jdk17` 的 `settings.xml` 里指向阿里云,无需额外换源。
|
||||
|
||||
**两阶段构建**:Maven 仓库、编译中间产物都留在 build 阶段,最终镜像只多一个 jar。这也是 `runtime-jdk*` 与 `compile-maven3-*` 分开的原因。
|
||||
|
||||
**监听地址**:容器内 8084(`server.port=${APP_PORT:8084}`),避开 dotnet6/8/9(8080-8082)、python310-flask(8083)、spring-war17(8085)。
|
||||
|
||||
**时区**:启动参数带 `-Duser.timezone=Asia/Shanghai`,compose 再声明 `TZ`;接口里额外按 UTC+8 输出时间,双保险。
|
||||
|
||||
**优雅停机**:`server.shutdown=graceful`,`ENTRYPOINT` 用 exec 形式,`docker stop` 的 SIGTERM 直接到 JVM。
|
||||
|
||||
**内存**:`-XX:MaxRAMPercentage=75.0` 让 JVM 按容器内存上限算堆;compose 里用 `JAVA_OPTS` 演示另一种覆盖方式(需自行在 Dockerfile 里接 `JAVA_OPTS`,本示例入口为固定数组)。
|
||||
|
||||
### 运维命令
|
||||
|
||||
```shell
|
||||
docker logs -f springboot-jar17-demo # 应用日志
|
||||
docker exec springboot-jar17-demo java -version # 确认 JDK 版本
|
||||
docker exec springboot-jar17-demo sh -c 'ls -lh /home/admin'
|
||||
```
|
||||
|
||||
### 调试时直接在编译镜像里运行
|
||||
|
||||
需要看编译报错或临时跑一下时,可以只挂载源码进编译镜像(Maven 依赖缓存在容器内 `~/.m2`):
|
||||
|
||||
```shell
|
||||
docker run --rm -it -p 8084:8084 -v "$PWD":/src -w /src \
|
||||
registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-maven3-jdk17 \
|
||||
mvn spring-boot:run
|
||||
```
|
||||
|
||||
想把宿主机的 `~/.m2` 复用到构建里,也可以给 `build.sh` 的 `docker build` 加
|
||||
`--build-arg`/`--mount=type=cache,target=/root/.m2` 之类的 BuildKit 缓存(本示例为简单起见未启用)。
|
||||
|
||||
### 停止
|
||||
|
||||
```shell
|
||||
sh shutdown.sh
|
||||
```
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
# 在示例目录下构建示例镜像(编译 + 运行两阶段均在本机 docker 内完成)
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
IMAGE=${IMAGE:-registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-springboot-jar17}
|
||||
docker build -t "${IMAGE}" .
|
||||
|
||||
if [ "${PUSH:-0}" = "1" ]; then
|
||||
docker push "${IMAGE}"
|
||||
fi
|
||||
@@ -0,0 +1,30 @@
|
||||
version: '3.8'
|
||||
|
||||
networks:
|
||||
springboot-jar17-demo:
|
||||
ipam:
|
||||
driver: default
|
||||
config:
|
||||
- subnet: "172.30.1.0/24"
|
||||
|
||||
services:
|
||||
demo:
|
||||
image: registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-springboot-jar17 # 镜像由 build.sh 构建
|
||||
container_name: springboot-jar17-demo
|
||||
restart: unless-stopped
|
||||
ports: # 映射端口
|
||||
- "8084:8084"
|
||||
environment: # 设置环境变量,相当于docker run命令中的-e
|
||||
TZ: Asia/Shanghai
|
||||
APP_PORT: "8084"
|
||||
JAVA_OPTS: "-Xms128m -Xmx256m"
|
||||
healthcheck:
|
||||
# 运行镜像自带 net-tools,直接用端口探测,不额外安装 curl
|
||||
test: ["CMD-SHELL", "netstat -tln | grep -q ':8084 '"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 30s
|
||||
networks:
|
||||
springboot-jar17-demo:
|
||||
ipv4_address: 172.30.1.20
|
||||
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<!-- 父 POM 锁 Spring Boot 3.4.1;3.x 要求 JDK 17+,与 compile-maven3-jdk17 对应 -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.4.1</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>cn.odboy</groupId>
|
||||
<artifactId>springboot-jar17</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>springboot-jar17</name>
|
||||
<description>Spring Boot 3 + 内嵌 Tomcat,打成可执行 jar</description>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Web MVC + Jackson + 内嵌 Tomcat(jar 包,可直接 java -jar 启动) -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
<!-- repackage 生成 BOOT-INF 结构的可执行 jar,Main-Class 为 JarLauncher -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")"
|
||||
docker-compose -f docker-compose.yml -p springboot-jar17-demo down
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.net.InetAddress;
|
||||
import java.time.Duration;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.SpringVersion;
|
||||
import org.springframework.boot.SpringBootVersion;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 运行时信息接口,用于确认镜像里的 JDK / 时区 / 端口等是否符合预期。
|
||||
*/
|
||||
@RestController
|
||||
public class DemoController {
|
||||
|
||||
private static final ZoneId TZ8 = ZoneId.of("Asia/Shanghai");
|
||||
private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss XXX");
|
||||
|
||||
@Value("${app.name:spring demo}")
|
||||
private String appName;
|
||||
|
||||
@Value("${server.port:0}")
|
||||
private int port;
|
||||
|
||||
/** 返回 JDK、Spring 版本、主机名、时区与运行时长,浏览器可直接看 JSON */
|
||||
@GetMapping({"/", "/api/info"})
|
||||
public Map<String, Object> info() {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
long uptimeMillis = ManagementFactory.getRuntimeMXBean().getUptime();
|
||||
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("message", appName + " is running");
|
||||
body.put("java", System.getProperty("java.version"));
|
||||
body.put("javaHome", System.getProperty("java.home"));
|
||||
body.put("vm", System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version"));
|
||||
body.put("spring", SpringVersion.getVersion());
|
||||
body.put("application", "Spring Boot " + SpringBootVersion.getVersion());
|
||||
body.put("os", System.getProperty("os.name") + " " + System.getProperty("os.arch"));
|
||||
body.put("hostname", hostname());
|
||||
body.put("port", port);
|
||||
body.put("zoneId", ZoneId.systemDefault().getId());
|
||||
body.put("time", ZonedDateTime.now(TZ8).format(TIME_FMT));
|
||||
body.put("uptime", Duration.ofMillis(uptimeMillis).toString());
|
||||
body.put("memory", runtime.totalMemory() / 1024 / 1024 + "MB / max " + runtime.maxMemory() / 1024 / 1024 + "MB");
|
||||
return body;
|
||||
}
|
||||
|
||||
/** 带路径参数的示例接口 */
|
||||
@GetMapping("/api/hello/{name}")
|
||||
public Map<String, Object> hello(@PathVariable String name) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("message", "Hello, " + name + "!");
|
||||
body.put("time", ZonedDateTime.now(TZ8).format(TIME_FMT));
|
||||
return body;
|
||||
}
|
||||
|
||||
private static String hostname() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostName();
|
||||
} catch (Exception e) {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.odboy.demo;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* 页面路由,模板见 src/main/resources/templates/index.html。
|
||||
*/
|
||||
@Controller
|
||||
public class PageController {
|
||||
|
||||
private final DemoController demoController;
|
||||
|
||||
public PageController(DemoController demoController) {
|
||||
this.demoController = demoController;
|
||||
}
|
||||
|
||||
/** 渲染 templates/index.html;只有 /page 返回页面,接口仍返回 JSON */
|
||||
@GetMapping("/page")
|
||||
public String page(Model model) {
|
||||
Map<String, Object> info = demoController.info();
|
||||
model.addAttribute("info", info);
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: ${APP_PORT:8084}
|
||||
shutdown: graceful
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: springboot-jar17
|
||||
lifecycle:
|
||||
timeout-per-shutdown-phase: 20s
|
||||
|
||||
app:
|
||||
name: springboot-jar17
|
||||
|
||||
# 健康检查只需要端口探测,actuator 只暴露 health
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: health
|
||||
endpoint:
|
||||
health:
|
||||
show-details: never
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: INFO
|
||||
@@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>springboot-jar17 运行信息</title>
|
||||
<style>
|
||||
body { font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; margin: 40px; color: #222; }
|
||||
h1 { font-size: 20px; }
|
||||
table { border-collapse: collapse; min-width: 520px; }
|
||||
th, td { border: 1px solid #ddd; padding: 6px 12px; text-align: left; }
|
||||
th { background: #f5f5f5; width: 160px; }
|
||||
code { background: #f5f5f5; padding: 1px 4px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>springboot-jar17 运行信息</h1>
|
||||
<table>
|
||||
<tr th:each="entry : ${info}">
|
||||
<th th:text="${entry.key}">key</th>
|
||||
<td th:text="${entry.value}">value</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
其他接口:<code>/api/info</code>、<code>/api/hello/{name}</code>、<code>/actuator/health</code>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")"
|
||||
docker-compose -f docker-compose.yml -p springboot-jar17-demo up -d
|
||||
Reference in New Issue
Block a user