Integrating Sentinel with Spring Boot for Real-time Limiting and Monitoring

Integrating Sentinel with Spring Boot for Real-time Limiting and Monitoring

As microservices become increasingly popular, ensuring the stability and reliability of services has become a top priority. In this article, we will explore how to integrate Sentinel, a powerful open-source flow control and protection system, with Spring Boot to achieve real-time limiting and monitoring.

Introduction to Sentinel

Sentinel is a flow control and protection system developed by Alibaba, which has been widely adopted in various scenarios, including spike traffic control, load shifting, and real-time fuse protection. Its rich features and widely used open-source ecology make it an ideal choice for integrating with Spring Boot.

Characteristics of Sentinel

Sentinel boasts the following characteristics:

  • Rich Scenarios: Sentinel has been used in nearly 10 years of Ali Baba’s core scenes, including spike traffic control, load shifting, and real-time fuse protection.
  • Complete Real-time Monitoring: Sentinel provides real-time monitoring capabilities, allowing you to view single-machine second-level data access applications in the console or even a summary of the operation of the cluster size of 500 or less.
  • Widely Used Open Source Ecology: Sentinel offers integration modules with other open-source frameworks and libraries out of the box, such as Spring Cloud, Dubbo, and gRPC.
  • Perfect SPI Extension Point: Sentinel provides easy-to-use SPI extension points, allowing you to achieve rapid custom logic, such as custom rules management and adaptive data sources.

Starting the Service Side of the Jar

To start the service side of the jar, you can download the release from the GitHub repository and run the following command:

java -jar sentinel-dashboard-1.6.0.jar

This will start the dashboard server on port 8080 by default. You can also add custom configuration to start the server:

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

Starting the Client

To start the client, you need to create a new Spring Boot project with the following pom.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.maimeng.baobanq</groupId>
    <artifactId>baobanserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>baobanserver</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Sentinel -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Note that the edition cited Spring Cloud Alibaba is 0.2.2, which is the most current version. If you are using Spring Boot 2.x, you should use 0.2.x, and if you are using Spring Boot 1.x, you should use 0.1.x.

Configuring the Client

To configure the client, you need to add the following configuration to the application.yml file:

spring:
  application:
    name: baobanserver
  cloud:
    sentinel:
      transport:
        dashboard:
          localhost: 8080

This will enable the client to connect to the dashboard server.

Creating a Controller

To create a controller, you can add the following code to the TestController class:

@RestController
public class TestController {
    @GetMapping(value = "/hello")
    public String hello() {
        return "Hello Sentinel";
    }
}

This will create a simple controller interface.

Starting the Client

To start the client, you can run the following command:

java -jar baobanserver.jar

This will start the client on port 8888.

Testing the Client

To test the client, you can access the hello interface by visiting http://localhost:8888/hello. You will see a response from the client.

Limiting the Interface

To limit the interface, you can add a rule to the dashboard server. You can create a new rule by clicking on the “Rules” tab in the dashboard server and clicking on the “Create Rule” button.

Conclusion

In this article, we have explored how to integrate Sentinel with Spring Boot for real-time limiting and monitoring. We have created a client and server configuration, created a controller, and tested the client. We have also limited the interface using a rule in the dashboard server.