{"record":{"id":"87658f63902d5e7e","repo":"wuyouzhuguli/SpringAll","slug":"error-87658f","errorCode":null,"errorMessage":"服务异常","messagePattern":"服务异常","errorType":"http","errorClass":"RuntimeException","httpStatus":500,"severity":"info","filePath":"77.spring-cloud-alibaba-sentinel-dashboard-guide/src/main/java/cc/mrbird/sentinel/controller/TestController.java","lineNumber":24,"sourceCode":"import org.slf4j.LoggerFactory;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n/**\n * @author MrBird\n */\n@RestController\npublic class TestController {\n\n    private Logger log = LoggerFactory.getLogger(this.getClass());\n\n    @Autowired\n    private HelloService helloService;\n\n    @GetMapping(\"test1\")\n    public String test1() {\n        throw new RuntimeException(\"服务异常\");\n        // return \"test1\";\n    }\n\n    @GetMapping(\"test2\")\n    public String test2() {\n        return \"test2 \" + helloService.hello();\n    }\n\n    @GetMapping(\"buy\")\n    @SentinelResource(value = \"buy\")\n    public String buy(String goodName, Integer count) {\n        return \"买\" + count + \"份\" + goodName;\n    }\n}","sourceCodeStart":6,"sourceCodeEnd":38,"githubUrl":"https://github.com/wuyouzhuguli/SpringAll/blob/614d2578d9495acf53cc02f2dee9c6131cc5e51a/77.spring-cloud-alibaba-sentinel-dashboard-guide/src/main/java/cc/mrbird/sentinel/controller/TestController.java#L6-L38","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (demo — always fails to trigger Sentinel degrade)\n@GetMapping(\"test1\")\npublic String test1() {\n    throw new RuntimeException(\"服务异常\");\n    // return \"test1\";\n}\n\n// after — keep the fault but route tripped calls through a Sentinel fallback\n@GetMapping(\"test1\")\n@SentinelResource(value = \"test1\", blockHandler = \"test1BlockHandler\", fallback = \"test1Fallback\")\npublic String test1() {\n    throw new RuntimeException(\"服务异常\");\n}\n\npublic String test1Fallback() {\n    return \"服务降级，请稍后重试\";\n}\n\npublic String test1BlockHandler(BlockException ex) {\n    return \"请求被限流/熔断\";\n}","handlingStrategy":"fallback","validationCode":"// No real 'validation' prevents an intentional throw — instead configure Sentinel rules.\n// Verify a degrade rule exists on the resource before relying on the endpoint:\n// In Sentinel dashboard: 熔断规则 -> 资源名 test1 -> 异常比例/异常数 strategy.","typeGuard":"// This is demo code; guard callers by treating /test1 as always-failing\nprivate boolean isAlwaysFailingDemoEndpoint(String path) {\n    return \"/test1\".equals(path); // expect failure; use fallback/degrade rules\n}","tryCatchPattern":"// Client of the demo endpoint should degrade gracefully\ntry {\n    String r = restTemplate.getForObject(\"/test1\", String.class);\n} catch (RuntimeException e) {\n    // expected in the Sentinel demo; rely on configured degrade/fallback rules\n    log.warn(\"test1 failed (expected in demo): {}\", e.getMessage());\n    return \"降级响应\";\n}","preventionTips":["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."],"tags":["sentinel","spring-cloud-alibaba","demo","circuit-breaker","fault-tolerance"],"backgroundTag":null,"analyzedSha":"614d2578d9495acf53cc02f2dee9c6131cc5e51a","analyzedAt":"2026-08-14T04:40:03.488Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}