{"id":13702727,"url":"https://github.com/code1986/sentinel-lib","last_synced_at":"2026-02-16T22:56:23.653Z","repository":{"id":39809277,"uuid":"190527700","full_name":"code1986/sentinel-lib","owner":"code1986","description":"annotation for using sentinel easy","archived":false,"fork":false,"pushed_at":"2023-04-17T19:08:01.000Z","size":19,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-13T10:37:43.987Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/code1986.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-06T06:35:12.000Z","updated_at":"2023-03-16T10:41:19.000Z","dependencies_parsed_at":"2024-11-13T10:41:15.686Z","dependency_job_id":null,"html_url":"https://github.com/code1986/sentinel-lib","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code1986%2Fsentinel-lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code1986%2Fsentinel-lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code1986%2Fsentinel-lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code1986%2Fsentinel-lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/code1986","download_url":"https://codeload.github.com/code1986/sentinel-lib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252451896,"owners_count":21750005,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T21:00:41.414Z","updated_at":"2026-02-16T22:56:23.623Z","avatar_url":"https://github.com/code1986.png","language":"Java","funding_links":[],"categories":["Extensions / Integrations"],"sub_categories":[],"readme":"# sentinel-lib 使用注解配置配置熔断限流规则\n## 背景和目的\n\n- Sentinel的限流降级配置需要手动编码实现,定义限流熔断规则的地方往往和使用SentinelResource注解的代码不在同一个文件,配置分散不方便管理.\n- ResourceName需要在SentinelResource中,降级的地方,限流的地方多处重复定义,可能会出现手误导致出错,\n\n为解决这些问题,sentinel-lib提供了一些注解,可以直接在使用SentinelResource注解的地方直接配置熔断限流规则.\n\n## 如何使用\n\n### 1. 编译项目\n\n### 2. SpringBoot配置\n如果是Spring Boot用户,可以通过`@EnableSentinel`注解开启包功能.注解生效时会自动注入Sentinel的`SentinelResourceAspect`类,所以**不需要**额外再配置.\n例:\n```java\n@EnableSentinel\n@SpringBootApplication\npublic class Starter extends SpringBootServletInitializer {\n\n    public static void main(String[] args) throws Exception {\n        SpringApplication.run(Starter.class, args);\n    }\n}\n```\n\n### 3. Spring的xml配置\n如果是基于xml的spring配置,需要配置如下这个两个bean.\n```xml\n\u003cbean class=\"com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect\"\u003e\u003c/bean\u003e\n\u003cbean class=\"com.ximalaya.business.Annotation.configuration.SentinelAnnotationBeanProcessor\"\u003e\u003c/bean\u003e\n```\n\n### 4. 可用的注解类简介 \nsentinel-lib库提供了多种流控配置注解和降级控制注解,如下\n\n| 注解类 | 功能 |\n| -------- | ------- |\n|FlowRuleDefine |默认直接拒绝行为的流控|\n|RateLimitFlowRuleDefine| 限速流控|\n|WarmUpFlowRuleDefine |预热和预热限速流控|\n|DegradeRuleDefine |降级控制|\n\n### 5. 使用例子\n```java\n@RestController\n@RequestMapping(\"sample\")\npublic class MyFlowSampleController {\n    \n    public String fallback() {\n      return \"fallback\";\n    }\n    \n    public String blockHandler(BlockException ex) {\n      return \"blocked!\";\n    }\n    \n    @WarmUpFlowRuleDefine\n    @FlowRuleDefine(count = \"500\")\n    @DegradeRuleDefine(count = \"500\", timeWindow = \"10\")\n    @SentinelResource(value=\"doSomething\", blockHandler = \"blockHandler\", fallback = \"fallback\")\n    @RequestMapping(\"/doSomething\")\n    public String doSomething() {\n        return new String(\"everything is ok\");\n    }\n\n    @RateLimitFlowRuleDefine\n    @FlowRuleDefine\n    @DegradeRuleDefine\n    @SentinelResource(value=\"exceptionApi\", blockHandler = \"blockHandler\", fallback = \"fallback\")\n    @RequestMapping(\"/exceptionApi\")\n    public Response exceptionApi() {\n        throw new RuntimeException(\"something is wrong!\");\n    }\n}\n```\n\n### 6. 使用properties\n注解中的配置可以支持properties文件.例如application.properties包含如下内容\n```ini\nmy.flow=123\nmy.degrade.count=500\nmy.degrade.tw = 21\n```\n\n则在注解中可以使用定义的变量,如下:\n```java\n@WarmUpFlowRuleDefine(rateLimit = \"30\")\n@FlowRuleDefine(count = \"${my.flow}\")\n@DegradeRuleDefine(count = \"${my.degrade.count}\", timeWindow = \"${my.degrade.tw}\")\n@SentinelResource(value=\"doSomething\", blockHandler = \"myBlockHandler\", fallback = \"myDegradeHandler\")\n@RequestMapping(\"/doSomething\")\npublic Response doSomething() {\n    return new Response(\"everything is ok\");\n}\n```\n\n### 7. 关于灵活性\n在注解中使用properties文件属性,可以支持在dev,test,prd环境使用不同的限流数量配置.\n\n如果需要更加灵活的在线实时修改配置能力,建议使用ASAH的Sentinel控制台来做.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode1986%2Fsentinel-lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcode1986%2Fsentinel-lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode1986%2Fsentinel-lib/lists"}