当前位置: 首页> 健康> 美食 > 如何进行网页设计和网站制作_51网站空间还有吗_资源搜索器_八宿县网站seo优化排名

如何进行网页设计和网站制作_51网站空间还有吗_资源搜索器_八宿县网站seo优化排名

时间:2025/7/11 0:34:34来源:https://blog.csdn.net/bandaotixiruiqiang/article/details/146499606 浏览次数:1次
如何进行网页设计和网站制作_51网站空间还有吗_资源搜索器_八宿县网站seo优化排名

当前项目源码

控制台下载

启动bin中的看板服务:账号密码:sentinel/sentinel

官方文档地址

项目引入依赖

<!-- sentinel注解支持 -->
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-annotation-aspectj</artifactId><version>1.8.8</version>
</dependency>
<!-- 参数限流 -->
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-parameter-flow-control</artifactId><version>1.8.8</version>
</dependency>
<!-- 限流客户端插件,默认读取资源配置sentinel.properties -->
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>1.8.8</version>
</dependency>

SpringBoot中自动配置

  • 创建配置类

/*** 容器启动成功后配置Sentinel规则*/
@Data
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "sentinel")
public class SentinelConfig implements InitializingBean {private List<FlowRule> rules;private List<ParamFlowRule> params;/*** 开启注解*/@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();}/*** 加载规则*/public void loadRules() {log.info("加载限流规则");FlowRuleManager.loadRules(rules);ParamFlowRuleManager.loadRules(params);}@Overridepublic void afterPropertiesSet() {triggerSentinelInit();loadRules();}/*** 触发sentinel初始化。*/private static void triggerSentinelInit() {new Thread(InitExecutor::doInit).start();}}
  • application.yml配置文件添加内容
sentinel:# 资源限流rules:- resource: "test"count: 1grade: 1# 参数限流params:- resource: "test"param-idx: 0count: 1grade: 1
  • 资源目录中添加sentinel配置sentinel.properties
  • sentinel-transport-simple-http插件自动读取这个文件
project.name=sentinel-test
# 配置sentinel控制台地址
csp.sentinel.dashboard.server=localhost:8181

创建测试接口


@Slf4j
@RestController
@ControllerAdvice // 使用当前类配置异常拦截
@RequestMapping("test")
public class TestController {@GetMapping@SentinelResource(value = "test",  // 资源名称blockHandler = "testBlock",// 规则异常blockHandlerClass = TestFallbackHandler.class,fallback = "testFallback", // 业务异常降级fallbackClass = TestFallbackHandler.class,exceptionsToIgnore = {CustomException.class} // 忽略异常,比如自定义异常可能在@ControllerAdvice配置)public String test(String value) {if ("admin".equalsIgnoreCase(value)) {throw new CustomException("管理员不能操作");}// 模拟未知异常if (Math.random() > 0.8) {throw new RuntimeException("其他异常情况");}log.info("request test path :{}", value);return "访问成功";}/*** 拦截CustomException异常*/@ExceptionHandler(CustomException.class)@ResponseBodypublic String handleCustomException(CustomException ex) {return "自定义异常:" + ex.getMessage();}/*** 自定义异常*/public static class CustomException extends RuntimeException {public CustomException(String message) {super(message);}}/*** 熔断降级单独类处理*/public static class TestFallbackHandler {/*** 规则熔断处理** @param value 这里参数需要跟原参数对应,否则不能触发熔断方法* @param ex    必须有异常参数*/public static String testBlock(String value, BlockException ex) {return "当前访问过于频繁,请稍后再试";}/*** 业务降级处理** @param value 这里参数需要跟原参数对应,否则不能触发熔断方法* @param ex    必须有异常参数*/public static String testFallback(String value, Throwable ex) {log.warn("触发熔断降级,value={}, 异常类型: {}", value, ex.getClass().getName());return "降级处理:" + ex.getClass().getName();}}
}

测试

访问http://localhost:8080/test 应该返回”访问成功“

访问成功

访问http://localhost:8080/test?value=admin 应该返回”自定义异常:管理员不能操作“ 的异常信息

自定义异常

频繁访问http://localhost:8080/test 应该返回”当前访问过于频繁,请稍后再试“

频繁访问

访问http://localhost:8080/test?value=test 随机数大于0.8 ”降级处理:java.lang.RuntimeException“

其他未知异常

控制台

  • 启动.bat脚本
title sentinel-dashboard
java -Dserver.port=8181 ^
-Dcsp.sentinel.dashboard.server=localhost:8181 ^
-Dproject.name=sentinel-dashboard ^
-jar sentinel-dashboard-1.8.8.jar

控制台

关键字:如何进行网页设计和网站制作_51网站空间还有吗_资源搜索器_八宿县网站seo优化排名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: