Skip to content

Commit 84ece9c

Browse files
committed
feat: Sentinel 示例
1 parent a44e5f4 commit 84ece9c

File tree

10 files changed

+274
-19
lines changed

10 files changed

+274
-19
lines changed

codes/distributed/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111

1212
<modules>
1313
<module>trace</module>
14+
<module>ratelimit</module>
1415
</modules>
1516
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>io.github.dunwu.spring</groupId>
7+
<artifactId>spring-distributed-ratelimit</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>pom</packaging>
10+
<name>Spring::分布式::流量控制</name>
11+
12+
<modules>
13+
<module>sentinel</module>
14+
</modules>
15+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.7.18</version>
10+
</parent>
11+
12+
<groupId>io.github.dunwu.spring</groupId>
13+
<artifactId>spring-distributed-ratelimit-sentinel</artifactId>
14+
<version>1.0.0</version>
15+
<name>Spring::分布式::流量控制::Sentinel</name>
16+
17+
<dependencies>
18+
<!-- Spring Web 的 Spring Boot 启动包,完成 Web 相关的自动化配置 -->
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<!-- Spring Test 的 Spring Boot 启动包,提供了一些便利的测试工具集 -->
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-test</artifactId>
27+
<scope>test</scope>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>com.alibaba.csp</groupId>
32+
<artifactId>sentinel-core</artifactId>
33+
<version>1.8.7</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.alibaba.csp</groupId>
37+
<artifactId>sentinel-transport-simple-http</artifactId>
38+
<version>1.8.7</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.projectlombok</groupId>
43+
<artifactId>lombok</artifactId>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
</project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package example.spring.ratelimit.sentinel;
2+
3+
import com.alibaba.csp.sentinel.Entry;
4+
import com.alibaba.csp.sentinel.SphO;
5+
import com.alibaba.csp.sentinel.SphU;
6+
import com.alibaba.csp.sentinel.annotation.SentinelResource;
7+
import com.alibaba.csp.sentinel.slots.block.BlockException;
8+
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
9+
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
10+
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
11+
import lombok.extern.slf4j.Slf4j;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import javax.annotation.PostConstruct;
18+
19+
/**
20+
* 限流示例 Http 接口
21+
*
22+
* @author <a href="mailto:[email protected]">Zhang Peng</a>
23+
* @date 2024-01-30
24+
*/
25+
@Slf4j
26+
@RestController
27+
public class RateLimitController {
28+
29+
/**
30+
* 抛出异常的方式定义资源并限流
31+
*/
32+
@GetMapping("/limit1")
33+
public String limit1() {
34+
// 1.5.0 版本开始可以利用 try-with-resources 特性
35+
// 资源名可使用任意有业务语义的字符串,比如方法名、接口名或其它可唯一标识的字符串。
36+
try (Entry entry = SphU.entry("limit1")) {
37+
// 被保护的业务逻辑
38+
log.info("limit1 -> 请求通过");
39+
return "ok";
40+
} catch (BlockException e) {
41+
// 资源访问阻止,被限流或被降级
42+
// 在此处进行相应的处理操作
43+
log.error("limit1 -> 请求限流", e);
44+
return "failed";
45+
}
46+
}
47+
48+
/**
49+
* 返回布尔值方式定义资源并限流
50+
*/
51+
@GetMapping("/limit2")
52+
public String limit2() {
53+
// 资源名可使用任意有业务语义的字符串
54+
if (SphO.entry("limit2")) {
55+
// 务必保证finally会被执行
56+
try {
57+
// 被保护的业务逻辑
58+
log.info("limit2 -> 请求通过");
59+
return "ok";
60+
} finally {
61+
SphO.exit();
62+
}
63+
} else {
64+
// 资源访问阻止,被限流或被降级
65+
// 进行相应的处理操作
66+
log.error("limit2 -> 请求限流");
67+
return "failed";
68+
}
69+
}
70+
71+
/**
72+
* 注解方式定义资源并限流
73+
*/
74+
@GetMapping("/limit3")
75+
@SentinelResource(value = "limit3")
76+
public String limit3() {
77+
try {
78+
log.info("limit3 -> 请求通过");
79+
return "ok";
80+
} catch (Exception e) {
81+
log.error("limit3 -> 请求限流", e);
82+
return "failed";
83+
}
84+
}
85+
86+
/**
87+
* 初始化流控规则
88+
*/
89+
@PostConstruct
90+
public void initFlowRules() {
91+
List<FlowRule> rules = new ArrayList<>();
92+
FlowRule rule = new FlowRule();
93+
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
94+
// 设置限流资源
95+
rule.setResource("limit1");
96+
// 设置 QPS
97+
rule.setCount(2);
98+
rules.add(rule);
99+
FlowRuleManager.loadRules(rules);
100+
}
101+
102+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package example.spring.ratelimit.sentinel;
2+
3+
import com.alibaba.csp.sentinel.Entry;
4+
import com.alibaba.csp.sentinel.SphU;
5+
import com.alibaba.csp.sentinel.slots.block.BlockException;
6+
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
7+
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
8+
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* @author <a href="mailto:[email protected]">Zhang Peng</a>
15+
* @date 2024-01-30
16+
*/
17+
public class SentinelDemo {
18+
19+
public static void main(String[] args) {
20+
// 定义规则
21+
initFlowRules();
22+
23+
// 定义资源
24+
while (true) {
25+
// 1.5.0 版本开始可以利用 try-with-resources 特性
26+
// 资源名可使用任意有业务语义的字符串,比如方法名、接口名或其它可唯一标识的字符串。
27+
try (Entry entry = SphU.entry("HelloWorld")) {
28+
// 被保护的业务逻辑
29+
System.out.println("hello world");
30+
} catch (BlockException ex) {
31+
// 资源访问阻止,被限流或被降级
32+
// 在此处进行相应的处理操作
33+
System.err.println("blocked!");
34+
}
35+
}
36+
}
37+
38+
private static void initFlowRules() {
39+
List<FlowRule> rules = new ArrayList<>();
40+
FlowRule rule = new FlowRule();
41+
rule.setResource("HelloWorld");
42+
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
43+
// Set limit QPS to 20.
44+
rule.setCount(20);
45+
rules.add(rule);
46+
FlowRuleManager.loadRules(rules);
47+
}
48+
49+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package example.spring.ratelimit.sentinel;
2+
3+
import org.springframework.boot.CommandLineRunner;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
/**
8+
* 启动类
9+
*
10+
* @author <a href="mailto:[email protected]">Zhang Peng</a>
11+
* @date 2024-01-30
12+
*/
13+
@SpringBootApplication
14+
public class SpringBootSentinelApplication implements CommandLineRunner {
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(SpringBootSentinelApplication.class, args);
18+
}
19+
20+
@Override
21+
public void run(String... args) throws Exception {
22+
}
23+
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
${AnsiColor.BRIGHT_YELLOW}${AnsiStyle.BOLD}
2+
________ ___ ___ ________ ___ __ ___ ___
3+
|\ ___ \|\ \|\ \|\ ___ \|\ \ |\ \|\ \|\ \
4+
\ \ \_|\ \ \ \\\ \ \ \\ \ \ \ \ \ \ \ \ \\\ \
5+
\ \ \ \\ \ \ \\\ \ \ \\ \ \ \ \ __\ \ \ \ \\\ \
6+
\ \ \_\\ \ \ \\\ \ \ \\ \ \ \ \|\__\_\ \ \ \\\ \
7+
\ \_______\ \_______\ \__\\ \__\ \____________\ \_______\
8+
\|_______|\|_______|\|__| \|__|\|____________|\|_______|
9+
${AnsiColor.CYAN}${AnsiStyle.BOLD}
10+
:: Java :: (v${java.version})
11+
:: Spring Boot :: (v${spring-boot.version})
12+
${AnsiStyle.NORMAL}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<configuration scan="true" scanPeriod="60 seconds" debug="false">
3+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%boldYellow(%thread)] [%highlight(%-5level)] %boldGreen(%c{36}.%M) - %boldBlue(%m%n)
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<logger name="example.spring" level="INFO" />
11+
12+
<root level="WARN">
13+
<appender-ref ref="CONSOLE" />
14+
</root>
15+
</configuration>
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package example.spring.web.helloworld;
22

3-
import example.spring.web.helloworld.entity.Weather;
43
import lombok.extern.slf4j.Slf4j;
54
import org.springframework.web.bind.annotation.RequestMapping;
65
import org.springframework.web.bind.annotation.RestController;
@@ -9,27 +8,9 @@
98
@RestController
109
public class HelloController {
1110

12-
private final WeatherService weatherService;
13-
14-
public HelloController(WeatherService weatherService) {
15-
this.weatherService = weatherService;
16-
}
17-
1811
@RequestMapping("hello")
1912
public String index() {
2013
return "Hello World";
2114
}
2215

23-
@RequestMapping("weather")
24-
public Weather weather() {
25-
log.info("查询南京市天气:");
26-
Weather weather = weatherService.getWeather("101190101");
27-
if (weather == null) {
28-
log.info("未查到数据!");
29-
return null;
30-
}
31-
weatherService.printBasicWeatherInfo(weather);
32-
return weather;
33-
}
34-
3516
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
<module>codes/web</module>
1717
<module>codes/integration</module>
1818
<module>codes/security</module>
19+
<module>codes/distributed</module>
1920
</modules>
2021
</project>

0 commit comments

Comments
 (0)