打包&发布应用
打包为 Fat Jar
Section titled “打包为 Fat Jar”Feat 应用可以通过 Maven 的 maven-shade-plugin 插件打包成一个包含所有依赖的可执行 jar 包(也称为 fat jar)。
在 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 packageDocker 部署
Section titled “Docker 部署”Feat 应用支持多种 Docker 部署方式,包括传统的基于 JRE 的镜像和高性能的 Native 镜像。下面我们将详细介绍这两种部署方式。
有关完整示例,请参见 helloworld_docker
基于 JRE 的 Docker 部署
Section titled “基于 JRE 的 Docker 部署”对于传统的 Docker 部署方式,我们可以使用 JRE 镜像来运行打包好的 jar 文件。
创建 Dockerfile
Section titled “创建 Dockerfile”创建一个基于轻量级 JRE 镜像的 Dockerfile:
FROM eclipse-temurin:21.0.7_6-jre-alpineWORKDIR /featCOPY target/helloworld_docker*.jar helloworld.jar
EXPOSE 8080
CMD ["java", "-jar", "helloworld.jar"]这个 Dockerfile 使用了 Eclipse Temurin 的 Alpine Linux 版本 JRE 镜像,这是一个非常轻量级的选择。
使用以下命令构建和运行基于 JRE 的镜像:
# 构建镜像docker build -t feat-docker:jre .
# 运行容器docker run -p 8080:8080 feat-docker:jre现在你可以通过访问 http://localhost:8080/hello 来测试你的应用了。
构建 Native 镜像
Section titled “构建 Native 镜像”Feat 支持使用 GraalVM 将应用编译为本地可执行文件,从而获得更快的启动速度和更低的资源消耗。我们可以将这些 Native 应用程序打包到 Docker 镜像中,以实现高效的容器化部署。
创建 Dockerfile
Section titled “创建 Dockerfile”以下是一个用于构建 Feat Native 应用的 Dockerfile 示例:
FROM container-registry.oracle.com/graalvm/native-image:21-ol8 AS builder
COPY target/helloworld_docker*.jar helloworld.jar
# BuildRUN native-image --no-fallback -jar helloworld.jar
# 运行阶段:用 ubuntu 最小镜像FROM ubuntu:18.04EXPOSE 8080
# Copy the native executable into the containersCOPY --from=builder /app/helloworld helloworldENTRYPOINT ["/helloworld"]这个 Dockerfile 采用了多阶段构建:
- 第一阶段使用 GraalVM 镜像将 jar 包编译为本地可执行文件
- 第二阶段使用轻量级的 Ubuntu 镜像作为运行环境,复制编译好的可执行文件
执行以下命令构建并运行 Docker 镜像:
# 打包应用mvn clean package
# 构建 Native 镜像docker build -t feat-docker:native .
# 运行容器docker run -p 8080:8080 feat-docker:native现在你可以通过访问 http://localhost:8080/hello 来测试你的应用了。