目录

部署

项目有多种部署方式

二进制文件部署

可以将各个模块编译成二进制可执行文件。

配置中心

对于统一配置中心,可以通过如下的powershell脚本进行打包:

configure打包脚本

configure打包脚本

  1. <#
  2. .SYNOPSIS
  3. 构建 Go 项目并复制配置文件到目标目录
  4.  
  5. .DESCRIPTION
  6. 此脚本将:
  7. 1. 编译 Go 项目为 Linux amd64 二进制文件
  8. 2. 复制配置文件到目标目录
  9. 3. 复制静态资源和 SQL 文件
  10. #>
  11.  
  12. # 配置参数
  13. $Config = @{
  14. GOOS = "linux"
  15. GOARCH = "amd64"
  16. OutputDir = ".\bin"
  17. AppName = "configure"
  18. SourceFiles = @(
  19. @{
  20. Source = ".\internal\conf\conf-prod.yaml"
  21. Destination = ".\bin\internal\conf\conf.yaml"
  22. IsDirectory = $false
  23. },
  24. @{
  25. Source = ".\deploy\data-pg.sql"
  26. Destination = ".\bin\deploy\data-pg.sql"
  27. IsDirectory = $false
  28. },
  29. @{
  30. Source = ".\static"
  31. Destination = ".\bin\static"
  32. IsDirectory = $true
  33. },
  34. @{
  35. Source = ".\web"
  36. Destination = ".\bin\web"
  37. IsDirectory = $true
  38. }
  39. )
  40. }
  41.  
  42. # 初始化日志函数
  43. function Write-Log {
  44. param (
  45. [string]$Message,
  46. [string]$Level = "INFO",
  47. [string]$Color = "White"
  48. )
  49.  
  50. $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
  51. $formattedMessage = "[$timestamp] [$Level] $Message"
  52. Write-Host $formattedMessage -ForegroundColor $Color
  53. }
  54.  
  55. # 复制文件或目录函数
  56. function Copy-FileOrDirectory {
  57. param (
  58. [string]$Source,
  59. [string]$Destination,
  60. [bool]$IsDirectory
  61. )
  62.  
  63. try {
  64. # 检查源路径是否存在
  65. if (-not (Test-Path $Source)) {
  66. Write-Log "源路径不存在: $Source" -Level "ERROR" -Color "Red"
  67. return $false
  68. }
  69.  
  70. # 创建目标目录
  71. $targetDir = if ($IsDirectory) { $Destination } else { [System.IO.Path]::GetDirectoryName($Destination) }
  72.  
  73. if (-not (Test-Path $targetDir)) {
  74. New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
  75. Write-Log "创建目录: $targetDir" -Level "DEBUG" -Color "Gray"
  76. }
  77.  
  78. # 执行复制
  79. if ($IsDirectory) {
  80. Copy-Item -Path $Source -Destination $Destination -Recurse -Force
  81. Write-Log "复制目录: $Source → $Destination" -Level "INFO" -Color "Green"
  82. }
  83. else {
  84. Copy-Item -Path $Source -Destination $Destination -Force
  85. Write-Log "复制文件: $Source → $Destination" -Level "INFO" -Color "Green"
  86. }
  87.  
  88. return $true
  89. }
  90. catch {
  91. Write-Log "复制失败: $_" -Level "ERROR" -Color "Red"
  92. return $false
  93. }
  94. }
  95.  
  96. # 主脚本开始
  97. Write-Log "开始构建过程..." -Level "INFO" -Color "Cyan"
  98.  
  99. # 1. 清理旧构建
  100. try {
  101. if (Test-Path $Config.OutputDir) {
  102. Remove-Item -Path $Config.OutputDir -Recurse -Force
  103. Write-Log "已清理旧构建目录: $($Config.OutputDir)" -Level "INFO" -Color "Yellow"
  104. }
  105. New-Item -ItemType Directory -Path $Config.OutputDir -Force | Out-Null
  106. }
  107. catch {
  108. Write-Log "清理目录失败: $_" -Level "ERROR" -Color "Red"
  109. exit 1
  110. }
  111.  
  112. # 2. 设置环境变量并编译
  113. try {
  114. $env:GOOS = $Config.GOOS
  115. $env:GOARCH = $Config.GOARCH
  116. $env:CGO_ENABLED = "0"
  117.  
  118. $buildCommand = "go build -ldflags='-s -w' -o `"$($Config.OutputDir)\$($Config.AppName)`" ./cmd/configure/main.go"
  119. Write-Log "执行编译命令: $buildCommand" -Level "DEBUG" -Color "Gray"
  120.  
  121. Invoke-Expression $buildCommand
  122. Write-Log "编译成功完成" -Level "INFO" -Color "Green"
  123. Write-Log "输出路径: $(Resolve-Path $Config.OutputDir)\$($Config.AppName)" -Level "INFO" -Color "Cyan"
  124. }
  125. catch {
  126. Write-Log "编译失败: $_" -Level "ERROR" -Color "Red"
  127. exit 1
  128. }
  129.  
  130. # 3. 复制所有配置文件
  131. $allSuccess = $true
  132. foreach ($file in $Config.SourceFiles) {
  133. $result = Copy-FileOrDirectory -Source $file.Source -Destination $file.Destination -IsDirectory $file.IsDirectory
  134. if (-not $result) {
  135. $allSuccess = $false
  136. }
  137. }
  138.  
  139. if (-not $allSuccess) {
  140. Write-Log "部分文件复制失败,请检查错误日志" -Level "WARNING" -Color "Yellow"
  141. exit 1
  142. }
  143.  
  144. Write-Log "所有构建步骤完成!" -Level "INFO" -Color "Cyan"
  145. exit 0

在服务器上部署时,可以使用pm2脚本ecosystem.config.js进行设置:

  1. module.exports = {
  2. apps: [{
  3. name: "configure", // 应用名称
  4. script: "./configure", // 二进制文件路径
  5. //args: "--port 8080", // 命令行参数(可选)
  6. //instances: 2, // 启动2个实例(负载均衡)
  7. autorestart: true, // 崩溃后自动重启
  8. watch: false, // 禁用文件监视
  9. max_memory_restart: "500M", // 内存超限后重启
  10. env: {
  11. NODE_ENV: "production",
  12. GO_ENV: "prod"
  13. },
  14. error_file: "./logs/err.log", // 错误日志
  15. out_file: "./logs/out.log", // 输出日志
  16. pid_file: "./logs/pm2.pid" // PID 文件
  17. }]
  18. };

使用命令行启动即可:

pm2 start ecosystem.config.js

配置中心的API HTTP端口(6081),gRPC端口(6082),Web页面端口(6080)都需要防火墙放行

网关

对于gateway,可以通过如下的Power Shell脚本进行打包:

gateway打包脚本

gateway打包脚本

<#
.SYNOPSIS
构建 Go 项目并复制配置文件到目标目录
 
.DESCRIPTION
此脚本将:
1. 编译 Go 项目为 Linux amd64 二进制文件
2. 复制配置文件到目标目录
3. 复制静态资源和 SQL 文件
#>
 
# 配置参数
$Config = @{
    GOOS = "linux"
    GOARCH = "amd64"
    OutputDir = ".\bin"
    AppName = "gateway"
 
}
 
# 初始化日志函数
function Write-Log {
    param (
        [string]$Message,
        [string]$Level = "INFO",
        [string]$Color = "White"
    )
 
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $formattedMessage = "[$timestamp] [$Level] $Message"
    Write-Host $formattedMessage -ForegroundColor $Color
}
 
 
# 主脚本开始
Write-Log "开始构建过程..." -Level "INFO" -Color "Cyan"
 
# 1. 清理旧构建
try {
    if (Test-Path $Config.OutputDir) {
        Remove-Item -Path $Config.OutputDir -Recurse -Force
        Write-Log "已清理旧构建目录: $($Config.OutputDir)" -Level "INFO" -Color "Yellow"
    }
    New-Item -ItemType Directory -Path $Config.OutputDir -Force | Out-Null
}
catch {
    Write-Log "清理目录失败: $_" -Level "ERROR" -Color "Red"
    exit 1
}
 
# 2. 设置环境变量并编译
try {
    $env:GOOS = $Config.GOOS
    $env:GOARCH = $Config.GOARCH
    $env:CGO_ENABLED = "0"
 
    $buildCommand = "go build -ldflags='-s -w' -o `"$($Config.OutputDir)\$($Config.AppName)`" ./cmd/gateway/main.go"
    Write-Log "执行编译命令: $buildCommand" -Level "DEBUG" -Color "Gray"
 
    Invoke-Expression $buildCommand
    Write-Log "编译成功完成" -Level "INFO" -Color "Green"
    Write-Log "输出路径: $(Resolve-Path $Config.OutputDir)\$($Config.AppName)" -Level "INFO" -Color "Cyan"
}
catch {
    Write-Log "编译失败: $_" -Level "ERROR" -Color "Red"
    exit 1
}
 
 
Write-Log "所有构建步骤完成!" -Level "INFO" -Color "Cyan"
exit 0

