Files
interview/questions/07-系统设计/API网关.md
yasinshaw 0e46a367c4 refactor: rename files to Chinese and organize by category
Organized 50 interview questions into 12 categories:
- 01-分布式系统 (9 files): 分布式事务, 分布式锁, 一致性哈希, CAP理论, etc.
- 02-数据库 (2 files): MySQL索引优化, MyBatis核心原理
- 03-缓存 (5 files): Redis数据结构, 缓存问题, LRU算法, etc.
- 04-消息队列 (1 file): RocketMQ/Kafka
- 05-并发编程 (4 files): 线程池, 设计模式, 限流策略, etc.
- 06-JVM (1 file): JVM和垃圾回收
- 07-系统设计 (8 files): 秒杀系统, 短链接, IM, Feed流, etc.
- 08-算法与数据结构 (4 files): B+树, 红黑树, 跳表, 时间轮
- 09-网络与安全 (3 files): TCP/IP, 加密安全, 性能优化
- 10-中间件 (4 files): Spring Boot, Nacos, Dubbo, Nginx
- 11-运维 (4 files): Kubernetes, CI/CD, Docker, 可观测性
- 12-面试技巧 (1 file): 面试技巧和职业规划

All files renamed to Chinese for better accessibility and
organized into categorized folders for easier navigation.

Generated with [Claude Code](https://claude.com/claude-code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-03-01 00:10:53 +08:00

172 lines
4.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# API 网关核心原理
## 问题
1. 什么是 API 网关?为什么需要 API 网关?
2. API 网关的核心功能有哪些?
3. Spring Cloud Gateway 和 Zuul 的区别?
4. 网关的限流、熔断、降级如何实现?
5. 网关的路由和负载均衡策略?
---
## 标准答案
### 1. API 网关的作用
**核心功能**
1. **路由转发**:将请求转发到后端服务
2. **统一鉴权**:集中的认证和授权
3. **限流熔断**:保护后端服务
4. **日志监控**:统一的日志和监控
5. **协议转换**HTTP → WebSocket、gRPC 等
6. **灰度发布**:按规则路由流量
---
### 2. Spring Cloud Gateway 核心概念
**三大组件**
```
Route路由
Predicate断言
Filter过滤器
```
**示例**
```yaml
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/api/users/**
- Header=Authorization, .*
filters:
- StripPrefix=1
- RequestRateLimiter=10 # 限流10 QPS
```
---
### 3. Gateway vs Zuul
| 特性 | Zuul 1.x | Zuul 2.x | Spring Cloud Gateway |
|------|----------|----------|---------------------|
| **模型** | Servlet 阻塞 | Netty 非阻塞 | Netty 非阻塞 |
| **性能** | 低 | 中 | 高 |
| **Spring** | 集成好 | 集成一般 | 原生支持 |
| **动态路由** | 不支持 | 支持 | 支持 |
| **限流** | 需自研 | 需自研 | 内置Redis |
---
### 4. 核心功能实现
#### **限流**
```yaml
spring:
cloud:
gateway:
routes:
- id: rate-limiter
uri: lb://user-service
predicates:
- Path=/api/users/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10 # 每秒填充 10 个令牌
redis-rate-limiter.burstCapacity: 20 # 桶容量 20
```
---
#### **熔断Circuit Breaker**
```yaml
spring:
cloud:
gateway:
routes:
- id: circuit-breaker
uri: lb://user-service
predicates:
- Path=/api/users/**
filters:
- name: CircuitBreaker
args:
fallbackUri: forward:/fallback
```
---
#### **统一鉴权**
```java
@Component
public class AuthFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String token = exchange.getRequest().getHeaders().getFirst("Authorization");
if (token == null || !validateToken(token)) {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
return chain.filter(exchange);
}
}
```
---
### 5. 网关高可用
**部署架构**
```
┌─────────────┐
│ 负载均衡 │
│ (Nginx) │
└──────┬──────┘
┌───────────────┼───────────────┐
│ │ │
┌────▼────┐ ┌────▼────┐ ┌────▼────┐
│Gateway 1│ │Gateway 2│ │Gateway 3│
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
└──────────────┼──────────────┘
┌──────────────┼──────────────┐
│ │ │
┌────▼────┐ ┌────▼────┐ ┌────▼────┐
│Service A│ │Service B│ │Service C│
└─────────┘ └─────────┘ └─────────┘
```
---
### 6. 阿里 P7 加分项
**深度理解**
- 理解 WebFlux 的响应式编程模型
- 理解 Netty 的事件循环
- 理解限流算法(令牌桶、漏桶)
**实战经验**
- 有网关性能调优的经验
- 有网关灰度发布的经验
- 有网关监控和故障排查的经验
**架构能力**
- 能设计高可用网关架构
- 能设计网关的监控体系
- 能设计网关的动态路由方案