Skip to content

Commit beae67a

Browse files
committed
Update Sentinel annotation AOP demo
Signed-off-by: Eric Zhao <[email protected]>
1 parent df1a94b commit beae67a

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

sentinel-demo/sentinel-demo-annotation-spring-aop/src/main/java/com/alibaba/csp/sentinel/demo/annotation/aop/controller/DemoController.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import org.springframework.beans.factory.annotation.Autowired;
2121
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.PathVariable;
23+
import org.springframework.web.bind.annotation.RequestParam;
2224
import org.springframework.web.bind.annotation.RestController;
2325

2426
/**
@@ -31,8 +33,16 @@ public class DemoController {
3133
private TestService service;
3234

3335
@GetMapping("/foo")
34-
public String foo() throws Exception {
36+
public String apiFoo(@RequestParam(required = false) Long t) throws Exception {
37+
if (t == null) {
38+
t = System.currentTimeMillis();
39+
}
3540
service.test();
36-
return service.hello(System.currentTimeMillis());
41+
return service.hello(t);
42+
}
43+
44+
@GetMapping("/baz/{name}")
45+
public String apiBaz(@PathVariable("name") String name) {
46+
return service.helloAnother(name);
3747
}
3848
}

sentinel-demo/sentinel-demo-annotation-spring-aop/src/main/java/com/alibaba/csp/sentinel/demo/annotation/aop/service/ExceptionUtil.java

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
public final class ExceptionUtil {
2424

2525
public static void handleException(BlockException ex) {
26+
// Handler method that handles BlockException when blocked.
27+
// The method parameter list should match original method, with the last additional
28+
// parameter with type BlockException. The return type should be same as the original method.
29+
// The block handler method should be located in the same class with original method by default.
30+
// If you want to use method in other classes, you can set the blockHandlerClass
31+
// with corresponding Class (Note the method in other classes must be static).
2632
System.out.println("Oops: " + ex.getClass().getCanonicalName());
2733
}
2834
}

sentinel-demo/sentinel-demo-annotation-spring-aop/src/main/java/com/alibaba/csp/sentinel/demo/annotation/aop/service/TestService.java

+2
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public interface TestService {
2323
void test();
2424

2525
String hello(long s);
26+
27+
String helloAnother(String name);
2628
}

sentinel-demo/sentinel-demo-annotation-spring-aop/src/main/java/com/alibaba/csp/sentinel/demo/annotation/aop/service/TestServiceImpl.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,35 @@ public void test() {
3333
}
3434

3535
@Override
36-
@SentinelResource(value = "hello", blockHandler = "exceptionHandler")
36+
@SentinelResource(value = "hello", fallback = "helloFallback")
3737
public String hello(long s) {
38+
if (s < 0) {
39+
throw new IllegalArgumentException("invalid arg");
40+
}
3841
return String.format("Hello at %d", s);
3942
}
4043

41-
public String exceptionHandler(long s, BlockException ex) {
44+
@Override
45+
@SentinelResource(value = "helloAnother", defaultFallback = "defaultFallback",
46+
exceptionsToIgnore = {IllegalStateException.class})
47+
public String helloAnother(String name) {
48+
if (name == null || "bad".equals(name)) {
49+
throw new IllegalArgumentException("oops");
50+
}
51+
if ("foo".equals(name)) {
52+
throw new IllegalStateException("oops");
53+
}
54+
return "Hello, " + name;
55+
}
56+
57+
public String helloFallback(long s, Throwable ex) {
4258
// Do some log here.
4359
ex.printStackTrace();
4460
return "Oops, error occurred at " + s;
4561
}
62+
63+
public String defaultFallback() {
64+
System.out.println("Go to default fallback");
65+
return "default_fallback";
66+
}
4667
}

0 commit comments

Comments
 (0)