Contents

Sentinel provides Servlet filter integration to enable flow control for web requests. Add the following dependency in pom.xml (if you are using Maven):

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-web-servlet</artifactId>
  4. <version>x.y.z</version>
  5. </dependency>

To use the filter, you can simply configure your web.xml with:

  1. <filter>
  2. <filter-name>SentinelCommonFilter</filter-name>
  3. <filter-class>com.alibaba.csp.sentinel.adapter.servlet.CommonFilter</filter-class>
  4. </filter>
  5.  
  6. <filter-mapping>
  7. <filter-name>SentinelCommonFilter</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </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 ownblock handler (the UrlBlockHandler interface) and register to WebCallbackManager.

Dubbo

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):

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-dubbo-adapter</artifactId>
  4. <version>x.y.z</version>
  5. </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:

  1. <!-- disable the Sentinel Dubbo consumer filter -->
  2. <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)
    Since version 0.1.1, Sentinel Dubbo Adapter supports global fallback configuration.The global fallback will handle exceptions and give replacement result when blocked byflow control, degrade or system load protection. You can implement your own DubboFallback interfaceand then register to DubboFallbackRegistry. If no fallback is configured, Sentinel will wrap the BlockExceptionthen directly throw it out. Besides, we can also leverage Dubbo mock mechanism to provide fallback implementation of degraded Dubbo services.

For Sentinel's best practice in Dubbo, please refer to Sentinel: the flow sentinel of Dubbo.

For more details of Dubbo filter, see here.

Spring Cloud

Sentinel Spring Cloud Starter provides out-of-box integration with Sentinel for Spring Cloud applications and services.

Please refer to Spring Cloud Alibaba.

gRPC

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):

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-grpc-adapter</artifactId>
  4. <version>x.y.z</version>
  5. </dependency>

To use Sentinel gRPC Adapter, you simply need to register the Interceptor to your client or server. The client sample:

  1. public class ServiceClient {
  2.  
  3. private final ManagedChannel channel;
  4.  
  5. ServiceClient(String host, int port) {
  6. this.channel = ManagedChannelBuilder.forAddress(host, port)
  7. .intercept(new SentinelGrpcClientInterceptor()) // Add the client interceptor.
  8. .build();
  9. // Init your stub here.
  10. }
  11. }

The server sample;

  1. import io.grpc.Server;
  2.  
  3. Server server = ServerBuilder.forPort(port)
  4. .addService(new MyServiceImpl()) // Add your service.
  5. .intercept(new SentinelGrpcServerInterceptor()) // Add the server interceptor.
  6. .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.

Apache RocketMQ

In Apache RocketMQ, when message consumers are consuming messages, there may a sudden inflow of messages, whether using pull or push mode. If all the messages were handled at this time, it would be likely to cause the system to be overloaded and then affect stability. However, in fact, there may be no messages coming within a few seconds. If redundant messages are directly discarded, the system's ability to process the message is not fully utilized. We hope that the sudden inflow of messages can be spread over a period of time, so that the system load can be kept on the stable level while processing as many messages as possible, thus achieving the effect of “shaving the peaks and filling the valley”.

shaving the peaks and filling the valley

Sentinel provides a feature for this kind of scenario: Rate Limiter, which can spread a large number of sudden request inflow in a uniform rate manner, let the request pass at a fixed interval. It is often used to process burst requests instead of rejecting them. This avoids traffic spurs causing system overloaded. Moreover, the pending requests will be queued and processed one by one. When the request is estimated to exceed the maximum queuing timeout, it will be rejected immediately.

For example, we configure the rule with uniform rate limiting mode and QPS count is 5, which indicates messages are consumed at fixed interval (200 ms) and pending messages will wait (virtual queue). We also set the maximum queuing timeout is 5s, then all requests estimated to exceed the timeout will be rejected immediately.

Uniform rate

Developer can set rules to different groups and topics (e.g. resouceName is groupName:topicName), set the control behaviour to RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER, then the messages can be handled at a fixed rate. Here is an example for the rule:

  1. private void initFlowControlRule() {
  2. FlowRule rule = new FlowRule();
  3. rule.setResource(KEY); // resource name can be `groupName:topicName`
  4. rule.setCount(5); // Indicates the interval between two adjacent requests is 200 ms.
  5. rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
  6. rule.setLimitApp("default");
  7.  
  8. // Enable rate limiting (uniform). This can ensure fixed intervals between two adjacent calls.
  9. // In this example, intervals between two incoming calls (message consumption) will be 200 ms constantly.
  10. rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
  11. // If more requests are coming, they'll be put into the waiting queue.
  12. // In this example, the max timeout is 5s.
  13. rule.setMaxQueueingTimeMs(5 * 1000);
  14. FlowRuleManager.loadRules(Collections.singletonList(rule));
  15. }

When using Sentinel with RocketMQ Client, developers should manually wrap their code of handling messages with Sentinel API. Here is a sample for pull consumer: Sentinel RocketMQ Demo.

原文: https://github.com/alibaba/Sentinel/wiki/Adapters-to-Popular-Framework