{T}

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

五、高可用架构实战案例

5.1 MySQL 高可用架构

主从复制架构

code
MySQL 主从复制:

写入请求 → MySQL 主库
              ↓
         Binlog 同步
              ↓
         ┌────┴────┐
         ↓         ↓
    MySQL 从库1  MySQL 从库2
         ↑         ↑
读取请求   读取请求

配置步骤:
1. 主库开启 binlog
2. 从库配置主库信息
3. 启动复制线程
4. 监控复制延迟

配置示例

sql
-- 主库配置 (my.cnf)
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog-format = ROW
sync_binlog = 1

-- 从库配置 (my.cnf)
[mysqld]
server-id = 2
relay-log = mysql-relay-bin
read-only = 1

-- 主库创建复制账号
CREATE USER 'repl'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';

-- 从库启动复制
CHANGE MASTER TO
  MASTER_HOST='master-ip',
  MASTER_USER='repl',
  MASTER_PASSWORD='password',
  MASTER_LOG_FILE='mysql-bin.000001',
  MASTER_LOG_POS=0;

START SLAVE;

5.2 Redis 高可用架构

Redis Sentinel 哨兵架构

code
Redis Sentinel 架构:

┌──────────────┐
│  Sentinel 1   │
└──────┬───────┘
       │ 监控 & 故障转移
┌──────┴───────┐
│              │
↓              ↓
┌────────┐  ┌────────┐
│ Redis  │  │ Redis  │
│  主库   │  │  从库   │
└────────┘  └────────┘

配置示例

bash
# sentinel.conf
port 26379
sentinel monitor mymaster 192.168.1.100 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

# 启动 Sentinel
redis-sentinel /path/to/sentinel.conf

Redis Cluster 集群架构

code
Redis Cluster 集群:

┌─────────────────────────────────┐
│         Redis Cluster           │
├─────────────────────────────────┤
│  ┌─────────┐    ┌─────────┐    │
│  │ Master1 │ ←→ │ Master2 │    │
│  │ 0-5460  │    │ 5461-10922  │
│  └────┬────┘    └────┬────┘    │
│       ↓              ↓          │
│  ┌─────────┐    ┌─────────┐    │
│  │ Slave1  │    │ Slave2  │    │
│  └─────────┘    └─────────┘    │
│                                  │
│  ┌─────────┐                    │
│  │ Master3 │                    │
│  │10923-16383│                  │
│  └────┬────┘                    │
│       ↓                          │
│  ┌─────────┐                    │
│  │ Slave3  │                    │
│  └─────────┘                    │
└─────────────────────────────────┘

特点:
- 数据分片存储
- 自动故障转移
- 水平扩展