在服务器上部署时,pm2脚本ecosystem.config.js和配置中心不同之处在于需要传递一些参数:

  1. module.exports = {
  2. apps: [{
  3. name: "gatewaye", // 应用名称
  4. script: "./gateway", // 二进制文件路径
  5. //instances: 2, // 启动2个实例(负载均衡)
  6. autorestart: true, // 崩溃后自动重启
  7. watch: false, // 禁用文件监视
  8. max_memory_restart: "500M", // 内存超限后重启
  9. env: {
  10. APP_NAME: "Gateway",
  11. CONF_HOST: "127.0.0.1:6082",
  12. CONF_TOKEN: "1025D32F6CA7A2A320FE091B22C5DF3C",
  13. NODE_ENV: "production",
  14. GO_ENV: "prod"
  15. },
  16. error_file: "./logs/err.log", // 错误日志
  17. out_file: "./logs/out.log", // 输出日志
  18. pid_file: "./logs/pm2.pid" // PID 文件
  19. }]
  20. };

管理中心

# 配置参数
$Config = @{
    GOOS = "linux"
    GOARCH = "amd64"
    OutputDir = ".\bin"
    AppName = "manager"
    SourceFiles = @(
    @{
        Source = ".\internal\conf\conf.yaml"
        Destination = ".\bin\internal\conf\conf.yaml"
        IsDirectory = $false
    },
    @{
        Source = ".\deploy\data.sql"
        Destination = ".\bin\deploy\data.sql"
        IsDirectory = $false
    },
    @{
        Source = ".\static"
        Destination = ".\bin"
        IsDirectory = $true
    }
    )
}
 
