What is Sentinel

Sentinel is an open source lightweight flow control product for distributed service architecture developed by Ali middleware team. It mainly takes traffic as the entry point and helps users protect the stability of services from multiple dimensions such as flow control, fuse downgrading and system load protection. Learn more about Sentinel at this address.

How to use Sentinel

Sentinel is divided into two parts: the client and the console.

  • The console is used to manage current limiting, release and monitor fusing rules.
  • The client is used to receive and execute the rules.

1. Download Sentinel console

The latest release is 1.4.0

Github.com/alibaba/Sen…

2. Run the Sentinel console

Note: JDK version 1.8 or later is required to start the Sentinel console.

Use the following command to start the console:

java -Dserver.port=8080 \
-Dcsp.sentinel.dashboard.server=localhost:8080 \
-Dproject.name=sentinel-dashboard  \
-jar sentinel-dashboard.jar
Copy the code

-dserver. port=8080 specifies the Sentinel console port as 8080.

Visit http://localhost:8080 to view console information.

3. Client Dubbo integration

Import the following modules (using Maven as an example) :

< the dependency > < groupId > com. Alibaba. CSP < / groupId > < artifactId > sentinel dubbo - adapter < / artifactId > < version > 1.4.0 < / version >  </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-transport-simple-http</artifactId> The < version > 1.4.0 < / version > < / dependency >Copy the code

Add JVM parameters at startup

-Djava.net.preferIPv4Stack=true \
-Dcsp.sentinel.api.port=8720 \
-Dcsp.sentinel.dashboard.server=localhost:8787 \
-Dproject.name=example-customer
Copy the code

Boot parameters:

  • – dcsp.sentinel.api. port Specifies the HTTP port for receiving data recommendations
  • – Dcsp. Sentinel. Dashboard. Server console address specified
  • – dproject. name Specifies the name of the project displayed on the console

For more information, see startup Configuration items.

Then you can happily open the console to limit the flow of your service, and the circuit breaker is downgraded.

Third, existing problems

  1. At present, rules are pushed in the form of HTTP interface for data processing. When publishing rules, it is necessary to traverse all clients and push data in the form of HTTP. The problem with this method is that as the number of service deployment increases, publishing rules becomes slower and more difficult.
  2. The statistics of fusible limit of Sentinel is based on the number of anomalies, and service anomalies need to be excluded in the real application process.
  3. The current practice of monitoring data is to iterate through all clients using HTTP for batch remote reading and storage, and real-time monitoring can only view the metric data within 5 minutes.
  4. The way Sentinel starts injection parameters is primitive.

Four, how to transform

1. To reform the data push of Dashboard, we can transfer data to the configuration center and use the configuration center to broadcast data push.

  • Before transformation: The client exposed a specific port using the Sentinel-transport-simple-HTTP module. Sentinel Dashboard pushed data in the form of HTTP. After receiving the data, the client saved the rules in the local memory.
  • After transformation: The client registers in the relevant registry, and the Sentinel Dashboard console pushes configuration information to the configuration center, such as NACOS and ZooKeeper, and the configuration center pushes configuration information.
    After transforming

2. About the Sentinel business exceptions, can consider to adopt the method of similar to the Hystrix, HystrixBadRequestException Hystrix recognized as it is a question of consumer oneself, rather than the service provider is not stable, namely we often say business exceptions are not fuse.

Method 1: Package all exceptions as a unified structure, and set the exception status code. For example, the value of service exceptions is 400, and the value of service exceptions is 500.

Public class Result<T> {private int code */ private int code; /** * message */ private String message; /** * data */ private T result; // TODO ignores get,set} Entry entry = null; try { entry = SphU.entry(resourceName, entryType, 1, pjp.getArgs()); Object result = pjp.proceed(); // Core judgmentif (result instanceof Result && ((Result) result).getCode() == 500) {
        Tracer.trace(new RuntimeException(((Result) result).getMessage()));
    }
    return result;
} catch (BlockException ex) {
    return handleBlockException(pjp, annotation, ex);
} catch (Throwable ex) {
    Tracer.trace(ex);
    throw ex;
} finally {
    if (entry != null) {
        entry.exit();
    }
}
Copy the code

Method 2: Define a BussinessException service exception in the form of an exception throw.

Entry entry = null;
try {
    entry = SphU.entry(resourceName, entryType, 1, pjp.getArgs());
    return  pjp.proceed();
} catch (BlockException ex) {
    returnhandleBlockException(pjp, annotation, ex); } catch (BussinessException ex) {// throw ex; } catch (Throwable ex) { Tracer.trace(ex); throw ex; } finally {if (entry != null) {
        entry.exit();
    }
}
Copy the code

As to which way to carry out the transformation, it is a matter of opinion.

3. For Sentinel monitoring, CrateDB + Grafana/or data landing InfluxDB can be considered.

Related links refer to:

  • Sentinel integrated monitoring solution CrateDB + Grafana
  • Persistent Monitoring data in Sentinel console

4. The method of starting and injecting parameters of Sentinel is too primitive, so the spring-boot-starter method can be considered and automatic configuration can be adopted.

V. Project recommendation

The owner of the building himself modified a version, so far has achieved the following functions:

  1. The new Filter of Dubbo wraps the exception as a uniform return body, defines the exception status code as a value >=500 (corresponding to HttpStatus), and changes the SentinelResourceAspect implementation to determine whether the returned status code is >=500. If so, fuse statistics are performed.
  2. Sentinel-dubo-starter is added for automatic configuration.
  3. Sentinel Dashboard revamp: Console Rules -> Configuration Center -> Client.

Welcome to start, if you have any questions, welcome to point out, common progress:

  • Gitee.com/imethsoft/s…
  • Github.com/pwh19920920…