Easy-Go-Web3
知识图谱Go 教程React Web3智能合约
需求分析系统设计设计模式Go 微服务
项目实战DevOps
Go 生态React 生态智能合约生态Web3 生态AI × Web3工具箱Web3 公司远程Web3求职
🎯 AA 工程师面试手册博客
GitHub
返回系统设计
高级

高可用架构

High Availability Architecture

设计 99.99% 可用性的系统,掌握负载均衡、容灾、降级等核心策略。

1高可用核心指标

**SLA (Service Level Agreement)** 是衡量系统可用性的核心指标: | 可用性 | 年故障时间 | 适用场景 | |--------|------------|----------| | 99% | 3.65 天 | 内部系统 | | 99.9% | 8.76 小时 | 普通业务 | | 99.99% | 52.56 分钟 | 核心业务 | | 99.999%| 5.26 分钟 | 金融/支付 | **Web3 特殊考量**: - 链上交易不可逆,对可用性要求更高 - 需要多节点冗余避免单点故障 - Gas 价格波动需要有降级策略

2负载均衡策略

Go 服务常用的负载均衡实现:
example.go
go
1// 加权轮询负载均衡器
2type WeightedRoundRobin struct {
3 nodes []*Node
4 current int
5 mu sync.Mutex
6}
7
8type Node struct {
9 Address string
10 Weight int
11 Current int
12 Health bool
13}
14
15func (wrr *WeightedRoundRobin) Next() *Node {
16 wrr.mu.Lock()
17 defer wrr.mu.Unlock()
18
19 total := 0
20 var best *Node
21
22 for _, node := range wrr.nodes {
23 if !node.Health {
24 continue
25 }
26 node.Current += node.Weight
27 total += node.Weight
28
29 if best == nil || node.Current > best.Current {
30 best = node
31 }
32 }
33
34 if best != nil {
35 best.Current -= total
36 }
37 return best
38}
39
40// 健康检查
41func (wrr *WeightedRoundRobin) HealthCheck(ctx context.Context) {
42 ticker := time.NewTicker(5 * time.Second)
43 for {
44 select {
45 case <-ticker.C:
46 for _, node := range wrr.nodes {
47 node.Health = wrr.ping(node.Address)
48 }
49 case <-ctx.Done():
50 return
51 }
52 }
53}

3服务降级与熔断

当下游服务不可用时,需要实现优雅降级: **熔断器模式 (Circuit Breaker)**: - Closed:正常状态,请求直接转发 - Open:熔断状态,直接返回降级响应 - Half-Open:半开状态,允许少量请求探测 **Go 熔断器实现**:
example.go
go
1type CircuitBreaker struct {
2 state State
3 failures int
4 successes int
5 threshold int
6 timeout time.Duration
7 lastFailure time.Time
8 mu sync.RWMutex
9}
10
11func (cb *CircuitBreaker) Execute(fn func() error) error {
12 if !cb.AllowRequest() {
13 return ErrCircuitOpen
14 }
15
16 err := fn()
17
18 cb.mu.Lock()
19 defer cb.mu.Unlock()
20
21 if err != nil {
22 cb.failures++
23 cb.lastFailure = time.Now()
24 if cb.failures >= cb.threshold {
25 cb.state = Open
26 }
27 return err
28 }
29
30 if cb.state == HalfOpen {
31 cb.successes++
32 if cb.successes >= cb.threshold {
33 cb.state = Closed
34 cb.failures = 0
35 }
36 }
37 return nil
38}

核心要点

  • •消除单点故障,关键组件至少 2 副本
  • •实现优雅降级,核心功能优先保障
  • •健康检查要快速准确
  • •故障转移要自动化

真实案例

  • →Uniswap:多 RPC 节点故障转移
  • →OpenSea:CDN + 多区域部署
  • →Binance:热备 + 冷备 + 异地容灾
高并发设计
Easy-Go-Web3

构建 Go 后端与 Web3 的学习之路。从基础到进阶,从理论到实践,助你成为全栈区块链开发者。

学习路径

  • 知识图谱
  • Go 教程
  • Go 微服务
  • 面试手册

资源中心

  • 工具箱
  • DevOps 工具
  • Web3 生态
  • 博客

© 2025 Easy-Go-Web3. All rights reserved.

Created withbyhardybao