# 初始化日志函数
function Write-Log {
    param (
        [string]$Message,
        [string]$Level = "INFO",
        [string]$Color = "White"
    )
 
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $formattedMessage = "[$timestamp] [$Level] $Message"
    Write-Host $formattedMessage -ForegroundColor $Color
}
 
# 复制文件或目录函数
function Copy-FileOrDirectory {
    param (
        [string]$Source,
        [string]$Destination,
        [bool]$IsDirectory
    )
 
    try {
        # 检查源路径是否存在
        if (-not (Test-Path $Source)) {
            Write-Log "源路径不存在: $Source" -Level "ERROR" -Color "Red"
            return $false
        }
 
        # 创建目标目录
        $targetDir = if ($IsDirectory) { $Destination } else { [System.IO.Path]::GetDirectoryName($Destination) }
 
        if (-not (Test-Path $targetDir)) {
            New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
            Write-Log "创建目录: $targetDir" -Level "DEBUG" -Color "Gray"
        }
 
        # 执行复制
        if ($IsDirectory) {
            Copy-Item -Path $Source -Destination $Destination -Recurse -Force
            Write-Log "复制目录: $Source → $Destination" -Level "INFO" -Color "Green"
        }
        else {
            Copy-Item -Path $Source -Destination $Destination -Force
            Write-Log "复制文件: $Source → $Destination" -Level "INFO" -Color "Green"
        }
 
        return $true
    }
    catch {
        Write-Log "复制失败: $_" -Level "ERROR" -Color "Red"
        return $false
    }
}
 
# 主脚本开始
Write-Log "开始构建过程..." -Level "INFO" -Color "Cyan"
 
# 1. 清理旧构建
try {
    if (Test-Path $Config.OutputDir) {
        Remove-Item -Path $Config.OutputDir -Recurse -Force
        Write-Log "已清理旧构建目录: $($Config.OutputDir)" -Level "INFO" -Color "Yellow"
    }
    New-Item -ItemType Directory -Path $Config.OutputDir -Force | Out-Null
}
catch {
    Write-Log "清理目录失败: $_" -Level "ERROR" -Color "Red"
    exit 1
}
 
# 2. 设置环境变量并编译
try {
    $env:GOOS = $Config.GOOS
    $env:GOARCH = $Config.GOARCH
    $env:CGO_ENABLED = "0"
 
    $buildCommand = "go build -ldflags='-s -w' -o `"$($Config.OutputDir)\$($Config.AppName)`" ./cmd/manager/main.go"
    Write-Log "执行编译命令: $buildCommand" -Level "DEBUG" -Color "Gray"
 
    Invoke-Expression $buildCommand
    Write-Log "编译成功完成" -Level "INFO" -Color "Green"
    Write-Log "输出路径: $(Resolve-Path $Config.OutputDir)\$($Config.AppName)" -Level "INFO" -Color "Cyan"
}
catch {
    Write-Log "编译失败: $_" -Level "ERROR" -Color "Red"
    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 "部分文件复制失败,请检查错误日志" -Level "WARNING" -Color "Yellow"
    exit 1
}
 
Write-Log "所有构建步骤完成!" -Level "INFO" -Color "Cyan"
exit 0