教程:go-platform:部署:部署
差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
教程:go-platform:部署:部署 [2025/07/04 07:34] – [部署] tom | 教程:go-platform:部署:部署 [2025/07/06 08:59] (当前版本) – [管理中心] tom | ||
---|---|---|---|
行 2: | 行 2: | ||
项目有多种部署方式 | 项目有多种部署方式 | ||
- | + | ===== 二进制文件部署===== | |
- | ===== 二进制文件===== | + | 可以将各个模块编译成二进制可执行文件。 |
- | 可以将各个模块编译成二进制可执行文件。对于统一配置中心,可以通过如下的powershell脚本进行打包: | + | ====配置中心==== |
- | + | 对于'' | |
- | <code powershell> | + | <hidden configure打包脚本> |
+ | <code powershell | ||
<# | <# | ||
.SYNOPSIS | .SYNOPSIS | ||
行 153: | 行 154: | ||
exit 0 | exit 0 | ||
</ | </ | ||
+ | </ | ||
+ | 在服务器上部署时,可以使用pm2脚本'' | ||
+ | <code js [enable_line_numbers=" | ||
+ | module.exports = { | ||
+ | apps: [{ | ||
+ | name: " | ||
+ | script: " | ||
+ | //args: " | ||
+ | // | ||
+ | autorestart: | ||
+ | watch: false, | ||
+ | max_memory_restart: | ||
+ | env: { | ||
+ | NODE_ENV: " | ||
+ | GO_ENV: " | ||
+ | }, | ||
+ | error_file: " | ||
+ | out_file: " | ||
+ | pid_file: " | ||
+ | }] | ||
+ | }; | ||
+ | </ | ||
+ | 使用命令行启动即可: | ||
+ | <code bash> | ||
+ | pm2 start ecosystem.config.js | ||
+ | </ | ||
+ | <WRAP center round notice 60%> | ||
+ | 配置中心的API HTTP端口(6081), | ||
+ | </ | ||
+ | ====网关==== | ||
+ | 对于'' | ||
+ | <hidden gateway打包脚本> | ||
+ | <code powershell> | ||
+ | <# | ||
+ | .SYNOPSIS | ||
+ | 构建 Go 项目并复制配置文件到目标目录 | ||
+ | |||
+ | .DESCRIPTION | ||
+ | 此脚本将: | ||
+ | 1. 编译 Go 项目为 Linux amd64 二进制文件 | ||
+ | 2. 复制配置文件到目标目录 | ||
+ | 3. 复制静态资源和 SQL 文件 | ||
+ | #> | ||
+ | |||
+ | # 配置参数 | ||
+ | $Config = @{ | ||
+ | GOOS = " | ||
+ | GOARCH = " | ||
+ | OutputDir = " | ||
+ | AppName = " | ||
+ | |||
+ | } | ||
+ | |||
+ | # 初始化日志函数 | ||
+ | function Write-Log { | ||
+ | param ( | ||
+ | [string]$Message, | ||
+ | [string]$Level = " | ||
+ | [string]$Color = " | ||
+ | ) | ||
+ | |||
+ | $timestamp = Get-Date -Format " | ||
+ | $formattedMessage = " | ||
+ | Write-Host $formattedMessage -ForegroundColor $Color | ||
+ | } | ||
+ | |||
+ | |||
+ | # 主脚本开始 | ||
+ | Write-Log " | ||
+ | |||
+ | # 1. 清理旧构建 | ||
+ | try { | ||
+ | if (Test-Path $Config.OutputDir) { | ||
+ | Remove-Item -Path $Config.OutputDir -Recurse -Force | ||
+ | Write-Log " | ||
+ | } | ||
+ | New-Item -ItemType Directory -Path $Config.OutputDir -Force | Out-Null | ||
+ | } | ||
+ | catch { | ||
+ | Write-Log " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | # 2. 设置环境变量并编译 | ||
+ | try { | ||
+ | $env:GOOS = $Config.GOOS | ||
+ | $env:GOARCH = $Config.GOARCH | ||
+ | $env: | ||
+ | |||
+ | $buildCommand = "go build -ldflags=' | ||
+ | Write-Log " | ||
+ | |||
+ | Invoke-Expression $buildCommand | ||
+ | Write-Log " | ||
+ | Write-Log " | ||
+ | } | ||
+ | catch { | ||
+ | Write-Log " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | |||
+ | Write-Log " | ||
+ | exit 0 | ||
+ | </ | ||
+ | </ | ||
+ | 在服务器上部署时,'' | ||
+ | <code javascript[enable_line_numbers=" | ||
+ | module.exports = { | ||
+ | apps: [{ | ||
+ | name: " | ||
+ | script: " | ||
+ | // | ||
+ | autorestart: | ||
+ | watch: false, | ||
+ | max_memory_restart: | ||
+ | env: { | ||
+ | APP_NAME: " | ||
+ | CONF_HOST: " | ||
+ | CONF_TOKEN: " | ||
+ | NODE_ENV: " | ||
+ | GO_ENV: " | ||
+ | }, | ||
+ | error_file: " | ||
+ | out_file: " | ||
+ | pid_file: " | ||
+ | }] | ||
+ | }; | ||
+ | </ | ||
+ | ==== 管理中心 ==== | ||
+ | |||
+ | <code powershell > | ||
+ | # 配置参数 | ||
+ | $Config = @{ | ||
+ | GOOS = " | ||
+ | GOARCH = " | ||
+ | OutputDir = " | ||
+ | AppName = " | ||
+ | SourceFiles = @( | ||
+ | @{ | ||
+ | Source = " | ||
+ | Destination = " | ||
+ | IsDirectory = $false | ||
+ | }, | ||
+ | @{ | ||
+ | Source = " | ||
+ | Destination = " | ||
+ | IsDirectory = $false | ||
+ | }, | ||
+ | @{ | ||
+ | Source = " | ||
+ | Destination = " | ||
+ | IsDirectory = $true | ||
+ | } | ||
+ | ) | ||
+ | } | ||
+ | |||
+ | # 初始化日志函数 | ||
+ | function Write-Log { | ||
+ | param ( | ||
+ | [string]$Message, | ||
+ | [string]$Level = " | ||
+ | [string]$Color = " | ||
+ | ) | ||
+ | |||
+ | $timestamp = Get-Date -Format " | ||
+ | $formattedMessage = " | ||
+ | Write-Host $formattedMessage -ForegroundColor $Color | ||
+ | } | ||
+ | |||
+ | # 复制文件或目录函数 | ||
+ | function Copy-FileOrDirectory { | ||
+ | param ( | ||
+ | [string]$Source, | ||
+ | [string]$Destination, | ||
+ | [bool]$IsDirectory | ||
+ | ) | ||
+ | |||
+ | try { | ||
+ | # 检查源路径是否存在 | ||
+ | if (-not (Test-Path $Source)) { | ||
+ | Write-Log " | ||
+ | return $false | ||
+ | } | ||
+ | |||
+ | # 创建目标目录 | ||
+ | $targetDir = if ($IsDirectory) { $Destination } else { [System.IO.Path]:: | ||
+ | |||
+ | if (-not (Test-Path $targetDir)) { | ||
+ | New-Item -ItemType Directory -Path $targetDir -Force | Out-Null | ||
+ | Write-Log " | ||
+ | } | ||
+ | |||
+ | # 执行复制 | ||
+ | if ($IsDirectory) { | ||
+ | Copy-Item -Path $Source -Destination $Destination -Recurse -Force | ||
+ | Write-Log " | ||
+ | } | ||
+ | else { | ||
+ | Copy-Item -Path $Source -Destination $Destination -Force | ||
+ | Write-Log " | ||
+ | } | ||
+ | |||
+ | return $true | ||
+ | } | ||
+ | catch { | ||
+ | Write-Log " | ||
+ | return $false | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # 主脚本开始 | ||
+ | Write-Log " | ||
+ | |||
+ | # 1. 清理旧构建 | ||
+ | try { | ||
+ | if (Test-Path $Config.OutputDir) { | ||
+ | Remove-Item -Path $Config.OutputDir -Recurse -Force | ||
+ | Write-Log " | ||
+ | } | ||
+ | New-Item -ItemType Directory -Path $Config.OutputDir -Force | Out-Null | ||
+ | } | ||
+ | catch { | ||
+ | Write-Log " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | # 2. 设置环境变量并编译 | ||
+ | try { | ||
+ | $env:GOOS = $Config.GOOS | ||
+ | $env:GOARCH = $Config.GOARCH | ||
+ | $env: | ||
+ | |||
+ | $buildCommand = "go build -ldflags=' | ||
+ | Write-Log " | ||
+ | |||
+ | Invoke-Expression $buildCommand | ||
+ | Write-Log " | ||
+ | Write-Log " | ||
+ | } | ||
+ | catch { | ||
+ | Write-Log " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | # 3. 复制所有配置文件 | ||
+ | $allSuccess = $true | ||
+ | foreach ($file in $Config.SourceFiles) { | ||
+ | $result = Copy-FileOrDirectory -Source $file.Source -Destination $file.Destination -IsDirectory $file.IsDirectory | ||
+ | if (-not $result) { | ||
+ | $allSuccess = $false | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (-not $allSuccess) { | ||
+ | Write-Log " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | Write-Log " | ||
+ | exit 0 | ||
+ | </ |
教程/go-platform/部署/部署.1751614457.txt.gz · 最后更改: 由 tom