wuyouzhuguli/SpringAll · info · RuntimeException
服务异常
Error message
服务异常
What it means
A RuntimeException with message '服务异常' (service exception) is thrown deliberately by TestController.test1() in a Sentinel dashboard guide. This is not a bug — it is an intentional fault injected in a sample endpoint to demonstrate Spring Cloud Alibaba Sentinel's degradation/fault-tolerance rules (block handlers, fallbacks, circuit breaking). The exception propagates unless a Sentinel degrade rule or a @SentinelResource blockHandler/fallback intercepts it.
Source
Thrown at 77.spring-cloud-alibaba-sentinel-dashboard-guide/src/main/java/cc/mrbird/sentinel/controller/TestController.java:24
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author MrBird
*/
@RestController
public class TestController {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private HelloService helloService;
@GetMapping("test1")
public String test1() {
throw new RuntimeException("服务异常");
// return "test1";
}
@GetMapping("test2")
public String test2() {
return "test2 " + helloService.hello();
}
@GetMapping("buy")
@SentinelResource(value = "buy")
public String buy(String goodName, Integer count) {
return "买" + count + "份" + goodName;
}
}View on GitHub (pinned to 614d2578d9)
Solutions
- Recognize this is intentional demo code — configure a Sentinel degrade rule (RT, exception-ratio, or exception-count) on the /test1 resource in the Sentinel dashboard to observe fault tolerance in action.
- If you need the endpoint to demonstrate fallback handling, add a @SentinelResource(value="test1", blockHandler=...) or fallback so Sentinel routes tripped calls to a graceful response instead of a raw 500.
- To stop the failure for local testing, replace the throw with a normal return (the commented-out 'return "test1";') — but only do this if you are no longer exercising the degrade demo.
- Do not deploy this controller as-is to production; it is a teaching artifact that always fails.
Example fix
// before (demo — always fails to trigger Sentinel degrade)
@GetMapping("test1")
public String test1() {
throw new RuntimeException("服务异常");
// return "test1";
}
// after — keep the fault but route tripped calls through a Sentinel fallback
@GetMapping("test1")
@SentinelResource(value = "test1", blockHandler = "test1BlockHandler", fallback = "test1Fallback")
public String test1() {
throw new RuntimeException("服务异常");
}
public String test1Fallback() {
return "服务降级,请稍后重试";
}
public String test1BlockHandler(BlockException ex) {
return "请求被限流/熔断";
} Defensive patterns
Strategy: fallback
Validate before calling
// No real 'validation' prevents an intentional throw — instead configure Sentinel rules. // Verify a degrade rule exists on the resource before relying on the endpoint: // In Sentinel dashboard: 熔断规则 -> 资源名 test1 -> 异常比例/异常数 strategy.
Type guard
// This is demo code; guard callers by treating /test1 as always-failing
private boolean isAlwaysFailingDemoEndpoint(String path) {
return "/test1".equals(path); // expect failure; use fallback/degrade rules
} Try / catch
// Client of the demo endpoint should degrade gracefully
try {
String r = restTemplate.getForObject("/test1", String.class);
} catch (RuntimeException e) {
// expected in the Sentinel demo; rely on configured degrade/fallback rules
log.warn("test1 failed (expected in demo): {}", e.getMessage());
return "降级响应";
} Prevention
- Treat this controller as demo-only; never ship it to production as-is.
- Configure a Sentinel degrade rule (exception-ratio/count) on the test1 resource in the dashboard before demonstrating.
- Add a @SentinelResource blockHandler/fallback so tripped calls return a graceful response instead of a raw 500.
- When the demo is no longer needed, restore the commented return statement.
When it happens
Trigger: A GET /test1 request is made to the sample controller. The method unconditionally throws new RuntimeException("服务异常") before returning, so every call fails. When a Sentinel degrade rule (exception-ratio or exception-count strategy) is configured on the /test1 resource in the dashboard, Sentinel trips the circuit after the configured threshold and routes subsequent calls to a fallback.
Common situations: Following the Sentinel dashboard tutorial and calling /test1 to observe circuit-breaker behavior; the demo expects you to configure a degrade rule in the Sentinel dashboard targeting the resource. If no rule is configured, the caller simply receives the raw 500 with '服务异常'. This is sample/demo code, not production logic.
Related errors
AI-assisted analysis of wuyouzhuguli/SpringAll@614d2578d9 (2026-08-14).
Data as JSON: /api/errors/87658f63902d5e7e.
Report an issue: GitHub.