教程:go-platform:部署:部署
差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
教程:go-platform:部署:部署 [2025/07/04 09:31] – [网关] tom | 教程:go-platform:部署:部署 [2025/07/06 08:59] (当前版本) – [管理中心] tom | ||
---|---|---|---|
行 180: | 行 180: | ||
pm2 start ecosystem.config.js | pm2 start ecosystem.config.js | ||
</ | </ | ||
+ | <WRAP center round notice 60%> | ||
+ | 配置中心的API HTTP端口(6081), | ||
+ | </ | ||
+ | |||
====网关==== | ====网关==== | ||
对于'' | 对于'' | ||
+ | <hidden gateway打包脚本> | ||
<code powershell> | <code powershell> | ||
<# | <# | ||
行 251: | 行 256: | ||
} | } | ||
+ | |||
+ | 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 " | Write-Log " | ||
exit 0 | exit 0 | ||
</ | </ |
教程/go-platform/部署/部署.1751621479.txt.gz · 最后更改: 由 tom