-
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>
The two filters are enabled by default. Once you add the dependency, the Dubbo services and methods will become protected resources in Sentinel, which can leverage Sentinel's flow control and guard ability when rules are configured. If you don't want to enable the filter, you can manually disable it. For example:
<!-- disable the Sentinel Dubbo consumer filter -->
<dubbo:consumer filter="-sentinel.dubbo.consumer.filter"/>
The guarded resource can be both service interface and service method:
- Service interface:resourceName format is
interfaceName
,e.g.com.alibaba.csp.sentinel.demo.dubbo.FooService
- Service method:resourceName format is
interfaceName:methodSignature
,e.g.com.alibaba.csp.sentinel.demo.dubbo.FooService:sayHello(java.lang.String)
For Sentinel's best practice in Dubbo, please refer to Sentinel: Dubbo 服务的流量哨兵.
For more details of Dubbo filter, see here.
Sentinel Spring Cloud Starter provides out-of-box integration with Sentinel for Spring Cloud applications and services.
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 provides out-of-box integration with Sentinel for Spring Boot applications. To enable flow control for web applications and Dubbo services, you can simply add the following dependency:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-boot-starter</artifactId>
<version>x.y.z</version>
</dependency>
All web requests (/*
) are under Sentinel's flow control by default. If you want to enable protection in some specific URL patterns, you can configure in the properties file:
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