diff --git a/yudao-framework/yudao-spring-boot-starter-protection/pom.xml b/yudao-framework/yudao-spring-boot-starter-protection/pom.xml index 86f83d8f1..a98614830 100644 --- a/yudao-framework/yudao-spring-boot-starter-protection/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-protection/pom.xml @@ -36,6 +36,18 @@ 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/SentinelExceptionHandlerAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/config/SentinelExceptionHandlerAutoConfiguration.java new file mode 100644 index 000000000..dddcb8093 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-protection/src/main/java/cn/iocoder/yudao/framework/sentinel/config/SentinelExceptionHandlerAutoConfiguration.java @@ -0,0 +1,21 @@ +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; + +/** + * 当Spring容器中不存在MyService类型的bean时,才创建这个bean + */ +@AutoConfiguration +@Slf4j +public class SentinelExceptionHandlerAutoConfiguration { + + @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..6a93c7150 --- /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,44 @@ +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自定义阻断异常处理 + */ +public class SentinelExceptionHandler implements BlockExceptionHandler { + + @Override + public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s, 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(httpServletResponse, 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..7a184101a 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.SentinelExceptionHandlerAutoConfiguration \ 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..a8347516e --- /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,23 @@ +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 67c96009c..55d8b7aeb 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 8c57cf28c..c58c4e065 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,66 @@ spring: namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP +--- #################### Sentinel相关配置 #################### +spring: + cloud: + sentinel: + enabled: true # 是否开启。默认为 true 开启 + eager: true # 是否饥饿加载。默认为 false 关闭 + transport: + dashboard: 127.0.0.1:8080 # Sentinel 控制台地址 + 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 + --- #################### 数据库相关配置 #################### 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 386c91176..771b710bb 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 @@ -4,8 +4,8 @@ spring: cloud: nacos: server-addr: 127.0.0.1:8848 # Nacos 服务器地址 - username: # Nacos 账号 - password: # Nacos 密码 + username: nacos # Nacos 账号 + password: nacos # Nacos 密码 discovery: # 【配置中心】配置项 namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP @@ -15,6 +15,66 @@ spring: namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP +--- #################### Sentinel相关配置 #################### +spring: + cloud: + sentinel: + enabled: true # 是否开启。默认为 true 开启 + eager: true # 是否饥饿加载。默认为 false 关闭 + transport: + dashboard: 127.0.0.1:8080 # Sentinel 控制台地址 + 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 + --- #################### 数据库相关配置 #################### 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 e62c4974c..d466250aa 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 @@ -165,6 +165,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 @@ -178,3 +179,9 @@ yudao: - infra_data_source_config debug: false + +--- #################### 开启feign对sentinel的支持 #################### + +feign: + sentinel: + enabled: true \ 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 5ca619768..95a4f5d85 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 df8f155fe..1b22e4a8b 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,67 @@ spring: namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP +--- #################### Sentinel相关配置 #################### + +spring: + cloud: + sentinel: + enabled: true # 是否开启。默认为 true 开启 + eager: true # 是否饥饿加载。默认为 false 关闭 + transport: + dashboard: 127.0.0.1:8080 # Sentinel 控制台地址 + 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 + --- #################### 数据库相关配置 #################### 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 ca8b88d7a..770275b25 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,67 @@ spring: namespace: dev # 命名空间。这里使用 dev 开发环境 group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP +--- #################### Sentinel相关配置 #################### + +spring: + cloud: + sentinel: + enabled: true # 是否开启。默认为 true 开启 + eager: true # 是否饥饿加载。默认为 false 关闭 + transport: + dashboard: 127.0.0.1:8080 # Sentinel 控制台地址 + 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 + --- #################### 数据库相关配置 #################### 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 533c34162..54ec8d2da 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 @@ -174,6 +174,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 @@ -205,3 +206,9 @@ yudao: end-code: 9999 # 这里配置 9999 的原因是,测试方便。 debug: false + +--- #################### 开启feign对sentinel的支持 #################### + +feign: + sentinel: + enabled: true \ No newline at end of file