610 lines
16 KiB
Markdown
610 lines
16 KiB
Markdown
# 资金服务平台 Assembly 使用示例
|
||
|
||
## 基础配置示例
|
||
|
||
### 1. 服务模块标准配置
|
||
|
||
**目录结构(包含部署目录管理):**
|
||
```
|
||
fundplatform/
|
||
├── deploy/ # 部署产物目录(核心实践)
|
||
│ ├── fund-gateway.tar.gz # 网关服务部署包
|
||
│ ├── fund-sys.tar.gz # 系统服务部署包
|
||
│ └── ... 其他服务部署包
|
||
├── scripts/ # 统一脚本和配置
|
||
│ ├── env.properties # 统一配置文件
|
||
│ ├── services-build.sh # 批量构建脚本
|
||
│ └── start.sh # 通用启动脚本
|
||
├── fund-sys/ # 服务源代码
|
||
│ ├── src/
|
||
│ │ ├── main/
|
||
│ │ │ ├── assembly/
|
||
│ │ │ │ └── assembly.xml
|
||
│ │ │ └── resources/
|
||
│ │ │ ├── service.properties
|
||
│ │ │ ├── application.yml
|
||
│ │ │ └── logback-spring.xml
|
||
│ │ └── test/
|
||
│ ├── pom.xml
|
||
│ └── target/
|
||
│ └── fund-sys.tar.gz # 临时构建产物
|
||
└── ... 其他服务模块
|
||
```
|
||
|
||
**核心实践要点**:
|
||
- 所有部署包统一存储在项目根目录的 `deploy/` 目录下
|
||
- 开发代码与部署产物物理分离,便于版本管理和CI/CD集成
|
||
- 通过 `scripts/services-build.sh` 脚本自动收集各服务构建产物
|
||
|
||
**pom.xml配置:**
|
||
```xml
|
||
<?xml version="1.0" encoding="UTF-8"?>
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0">
|
||
<modelVersion>4.0.0</modelVersion>
|
||
|
||
<parent>
|
||
<groupId>com.fundplatform</groupId>
|
||
<artifactId>fund-platform</artifactId>
|
||
<version>1.0.0</version>
|
||
</parent>
|
||
|
||
<artifactId>fund-sys</artifactId>
|
||
<packaging>jar</packaging>
|
||
|
||
<properties>
|
||
<!-- 从service.properties读取主类 -->
|
||
<main.class>com.fundplatform.sys.SysApplication</main.class>
|
||
</properties>
|
||
|
||
<build>
|
||
<plugins>
|
||
<plugin>
|
||
<groupId>org.springframework.boot</groupId>
|
||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||
<configuration>
|
||
<mainClass>${main.class}</mainClass>
|
||
</configuration>
|
||
</plugin>
|
||
|
||
<plugin>
|
||
<groupId>org.apache.maven.plugins</groupId>
|
||
<artifactId>maven-assembly-plugin</artifactId>
|
||
<configuration>
|
||
<descriptors>
|
||
<descriptor>src/main/assembly/assembly.xml</descriptor>
|
||
</descriptors>
|
||
<finalName>${project.artifactId}</finalName>
|
||
</configuration>
|
||
</plugin>
|
||
</plugins>
|
||
</build>
|
||
</project>
|
||
```
|
||
|
||
**service.properties配置:**
|
||
```properties
|
||
# ============================================
|
||
# fund-sys 服务配置
|
||
# ============================================
|
||
|
||
APP_NAME=fund-sys
|
||
MAIN_CLASS=com.fundplatform.sys.SysApplication
|
||
INSTANCE_NAME=fund-sys
|
||
TENANT_ID=
|
||
|
||
# 可选配置
|
||
SERVER_PORT=8100
|
||
```
|
||
|
||
### 2. Assembly Descriptor配置
|
||
|
||
**src/main/assembly/assembly.xml:**
|
||
```xml
|
||
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0">
|
||
<id>dist</id>
|
||
<formats>
|
||
<format>tar.gz</format>
|
||
</formats>
|
||
<includeBaseDirectory>true</includeBaseDirectory>
|
||
|
||
<fileSets>
|
||
<!-- 使用项目根目录的统一脚本 -->
|
||
<fileSet>
|
||
<directory>${project.basedir}/../../scripts</directory>
|
||
<outputDirectory>bin</outputDirectory>
|
||
<includes>
|
||
<include>start.sh</include>
|
||
<include>stop.sh</include>
|
||
<include>restart.sh</include>
|
||
<include>status.sh</include>
|
||
</includes>
|
||
<fileMode>0755</fileMode>
|
||
</fileSet>
|
||
|
||
<!-- 统一配置文件 -->
|
||
<fileSet>
|
||
<directory>${project.basedir}/../../scripts</directory>
|
||
<outputDirectory>conf</outputDirectory>
|
||
<includes>
|
||
<include>env.properties</include>
|
||
</includes>
|
||
</fileSet>
|
||
|
||
<!-- 服务个性化配置 -->
|
||
<fileSet>
|
||
<directory>src/main/resources</directory>
|
||
<outputDirectory>conf</outputDirectory>
|
||
<includes>
|
||
<include>service.properties</include>
|
||
<include>application.yml</include>
|
||
<include>logback-spring.xml</include>
|
||
</includes>
|
||
</fileSet>
|
||
</fileSets>
|
||
|
||
<dependencySets>
|
||
<dependencySet>
|
||
<outputDirectory>lib</outputDirectory>
|
||
<useProjectArtifact>true</useProjectArtifact>
|
||
<scope>runtime</scope>
|
||
</dependencySet>
|
||
</dependencySets>
|
||
</assembly>
|
||
```
|
||
|
||
## 实际应用场景
|
||
|
||
### 场景1: 单服务构建与部署
|
||
|
||
**构建命令:**
|
||
```bash
|
||
# 进入服务目录
|
||
cd fund-sys
|
||
|
||
# 清理并构建
|
||
mvn clean package -DskipTests
|
||
|
||
# 验证构建结果
|
||
ls -lh target/fund-sys.tar.gz
|
||
|
||
# 查看包内容
|
||
tar -tzf target/fund-sys.tar.gz | head -10
|
||
```
|
||
|
||
**部署验证:**
|
||
```bash
|
||
# 解压部署包
|
||
mkdir -p /opt/deploy/fund-sys
|
||
tar -xzf target/fund-sys.tar.gz -C /opt/deploy/
|
||
|
||
# 验证配置文件
|
||
cat /opt/deploy/fund-sys/conf/service.properties
|
||
cat /opt/deploy/fund-sys/conf/env.properties
|
||
|
||
# 启动服务
|
||
cd /opt/deploy/fund-sys
|
||
./bin/start.sh
|
||
```
|
||
|
||
### 场景2: 批量构建所有服务(部署目录管理实践)
|
||
|
||
**创建批量构建脚本 (scripts/services-build.sh):**
|
||
```bash
|
||
#!/bin/bash
|
||
# 资金服务平台批量构建脚本 - 统一输出到deploy目录
|
||
|
||
set -e
|
||
|
||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
PROJECT_ROOT="${SCRIPT_DIR}"
|
||
DEPLOY_DIR="${PROJECT_ROOT}/deploy"
|
||
|
||
# 服务列表
|
||
SERVICES=(
|
||
"fund-gateway"
|
||
"fund-sys"
|
||
"fund-cust"
|
||
"fund-proj"
|
||
"fund-exp"
|
||
"fund-receipt"
|
||
"fund-report"
|
||
"fund-req"
|
||
"fund-file"
|
||
)
|
||
|
||
# 部署目录初始化
|
||
initialize_deploy_directory() {
|
||
echo "=== 部署目录初始化 ==="
|
||
|
||
# 创建部署目录
|
||
mkdir -p "${DEPLOY_DIR}"
|
||
echo "✓ 部署目录: ${DEPLOY_DIR}"
|
||
|
||
# 显示当前部署包
|
||
echo "当前部署包:"
|
||
if ls "${DEPLOY_DIR}"/*.tar.gz >/dev/null 2>&1; then
|
||
ls -lh "${DEPLOY_DIR}"/*.tar.gz
|
||
else
|
||
echo " (无)"
|
||
fi
|
||
|
||
# 询问是否清理旧包
|
||
echo ""
|
||
read -p "是否清理旧的部署包? (y/N): " -n 1 -r
|
||
echo
|
||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||
rm -f "${DEPLOY_DIR}"/*.tar.gz
|
||
echo "✓ 已清理旧部署包"
|
||
fi
|
||
echo ""
|
||
}
|
||
|
||
build_service() {
|
||
local service=$1
|
||
local service_dir="${PROJECT_ROOT}/${service}"
|
||
|
||
echo "=== 构建服务: ${service} ==="
|
||
|
||
if [ ! -d "${service_dir}" ]; then
|
||
echo "服务目录不存在: ${service_dir}"
|
||
return 1
|
||
fi
|
||
|
||
cd "${service_dir}"
|
||
|
||
# Maven构建
|
||
mvn clean package -DskipTests -q
|
||
|
||
# 复制到统一部署目录
|
||
local tar_file=$(find target -name "${service}*.tar.gz" | head -1)
|
||
if [ -n "${tar_file}" ]; then
|
||
cp "${tar_file}" "${DEPLOY_DIR}/"
|
||
local file_size=$(du -h "${tar_file}" | cut -f1)
|
||
echo "✓ ${service} 构建完成 (${file_size})"
|
||
echo " 输出到: ${DEPLOY_DIR}/$(basename ${tar_file})"
|
||
else
|
||
echo "✗ ${service} 构建失败"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
# 主构建流程
|
||
main() {
|
||
echo "========================================="
|
||
echo " 资金服务平台批量构建"
|
||
echo " 部署目录: ${DEPLOY_DIR}"
|
||
echo "========================================="
|
||
|
||
# 初始化部署目录
|
||
initialize_deploy_directory
|
||
|
||
local success_count=0
|
||
local fail_count=0
|
||
|
||
# 批量构建
|
||
for service in "${SERVICES[@]}"; do
|
||
if build_service "${service}"; then
|
||
((success_count++))
|
||
else
|
||
((fail_count++))
|
||
fi
|
||
echo ""
|
||
done
|
||
|
||
echo "========================================="
|
||
echo "构建完成统计:"
|
||
echo " 成功: ${success_count}"
|
||
echo " 失败: ${fail_count}"
|
||
echo ""
|
||
|
||
if [ ${fail_count} -eq 0 ]; then
|
||
echo "✓ 所有服务构建成功!"
|
||
echo ""
|
||
echo "生成的部署包:"
|
||
ls -lh "${DEPLOY_DIR}"/*.tar.gz
|
||
echo ""
|
||
echo "部署目录总大小: $(du -sh "${DEPLOY_DIR}" | cut -f1)"
|
||
else
|
||
echo "✗ ${fail_count}个服务构建失败"
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
main "$@"
|
||
```
|
||
|
||
**使用方法:**
|
||
```bash
|
||
# 给脚本执行权限
|
||
chmod +x scripts/services-build.sh
|
||
|
||
# 执行批量构建
|
||
./scripts/services-build.sh
|
||
|
||
# 构建完成后查看部署目录
|
||
ls -lh deploy/
|
||
|
||
# 验证部署包完整性
|
||
scripts/validate-deployment.sh
|
||
```
|
||
|
||
**部署目录管理优势**:
|
||
1. **统一管理**:所有服务部署包集中存储在 `deploy/` 目录
|
||
2. **版本控制**:便于CI/CD流水线统一处理部署包
|
||
3. **权限分离**:开发代码与部署产物物理分离
|
||
4. **清理维护**:易于批量清理旧版本部署包
|
||
|
||
**使用方法:**
|
||
```bash
|
||
# 给脚本执行权限
|
||
chmod +x scripts/services-build.sh
|
||
|
||
# 执行批量构建
|
||
./scripts/services-build.sh
|
||
```
|
||
|
||
### 场景3: 多租户实例部署
|
||
|
||
**共享实例配置:**
|
||
```properties
|
||
# fund-sys/src/main/resources/service.properties
|
||
APP_NAME=fund-sys
|
||
MAIN_CLASS=com.fundplatform.sys.SysApplication
|
||
INSTANCE_NAME=fund-sys-shared
|
||
TENANT_ID=
|
||
```
|
||
|
||
**VIP专属实例配置:**
|
||
```properties
|
||
# fund-sys-vip001/src/main/resources/service.properties
|
||
APP_NAME=fund-sys
|
||
MAIN_CLASS=com.fundplatform.sys.SysApplication
|
||
INSTANCE_NAME=fund-sys-vip001
|
||
TENANT_ID=vip001
|
||
```
|
||
|
||
**部署脚本支持多实例:**
|
||
```bash
|
||
#!/bin/bash
|
||
# multi-instance-deploy.sh
|
||
|
||
INSTANCE_TYPE=${1:-"shared"} # shared 或 vip001
|
||
SERVICE_NAME="fund-sys"
|
||
|
||
case "${INSTANCE_TYPE}" in
|
||
shared)
|
||
CONFIG_SUFFIX=""
|
||
DEPLOY_PATH="/opt/apps/${SERVICE_NAME}-shared"
|
||
;;
|
||
vip*)
|
||
CONFIG_SUFFIX="-${INSTANCE_TYPE}"
|
||
DEPLOY_PATH="/opt/apps/${SERVICE_NAME}-${INSTANCE_TYPE}"
|
||
;;
|
||
*)
|
||
echo "未知实例类型: ${INSTANCE_TYPE}"
|
||
exit 1
|
||
;;
|
||
esac
|
||
|
||
# 构建特定实例
|
||
cd "${SERVICE_NAME}${CONFIG_SUFFIX}"
|
||
mvn clean package -DskipTests
|
||
|
||
# 部署
|
||
mkdir -p "${DEPLOY_PATH}"
|
||
tar -xzf target/${SERVICE_NAME}*.tar.gz -C "${DEPLOY_PATH}"
|
||
|
||
# 启动
|
||
${DEPLOY_PATH}/bin/start.sh
|
||
```
|
||
|
||
## 高级配置示例
|
||
|
||
### 场景4: 环境差异化配置
|
||
|
||
**开发环境配置 (dev-env.properties):**
|
||
```properties
|
||
# scripts/dev-env.properties
|
||
NACOS_SERVER_ADDR=dev-nacos:8848
|
||
REDIS_HOST=dev-redis
|
||
REDIS_PASSWORD=dev-password
|
||
LOG_LEVEL_ROOT=DEBUG
|
||
```
|
||
|
||
**生产环境配置 (prod-env.properties):**
|
||
```properties
|
||
# scripts/prod-env.properties
|
||
NACOS_SERVER_ADDR=prod-nacos:8848
|
||
REDIS_HOST=prod-redis
|
||
REDIS_PASSWORD=prod-password
|
||
LOG_LEVEL_ROOT=INFO
|
||
```
|
||
|
||
**Profile配置:**
|
||
```xml
|
||
<profiles>
|
||
<profile>
|
||
<id>dev</id>
|
||
<activation>
|
||
<activeByDefault>true</activeByDefault>
|
||
</activation>
|
||
<build>
|
||
<plugins>
|
||
<plugin>
|
||
<groupId>org.apache.maven.plugins</groupId>
|
||
<artifactId>maven-assembly-plugin</artifactId>
|
||
<configuration>
|
||
<descriptors>
|
||
<descriptor>src/main/assembly/dev-assembly.xml</descriptor>
|
||
</descriptors>
|
||
</configuration>
|
||
</plugin>
|
||
</plugins>
|
||
</build>
|
||
</profile>
|
||
|
||
<profile>
|
||
<id>prod</id>
|
||
<build>
|
||
<plugins>
|
||
<plugin>
|
||
<groupId>org.apache.maven.plugins</groupId>
|
||
<artifactId>maven-assembly-plugin</artifactId>
|
||
<configuration>
|
||
<descriptors>
|
||
<descriptor>src/main/assembly/prod-assembly.xml</descriptor>
|
||
</descriptors>
|
||
</configuration>
|
||
</plugin>
|
||
</plugins>
|
||
</build>
|
||
</profile>
|
||
</profiles>
|
||
```
|
||
|
||
### 场景5: 条件配置打包
|
||
|
||
**assembly.xml支持条件包含:**
|
||
```xml
|
||
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0">
|
||
<id>conditional-dist</id>
|
||
<formats>
|
||
<format>tar.gz</format>
|
||
</formats>
|
||
|
||
<fileSets>
|
||
<!-- 基础文件 -->
|
||
<fileSet>
|
||
<directory>${project.basedir}/../../scripts</directory>
|
||
<outputDirectory>bin</outputDirectory>
|
||
<includes>
|
||
<include>start.sh</include>
|
||
<include>stop.sh</include>
|
||
</includes>
|
||
<fileMode>0755</fileMode>
|
||
</fileSet>
|
||
|
||
<!-- 根据环境包含不同配置 -->
|
||
<fileSet>
|
||
<directory>${project.basedir}/../../scripts/${env.name}</directory>
|
||
<outputDirectory>conf</outputDirectory>
|
||
<includes>
|
||
<include>env.properties</include>
|
||
</includes>
|
||
</fileSet>
|
||
|
||
<!-- 可选安全组件 -->
|
||
<fileSet>
|
||
<directory>src/main/security</directory>
|
||
<outputDirectory>security</outputDirectory>
|
||
<includes>
|
||
<include>*</include>
|
||
</includes>
|
||
</fileSet>
|
||
</fileSets>
|
||
</assembly>
|
||
```
|
||
|
||
## 构建验证示例
|
||
|
||
### 自动化验证脚本
|
||
|
||
**创建验证脚本 (scripts/validate-build.sh):**
|
||
```bash
|
||
#!/bin/bash
|
||
# 构建结果验证脚本
|
||
|
||
validate_service() {
|
||
local service=$1
|
||
local tar_file="target/${service}.tar.gz"
|
||
|
||
echo "验证 ${service}..."
|
||
|
||
# 检查文件存在
|
||
if [ ! -f "${tar_file}" ]; then
|
||
echo "✗ 未找到部署包: ${tar_file}"
|
||
return 1
|
||
fi
|
||
|
||
# 检查包结构
|
||
local required_files=(
|
||
"bin/start.sh"
|
||
"bin/stop.sh"
|
||
"conf/env.properties"
|
||
"conf/service.properties"
|
||
"lib/${service}*.jar"
|
||
)
|
||
|
||
for file in "${required_files[@]}"; do
|
||
if ! tar -tzf "${tar_file}" | grep -q "${file}"; then
|
||
echo "✗ 缺失文件: ${file}"
|
||
return 1
|
||
fi
|
||
done
|
||
|
||
echo "✓ ${service} 验证通过"
|
||
return 0
|
||
}
|
||
|
||
# 验证所有服务
|
||
SERVICES=("fund-gateway" "fund-sys" "fund-cust" "fund-proj"
|
||
"fund-exp" "fund-receipt" "fund-report" "fund-req" "fund-file")
|
||
|
||
for service in "${SERVICES[@]}"; do
|
||
if [ -d "${service}" ]; then
|
||
cd "${service}"
|
||
validate_service "${service}" || exit 1
|
||
cd ..
|
||
fi
|
||
done
|
||
|
||
echo "所有服务验证完成!"
|
||
```
|
||
|
||
### 部署前检查清单
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
# pre-deployment-check.sh
|
||
|
||
echo "=== 部署前检查清单 ==="
|
||
|
||
# 1. 检查配置文件完整性
|
||
echo "1. 配置文件检查:"
|
||
for service in fund-*; do
|
||
if [ -d "${service}" ]; then
|
||
conf_file="${service}/src/main/resources/service.properties"
|
||
if [ -f "${conf_file}" ]; then
|
||
echo "✓ ${service}: service.properties 存在"
|
||
# 检查必需参数
|
||
if grep -q "APP_NAME=" "${conf_file}" && grep -q "MAIN_CLASS=" "${conf_file}"; then
|
||
echo "✓ ${service}: 必需参数完整"
|
||
else
|
||
echo "✗ ${service}: 缺少必需参数"
|
||
fi
|
||
else
|
||
echo "✗ ${service}: 缺少 service.properties"
|
||
fi
|
||
fi
|
||
done
|
||
|
||
# 2. 检查统一配置
|
||
echo "2. 统一配置检查:"
|
||
if [ -f "scripts/env.properties" ]; then
|
||
echo "✓ scripts/env.properties 存在"
|
||
else
|
||
echo "✗ 缺少统一配置文件"
|
||
fi
|
||
|
||
# 3. 检查构建脚本
|
||
echo "3. 构建脚本检查:"
|
||
if [ -f "scripts/services-build.sh" ]; then
|
||
echo "✓ 批量构建脚本存在"
|
||
else
|
||
echo "✗ 缺少批量构建脚本"
|
||
fi
|
||
|
||
echo "检查完成!"
|
||
```
|
||
|
||
这些示例展示了资金服务平台Assembly配置的各种实际应用场景,涵盖了从基础配置到高级特性的完整使用方式。 |