Sentinel教程:02 快速开始

Sentinel 的使用可以分为两个部分:

  • 核心库(Java 客户端):不依赖任何框架/库,能够运行于 Java 8 及以上的版本的运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持(见 主流框架适配)。
  • 控制台(Dashboard):Dashboard 主要负责管理推送规则、监控、管理机器信息等。

本地运行 demo

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>tech.foolfish</groupId>
    <artifactId>my-sentinel-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-sentinel-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.8.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>tech.foolfish.demo.MySentinelDemoApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

代码

package tech.foolfish.demo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;

@SpringBootApplication
public class MySentinelDemoApplication {

	public static void main(String[] args) throws InterruptedException {
		SpringApplication.run(MySentinelDemoApplication.class, args);

		initFlowRules();
		while (true) {
			Thread.sleep(200);
			try (Entry entry = SphU.entry("HelloWorld")) {
				System.out.println("hello world");
			} catch (BlockException ex) {
				System.out.println("FlowException blocked!");
			}
		}
	}

	private static void initFlowRules() {
		List<FlowRule> rules = new ArrayList<>();
		FlowRule rule = new FlowRule();
		rule.setResource("HelloWorld");
		rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
		rule.setCount(4);
		rules.add(rule);
		FlowRuleManager.loadRules(rules);
	}

}

IDE Console日志


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.3.12.RELEASE)

2023-11-25 18:21:09.735  INFO 28727 --- [           main] t.f.demo.MySentinelDemoApplication       : Starting MySentinelDemoApplication on localhost.localdomain with PID 28727 (/home/xuej/Documents/workspace-sts/my-sentinel-demo/target/classes started by xuej in /home/xuej/Documents/workspace-sts/my-sentinel-demo)
2023-11-25 18:21:09.738  INFO 28727 --- [           main] t.f.demo.MySentinelDemoApplication       : No active profile set, falling back to default profiles: dev
2023-11-25 18:21:10.131  INFO 28727 --- [           main] t.f.demo.MySentinelDemoApplication       : Started MySentinelDemoApplication in 0.633 seconds (JVM running for 1.06)
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /home/xuej/logs/csp/
INFO: Sentinel log name use pid is: false
INFO: Sentinel log level is: INFO
hello world
hello world
hello world
hello world
hello world
FlowException blocked!
hello world
hello world
hello world
hello world
FlowException blocked!
hello world
hello world
hello world
hello world
FlowException blocked!

本地日志:~/logs/csp/

[xuej@localhost ~]$ cat ~/logs/csp/tech-foolfish-demo-MySentinelDemoApplication-metrics.log.2023-11-25
1700907670000|2023-11-25 18:21:10|HelloWorld|4|0|4|0|1|0|0|0
1700907671000|2023-11-25 18:21:11|HelloWorld|3|1|3|0|0|0|0|0
1700907672000|2023-11-25 18:21:12|HelloWorld|4|1|4|0|0|0|0|0