What is a Sentinel

With the popularity of microservices, stability between services becomes increasingly important. Sentinel takes flow as the entry point to protect the stability of service from multiple dimensions such as flow control, fusing downgrading and system load protection.

The characteristics of the Sentinel

Rich application scenarios: Sentinel follows the core scenarios of Alibaba’s double Eleven traffic drive in the past 10 years, such as SEC killing (that is, sudden traffic is controlled within the range of system capacity), message peak-cutting and valley filling (for sudden large number of requests, you can configure flow control rules to gradually process these requests at a stable rate. In this way, the system load is too high due to traffic spike), cluster flow control, and real-time fusing of downstream unavailable applications

Complete real-time monitoring: Sentinel also provides real-time monitoring capabilities. From the console, you can see a summary of the performance of a single machine-by-second data, or even a cluster of less than 500 machines, for accessing the application

Extensive Open source ecosystem: Sentinel provides out-of-the-box integration modules with other open source frameworks/libraries, such as Spring Cloud, Dubbo, and gRPC. You can quickly access Sentinel by introducing the appropriate dependencies and simple configuration

Sophisticated SPI extension points: Sentinel provides an easy-to-use, sophisticated SPI extension interface. You can quickly customize the logic by implementing an extension interface. For example, customize rule management and adapt dynamic data sources

Key characteristics of Sentinel

The composition of Sentinel

  • Core library (Java client) : independent of any framework/library, can run in all Java runtime environments, and has good support for frameworks such as Dubbo/Spring Cloud
  • Console (Dashboard) : Developed based on Spring Boot, packaged can run directly, no additional application containers such as Tomcat

You can download the latest version of the console JAR package from the official GitHub Release page.

Start Note: JDK version 1.8 or later is required to start the Sentinel 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 is used to specify the Sentinel console port as 8080. Note: Starting with Sentinel 1.6.0, the Sentinel console introduces basic login functions. The default user name and password are Sentinel

authentication

You can set the following parameters

  • – Dsentinel. Dashboard. The auth. Username = sentinel is used to specify the console login user called sentinel
  • – Dsentinel. Dashboard. The auth. The password is used to specify the console login password = 123456 to 123456; If these two parameters are omitted, the default user and password are both sentinel
  • – Dserver. Servlet. The session. The timeout = 7200 is used to specify the Spring of the Boot server session expiration time, such as 7200 7200 seconds; 60m indicates 60 minutes. The default value is 30 minutes

Verify that the installation is successful

Visit http://localhost:8080/#/login through your browser

Sentinel client access

POM

  <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 </dependency>
Copy the code

Modified The configuration file Modified the support for Sentinel in the configuration file

spring:  application:
    # service name
    name: service-consumer
  cloud:
    nacos:
      discovery:
        # Service registryServer - addr: 192.168.141.132:8848 config:# Service configuration centerServer - addr: 192.168.141.132:8848# Fusing current limiting
    sentinel:
      transport:
        dashboard: localhost:8888

# Enable Feign support for Sentinel

feign:
  sentinel:
    enabled: true

server:
  # service port
  port: 8080

management:
  Endpoint check (health check)
  endpoints:
    web:
      exposure:
        include: "*"
Copy the code

Configure fuses

Write an implementation class for the Feign interface and add the @Component annotation

import org.springframework.stereotype.Component;
@Component
public class EchoServiceFallback implements EchoService {

    @Override
    public String echo(String string) {
        return "echo fallback"; }}Copy the code

Example Modify the Feign interface

Add the fallback attribute to the @feignClient interface to specify the fuse class

@FeignClient(value = "service-provider", fallback = EchoServiceFallback.class)public interface EchoService {

    @GetMapping(value = "/echo/{string}")
    String echo(@PathVariable("string") String string);

}
Copy the code

Test fusing

Start the service-consumer service and stop the service-provider service. Access the service using a browser.