本文档详细说明了如何将您的服务接入 Mo Gateway。
确保您的服务实现了健康检查接口:
@RestController
public class HealthController {
@GetMapping("/actuator/health")
public ResponseEntity<Health> health() {
return ResponseEntity.ok(Health.up().build());
}
}实现基本的监控指标:
@Configuration
public class MetricsConfig {
@Bean
public MeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}
}- 添加服务标签:
metadata:
labels:
gateway.enabled: "true"
gateway.service-type: "your-service-type"- 配置健康检查:
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10- 配置服务路由:
gateway:
routes:
- id: your-service
uri: http://your-service:8080
predicates:
- Path=/api/your-service/**
filters:
- StripPrefix=1gateway:
ratelimit:
service-strategies:
your-service:
capacity: 1000
refill-rate: 100
algorithm: tokenBucketgateway:
ratelimit:
api-strategies:
your-service:
/api/v1/users:
capacity: 100
refill-rate: 10
/api/v1/products:
capacity: 200
refill-rate: 20gateway:
loadbalancer:
service-strategies:
your-service:
type: weightedRoundRobin
weights:
instance1: 2
instance2: 1gateway:
loadbalancer:
service-strategies:
your-service:
type: responseTimeWeighted
response-time-weight: 0.7
connection-weight: 0.3gateway:
security:
auth:
type: jwt
jwt:
secret: your-secret-key
expiration: 3600gateway:
security:
authorization:
rules:
- path: /api/v1/admin/**
roles: [ADMIN]
- path: /api/v1/user/**
roles: [USER]- 实现优雅的服务启动和关闭
- 提供详细的健康检查信息
- 实现服务降级策略
- 添加必要的监控指标
- 使用连接池
- 实现请求缓存
- 优化响应数据大小
- 使用异步处理
- 实现请求签名验证
- 使用 HTTPS
- 实现请求频率限制
- 添加必要的日志记录
# 检查服务是否注册成功
curl http://localhost:8080/actuator/gateway/routes# 测试限流功能
for i in {1..100}; do
curl http://localhost:8080/api/your-service/test
done# 测试负载均衡
for i in {1..10}; do
curl http://localhost:8080/api/your-service/test
done- 检查服务健康检查接口
- 验证服务标签配置
- 确认网络连接
- 检查限流配置
- 验证 Redis 连接
- 确认限流算法配置
- 检查服务实例状态
- 验证负载均衡配置
- 确认服务权重设置