Skip to content

生产部署 ​

本文档介绍 Temporal 的生产部署方案,包括 Temporal Cloud 和自托管部署。

10.1 Temporal Cloud ​

Temporal Cloud 是 Temporal 官方提供的托管服务,无需自行维护集群。

优势 ​

优势说明
零运维无需管理基础设施
高可用多区域部署,自动故障转移
自动扩展根据负载自动扩缩容
安全合规内置安全认证和加密
支持官方技术支持

连接 Temporal Cloud ​

go
package main

import (
    "crypto/tls"
    "log"
    
    "go.temporal.io/sdk/client"
)

func main() {
    // Temporal Cloud 连接配置
    c, err := client.Dial(client.Options{
        HostPort:  "your-namespace.tmprl.cloud:7233",
        Namespace: "your-namespace",
        
        // TLS 配置
        ConnectionOptions: client.ConnectionOptions{
            TLS: &tls.Config{
                InsecureSkipVerify: false,
            },
        },
    })
    if err != nil {
        log.Fatalln("无法连接 Temporal Cloud", err)
    }
    defer c.Close()
    
    log.Println("已连接 Temporal Cloud")
}

认证方式 ​

Temporal Cloud 支持:

  • mTLS(双向 TLS)
  • API Key 认证
  • OAuth 2.0

10.2 自托管部署 ​

10.2.1 Docker Compose 部署 ​

适用于开发和测试环境:

yaml
# docker-compose.yml
version: '3.8'

services:
  temporal:
    image: temporalio/server:1.26.1
    ports:
      - "7233:7233"
      - "8233:8233"
    environment:
      - DB=postgresql
      - DB_PORT=5432
      - POSTGRES_USER=temporal
      - POSTGRES_PWD=temporal
      - POSTGRES_SEEDS=postgres
    depends_on:
      - postgres
    volumes:
      - temporal-data:/var/lib/temporal

  postgres:
    image: postgres:15
    environment:
      POSTGRES_USER: temporal
      POSTGRES_PASSWORD: temporal
      POSTGRES_DB: temporal
    volumes:
      - postgres-data:/var/lib/postgresql/data

  temporal-ui:
    image: temporalio/ui:2.31.0
    ports:
      - "8080:8080"
    environment:
      - TEMPORAL_ADDRESS=temporal:7233
    depends_on:
      - temporal

  temporal-admin-tools:
    image: temporalio/admin-tools:1.26.1
    depends_on:
      - temporal
    environment:
      - TEMPORAL_CLI_ADDRESS=temporal:7233

volumes:
  temporal-data:
  postgres-data:

启动:

bash
docker-compose up -d

10.2.2 Kubernetes/Helm 部署 ​

适用于生产环境:

bash
# 添加 Helm 仓库
helm repo add temporalio https://helm.temporal.io
helm repo update

# 创建命名空间
kubectl create namespace temporal

# 安装(开发环境)
helm install temporal temporalio/temporal \
  --namespace temporal \
  --set server.replicaCount=1 \
  --set cassandra.config.cluster_size=1

# 安装(生产环境)
helm install temporal temporalio/temporal \
  --namespace temporal \
  --set server.replicaCount=3 \
  --set cassandra.config.cluster_size=3 \
  --set server.config.persistence.default.sql.plugin=postgres \
  --set server.config.persistence.default.sql.host=postgres.default.svc.cluster.local

生产环境 values.yaml ​

yaml
# values-production.yaml
server:
  replicaCount: 3
  
  resources:
    limits:
      cpu: 4
      memory: 8Gi
    requests:
      cpu: 2
      memory: 4Gi

  config:
    persistence:
      default:
        driver: "sql"
        sql:
          plugin: "postgres"
          host: "postgres.production.svc.cluster.local"
          port: 5432
          database: "temporal"
          user: "temporal"
          
    ring:
      announceHost: "temporal-server.default.svc.cluster.local"

cassandra:
  enabled: false

prometheus:
  enabled: true
  
grafana:
  enabled: true

10.3 高可用配置 ​

10.3.1 多节点集群 ​

┌─────────────────────────────────────────────────────────────┐
│                    Load Balancer                             │
│                    (Nginx/HAProxy)                           │
└─────────────────────┬───────────────────────────────────────┘
                      │
        ┌─────────────┼─────────────┐
        │             │             │
        ▼             ▼             ▼
┌───────────┐ ┌───────────┐ ┌───────────┐
│ Temporal  │ │ Temporal  │ │ Temporal  │
│ Server 1  │ │ Server 2  │ │ Server 3  │
└─────┬─────┘ └─────┬─────┘ └─────┬─────┘
      │             │             │
      └─────────────┼─────────────┘
                    │
        ┌───────────┼───────────┐
        │           │           │
        ▼           ▼           ▼
