Skip to content

Commit 959a476

Browse files
committed
add setError
1 parent 4a6a208 commit 959a476

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

mse-simple-demo/C/src/main/java/com/alibabacloud/mse/demo/c/CController.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.web.bind.annotation.RestController;
2020

2121
import javax.annotation.PostConstruct;
22+
import java.io.IOException;
2223
import java.util.Random;
2324
import java.util.concurrent.ExecutionException;
2425
import java.util.concurrent.TimeUnit;
@@ -65,14 +66,16 @@ public String c() {
6566
throw new RuntimeException();
6667
}
6768
try (Entry entry1 = SphU.entry("HelloWorld-c-1", EntryType.IN)) {
68-
log.debug("Hello Sentinel!1");
69-
try (Entry entry2 = SphU.entry("H\"elloWorld-c-2", EntryType.IN)) {
70-
log.debug("Hello Sentinel!2");
69+
// 具体的业务逻辑
70+
try {
7171
return "C" + serviceTag + "[" + inetUtils.findFirstNonLoopbackAddress().getHostAddress() + "]";
72-
} catch (BlockException e) {
73-
throw new RuntimeException(e);
72+
} catch (Throwable e) {
73+
// 标记此次资源调用失败
74+
entry1.setError(e);
75+
throw e;
7476
}
7577
} catch (BlockException e) {
78+
// 处理限流发生后的逻辑
7679
throw new RuntimeException(e);
7780
}
7881
}
@@ -82,7 +85,17 @@ public String cZone() {
8285
if (throwException) {
8386
throw new RuntimeException();
8487
}
85-
return "C" + serviceTag + "[" + currentZone + "]";
88+
try (Entry entry2 = SphU.entry("H\"elloWorld-c-2", EntryType.IN)) {
89+
try {
90+
log.debug("Hello Sentinel!2");
91+
return "C" + serviceTag + "[" + currentZone + "]";
92+
} catch (Throwable e) {
93+
entry2.setError(e);
94+
throw e;
95+
}
96+
} catch (BlockException e) {
97+
throw new RuntimeException(e);
98+
}
8699
}
87100

88101
@GetMapping("/spring_boot")

0 commit comments

Comments
 (0)