用户工具

站点工具


教程:go-platform:部署:部署

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
教程:go-platform:部署:部署 [2025/07/04 09:32] – [网关] tom教程:go-platform:部署:部署 [2025/07/06 08:59] (当前版本) – [管理中心] tom
行 180: 行 180:
 pm2 start ecosystem.config.js pm2 start ecosystem.config.js
 </code> </code>
 +<WRAP center round notice 60%>
 +配置中心的API HTTP端口(6081),gRPC端口(6082),Web页面端口(6080)都需要防火墙放行
 +</WRAP>
 +
 ====网关==== ====网关====
 对于''gateway'',可以通过如下的''Power Shell''脚本进行打包: 对于''gateway'',可以通过如下的''Power Shell''脚本进行打包:
 +<hidden gateway打包脚本>
 <code powershell> <code powershell>
 <# <#
行 255: 行 260:
 exit 0 exit 0
 </code> </code>
 +</hidden>
 在服务器上部署时,''pm2''脚本ecosystem.config.js和配置中心不同之处在于需要传递一些参数: 在服务器上部署时,''pm2''脚本ecosystem.config.js和配置中心不同之处在于需要传递一些参数:
-<code >+<code javascript[enable_line_numbers="true",highlight_lines_extra="10,11,12"]> 
 +module.exports = { 
 +  apps: [{ 
 +    name: "gatewaye",      // 应用名称 
 +    script: "./gateway",      // 二进制文件路径 
 +    //instances: 2,          // 启动2个实例(负载均衡) 
 +    autorestart: true,     // 崩溃后自动重启 
 +    watch: false,          // 禁用文件监视 
 +    max_memory_restart: "500M",  // 内存超限后重启 
 +    env: { 
 +      APP_NAME: "Gateway", 
 +      CONF_HOST: "127.0.0.1:6082",   
 +      CONF_TOKEN: "1025D32F6CA7A2A320FE091B22C5DF3C",    
 +      NODE_ENV: "production", 
 +      GO_ENV: "prod" 
 +    }, 
 +    error_file: "./logs/err.log",  // 错误日志 
 +    out_file: "./logs/out.log",    // 输出日志 
 +    pid_file: "./logs/pm2.pid"      // PID 文件 
 +  }] 
 +}; 
 +</code> 
 +==== 管理中心 ====
  
 +<code powershell >
 +# 配置参数
 +$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
 </code> </code>
教程/go-platform/部署/部署.1751621560.txt.gz · 最后更改: tom