┌───────────┐ ┌───────────┐ ┌───────────┐
│ PostgreSQL│ │ PostgreSQL│ │ PostgreSQL│
│ Primary   │ │ Replica 1 │ │ Replica 2 │
└───────────┘ └───────────┘ └───────────┘

10.3.2 故障转移 ​

Temporal 自动处理:

  • Worker 故障:任务自动重新分配
  • Server 故障:请求路由到其他节点
  • 数据库故障:自动切换到副本

10.4 监控与告警 ​

10.4.1 Prometheus 指标 ​

Temporal 内置 Prometheus 指标端点:

yaml
# prometheus.yml
scrape_configs:
  - job_name: 'temporal'
    static_configs:
      - targets: ['temporal:9090']

关键指标:

指标说明告警阈值
temporal_service_errors服务错误数> 100/min
temporal_workflow_success成功的 Workflow监控趋势
temporal_workflow_failed失败的 Workflow> 10/min
temporal_activity_latencyActivity 延迟p99 > 10s
temporal_task_queue_backlog任务队列积压> 10000

10.4.2 Grafana Dashboard ​

使用官方 Dashboard:

  • Temporal Cluster Overview
  • Temporal Workflow Metrics
  • Temporal Activity Metrics

10.4.3 告警规则 ​

yaml
# alert-rules.yml
groups:
  - name: temporal
    rules:
      - alert: HighErrorRate
        expr: rate(temporal_service_errors[5m]) > 100
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Temporal 服务错误率过高"
          
      - alert: WorkflowBacklog
        expr: temporal_task_queue_backlog > 10000
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "任务队列积压过多"

10.5 安全配置 ​

10.5.1 TLS 加密 ​

yaml
# server-config.yaml
tls:
  internode:
    certFile: /etc/temporal/certs/server.crt
    keyFile: /etc/temporal/certs/server.key
    caFile: /etc/temporal/certs/ca.crt
    
  frontend:
    certFile: /etc/temporal/certs/server.crt
    keyFile: /etc/temporal/certs/server.key
    caFile: /etc/temporal/certs/ca.crt
    requireClientAuth: true

10.5.2 认证与授权 ​

yaml
# server-config.yaml
authentication:
  jwt:
    provider: "my-jwt-provider"
    keyPath: "/etc/temporal/keys/jwt-public-key"
    
authorization:
  jwt:
    claimMappings:
      - claim: "email"
        key: "user-email"

10.5.3 Namespace 隔离 ​

bash
# 创建带权限控制的 Namespace
temporal operator namespace create production \
  --description "Production namespace" \
  --retention 7d \
  --data "owner=team-platform"

10.6 性能调优 ​

10.6.1 数据库优化 ​

sql
-- PostgreSQL 配置
-- postgresql.conf
shared_buffers = 4GB
effective_cache_size = 12GB
maintenance_work_mem = 1GB
max_connections = 500

-- 索引优化
CREATE INDEX idx_workflow_execution ON execution(workspace_id, workflow_id);

10.6.2 Temporal Server 配置 ​

yaml
# server-config.yaml
persistence:
  defaultStore: default
  visibilityStore: visibility
  
  numHistoryShards: 512  # 增加分片数提高并发

service:
  frontend:
    rpc:
      grpc:
        MaxConcurrentStreams: 10000
        
  history:
    cache:
      initialSize: 10000
      maxSize: 50000

matching:
  numTaskQueueReadPartitions: 10
  numTaskQueueWritePartitions: 10

10.6.3 Worker 调优 ​

go
// Worker 配置优化
w := worker.New(c, taskQueue, worker.Options{
    MaxConcurrentWorkflowTaskExecutionSize: 100,
    MaxConcurrentActivityExecutionSize:     1000,
    MaxConcurrentLocalActivityExecutionSize: 1000,
    
    // 资源限制
    Identity: "worker-" + hostname,
})

10.7 备份与恢复 ​

备份策略 ​

bash
# PostgreSQL 备份
pg_dump -h localhost -U temporal temporal > backup.sql

# 定期备份脚本
0 2 * * * pg_dump -h localhost -U temporal temporal > /backup/temporal-$(date +\%Y\%m\%d).sql

恢复流程 ​

bash
# 恢复数据库
psql -h localhost -U temporal temporal < backup.sql

# 重启 Temporal 服务
docker-compose restart temporal

10.8 常见问题排查 ​

问题:Workflow 积压 ​

bash
# 检查任务队列状态
temporal task-queue describe --task-queue my-queue

# 增加 Worker 数量
docker-compose up -d --scale worker=5 worker

问题:数据库连接池耗尽 ​

yaml
# 增加连接池大小
persistence:
  defaultStore:
    sql:
      maxConns: 500
      maxIdleConns: 100
      maxConnLifetime: 1h

问题:内存使用过高 ​

yaml
# 调整缓存大小
history:
  cache:
    maxSize: 30000  # 减小缓存

参考资源 ​