-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Adapters to Popular Framework
Sentinel provides Servlet filter integration to enable flow control for web requests. Add the following dependency in `pom.xml (if you are using Maven):
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-web-servlet</artifactId>
<version>x.y.z</version>
</dependency>
To use the filter, you can simply configure your web.xml
with:
<filter>
<filter-name>SentinelCommonFilter</filter-name>
<filter-class>com.alibaba.csp.sentinel.adapter.servlet.CommonFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SentinelCommonFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
When a request is blocked, Sentinel servlet filter will give a default page indicating the request blocked.
If customized block page is set (via WebServletConfig.setBlockPage(blockPage)
method),
the filter will redirect the request to provided URL. You can also implement your own
block handler (the UrlBlockHandler
interface) and register to WebCallbackManager
.
Sentinel Dubbo Adapter provides service consumer filter and provider filter for Dubbo services. Add the following dependency in `pom.xml (if you are using Maven):
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-dubbo-adapter</artifactId>
<version>x.y.z</version>
</dependency>
引入此依赖后,Dubbo 的服务接口和方法(包括调用端和服务端)就会成为 Sentinel 中的资源,在配置了规则后就可以自动享受到 Sentinel 的防护能力。若不希望开启 Sentinel Dubbo Adapter 中的某个 Filter,可以手动关闭对应的 Filter,比如:
<!-- disable the Sentinel Dubbo consumer filter -->
<dubbo:consumer filter="-sentinel.dubbo.consumer.filter"/>
限流粒度可以是服务接口和服务方法两种粒度:
- 服务接口:resourceName 为
接口全限定名
,如com.alibaba.csp.sentinel.demo.dubbo.FooService
- 服务方法:resourceName 为
接口全限定名:方法签名
,如com.alibaba.csp.sentinel.demo.dubbo.FooService:sayHello(java.lang.String)
有关 Sentinel 在 Dubbo 中的最佳实践,请参考 Sentinel: Dubbo 服务的流量哨兵。
关于 Dubbo Filter 的更多信息,请参考 Dubbo Filter 文档。
Please refer to Spring Cloud Alibaba。
Sentinel provides integration with gRPC Java. Sentinel gRPC Adapter provides client and server interceptor for gRPC services. Add the following dependency in `pom.xml (if you are using Maven):
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-grpc-adapter</artifactId>
<version>x.y.z</version>
</dependency>
To use Sentinel gRPC Adapter, you simply need to register the Interceptor
to your client or server. The client sample:
public class ServiceClient {
private final ManagedChannel channel;
ServiceClient(String host, int port) {
this.channel = ManagedChannelBuilder.forAddress(host, port)
.intercept(new SentinelGrpcClientInterceptor()) // Add the client interceptor.
.build();
// Init your stub here.
}
}
The server sample;
import io.grpc.Server;
Server server = ServerBuilder.forPort(port)
.addService(new MyServiceImpl()) // Add your service.
.intercept(new SentinelGrpcServerInterceptor()) // Add the server interceptor.
.build();
Note that currently the interceptor only supports unary methods in gRPC. In some circumstances (e.g. asynchronous call), the RT metrics might not be accurate.
在 RocketMQ 中,当消费者去消费消息的时候,无论是通过 pull 的方式还是 push 的方式,每次收到的消息可能都是批量的多个消息。即使配置了 QPS 模式的限流,如果消费者一瞬间收到了非常多的消息并且同时处理的话,很可能会出现处理不过来导致不稳定的情况。Sentinel 专门为这种场景提供了匀速器的特性,可以把突然到来的大量请求以匀速的形式均摊,以固定的间隔时间让请求通过,预计超过超时时长的请求将直接拒绝,从而避免流量突刺造成系统负载过高。
RocketMQ 用户可以根据不同的 group 和不同的 topic 分别设置限流规则,限流控制模式设置为匀速器模式(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER
),比如:
private void initFlowControlRule() {
FlowRule rule = new FlowRule();
rule.setResource(KEY); // 对应的 key 为 `groupName:topicName`
// Max QPS is 2.
rule.setCount(2);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setLimitApp("default");
// 匀速器模式下,设置了 QPS 为 2,则请求每 500 ms 允许通过 1 个
rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
// 如果更多的请求到达,这些请求会被置于等待队列中。等待队列有一个 timeout,如果请求预计的等待时间超过这个时间会直接被 block
// 在这里,timeout 为 10s
rule.setMaxQueueingTimeMs(10 * 1000);
FlowRuleManager.loadRules(Collections.singletonList(rule));
}
结合 RocketMQ Client 使用 Sentinel 时,用户需要在处理消息时手动埋点。详情请见 Sentinel RocketMQ Demo。
Sentinel Spring Boot Starter 为使用 Spring Boot 的开发者提供自动化的整合,只要引入以下依赖即自动开启对 Web 应用和 Dubbo 服务的流量控制,享受 Sentinel 的流量防护和监控能力:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-boot-starter</artifactId>
<version>x.y.z</version>
</dependency>
默认情况下 Sentinel 会对所有的 Web 请求进行流量控制(/*
)。若希望配置对指定的 URL 模式生效,可以在 Properties 文件中进行配置:
spring.sentinel.servletFilter.urlPatterns=/foo
-
文档
-
Documents
- Read Me
- Introduction
- How to Use
- How it Works
- Flow Control
- Parameter Flow Control
- Cluster Flow Control
- API Gateway Flow Control
- Circuit Breaking
- Adaptive System Protection
- Metrics
- General Configuration
- Dynamic Rule Configuration
- Dashboard
- Integrations with open-source frameworks
- Contribution Guideline