“天猫双11”背后的流量治理技术与标准实践
一年一度的天猫双11已经落下帷幕,大家在疯狂买买买的过程中一定会有疑问:如何保障微服务在双十一的超级峰值下也能如丝般顺滑稳定?这背后的技术原理是怎样的,有没有一些最佳实践与标准?这篇文章就为大家介绍如何结合 Sentinel 与 OpenSergo 玩转双十一背后的流量治理技术与标准。赵奕豪(宿何):Sentinel & OpenSergo 开源项目负责人
- 流量治理与服务容错:流量路由、流量染色、全链路灰度、流量防护与自愈(流量控制、服务熔断、容错防抖)
- 微服务视角的数据库与缓存治理:端侧连接池治理、读写流量路由、SQL 流控等
- 服务元信息与服务发现
- 演唱会抢票瞬间洪峰流量导致系统超出最大负载,load 飙高,用户无法正常下单;
- 在线选课时同一时刻提交选课的请求过多,系统无法响应;
- 页面服务中某一块内容访问很慢,一直加载不出来,导致整个页面都卡住,无法正常操作
- 服务自身流量超过承载能力导致不可用。比如激增流量、批量任务投递导致服务负载飙高,无法正常处理请求。
- 服务因依赖其他不可用服务,导致自身连环不可用。比如我们的服务可能依赖好几个第三方服务,假设某个支付服务出现异常,调用非常慢,而调用端又没有有效地进行预防与处理,则调用端的线程池会被占满,影响服务自身正常运转。在分布式系统中,调用关系是网状的、错综复杂的,某个服务出现故障可能会导致级联反应,导致整个链路不可用。
- Target: 针对什么样的请求。可以通过通用的 resourceKey(Sentinel 中即为资源名的概念)来标识,也可以用细化的规则来标识(如具有某个特定 HTTP header 的请求);
- Strategy: 容错或控制策略,如流控、熔断、并发控制、自适应过载保护、离群实例摘除等;
- FallbackAction: 触发后的 fallback 行为,如返回某个错误或状态码。
apiVersion: fault-tolerance.opensergo.io/v1alpha1kind: RateLimitStrategymetadata:name: rate-limit-foospec:metricType: RequestAmountlimitMode: Globalthreshold: 10statDuration: "1s"---apiVersion: fault-tolerance.opensergo.io/v1alpha1kind: HttpRequestFallbackActionmetadata:name: fallback-foospec:behavior: ReturnProvidedResponsebehaviorDesc:# 触发策略控制后,HTTP 请求返回 429 状态码,同时携带指定的内容和 header.responseStatusCode: 429responseContentBody: "Blocked by Sentinel"responseAdditionalHeaders:- key: X-Sentinel-Limitvalue: "foo"---apiVersion: fault-tolerance.opensergo.io/v1alpha1kind: FaultToleranceRulemetadata:name: my-rulenamespace: prodlabels:app: my-appspec:selector:app: foo-app # 规则配置生效的服务名targets:- targetResourceName: '/foo'strategies:- name: rate-limit-foofallbackAction: fallback-foo
Sentinel 原生支持 OpenSergo 流量防护与容错标准
Sentinel 原生支持 OpenSergo 流量防护与容错标准
-
Sentinel Java OpenSergo data-source:
https://github.com/alibaba/Sentinel/tree/master/sentinel-extension/sentinel-datasource-opensergo -
Sentinel Go OpenSergo data-source (work-in-progress):
https://github.com/alibaba/sentinel-golang/pull/489
第三步:在项目合适的位置(如 Spring 初始化 hook 或 Sentinel InitFunc 中)中创建并注册 Sentinel OpenSergo 数据源:<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-opensergo</artifactId><!-- 对应 Sentinel 1.8.6 版本 --><version>0.1.0-beta</version></dependency>
第四步:启动应用,访问项目中的 Web 接口,在没有配置规则的情况下应该一直返回正常结果。接下来我们按 OpenSergo CRD 的方式针对 /foo 这个 Web 接口配置一个 QPS=2 的流控规则:// 传入 OpenSergo Control Plane 的 endpoint,以及希望监听的应用名.// 在我们的例子中,假定应用名为 foo-appOpenSergoDataSourceGroup openSergo = new OpenSergoDataSourceGroup("localhost", 10246, "default", "foo-app");// 初始化 OpenSergo 数据源.openSergo.start();// 订阅 OpenSergo 流控规则,并注册数据源到 Sentinel 流控规则数据源中.FlowRuleManager.register2Property(openSergo.subscribeFlowRules());
我们将这个配置保存为 YAML 文件,然后通过 kubectl apply 到集群中。我们查看 Sentinel 日志目录(默认目录为 ~/logs/csp)下的 sentinel-record.log,可以看到规则已经成功下发到 Sentinel 侧。apiVersion: fault-tolerance.opensergo.io/v1alpha1kind: RateLimitStrategymetadata:name: rate-limit-foolabels:app: foo-appspec:metricType: RequestAmountlimitMode: Localthreshold: 2statDurationSeconds: 1---apiVersion: fault-tolerance.opensergo.io/v1alpha1kind: FaultToleranceRulemetadata:name: my-opensergo-rule-1labels:app: foo-appspec:targets:# 这里对应 Sentinel 的资源名,根据实际情况填写- targetResourceName: '/foo'strategies:- name: rate-limit-fookind: RateLimitStrategy
2022-10-26 14:26:59.390 INFO Subscribing OpenSergo base fault-tolerance rules for target <default, foo-app>2022-10-26 14:26:59.391 INFO Subscribing OpenSergo config for target: SubscribeKey{namespace='default', app='foo-app', kind=RATE_LIMIT_STRATEGY}2022-10-26 14:27:59.552 INFO [FlowRuleManager] Flow rules received: {/foo=[FlowRule{resource=/foo, limitApp=default, grade=1, count=2.0, strategy=0, refResource=null, controlBehavior=0, warmUpPeriodSec=10, maxQueueingTimeMs=500, clusterMode=false, clusterConfig=null, controller=com.alibaba.csp.sentinel.slots.block.flow.controller.DefaultController@4bbadef7}]}
流量防护与容错是微服务流量治理中的重要的一环,同时 OpenSergo 还提供更广范围、更多场景的微服务治理标准与最佳实践,包括流量路由、流量染色、微服务视角的数据库治理、日志治理等一系列的微服务治理能力与场景。 服务治理是微服务改造深入到一定阶段之后的必经之路,是将微服务做稳做好的关键。
同时我们也在与 CloudWeGo、Kratos、Spring Cloud Alibaba、Dubbo、ShardingSphere、Database Mesh 等社区共同建设 OpenSergo 微服务治理标准,将企业与社区中微服务治理的场景与最佳实践共同提取成标准规范,也欢迎更多社区与企业一起参与 OpenSergo 微服务治理标准的共建。
-
Sentinel 项目官网:
https://sentinelguard.io/zh-cn/ -
OpenSergo 项目官网:
https://opensergo.io/zh-cn/ -
MSE Sentinel 流量治理:
https://help.aliyun.com/document_detail/420631.html