fix: java 示例改用自建 runtime-tomcat10;war 补录 Jackson 依赖,jar 去掉错误的 RuntimeInfo 抽象
This commit is contained in:
@@ -84,7 +84,7 @@ demo/ 构建示例:基于 image/ 下的镜像做构建与运行演示(dotn
|
|||||||
| [`demo/dotnet9`](demo/dotnet9/README.md) | `compile-dotnet9` / `runtime-dotnet9` | 8082 | .NET 9 两阶段构建示例 |
|
| [`demo/dotnet9`](demo/dotnet9/README.md) | `compile-dotnet9` / `runtime-dotnet9` | 8082 | .NET 9 两阶段构建示例 |
|
||||||
| [`demo/python310-flask`](demo/python310-flask/README.md) | `runtime-python310` | 8083 | Python 3.10 + Flask 3.0 单阶段构建示例 |
|
| [`demo/python310-flask`](demo/python310-flask/README.md) | `runtime-python310` | 8083 | Python 3.10 + Flask 3.0 单阶段构建示例 |
|
||||||
| [`demo/springboot-jar17`](demo/springboot-jar17/README.md) | `compile-maven3-jdk17` / `runtime-jdk17` | 8084 | Spring Boot 3.4 可执行 jar 两阶段构建示例 |
|
| [`demo/springboot-jar17`](demo/springboot-jar17/README.md) | `compile-maven3-jdk17` / `runtime-jdk17` | 8084 | Spring Boot 3.4 可执行 jar 两阶段构建示例 |
|
||||||
| [`demo/spring-war17`](demo/spring-war17/README.md) | `compile-maven3-jdk17` / `tomcat:10.1-jdk17` | 8085 | Spring MVC 6 war 包 + 外置 Tomcat 10.1 两阶段构建示例 |
|
| [`demo/spring-war17`](demo/spring-war17/README.md) | `compile-maven3-jdk17` / `runtime-tomcat10` | 8085 | Spring MVC 6 war 包 + 外置 Tomcat 10.1 两阶段构建示例 |
|
||||||
| [`demo/tengine3`](demo/tengine3/README.md) | `proxy-tengine3` | 80、443 | Tengine 3.1.0 反向代理示例,证书与站点配置挂载目录 |
|
| [`demo/tengine3`](demo/tengine3/README.md) | `proxy-tengine3` | 80、443 | Tengine 3.1.0 反向代理示例,证书与站点配置挂载目录 |
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,17 +9,10 @@ RUN mvn -B -q dependency:go-offline
|
|||||||
COPY src ./src
|
COPY src ./src
|
||||||
RUN mvn -B -q clean package -DskipTests
|
RUN mvn -B -q clean package -DskipTests
|
||||||
|
|
||||||
# 运行阶段:外置 Tomcat 10.1(Servlet 6.0 / Jakarta EE 10,对应 Spring 6),仅含 JDK 17 运行时
|
# 运行阶段:自建 runtime-tomcat10(runtime-jdk17 + Tomcat 10.1.34,Servlet 6.0 / Jakarta EE 10,对应 Spring 6)
|
||||||
FROM tomcat:10.1-jdk17-temurin AS runtime
|
FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:runtime-tomcat10 AS runtime
|
||||||
LABEL MAINTAINER="tianjun@odboy.cn"
|
LABEL MAINTAINER="tianjun@odboy.cn"
|
||||||
ENV TimeZone=Asia/Shanghai
|
WORKDIR /home/admin
|
||||||
# 清掉官方镜像自带的 welcome 应用,只部署本示例
|
|
||||||
RUN rm -rf /usr/local/tomcat/webapps/* \
|
|
||||||
&& ln -snf /usr/share/zoneinfo/${TimeZone} /etc/localtime
|
|
||||||
|
|
||||||
# 带版本号的 war 改名后部署,上下文路径 = /spring-war17
|
# 带版本号的 war 改名后部署,上下文路径 = /spring-war17
|
||||||
COPY --from=build /src/target/spring-war17.war /usr/local/tomcat/webapps/spring-war17.war
|
# 基础镜像已清空 webapps/*,时区、curl、EXPOSE 8080、ENTRYPOINT catalina.sh run 均由基础镜像提供
|
||||||
|
COPY --from=build /src/target/spring-war17.war ${CATALINA_HOME}/webapps/spring-war17.war
|
||||||
EXPOSE 8080
|
|
||||||
# exec 形式:catalina.sh run 在前台运行,SIGTERM 可正常触发 Tomcat 优雅停机
|
|
||||||
ENTRYPOINT ["catalina.sh", "run"]
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
| 阶段 | 镜像 | 说明 |
|
| 阶段 | 镜像 | 说明 |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| 编译 | `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:compile-maven3-jdk17` | JDK 17 + Maven 3.9.9 + 阿里云 Maven 源(见 `image/compile-maven3-jdk17`) |
|
||||||
| 运行 | `tomcat:10.1-jdk17-temurin`(Docker Hub 官方镜像) | Servlet 6.0 / Jakarta EE 10,自带 JDK 17 运行时;仓库内暂无自建 Tomcat 镜像 |
|
| 运行 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:runtime-tomcat10` | 自建镜像:JDK 17 + Tomcat 10.1.34(Servlet 6.0 / Jakarta EE 10,对应 Spring 6),见 `image/runtime-tomcat10` |
|
||||||
|
|
||||||
> Spring 6 要求 Jakarta EE 9+,即 **Tomcat 10.1 及以上**;Tomcat 9(`javax.servlet`)跑不了本示例。
|
> Spring 6 要求 Jakarta EE 9+,即 **Tomcat 10.1 及以上**;Tomcat 9(`javax.servlet`)跑不了本示例。
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
```
|
```
|
||||||
spring-war17/
|
spring-war17/
|
||||||
├── pom.xml # packaging=war,Spring 6.2.1 + Thymeleaf 3.1,不依赖 Boot
|
├── pom.xml # packaging=war,Spring 6.2.1 + Thymeleaf 3.1 + Jackson 2.18,不依赖 Boot
|
||||||
├── src/main/java/cn/odboy/demo/
|
├── src/main/java/cn/odboy/demo/
|
||||||
│ ├── WebAppInitializer.java # 替代 web.xml,注册 DispatcherServlet(映射 /api/*)
|
│ ├── WebAppInitializer.java # 替代 web.xml,注册 DispatcherServlet(映射 /api/*)
|
||||||
│ ├── WebConfig.java # @EnableWebMvc + Thymeleaf 视图解析 + Jackson
|
│ ├── WebConfig.java # @EnableWebMvc + Thymeleaf 视图解析 + Jackson
|
||||||
@@ -22,7 +22,7 @@ spring-war17/
|
|||||||
│ └── PageController.java # @Controller:/api/page 渲染模板
|
│ └── PageController.java # @Controller:/api/page 渲染模板
|
||||||
├── src/main/resources/templates/index.html
|
├── src/main/resources/templates/index.html
|
||||||
├── src/main/webapp/WEB-INF/web.xml # 仅声明 metadata-complete=false 与会话超时,无 servlet 映射
|
├── src/main/webapp/WEB-INF/web.xml # 仅声明 metadata-complete=false 与会话超时,无 servlet 映射
|
||||||
├── Dockerfile # 两阶段:compile-maven3-jdk17 -> tomcat:10.1-jdk17-temurin
|
├── Dockerfile # 两阶段:compile-maven3-jdk17 -> runtime-tomcat10
|
||||||
├── build.sh # 构建镜像(PUSH=1 时同时推送)
|
├── build.sh # 构建镜像(PUSH=1 时同时推送)
|
||||||
├── docker-compose.yml # 部署编排(8085 -> 容器 8080,固定 IP 172.30.2.20)
|
├── docker-compose.yml # 部署编排(8085 -> 容器 8080,固定 IP 172.30.2.20)
|
||||||
├── startup.sh # docker-compose up -d
|
├── startup.sh # docker-compose up -d
|
||||||
@@ -68,8 +68,13 @@ Tomcat 通过 Servlet 3+ 的 `ServletContainerInitializer` 机制自动发现它
|
|||||||
|
|
||||||
**端口**:容器内 Tomcat 仍是 8080(官方镜像默认),宿主机映射到 8085。
|
**端口**:容器内 Tomcat 仍是 8080(官方镜像默认),宿主机映射到 8085。
|
||||||
|
|
||||||
|
**JSON 输出**:`@EnableWebMvc` 只会注册 Spring 自带的转换器(ByteArray/String/Resource 及 JAXB 等),
|
||||||
|
非 Boot 项目里 **Jackson 不会自动带入**。所以 `pom.xml` 显式声明了 `jackson-databind`,
|
||||||
|
Spring 检测到它会自动注册 `MappingJackson2HttpMessageConverter`,`/api/*` 返回 `Map` 即得 JSON;
|
||||||
|
缺了它就会像最常见的排查题那样返回 `HTTP 406 Not Acceptable`(Tomcat 自己的错误页,`Acceptable representations: []`)。
|
||||||
|
|
||||||
**健康检查**:compose 用 `curl` 探测 `/spring-war17/api/info`,`start_period: 30s` 覆盖 war 解压与 Spring 初始化;
|
**健康检查**:compose 用 `curl` 探测 `/spring-war17/api/info`,`start_period: 30s` 覆盖 war 解压与 Spring 初始化;
|
||||||
temurin 镜像自带 curl,无需额外安装。
|
`runtime-tomcat10` 已装 `curl`,无需额外安装。
|
||||||
|
|
||||||
### 运维命令
|
### 运维命令
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ services:
|
|||||||
JAVA_OPTS: "-Xms128m -Xmx256m -Duser.timezone=Asia/Shanghai"
|
JAVA_OPTS: "-Xms128m -Xmx256m -Duser.timezone=Asia/Shanghai"
|
||||||
CATALINA_OPTS: "-Djava.security.egd=file:/dev/./urandom"
|
CATALINA_OPTS: "-Djava.security.egd=file:/dev/./urandom"
|
||||||
healthcheck:
|
healthcheck:
|
||||||
# temurin 镜像自带 curl,直接探测健康页面;start_period 覆盖 war 解压与 Spring 初始化
|
# runtime-tomcat10 已装 curl,直接探测健康页面;start_period 覆盖 war 解压与 Spring 初始化
|
||||||
test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:8080/spring-war17/api/info > /dev/null"]
|
test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:8080/spring-war17/api/info > /dev/null"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
|
|||||||
@@ -17,8 +17,8 @@
|
|||||||
<maven.compiler.release>${java.version}</maven.compiler.release>
|
<maven.compiler.release>${java.version}</maven.compiler.release>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<spring.version>6.2.1</spring.version>
|
<spring.version>6.2.1</spring.version>
|
||||||
<thymeleaf.version>3.1.2.RELEASE</thymeleaf.version>
|
|
||||||
<thymeleaf-spring6.version>3.1.2.RELEASE</thymeleaf-spring6.version>
|
<thymeleaf-spring6.version>3.1.2.RELEASE</thymeleaf-spring6.version>
|
||||||
|
<jackson.version>2.18.2</jackson.version>
|
||||||
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
|
<jakarta.servlet-api.version>6.0.0</jakarta.servlet-api.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
@@ -34,6 +34,12 @@
|
|||||||
<artifactId>thymeleaf-spring6</artifactId>
|
<artifactId>thymeleaf-spring6</artifactId>
|
||||||
<version>${thymeleaf-spring6.version}</version>
|
<version>${thymeleaf-spring6.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- JSON 序列化:非 Boot 项目不会自动带入 Jackson,@RestController 返回 Map 必须显式声明 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
<!-- JSP/Servlet API 由 Tomcat 提供,编译期需要,不打进 war -->
|
<!-- JSP/Servlet API 由 Tomcat 提供,编译期需要,不打进 war -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>jakarta.servlet</groupId>
|
<groupId>jakarta.servlet</groupId>
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ package cn.odboy.demo;
|
|||||||
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
|
||||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
|
||||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
|
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
@@ -12,10 +9,9 @@ import org.thymeleaf.spring6.SpringTemplateEngine;
|
|||||||
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
|
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
|
||||||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring MVC 配置。@EnableWebMvc 负责 JSON 等默认消息转换器,这里额外加上 Thymeleaf 视图解析。
|
* Spring MVC 配置。@EnableWebMvc 已注册包括 Jackson JSON 在内的默认消息转换器,
|
||||||
|
* 这里只额外加上 Thymeleaf 视图解析。
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
@@ -41,10 +37,4 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
viewResolver.setOrder(1);
|
viewResolver.setOrder(1);
|
||||||
registry.viewResolver(viewResolver);
|
registry.viewResolver(viewResolver);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
||||||
// 显式给出 Jackson 转换器,方便后续统一改日期格式等;默认配置已足够
|
|
||||||
converters.add(new MappingJackson2HttpMessageConverter(Jackson2ObjectMapperBuilder.json().build()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ springboot-jar17/
|
|||||||
├── src/main/java/cn/odboy/demo/
|
├── src/main/java/cn/odboy/demo/
|
||||||
│ ├── DemoApplication.java # @SpringBootApplication 入口
|
│ ├── DemoApplication.java # @SpringBootApplication 入口
|
||||||
│ ├── DemoController.java # @RestController:/ 、/api/info、/api/hello/{name}
|
│ ├── DemoController.java # @RestController:/ 、/api/info、/api/hello/{name}
|
||||||
│ ├── PageController.java # @Controller:/page 渲染 Thymeleaf 模板
|
│ └── PageController.java # @Controller:/page 渲染 Thymeleaf 模板
|
||||||
│ └── BootRuntimeInfo.java # 输出 Spring Boot 版本号与主机名
|
|
||||||
├── src/main/resources/
|
├── src/main/resources/
|
||||||
│ ├── application.yml # 端口 8084、优雅停机、actuator 只开 health
|
│ ├── application.yml # 端口 8084、优雅停机、actuator 只开 health
|
||||||
│ └── templates/index.html # /page 页面
|
│ └── templates/index.html # /page 页面
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
package cn.odboy.demo;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringBootVersion;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Boot 环境下的版本信息实现。
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class BootRuntimeInfo implements DemoController.RuntimeInfo {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String appVersion() {
|
|
||||||
return "Spring Boot " + SpringBootVersion.getVersion();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String hostname() {
|
|
||||||
return RuntimeInfo.hostnameOrUnknown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,16 +9,15 @@ import java.time.format.DateTimeFormatter;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.SpringVersion;
|
import org.springframework.core.SpringVersion;
|
||||||
|
import org.springframework.boot.SpringBootVersion;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 运行时信息接口,用于确认镜像里的 JDK / 时区 / 端口等是否符合预期。
|
* 运行时信息接口,用于确认镜像里的 JDK / 时区 / 端口等是否符合预期。
|
||||||
* 宿主 Profile(含 Boot 版本)通过构造注入,非 Boot 环境传空即可。
|
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class DemoController {
|
public class DemoController {
|
||||||
@@ -26,12 +25,6 @@ public class DemoController {
|
|||||||
private static final ZoneId TZ8 = ZoneId.of("Asia/Shanghai");
|
private static final ZoneId TZ8 = ZoneId.of("Asia/Shanghai");
|
||||||
private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss XXX");
|
private static final DateTimeFormatter TIME_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss XXX");
|
||||||
|
|
||||||
private final RuntimeInfo host;
|
|
||||||
|
|
||||||
public DemoController(@Autowired(required = false) RuntimeInfo host) {
|
|
||||||
this.host = host == null ? new RuntimeInfo("", "unknown") : host;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Value("${app.name:spring demo}")
|
@Value("${app.name:spring demo}")
|
||||||
private String appName;
|
private String appName;
|
||||||
|
|
||||||
@@ -50,9 +43,9 @@ public class DemoController {
|
|||||||
body.put("javaHome", System.getProperty("java.home"));
|
body.put("javaHome", System.getProperty("java.home"));
|
||||||
body.put("vm", System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version"));
|
body.put("vm", System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version"));
|
||||||
body.put("spring", SpringVersion.getVersion());
|
body.put("spring", SpringVersion.getVersion());
|
||||||
body.put("application", host.appVersion());
|
body.put("application", "Spring Boot " + SpringBootVersion.getVersion());
|
||||||
body.put("os", System.getProperty("os.name") + " " + System.getProperty("os.arch"));
|
body.put("os", System.getProperty("os.name") + " " + System.getProperty("os.arch"));
|
||||||
body.put("hostname", host.hostname());
|
body.put("hostname", hostname());
|
||||||
body.put("port", port);
|
body.put("port", port);
|
||||||
body.put("zoneId", ZoneId.systemDefault().getId());
|
body.put("zoneId", ZoneId.systemDefault().getId());
|
||||||
body.put("time", ZonedDateTime.now(TZ8).format(TIME_FMT));
|
body.put("time", ZonedDateTime.now(TZ8).format(TIME_FMT));
|
||||||
@@ -70,19 +63,11 @@ public class DemoController {
|
|||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 应用名与版本号;由各示例的实现类提供 */
|
private static String hostname() {
|
||||||
public interface RuntimeInfo {
|
|
||||||
/** 形如 "Spring Boot 3.4.1" 或 "Spring MVC 6.2.1 + Tomcat 10.1.34" */
|
|
||||||
String appVersion();
|
|
||||||
|
|
||||||
String hostname();
|
|
||||||
|
|
||||||
static String hostnameOrUnknown() {
|
|
||||||
try {
|
try {
|
||||||
return InetAddress.getLocalHost().getHostName();
|
return InetAddress.getLocalHost().getHostName();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "unknown";
|
return "unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user