diff --git a/script/sentinel/gateway-server/gateway-server-degrade-rules.json b/script/sentinel/gateway-server/gateway-server-degrade-rules.json new file mode 100644 index 000000000..5cd82e079 --- /dev/null +++ b/script/sentinel/gateway-server/gateway-server-degrade-rules.json @@ -0,0 +1,11 @@ +[ + { + "resource": "feign-test", // 资源名 + "count": 1000, // 当熔断策略是慢调用时,此值为最大RT,单位是毫秒。当熔断是策略是异常比例时,此值为异常比例。当熔断策略是异常数时,此值为异常数目 + "timeWindow": 5, // 熔断时长,单位是秒 + "grade": 0, // 0: 慢调用, 1: 异常比例,2:异常数 + "minRequestAmount": 2, // 最小请求数 + "slowRatioThreshold": 0.2, // 比例阈值 + "statIntervalMs": 10000 // 统计时长,单位毫秒 + } +] \ No newline at end of file diff --git a/script/sentinel/gateway-server/gateway-server-gw-flow-rules.json b/script/sentinel/gateway-server/gateway-server-gw-flow-rules.json new file mode 100644 index 000000000..f80c1d360 --- /dev/null +++ b/script/sentinel/gateway-server/gateway-server-gw-flow-rules.json @@ -0,0 +1,12 @@ +[ + { + "resource": "feign-test", // 资源名 + "resourceMode": 0, // API类型, 0: ROUTE ID区分, 1: API + "count": 1, // 阈值,QPS类型就是限制时间间隔的请求数,线程数类型就是指并行的线程数 + "grade": 1, // 阈值类型,0:线程数,1:QPS + "controlBehavior": 2, // 流控方式,0:快速失败,2:匀速排队 + "intervalSec": 10, // 间隔,单位是秒 + "burst": 0, // burst,只有在快速失败时才有效 + "maxQueueingTimeoutMs": 1000 // 超时时间,单位是毫秒 + } +] \ No newline at end of file diff --git a/script/sentinel/gateway-server/gateway-server-system-rules.json b/script/sentinel/gateway-server/gateway-server-system-rules.json new file mode 100644 index 000000000..6fb378ca0 --- /dev/null +++ b/script/sentinel/gateway-server/gateway-server-system-rules.json @@ -0,0 +1,10 @@ +[ + { + "avgRt": 1, // RT + "highestCpuUsage": -1, // CPU 使用率 + "highestSystemLoad": -1, // LOAD + "maxThread": -1, // 线程数 + "qps": -1, // 入口 QPS + "count": 55 // 阈值,在CPU使用率中是百分比 + } +] \ No newline at end of file diff --git a/script/sentinel/system-server/system-server-authority-rules.json b/script/sentinel/system-server/system-server-authority-rules.json new file mode 100644 index 000000000..a8d6241d8 --- /dev/null +++ b/script/sentinel/system-server/system-server-authority-rules.json @@ -0,0 +1,7 @@ +[ + { + "resource": "sentinel_spring_web_context", + "limitApp": "/test", + "strategy": 0 // 授权类型(0代表白名单;1代表黑名单。) + } +] \ No newline at end of file diff --git a/script/sentinel/system-server/system-server-degrade-rules.json b/script/sentinel/system-server/system-server-degrade-rules.json new file mode 100644 index 000000000..75f6047b1 --- /dev/null +++ b/script/sentinel/system-server/system-server-degrade-rules.json @@ -0,0 +1,11 @@ +[ + { + "resource": "/degrade-test", + "grade": 0, // 熔断策略,支持慢调用比例(0),异常比例(1),异常数(2)策略 + "count": 1000, // 慢调用比例模式下为慢调用临界 RT(超出该值计为慢调用,单位ms);异常比例/异常数模式下为对应的阈值 + "slowRatioThreshold": 0.1,// 慢调用比例阈值,仅慢调用比例模式有效(1.8.0 引入) + "minRequestAmount": 10, //熔断触发的最小请求数,请求数小于该值时即使异常比率超出阈值也不会熔断 + "timeWindow": 10, // 熔断时长,单位为 s + "statIntervalMs": 1000 // 统计时长(单位为 ms),如 60*1000 代表分钟级 + } +] diff --git a/script/sentinel/system-server/system-server-flow-rules.json b/script/sentinel/system-server/system-server-flow-rules.json new file mode 100644 index 000000000..3b07d3d7b --- /dev/null +++ b/script/sentinel/system-server/system-server-flow-rules.json @@ -0,0 +1,14 @@ +[ + { + "resource": "/flow-test", // 资源名 + "limitApp": "default", // // 针对来源,若为 default 则不区分调用来源 + "grade": 1, // 限流阈值类型(1:QPS; 0:并发线程数) + "count": 1, // 阈值 + "clusterMode": false, // 是否是集群模式 + "controlBehavior": 0, // 流控效果 (0:快速失败; 1:Warm Up(预热模式); 2:排队等待) + "strategy": 0, // 流控模式(0:直接; 1:关联; 2:链路) + "warmUpPeriodSec": 10, // 预热时间(秒,预热模式需要此参数) + "maxQueueingTimeMs": 500, // 超时时间(排队等待模式需要此参数) + "refResource": "rrr" // 关联资源、入口资源(关联、链路模式) + } +] diff --git a/script/sentinel/system-server/system-server-param-flow-rules.json b/script/sentinel/system-server/system-server-param-flow-rules.json new file mode 100644 index 000000000..ccdfd4c2b --- /dev/null +++ b/script/sentinel/system-server/system-server-param-flow-rules.json @@ -0,0 +1,18 @@ +[ + { + "resource": "/param-flow-test", + "grade": 1, // 限流模式(QPS 模式,不可更改) + "paramIdx": 0, // 参数索引 + "count": 13, // 单机阈值 + "durationInSec": 6, // 统计窗口时长 + "clusterMode": false, // 是否集群 默认false + "controlBehavior": 0, // 流控效果(支持快速失败和匀速排队模式) + "limitApp": "default", + // 高级选项 + "paramFlowItemList": [{ + "classType": "int", // 参数类型 + "count": 222, // 限流阈值 + "object": "2" // 参数值 + }] + } +] \ No newline at end of file diff --git a/script/sentinel/system-server/system-server-system-rules.json b/script/sentinel/system-server/system-server-system-rules.json new file mode 100644 index 000000000..6fb378ca0 --- /dev/null +++ b/script/sentinel/system-server/system-server-system-rules.json @@ -0,0 +1,10 @@ +[ + { + "avgRt": 1, // RT + "highestCpuUsage": -1, // CPU 使用率 + "highestSystemLoad": -1, // LOAD + "maxThread": -1, // 线程数 + "qps": -1, // 入口 QPS + "count": 55 // 阈值,在CPU使用率中是百分比 + } +] \ No newline at end of file diff --git a/yudao-framework/yudao-spring-boot-starter-protection/pom.xml b/yudao-framework/yudao-spring-boot-starter-protection/pom.xml index 86f83d8f1..012e53f5a 100644 --- a/yudao-framework/yudao-spring-boot-starter-protection/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-protection/pom.xml @@ -36,6 +36,16 @@ true + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + com.alibaba.csp + sentinel-datasource-nacos + + cn.iocoder.cloud diff --git a/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/config/YudaoSentinelAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/config/YudaoSentinelAutoConfiguration.java new file mode 100644 index 000000000..c636a31c3 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/config/YudaoSentinelAutoConfiguration.java @@ -0,0 +1,18 @@ +package cn.iocoder.yudao.framework.sentinel.config; + +import cn.iocoder.yudao.framework.sentinel.core.handler.SentinelExceptionHandler; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; + +@AutoConfiguration +@Slf4j +public class YudaoSentinelAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public SentinelExceptionHandler sentinelExceptionHandler() { + return new SentinelExceptionHandler(); + } +} \ No newline at end of file diff --git a/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/core/handler/SentinelExceptionHandler.java b/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/core/handler/SentinelExceptionHandler.java new file mode 100644 index 000000000..0508c78b5 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/core/handler/SentinelExceptionHandler.java @@ -0,0 +1,43 @@ +package cn.iocoder.yudao.framework.sentinel.core.handler; + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; +import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler; +import com.alibaba.csp.sentinel.slots.block.BlockException; +import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; +import com.alibaba.csp.sentinel.slots.block.flow.FlowException; +import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException; +import com.alibaba.csp.sentinel.slots.system.SystemBlockException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.http.HttpStatus; + + +/** + * Sentinel 自定义阻断异常处理 + * @author koltZhang + */ +public class SentinelExceptionHandler implements BlockExceptionHandler { + + @Override + public void handle(HttpServletRequest request, HttpServletResponse response, String resourceName, BlockException e) throws Exception { + String msg = "未知异常"; + int status = HttpStatus.TOO_MANY_REQUESTS.value(); + if (e instanceof FlowException) { + msg = "请求限流"; + } else if (e instanceof ParamFlowException) { + msg = "请求被热点参数限流"; + } else if (e instanceof DegradeException) { + msg = "请求降级"; + } else if (e instanceof AuthorityException) { + msg = "没有权限访问"; + status = HttpStatus.UNAUTHORIZED.value(); + } else if (e instanceof SystemBlockException) { + msg = "系统规则限流或降级"; + } + ServletUtils.writeJSON(response, CommonResult.error(status, msg)); + + } + +} \ No newline at end of file diff --git a/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/core/package-info.java b/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/core/package-info.java new file mode 100644 index 000000000..b9d23aee6 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/core/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.yudao.framework.sentinel.core; \ No newline at end of file diff --git a/yudao-framework/yudao-spring-boot-starter-protection/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/yudao-framework/yudao-spring-boot-starter-protection/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index abca5135a..3cd3b5ff3 100644 --- a/yudao-framework/yudao-spring-boot-starter-protection/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/yudao-framework/yudao-spring-boot-starter-protection/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,4 +1,5 @@ cn.iocoder.yudao.framework.idempotent.config.YudaoIdempotentConfiguration cn.iocoder.yudao.framework.lock4j.config.YudaoLock4jConfiguration cn.iocoder.yudao.framework.ratelimiter.config.YudaoRateLimiterConfiguration -cn.iocoder.yudao.framework.signature.config.YudaoApiSignatureAutoConfiguration \ No newline at end of file +cn.iocoder.yudao.framework.signature.config.YudaoApiSignatureAutoConfiguration +cn.iocoder.yudao.framework.sentinel.config.YudaoSentinelAutoConfiguration \ No newline at end of file diff --git a/yudao-gateway/pom.xml b/yudao-gateway/pom.xml index fd75115d9..df30365f0 100644 --- a/yudao-gateway/pom.xml +++ b/yudao-gateway/pom.xml @@ -69,6 +69,24 @@ com.google.guava guava + + + + com.alibaba.cloud + spring-cloud-alibaba-sentinel-gateway + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + + com.alibaba.csp + sentinel-datasource-nacos + diff --git a/yudao-gateway/src/main/resources/application-local.yaml b/yudao-gateway/src/main/resources/application-local.yaml index a7132bd6f..e41a892dd 100644 --- a/yudao-gateway/src/main/resources/application-local.yaml +++ b/yudao-gateway/src/main/resources/application-local.yaml @@ -13,6 +13,13 @@ spring: namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP +--- #################### Sentinel相关配置 #################### +spring: + cloud: + sentinel: + transport: + dashboard: 127.0.0.1:8080 # Sentinel 控制台地址 + # 日志文件配置 logging: level: diff --git a/yudao-gateway/src/main/resources/application.yaml b/yudao-gateway/src/main/resources/application.yaml index a03f08ba5..acb86048c 100644 --- a/yudao-gateway/src/main/resources/application.yaml +++ b/yudao-gateway/src/main/resources/application.yaml @@ -250,4 +250,49 @@ knife4j: yudao: info: - version: 1.0.0 \ No newline at end of file + version: 1.0.0 + +--- #################### Sentinel相关配置 #################### +spring: + cloud: + sentinel: + enabled: true # 是否开启。默认为 true 开启 + eager: true # 是否饥饿加载。默认为 false 关闭 + filter: + url-patterns: /** # 拦截请求的地址。默认为 /* + #自定义流控降级异常信息 + scg: + fallback: + mode: response + response-body: '{"code": 429, "msg":"服务压力过大,请稍后重试!"}' + # sentinel用nacos作为数据源的配置 (可导入/yudao-cloud/script/sentinel/system-server下对应规则,使用时需去掉注释) + datasource: + # 流控规则 + flow: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + data-id: ${spring.application.name}-gw-flow-rules # 在修改的sentinel 源码中定义的规则名 + rule-type: gw_flow + # 降级规则 + degrade: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + dataId: ${spring.application.name}-degrade-rules + rule-type: degrade + # 系统规则 + system: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + dataId: ${spring.application.name}-system-rules + groupId: DEFAULT_GROUP + data-type: json + rule-type: system \ No newline at end of file diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/demo/SentinelFeigenDemoApi.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/demo/SentinelFeigenDemoApi.java new file mode 100644 index 000000000..ccc549033 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/demo/SentinelFeigenDemoApi.java @@ -0,0 +1,22 @@ +package cn.iocoder.yudao.module.infra.api.demo; + + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.module.infra.api.demo.fallback.SentinelFeigenDemoFallback; +import cn.iocoder.yudao.module.infra.enums.ApiConstants; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +/** + * sentinel Feign demo + */ +@FeignClient(name = ApiConstants.NAME, fallback = SentinelFeigenDemoFallback.class) +public interface SentinelFeigenDemoApi { + + String PREFIX = ApiConstants.PREFIX + "/provider/sentinel"; + + @GetMapping(PREFIX +"/test/{message}") + public CommonResult providerSentinelTest(@PathVariable("message") String message); + +} diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/demo/fallback/SentinelFeigenDemoFallback.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/demo/fallback/SentinelFeigenDemoFallback.java new file mode 100644 index 000000000..c5f4bee5b --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/demo/fallback/SentinelFeigenDemoFallback.java @@ -0,0 +1,15 @@ +package cn.iocoder.yudao.module.infra.api.demo.fallback; + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.module.infra.api.demo.SentinelFeigenDemoApi; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; + +@Component +public class SentinelFeigenDemoFallback implements SentinelFeigenDemoApi { + + @Override + public CommonResult providerSentinelTest(String message) { + return CommonResult.error(HttpStatus.TOO_MANY_REQUESTS.value(), "对方服务不可用,开始服务降级处理"); + } +} diff --git a/yudao-module-infra/yudao-module-infra-biz/pom.xml b/yudao-module-infra/yudao-module-infra-biz/pom.xml index 799711d6e..9e5095f50 100644 --- a/yudao-module-infra/yudao-module-infra-biz/pom.xml +++ b/yudao-module-infra/yudao-module-infra-biz/pom.xml @@ -147,6 +147,12 @@ tika-core + + + cn.iocoder.cloud + yudao-spring-boot-starter-protection + + diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/demo/SentinelFeigenApiImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/demo/SentinelFeigenApiImpl.java new file mode 100644 index 000000000..af510e5c1 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/demo/SentinelFeigenApiImpl.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.module.infra.api.demo; + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RestController; + +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; + +/** + * 提供 RESTful API 接口,给 Feign 调用 + */ +@RestController +@Validated +public class SentinelFeigenApiImpl implements SentinelFeigenDemoApi { + + @Override + public CommonResult providerSentinelTest(String message) { + return success("OK"); + } + +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/sentinel/SentinelDemoController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/sentinel/SentinelDemoController.java new file mode 100644 index 000000000..7d4ce9569 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/demo/sentinel/SentinelDemoController.java @@ -0,0 +1,68 @@ +package cn.iocoder.yudao.module.infra.controller.admin.demo.sentinel; + + +import com.alibaba.csp.sentinel.annotation.SentinelResource; +import com.alibaba.csp.sentinel.slots.block.BlockException; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.security.PermitAll; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.UUID; + +/** + * sentinel 示例 + * TODO 芋艿:考虑后续优化示例 + */ +@Tag(name = "管理后台 - sentinel") +@RestController +@RequestMapping("/infra/sentinel/demo") +@Validated +public class SentinelDemoController { + + @GetMapping("flow") + @PermitAll + public String flow(String userName){ + return "hello "+userName; + } + + /** + * 关联资源 + */ + @GetMapping("flow-ref") + @PermitAll + public String flowRef(){ + return "hello flowRef"; + } + + @GetMapping("degrade") + @PermitAll + public String degrade(String userName){ + return "hello "+userName; + } + + /** + * 按照资源名称的方式限流,并对限流流进行友好处理 + * @param request + * @return + */ + @GetMapping("getOrderNo") + @SentinelResource(value = "getOrderNoResource",blockHandler = "getOrderNoBlockHandler",blockHandlerClass = SentinelDemoController.class) + @PermitAll + public String getOrderNo(HttpServletRequest request){ + return UUID.randomUUID().toString(); + } + + /** + * 限流后续操作方法 + * @param e + * @return + */ + public static String getOrderNoBlockHandler(HttpServletRequest request, BlockException e){ + return "不好意思,前方拥挤,请您稍后再试"; + } + +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application-dev.yaml b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application-dev.yaml index 80dc8f7bf..5291b9238 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application-dev.yaml +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application-dev.yaml @@ -15,6 +15,13 @@ spring: namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP +--- #################### Sentinel相关配置 #################### +spring: + cloud: + sentinel: + transport: + dashboard: 127.0.0.1:8080 # Sentinel 控制台地址 + --- #################### 数据库相关配置 #################### spring: # 数据源配置项 diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application-local.yaml b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application-local.yaml index aaa8ccef7..c9e8a25b7 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application-local.yaml +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application-local.yaml @@ -15,6 +15,13 @@ spring: namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP +--- #################### Sentinel相关配置 #################### +spring: + cloud: + sentinel: + transport: + dashboard: 127.0.0.1:8080 # Sentinel 控制台地址 + --- #################### 数据库相关配置 #################### spring: diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application.yaml b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application.yaml index 60a403083..e0fa71de6 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application.yaml +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/resources/application.yaml @@ -91,6 +91,11 @@ easy-trans: --- #################### RPC 远程调用相关配置 #################### +# 开启 feign 对 sentinel 的支持 +feign: + sentinel: + enabled: true + --- #################### 消息队列相关 #################### # rocketmq 配置项,对应 RocketMQProperties 配置类 @@ -166,6 +171,7 @@ yudao: enable: true ignore-urls: - /admin-api/infra/file/*/get/** # 获取图片,和租户无关 + - /admin-api/infra/sentinel/demo/** # sentinel demo ignore-tables: - infra_codegen_column - infra_codegen_table @@ -179,3 +185,61 @@ yudao: - infra_data_source_config debug: false + +--- #################### Sentinel相关配置 #################### +spring: + cloud: + sentinel: + enabled: true # 是否开启。默认为 true 开启 + eager: true # 是否饥饿加载。默认为 false 关闭 + filter: + url-patterns: /** # 拦截请求的地址。默认为 /* + # sentinel用nacos作为数据源的配置 (可导入/yudao-cloud/script/sentinel/gateway-server下对应规则,使用时需去掉注释) + datasource: + # 流控规则 + flow: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + data-id: ${spring.application.name}-flow-rules # 在修改的sentinel 源码中定义的规则名 + rule-type: flow + # 降级规则 + degrade: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + dataId: ${spring.application.name}-degrade-rules + rule-type: degrade + # 授权规则 +# authority: +# nacos: +# server-addr: ${spring.cloud.nacos.server-addr} +# namespace: ${spring.cloud.nacos.discovery.namespace} +# username: ${spring.cloud.nacos.username} +# password: ${spring.cloud.nacos.password} +# dataId: ${spring.application.name}-authority-rules +# rule-type: authority +# # 热点规则 +# param-flow: +# nacos: +# server-addr: ${spring.cloud.nacos.server-addr} +# namespace: ${spring.cloud.nacos.discovery.namespace} +# username: ${spring.cloud.nacos.username} +# password: ${spring.cloud.nacos.password} +# dataId: ${spring.application.name}-param-flow-rules +# rule-type: param-flow +# # 系统规则 +# system: +# nacos: +# server-addr: ${spring.cloud.nacos.server-addr} +# namespace: ${spring.cloud.nacos.discovery.namespace} +# username: ${spring.cloud.nacos.username} +# password: ${spring.cloud.nacos.password} +# dataId: ${spring.application.name}-system-rules +# groupId: DEFAULT_GROUP +# data-type: json +# rule-type: system \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-biz/pom.xml b/yudao-module-system/yudao-module-system-biz/pom.xml index 5c8b73f85..2504187b3 100644 --- a/yudao-module-system/yudao-module-system-biz/pom.xml +++ b/yudao-module-system/yudao-module-system-biz/pom.xml @@ -98,10 +98,10 @@ - - - - + + cn.iocoder.cloud + yudao-spring-boot-starter-protection + diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/demo/SentinelFeigenDemoController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/demo/SentinelFeigenDemoController.java new file mode 100644 index 000000000..d8e9f9519 --- /dev/null +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/demo/SentinelFeigenDemoController.java @@ -0,0 +1,28 @@ +package cn.iocoder.yudao.module.system.controller.admin.demo; + + +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.module.infra.api.demo.SentinelFeigenDemoApi; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import jakarta.annotation.security.PermitAll; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + + +@Tag(name = "管理后台 - Sentinel Feigen Demo") +@RestController() +@RequestMapping("/system/sentinel/demo") +public class SentinelFeigenDemoController { + + @Resource + private SentinelFeigenDemoApi sentinelFeigenDemoApiService; + + @GetMapping("/provider/{message}") + @PermitAll + public CommonResult providerSentinelTest(@PathVariable("message") String message) { + return sentinelFeigenDemoApiService.providerSentinelTest(message); + } +} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/config/RpcConfiguration.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/config/RpcConfiguration.java index c29757003..bc6c4ab6e 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/config/RpcConfiguration.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/rpc/config/RpcConfiguration.java @@ -1,12 +1,16 @@ package cn.iocoder.yudao.module.system.framework.rpc.config; import cn.iocoder.yudao.module.infra.api.config.ConfigApi; +import cn.iocoder.yudao.module.infra.api.demo.SentinelFeigenDemoApi; +import cn.iocoder.yudao.module.infra.api.demo.fallback.SentinelFeigenDemoFallback; import cn.iocoder.yudao.module.infra.api.file.FileApi; import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; @Configuration(proxyBeanMethods = false) -@EnableFeignClients(clients = {FileApi.class, WebSocketSenderApi.class, ConfigApi.class}) +@EnableFeignClients(clients = {FileApi.class, WebSocketSenderApi.class, ConfigApi.class, SentinelFeigenDemoApi.class}) +@Import({SentinelFeigenDemoFallback.class}) public class RpcConfiguration { } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/resources/application-dev.yaml b/yudao-module-system/yudao-module-system-biz/src/main/resources/application-dev.yaml index edaf1e540..0137d842b 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/resources/application-dev.yaml +++ b/yudao-module-system/yudao-module-system-biz/src/main/resources/application-dev.yaml @@ -15,6 +15,14 @@ spring: namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP +--- #################### Sentinel相关配置 #################### + +spring: + cloud: + sentinel: + transport: + dashboard: 127.0.0.1:8080 # Sentinel 控制台地址 + --- #################### 数据库相关配置 #################### spring: # 数据源配置项 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/resources/application-local.yaml b/yudao-module-system/yudao-module-system-biz/src/main/resources/application-local.yaml index 881b442ce..96f278f31 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/resources/application-local.yaml +++ b/yudao-module-system/yudao-module-system-biz/src/main/resources/application-local.yaml @@ -15,6 +15,14 @@ spring: namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP +--- #################### Sentinel相关配置 #################### + +spring: + cloud: + sentinel: + transport: + dashboard: 127.0.0.1:8080 # Sentinel 控制台地址 + --- #################### 数据库相关配置 #################### spring: # 数据源配置项 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml b/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml index 19ed55654..3e97b22f6 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml +++ b/yudao-module-system/yudao-module-system-biz/src/main/resources/application.yaml @@ -91,6 +91,11 @@ easy-trans: --- #################### RPC 远程调用相关配置 #################### +# 开启 feign 对 sentinel 的支持 +feign: + sentinel: + enabled: true + --- #################### 消息队列相关 #################### # rocketmq 配置项,对应 RocketMQProperties 配置类 @@ -175,6 +180,7 @@ yudao: - /rpc-api/system/tenant/valid # 防止递归。避免调用 /rpc-api/system/tenant/valid 接口时,又去触发 /rpc-api/system/tenant/valid 去校验 - /rpc-api/system/tenant/id-list # 获得租户列表的时候,无需传递租户编号 - /rpc-api/system/oauth2/token/check # 访问令牌校验时,无需传递租户编号;主要解决上传文件的场景,前端不会传递 tenant-id! + - /admin-api/system/sentinel/demo/provider/* # 示例演示,演示 Sentinel 熔断降级 ignore-tables: - system_tenant - system_tenant_package @@ -207,3 +213,62 @@ yudao: end-code: 9999 # 这里配置 9999 的原因是,测试方便。 debug: false + +--- #################### Sentinel相关配置 #################### + +spring: + cloud: + sentinel: + enabled: true # 是否开启。默认为 true 开启 + eager: true # 是否饥饿加载。默认为 false 关闭 + filter: + url-patterns: /** # 拦截请求的地址。默认为 /* + # sentinel用nacos作为数据源的配置 (可导入/yudao-cloud/script/sentinel/system-server下对应规则,使用时需去掉注释) + datasource: + # 流控规则 + flow: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + data-id: ${spring.application.name}-flow-rules # 在修改的sentinel 源码中定义的规则名 + rule-type: flow + # 授权规则 + authority: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + dataId: ${spring.application.name}-authority-rules + rule-type: authority + # 降级规则 + degrade: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + dataId: ${spring.application.name}-degrade-rules + rule-type: degrade + # 热点规则 + param-flow: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + dataId: ${spring.application.name}-param-flow-rules + rule-type: param-flow + # 系统规则 + system: + nacos: + server-addr: ${spring.cloud.nacos.server-addr} + namespace: ${spring.cloud.nacos.discovery.namespace} + username: ${spring.cloud.nacos.username} + password: ${spring.cloud.nacos.password} + dataId: ${spring.application.name}-system-rules + groupId: DEFAULT_GROUP + data-type: json + rule-type: system \ No newline at end of file