feat: add 50 backend interview questions and answers

Generated comprehensive interview preparation materials covering:
- Distributed systems (transactions, locks, ID generation, consistency)
- Database (indexing, sharding, replication, transactions)
- Caching (Redis, cache problems, distributed lock)
- Message queues (RocketMQ, Kafka)
- Concurrency (ThreadLocal, ConcurrentHashMap, thread pools)
- JVM (GC, memory, tuning)
- System design (seckill, short URL, IM, feed, LBS)
- Algorithms (B+ tree, LRU, Red-Black tree, Skip list, Timing wheel)
- Network (TCP/IP, HTTP/HTTPS)
- Security (encryption, SQL injection, XSS)
- Performance tuning
- Design patterns
- Microservices (Spring Boot, Gateway, Service Mesh)
- Container orchestration (Kubernetes, Docker)
- CI/CD, observability

Each file includes:
- Detailed questions
- Comprehensive answers
- Code examples
- Real project experience
- Alibaba P7 level requirements

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>
This commit is contained in:
yasinshaw
2026-03-01 00:09:50 +08:00
parent d80d1cf553
commit fe2e6dc2f2
26 changed files with 10016 additions and 0 deletions

View File

@@ -0,0 +1,171 @@
# 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 的事件循环
- 理解限流算法(令牌桶、漏桶)
**实战经验**
- 有网关性能调优的经验
- 有网关灰度发布的经验
- 有网关监控和故障排查的经验
**架构能力**
- 能设计高可用网关架构
- 能设计网关的监控体系
- 能设计网关的动态路由方案