一、多环境配置管理(Profile方案)

推荐方案:通过Maven Profiles实现环境隔离
pom.xml中定义不同环境配置,避免硬编码application.yml中:

<profiles>
  <!-- 默认环境 -->
    <profile>
    <id>node</id>
      <properties>
      <activeProfile>node</activeProfile> <!-- 环境标识 -->
      <package-name>${project.artifactId}</package-name>
      <boot-main>com.example.Application</boot-main>
      </properties>
      <activation>
      <activeByDefault>true</activeByDefault>
      </activation>
    </profile>
    <!-- 其他环境 -->
      <profile>
      <id>node1</id>
        <properties>
        <activeProfile>node1</activeProfile>
          <!-- 其他参数同上 -->
          </properties>
        </profile>
      </profiles>

目录结构

src/main/
└── profiles/
├── node/     # 默认环境配置
├── node1/    # 环境1配置
└── node2/    # 环境2配置
二、使用assembly-plugin打包发布

目标:生成包含启动脚本、配置文件和依赖的ZIP包

1. Maven插件配置
<build>
  <plugins>
    <!-- 排除配置文件,由assembly单独处理 -->
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.2.0</version>
        <configuration>
          <excludes>
          <exclude>**/*.yml</exclude>
          <exclude>**/*.properties</exclude>
          </excludes>
        </configuration>
      </plugin>
      <!-- 核心打包插件 -->
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
          <configuration>
            <descriptors>
            <descriptor>src/assembly/assembly.xml</descriptor>
            </descriptors>
          </configuration>
          <executions>
            <execution>
            <phase>package</phase>
            <goals><goal>single</goal></goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
2. Assembly描述文件 (assembly.xml)
<assembly>
<id>${activeProfile}</id>
<formats><format>zip</format></formats>
<includeBaseDirectory>false</includeBaseDirectory>
  <!-- 依赖库 -->
    <dependencySets>
      <dependencySet>
      <outputDirectory>${package-name}-${activeProfile}/lib</outputDirectory>
      <useProjectArtifact>false</useProjectArtifact>
      </dependencySet>
    </dependencySets>
    <!-- 配置文件 -->
      <fileSets>
        <fileSet>
        <directory>src/main/profiles/${activeProfile}</directory>
        <outputDirectory>${package-name}-${activeProfile}/conf</outputDirectory>
        </fileSet>
        <!-- 启动脚本 -->
          <fileSet>
          <directory>src/main/scripts</directory>
          <fileMode>0755</fileMode>
          <filtered>true</filtered> <!-- 启用变量替换 -->
          </fileSet>
          <!-- 应用JAR -->
            <fileSet>
            <directory>target</directory>
              <includes>
              <include>*.jar</include>
              </includes>
            <outputDirectory>${package-name}-${activeProfile}</outputDirectory>
            </fileSet>
          </fileSets>
        </assembly>

打包命令

mvn clean package -P node1 # 指定node1环境
三、Shell启动工具详解 (shenniu_publish.sh)

功能:解压、启动、停止、重启应用

核心代码片段:
#!/usr/bin/env bash
# 从Maven接收的参数
baseZipName="${package-name}-${activeProfile}"
packageName="${package-name}"
mainClass="${boot-main}"
# 解压部署包
function unzip_pkg(
) {
if [ -f "${baseZipPath}" ]
;
then
unzip -o -d ${deployDir} ${baseZipPath}
chmod +x ${deployDir}/*.sh
else
echo "错误:部署包不存在 ${baseZipPath}"
exit 1
fi
}
# 启动应用(支持Java/NetCore)
function start_app(
) {
# 停止已有进程
stop_app
cd ${deployDir}
case "${languageType}"
in
"javac"
)
nohup java -cp conf:lib/*:${packageName}.jar ${mainClass} >nohup.out 2>
&1 &
;
;
"java"
)
nohup java -jar ${packageName}.jar >nohup.out 2>
&1 &
;
;
"netcore"
)
nohup ./${packageName} >nohup.out 2>
&1 &
;
;
esac
# 检查进程并跟踪日志
check_pid &&
tail -f nohup.out
}
# 停止应用
function stop_app(
) {
pid=$(pgrep -f "${packageName}")
[ -n "$pid" ] &&
kill -9 $pid
}
# 主逻辑
case "$1"
in
"start"
) start_app ;
;
"stop"
) stop_app ;
;
"restart"
) stop_app && start_app ;
;
"deploy"
) unzip_pkg && start_app ;
;
*) echo "用法: $0 {start|stop|restart|deploy}"
;
;
esac
四、Linux部署操作流程
  1. 上传ZIP包至服务器

  2. 执行部署命令

    # 1. 解决Windows格式问题
    vim shenniu_publish.sh
    :set ff=unix # 转换行尾符
    :wq
    # 2. 赋予执行权限
    chmod +x shenniu_publish.sh
    # 3. 执行一键部署
    ./shenniu_publish.sh deploy
  3. 管理应用状态

    ./shenniu_publish.sh stop # 停止
    ./shenniu_publish.sh restart # 重启
五、关键要点总结
  1. 环境隔离:通过Maven Profiles实现配置与代码分离
  2. 打包策略
    • 配置文件独立到/conf目录
    • 启动脚本自动赋权(fileMode>755
    • 动态替换脚本中的Maven变量(<filtered>true</filtered>
  3. Shell工具优势
    • 统一管理应用生命周期
    • 支持Java/NetCore多语言
    • 日志自动重定向(nohup.out
  4. 避坑指南
    • Windows编辑的Shell脚本需转换Unix行尾符(:set ff=unix
    • 确保Linux环境有JRE/JDK运行时
posted on 2025-10-08 14:51  lxjshuju  阅读(5)  评论(0)    收藏  举报