单实例容量 = (CPU核心数 * 1000ms) / 平均RT(ms)
# 使用tcpcopy复制生产流量
./tcpcopy -x 80-测试环境IP:80 -s 生产环境IP -c 10.0.0.0/16 -n 2
// 全链路监控埋点示例
@Trace(operationName = "orderService/createOrder")
@Metrics(enable = true)
public OrderResult createOrder(@Tag(key = "userId") Long userId) {
// 业务逻辑
}
# Tomcat线程池优化
server:
tomcat:
max-threads: 800
min-spare-threads: 100
max-connections: 1000
accept-count: 500
connection-timeout: 3000
# Druid连接池配置
spring:
datasource:
druid:
max-active: 50
min-idle: 10
initial-size: 10
validation-query: SELECT 1
test-while-idle: true
time-between-eviction-runs-millis: 60000
// 集群限流配置
FlowRule rule = new FlowRule();
rule.setResource("orderService");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(1000);
rule.setClusterMode(true); // 开启集群模式
rule.setClusterConfig(new ClusterFlowConfig()
.setFlowId(12345)
.setThresholdType(ClusterFlowConfig.THRESHOLD_AVG_LOCAL));
// 异常比例熔断
DegradeRule rule = new DegradeRule();
rule.setResource("paymentService");
rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
rule.setCount(0.5); // 异常比例阈值50%
rule.setTimeWindow(60); // 熔断持续时间60s
rule.setMinRequestAmount(100); // 最小请求数
spring:
cloud:
sentinel:
system:
enable: true
highest-system-load: 8.0 # 最大系统负载
avg-rt: 200 # 平均响应时间阈值
max-thread: 800 # 最大并发线程数
qps: 5000 # 入口QPS阈值
// 商品维度限流
ParamFlowRule rule = new ParamFlowRule("seckill")
.setParamIdx(0)
.setGrade(RuleConstant.FLOW_GRADE_QPS)
.setDurationInSec(1)
.setParamFlowItemList(Arrays.asList(
new ParamFlowItem().setObject("goods:1001").setCount(500),
new ParamFlowItem().setObject("goods:1002").setCount(1000)
));
// IP黑白名单配置
AuthorityRule rule = new AuthorityRule();
rule.setResource("adminAPI");
rule.setStrategy(RuleConstant.AUTHORITY_BLACK); // 黑名单模式
rule.setLimitApp("192.168.1.100,10.0.0.0/24");
// 匀速排队配置
FlowRule rule = new FlowRule();
rule.setResource("createOrder");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(1000);
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
rule.setMaxQueueingTimeMs(20000); // 最大排队时间20s
// 热点数据缓存方案
public Product getProduct(String id) {
String redisKey = "product:" + id;
Product product = redisTemplate.opsForValue().get(redisKey);
if (product == null) {
product = dbQuery(id);
redisTemplate.opsForValue().set(redisKey, product, 30, TimeUnit.SECONDS);
}
return product;
}
// RocketMQ异步削峰
@SentinelResource(value = "submitOrder", blockHandler = "flowBlockHandler")
public void asyncSubmitOrder(Order order) {
rocketMQTemplate.asyncSend("order_topic", order, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
// 发送成功处理
}
});
}
// 流控降级处理
public void flowBlockHandler(Order order, BlockException ex) {
rocketMQTemplate.send("order_fallback_topic", order);
}
// 多级降级策略
@SentinelResource(
value = "getPrice",
fallback = "localCacheFallback",
blockHandler = "defaultPriceHandler"
)
public BigDecimal getPrice(String sku) {
// 正常业务逻辑
}
// 一级降级:本地缓存
public BigDecimal localCacheFallback(String sku) {
return localCache.get(sku);
}
// 二级降级:默认值
public BigDecimal defaultPriceHandler(String sku, BlockException ex) {
return new BigDecimal("99.00");
}
// Dubbo服务超时配置
@DubboReference(timeout = 1000, mock = "force:return null")
private InventoryService inventoryService;
// Sentinel异常比例规则
DegradeRule rule = new DegradeRule();
rule.setResource("payment");
rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_RATIO);
rule.setCount(0.6); // 异常比例60%
rule.setTimeWindow(30); // 熔断30秒
// 基于线程数的熔断规则
DegradeRule rule = new DegradeRule();
rule.setResource("exportService");
rule.setGrade(RuleConstant.DEGRADE_GRADE_THREAD_COUNT);
rule.setCount(200); // 最大并发线程数
rule.setTimeWindow(60);
# 集群流控配置
spring:
cloud:
sentinel:
transport:
client-ip: ${POD_IP}
cluster-server:
enable: true
port: 18730
max-idle: 600
// 慢接口优化示例
@SentinelResource("slowAPI")
public void process() {
try (Entry entry = SphU.entry("slowAPI")) {
// 1. 异步化处理
CompletableFuture.runAsync(() -> heavyTask());
// 2. 结果缓存
cacheResult();
// 3. 批量处理
batchUpdate();
}
}
Scenario: 大促流量压测
Given 订单服务集群(10节点)
When 持续5分钟5000 QPS压力
Then 平均RT < 800ms
And 错误率 < 0.1%
And CPU使用率 < 70%
# 使用GoReplay回放流量
gor --input-file request.gor --output-http "http://test-env:8080"
服务级别 | 可用性 | 响应时间 | 补偿措施 |
---|---|---|---|
核心交易 | 99.99% | ≤500ms | 自动容灾切换 |
普通服务 | 99.9% | ≤1s | 优先扩容 |
查询服务 | 99% | ≤2s | 服务降级 |
该方案在某金融系统落地后效果: