Sentinel is compatible with various open source frameworks

Sentinel is an excellent flow control framework. In actual production, our projects will use frameworks such as Spring boot and Dubbo. If flow control is implemented in these frameworks, what configuration is the best solution? This article The article focuses on analyzing how Sentinel adapts to various open source frameworks.

Web Servlet

Sentinel provides native integration for Servlets, which can control the flow of web requests. The following modules need to be introduced when using it (take Maven as an example):

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-web-servlet</artifactId>
    <version>x.y.z</version>
</dependency>

Add configuration in Spring and define Bean:

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean sentinelFilterRegistration() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CommonFilter());
        registration.addUrlPatterns("/*");
        registration.setName("sentinelFilter");
        registration.setOrder(1);

        return registration;
    }
}

In fact, it is intercepted through the Filter mechanism, and the class that specifically implements flow control is CommonFilter

1) Clean the URL

Users need to implement the UrlCleaner interface by themselves to clean resources (for example, put URLs satisfying /foo/:id under /foo/* resources), and then register them in WebCallbackManager. Otherwise, the number of resources will be too large, and the rules for excess resources will not take effect when the resource number threshold (currently 6000) is exceeded.

// Clean and unify the URL.
// For REST APIs, you have to clean the URL (e.g. `/foo/1` and `/foo/2` -> `/foo/:id`), or
// the amount of context and resources will exceed the threshold.
UrlCleaner urlCleaner = WebCallbackManager.getUrlCleaner();
if (urlCleaner != null) {
    target = urlCleaner.clean(target);
}

Web.png

In the adaptation Web Servlet, it supports flow limitation according to the source. The resource is the processed URL, and the method name can also be spliced. The defined resource type is Web.

Dubbo

In Dubbo, it can be divided into Service Provider and Service Consumer. The focus of the two dimensions is a little different.

Service Provider

In order to protect the Provider from being dragged down by the surge of traffic and affecting the stability, you can configure the QPS mode flow limit for the Provider, so that when the number of requests per second exceeds the set threshold, many requests will be automatically rejected. The current limiting granularity can be two kinds of granularity: service interface and service method.

The specific implementation is based on Dubbo Filter and SPI mechanism.

provider.png

The defined resource type is RPC.

Service Consumer

Service Consumer 作为客户端去调用远程服务。每一个服务都可能会依赖几个下游服务,若某个服务 A 依赖的下游服务 B 出现了不稳定的情况,服务 A 请求 服务 B 的响应时间变长,从而服务 A 调用服务 B 的线程就会产生堆积,最终可能耗尽服务 A 的线程数。我们通过用并发线程数来控制对下游服务 B 的访问,来保证下游服务不可靠的时候,不会拖垮服务自身。基于这种场景,推荐给 Consumer 配置线程数模式的限流,来保证自身不被不稳定服务所影响。

代码基本与 Provider一致,Entry方向为 EntryType.OUT。

gRPC

在使用 Sentinel gRPC Adapter 时,只需要将对应的 Interceptor 注册至对应的客户端或服务端中。其中客户端的示例如下:

public class ServiceClient {

    private final ManagedChannel channel;

    ServiceClient(String host, int port) {
        this.channel = ManagedChannelBuilder.forAddress(host, port)
            .intercept(new SentinelGrpcClientInterceptor()) // 在此处注册拦截器
            .build();
        // 在此处初始化客户端 stub 类
    }
}

服务端的示例如下:

import io.grpc.Server;

Server server = ServerBuilder.forPort(port)
     .addService(new MyServiceImpl()) // 添加自己的服务实现
     .intercept(new SentinelGrpcServerInterceptor()) // 在此处注册拦截器
     .build();

grpc.png

Guess you like

Origin juejin.im/post/7235435351049076792