# Activity服务接入YARP网关配置脚本 # PowerShell: pwsh Register-ActivityService.ps1 -GatewayUrl "http://localhost:5000" param( [string]$GatewayUrl = "http://localhost:5000", [string]$ServiceName = "activity", [string]$ClusterId = "activity-service", [string]$PathPattern = "/api/activity/{**path}", [string]$InstanceAddress = "http://localhost:5001", [string]$DestinationId = "activity-1", [int]$Weight = 1, [switch]$IsGlobal = $true, [string]$TenantCode = "" ) $ErrorActionPreference = "Stop" function Register-ActivityService { param( [string]$GatewayUrl, [string]$ServiceName, [string]$ClusterId, [string]$PathPattern, [string]$InstanceAddress, [string]$DestinationId, [int]$Weight, [bool]$IsGlobal, [string]$TenantCode ) Write-Host "========================================" -ForegroundColor Cyan Write-Host "Activity Service Gateway Configuration" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" # Step 1: Add Service Instance (Cluster) Write-Host "[Step 1] Adding service instance to cluster..." -ForegroundColor Yellow $instanceBody = @{ destinationId = $DestinationId address = $InstanceAddress weight = $Weight } | ConvertTo-Json try { $instanceResponse = Invoke-RestMethod -Uri "$GatewayUrl/api/gateway/clusters/$ClusterId/instances" ` -Method Post ` -ContentType "application/json" ` -Body $instanceBody Write-Host " ✓ Instance added: $DestinationId -> $InstanceAddress" -ForegroundColor Green } catch { if ($_.Exception.Response.StatusCode -eq "BadRequest") { Write-Host " ℹ Instance already exists, skipping..." -ForegroundColor Gray } else { throw $_ } } # Step 2: Add Route Write-Host "" Write-Host "[Step 2] Adding gateway route..." -ForegroundColor Yellow if ($IsGlobal) { $routeBody = @{ serviceName = $ServiceName clusterId = $ClusterId pathPattern = $PathPattern } | ConvertTo-Json $routeUrl = "$GatewayUrl/api/gateway/routes/global" $routeDescription = "global route" } else { $routeBody = @{ serviceName = $ServiceName pathPattern = $PathPattern } | ConvertTo-Json $routeUrl = "$GatewayUrl/api/gateway/tenants/$TenantCode/routes" $routeDescription = "tenant route for $TenantCode" } try { $routeResponse = Invoke-RestMethod -Uri $routeUrl ` -Method Post ` -ContentType "application/json" ` -Body $routeBody Write-Host " ✓ Route added: $PathPattern -> $ClusterId" -ForegroundColor Green } catch { if ($_.Exception.Response.StatusCode -eq "BadRequest") { Write-Host " ℹ Route already exists, skipping..." -ForegroundColor Gray } else { throw $_ } } # Step 3: Reload Config Write-Host "" Write-Host "[Step 3] Reloading gateway configuration..." -ForegroundColor Yellow $reloadResponse = Invoke-RestMethod -Uri "$GatewayUrl/api/gateway/reload" -Method Post Write-Host " ✓ Configuration reloaded" -ForegroundColor Green # Summary Write-Host "" Write-Host "========================================" -ForegroundColor Cyan Write-Host "Configuration Complete!" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" Write-Host "Service: $ServiceName" -ForegroundColor White Write-Host "Cluster: $ClusterId" -ForegroundColor White Write-Host "Route: $routeDescription" -ForegroundColor White Write-Host "Pattern: $PathPattern" -ForegroundColor White Write-Host "Target: $InstanceAddress" -ForegroundColor White Write-Host "" Write-Host "Test the service at:" -ForegroundColor Yellow Write-Host " $GatewayUrl$($PathPattern.Replace('{**path}', 'campaigns'))" -ForegroundColor Cyan Write-Host "" } # Execute Register-ActivityService -GatewayUrl $GatewayUrl ` -ServiceName $ServiceName ` -ClusterId $ClusterId ` -PathPattern $PathPattern ` -InstanceAddress $InstanceAddress ` -DestinationId $DestinationId ` -Weight $Weight ` -IsGlobal $IsGlobal.IsPresent ` -TenantCode $TenantCode