{T}

高可用架构 学习笔记(第 6 部分)

多活架构实现示例

javascript
// 多活数据中心管理
class MultiDataCenter {
  constructor() {
    this.datacenters = {
      beijing: {
        region: 'cn-north',
        status: 'active',
        weight: 30,
        latency: { shanghai: 20, guangzhou: 50 }
      },
      shanghai: {
        region: 'cn-east',
        status: 'active',
        weight: 30,
        latency: { beijing: 20, guangzhou: 30 }
      },
      guangzhou: {
        region: 'cn-south',
        status: 'active',
        weight: 40,
        latency: { beijing: 50, shanghai: 30 }
      }
    }
  }
  
  // 路由用户请求
  route(userLocation) {
    const activeDCs = this.getActiveDataCenters()
    
    // 按延迟排序
    const sorted = activeDCs.sort((a, b) => {
      return this.datacenters[a].latency[userLocation] - 
             this.datacenters[b].latency[userLocation]
    })
    
    // 返回最近的活跃数据中心
    return sorted[0]
  }
  
  // 获取活跃数据中心
  getActiveDataCenters() {
    return Object.keys(this.datacenters).filter(
      dc => this.datacenters[dc].status === 'active'
    )
  }
  
  // 健康检查
  healthCheck() {
    Object.keys(this.datacenters).forEach(dc => {
      const isHealthy = this.checkHealth(dc)
      
      if (!isHealthy && this.datacenters[dc].status === 'active') {
        this.failover(dc)
      }
    })
  }
  
  // 故障转移
  failover(failedDC) {
    console.log(`数据中心 ${failedDC} 故障,开始转移流量`)
    
    // 将故障数据中心的权重分配给其他数据中心
    const failedWeight = this.datacenters[failedDC].weight
    const activeDCs = this.getActiveDataCenters()
    
    // 标记为故障
    this.datacenters[failedDC].status = 'failed'
    this.datacenters[failedDC].weight = 0
    
    // 重新分配权重
    const weightPerDC = failedWeight / activeDCs.length
    activeDCs.forEach(dc => {
      this.datacenters[dc].weight += weightPerDC
    })
    
    console.log('流量转移完成:', this.datacenters)
  }
  
  // 健康检查
  checkHealth(dc) {
    // 模拟健康检查
    return Math.random() > 0.01  // 99% 可用性
  }
}

// 使用示例
const multiDC = new MultiDataCenter()

// 路由用户请求
const dc1 = multiDC.route('shanghai')
console.log(`上海用户路由到: ${dc1}`)

// 健康检查
multiDC.healthCheck()

// 模拟故障
console.log('\n模拟北京数据中心故障...')
multiDC.datacenters.beijing.status = 'failed'
multiDC.failover('beijing')

三、CAP 理论详解

3.1 CAP 三要素

CAP 理论:分布式系统最多只能同时满足三个要素中的两个

code
CAP 三要素:

C - Consistency (一致性)
    所有节点在同一时间看到的数据是一致的
    
A - Availability (可用性)
    每个请求都能在合理时间内得到响应
    
P - Partition Tolerance (分区容错)
    系统在网络分区发生时仍能继续运行

3.2 CAP 组合选择

code
CAP 组合方案:

┌─────────────────────────────────────┐
│         CA (无 P)                    │
│  一致性 + 可用性                      │
│   数据强一致                        │
│   高可用                            │
│   不支持网络分区                     │
│                                      │
│  适用: 单机数据库、集群架构            │
│  示例: MySQL 主从、Oracle RAC        │
└─────────────────────────────────────┘

┌─────────────────────────────────────┐
│         CP (无 A)                    │
│  一致性 + 分区容错                    │
│   数据强一致                        │
│   支持网络分区                       │
│   网络分区时不可用                   │
│                                      │
│  适用: 分布式数据库                   │
│  示例: MongoDB、HBase、Redis Cluster │
└─────────────────────────────────────┘

┌─────────────────────────────────────┐
│         AP (无 C)                    │
│  可用性 + 分区容错                    │
│   高可用                            │
│   支持网络分区                       │
│   数据可能不一致                     │
│                                      │
│  适用: 分布式存储、缓存               │
│  示例: Cassandra、DynamoDB、CouchDB  │
└─────────────────────────────────────┘