Skip to content

Commit 6a4e8dc

Browse files
committed
Spring Cloud Alibaba基础教程:@SentinelResource注解使用详解
1 parent ea1e412 commit 6a4e8dc

File tree

7 files changed

+167
-1
lines changed

7 files changed

+167
-1
lines changed

4-Finchley/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
- [Spring Cloud Alibaba基础教程:Sentinel使用Apollo存储规则](http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-2/)
5252
- [Spring Cloud Alibaba基础教程:Sentinel Dashboard中修改规则同步到Apollo](http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-3/)
5353
- [Spring Cloud Alibaba基础教程:Sentinel Dashboard中修改规则同步到Nacos](http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-4/)
54-
54+
- [Spring Cloud Alibaba基础教程:@SentinelResource注解使用详解](http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-5/)
5555

5656
##### Spring Cloud Stream专题补充
5757

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>com.didispace</groupId>
7+
<artifactId>alibaba-sentinel-annotation</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.0.5.RELEASE</version>
15+
<relativePath/> <!-- lookup parent from repository -->
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<java.version>1.8</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.projectlombok</groupId>
36+
<artifactId>lombok</artifactId>
37+
<version>1.18.2</version>
38+
<optional>true</optional>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-test</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<dependencyManagement>
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.springframework.cloud</groupId>
51+
<artifactId>spring-cloud-dependencies</artifactId>
52+
<version>Finchley.SR1</version>
53+
<type>pom</type>
54+
<scope>import</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.cloud</groupId>
58+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
59+
<version>0.2.2.RELEASE</version>
60+
<type>pom</type>
61+
<scope>import</scope>
62+
</dependency>
63+
</dependencies>
64+
</dependencyManagement>
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-maven-plugin</artifactId>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.didispace.alibaba.sentinel;
2+
3+
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
8+
9+
@SpringBootApplication
10+
public class TestApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(TestApplication.class, args);
14+
}
15+
16+
@Bean
17+
public SentinelResourceAspect sentinelResourceAspect() {
18+
return new SentinelResourceAspect();
19+
}
20+
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.didispace.alibaba.sentinel.service;
2+
3+
import com.alibaba.csp.sentinel.annotation.SentinelResource;
4+
import com.alibaba.csp.sentinel.slots.block.BlockException;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.stereotype.Service;
7+
8+
9+
@Slf4j
10+
@Service
11+
public class TestService {
12+
13+
// 限流与阻塞处理
14+
@SentinelResource(value = "doSomeThing", blockHandler = "exceptionHandler")
15+
public void doSomeThing(String str) {
16+
log.info(str);
17+
}
18+
19+
public void exceptionHandler(String str, BlockException ex) {
20+
log.error("blockHandler:" + str, ex);
21+
}
22+
23+
// 熔断与降级处理
24+
@SentinelResource(value = "doSomeThing2", fallback = "fallbackHandler")
25+
public void doSomeThing2(String str) {
26+
log.info(str);
27+
throw new RuntimeException("发生异常");
28+
}
29+
30+
public void fallbackHandler(String str) {
31+
log.error("fallbackHandler:" + str);
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.didispace.alibaba.sentinel.web;
2+
3+
import com.didispace.alibaba.sentinel.service.TestService;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import java.util.Date;
10+
11+
@Slf4j
12+
@RestController
13+
public class TestController {
14+
15+
@Autowired
16+
private TestService testService;
17+
18+
@GetMapping("/hello")
19+
public String hello() {
20+
testService.doSomeThing("hello " + new Date());
21+
return "didispace.com";
22+
}
23+
24+
@GetMapping("/hello2")
25+
public String hello2() {
26+
testService.doSomeThing2("hello2 " + new Date());
27+
return "didispace.com";
28+
}
29+
30+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring.application.name=alibaba-sentinel-annotation
2+
server.port=8001
3+
4+
# sentinel dashboard
5+
spring.cloud.sentinel.transport.dashboard=localhost:8080

4-Finchley/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<module>alibaba-sentinel-datasource-apollo</module> <!-- 使用Apollo存储规则 -->
2424
<module>alibaba-sentinel-dashboard-apollo</module> <!-- 使用Apollo存储规则的Dashboard -->
2525
<module>alibaba-sentinel-dashboard-nacos</module> <!-- 使用Nacos存储规则的Dashboard -->
26+
<module>alibaba-sentinel-annotation</module> <!-- @SentinelResource注解的使用 -->
2627

2728
<!--spring cloud stream专题-->
2829
<module>stream-consumer-self</module> <!-- 消费自己的消息 -->

0 commit comments

Comments
 (0)