一鍵搞定!為 Spring Boot 服務配置通用腳本,提升運維效率
在日常開發和運維工作中,我們常常會接觸到各種優秀的開源 Java 項目,如 Kafka、Flink、RocketMQ、StreamPark 等。這些項目在服務部署時,通常都會在根目錄下提供 bin 目錄用于存放啟動和關閉腳本,以及 conf 目錄用于管理配置文件。這種目錄結構不僅方便了運維管理,也極大提升了服務的可維護性和靈活性。
然而,當我們使用 Spring Boot 框架開發服務時,默認的項目結構并不會包含 bin 和 conf 目錄,而是將所有配置文件打包到 jar 內部。這種方式雖然簡化了項目的構建流程,但也帶來了不便:
- 運維管理不便每次修改配置都需要重新打包并部署,導致運維成本上升。
- 缺乏標準化無法像其他開源項目一樣,直接通過腳本進行服務的啟動、停止等管理操作。
- 不利于配置靈活性所有配置都在 jar 內部,手動修改較為困難,無法在不同環境間快速切換。
針對這些問題,我們可以通過 maven-assembly-plugin 插件為 Spring Boot 3.4 項目引入 bin和 conf 目錄,從而實現與優秀開源項目類似的部署管理方式,提高運維效率。本文將詳細介紹如何在 Spring Boot 項目中實現這一方案。
Spring Boot 目錄優化
在 Spring Boot 項目中,應用的配置文件通常會被打包到 JAR 內部,這導致修改配置文件時需要重新構建項目或依賴配置中心。然而,在某些情況下,我們更希望像傳統 Java Web 應用一樣,直接修改外部的 conf 配置文件,從而減少運維成本。
為此,我們可以采取以下措施:
- 在項目根目錄下創建 distribution 子模塊。
- 配置 maven-assembly-plugin 插件,實現服務的完整打包。
- 在 distribution 目錄下創建 bin 目錄,存放啟動和停止腳本。
- 在 distribution 目錄下創建 conf 目錄,存放外部配置文件。
Maven 配置
創建 distribution 子模塊
首先,在 pom.xml 文件中添加 distribution 子模塊,并引入 maven-assembly-plugin 插件。
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.icoderoad</groupId>
<artifactId>springboot-service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-distribution</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<tarLongFileMode>posix</tarLongFileMode>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
assembly.xml 配置
在 distribution 目錄下創建 assembly.xml,定義 bin 和 conf 目錄的打包規則。
<assembly>
<id>${project.version}</id>
<includeBaseDirectory>true</includeBaseDirectory>
<formats>
<format>dir</format>
<format>tar.gz</format>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<includes>
<include>conf/**</include>
</includes>
</fileSet>
<fileSet>
<includes>
<include>bin/*</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
</assembly>
bin 目錄與腳本
在 distribution 目錄下創建 bin 目錄,并添加以下四個腳本文件:
- shutdown.cmd
- shutdown.sh
- startup.cmd
- startup.sh
shutdown.sh(Linux/macOS)
#!/bin/bash
cd`dirname $0`/../target
target_dir=`pwd`
pid=`ps ax | grep -i 'springboot-service' | grep ${target_dir} | grep java | grep -v grep | awk '{print $1}'`
if[-z"$pid"];then
echo"No running springboot-service found."
exit -1;
fi
echo"Stopping springboot-service (PID: ${pid})..."
kill${pid}
echo"Shutdown completed."
startup.sh(Linux/macOS)
#!/bin/bash
cd `dirname $0`/../target
java -jar springboot-service.jar > /dev/null 2>&1 &
echo "Spring Boot service started!"
shutdown.cmd(Windows)
@echo off
setlocal
echo Stopping springboot-service...
taskkill /F /IM java.exe
startup.cmd(Windows)
@echo off
setlocal
echo Starting springboot-service...
start java -jar springboot-service.jar
結論
通過本文的方法,我們可以輕松為 Spring Boot 3.4 項目添加 bin 目錄和 conf 目錄,實現標準化的運維管理。主要優勢包括:
- 提升運維效率通過腳本一鍵啟動、停止服務,無需手動輸入繁瑣的命令。
- 增強配置靈活性支持外部 conf 目錄存放配置文件,避免每次修改配置都要重新打包。
- 提高可維護性項目結構更清晰,符合主流開源項目的標準,方便團隊協作與維護。
在實際開發中,我們可以根據具體業務需求,進一步擴展 bin 目錄下的腳本,例如添加日志管理、自動重啟等功能,使 Spring Boot 項目更加高效、易用。如果你希望你的 Spring Boot 項目像 Kafka、RocketMQ 一樣具備高效的運維能力,不妨嘗試本文介紹的方案!