打包与部署
This content is not available in your language yet.
本指南介绍如何将 Feat 应用打包为可执行文件,并使用 Docker 进行部署。
打包为 Fat Jar
Section titled “打包为 Fat Jar”使用 Maven 的 maven-shade-plugin 插件打包成包含所有依赖的可执行 jar 包。
配置 pom.xml
Section titled “配置 pom.xml”<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <!-- 追加方式合并服务文件 --> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/services/tech.smartboot.feat.cloud.CloudService</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>your.package.MainClass</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins></build>mvn clean package打包完成后,在 target 目录下会生成包含所有依赖的 jar 文件。
Docker 部署
Section titled “Docker 部署”基于 JRE 的部署
Section titled “基于 JRE 的部署”适用于传统部署场景,使用 JRE 镜像运行 jar 文件。
创建 Dockerfile
Section titled “创建 Dockerfile”FROM eclipse-temurin:21.0.7_6-jre-alpineWORKDIR /featCOPY target/helloworld_docker*.jar helloworld.jar
EXPOSE 8080
CMD ["java", "-jar", "helloworld.jar"]# 构建镜像docker build -t feat-docker:jre .
# 运行容器docker run -p 8080:8080 feat-docker:jre测试:http://localhost:8080/hello
构建 Native 镜像
Section titled “构建 Native 镜像”使用 GraalVM 将应用编译为本地可执行文件,获得更快的启动速度和更低的资源消耗。
创建 Dockerfile
Section titled “创建 Dockerfile”FROM container-registry.oracle.com/graalvm/native-image:21-ol8 AS builder
COPY target/helloworld_docker*.jar helloworld.jar
# 编译为本地可执行文件RUN native-image --no-fallback -jar helloworld.jar
# 运行阶段:使用轻量级基础镜像FROM ubuntu:18.04EXPOSE 8080
COPY --from=builder /app/helloworld helloworldENTRYPOINT ["/helloworld"]# 打包应用mvn clean package
# 构建 Native 镜像docker build -t feat-docker:native .
# 运行容器docker run -p 8080:8080 feat-docker:native测试:http://localhost:8080/hello
部署方式对比
Section titled “部署方式对比”| 部署方式 | 启动速度 | 内存占用 | 适用场景 |
|---|---|---|---|
| Fat Jar + JRE | 中等 | 中等 | 传统部署、开发测试 |
| Native 镜像 | 极快(毫秒级) | 极低 | 云原生、Serverless、微服务 |
有关完整示例,请参见 helloworld_docker 示例项目