查询系统网站模板浙江做网站平台的科技公司
查询系统网站模板,浙江做网站平台的科技公司,怎么让百度蜘蛛围着网站爬取,购物网站模板免费SpringCloudAlibaba之Sentinel简单使用 文章目录 SpringCloudAlibaba之Sentinel简单使用sentinel入门资源定义SphU(抛出异常方式)SphO(布尔类型方式)SentinelResource(注解的方式定义)SentinelResource使用前置条件使用SentinelResource定义资源定义blockHandler和fallback方法…SpringCloudAlibaba之Sentinel简单使用 文章目录 SpringCloudAlibaba之Sentinel简单使用sentinel入门资源定义SphU(抛出异常方式)SphO(布尔类型方式)SentinelResource(注解的方式定义)SentinelResource使用前置条件使用SentinelResource定义资源定义blockHandler和fallback方法(方法名称和sentinelResoruce的参数名称需要一直并且方法必须是public修饰)SentinelResoruce参数释义完整的service代码(资源定义) 定义sentinel规则校验规则是否生效 使用sentinel三部曲
定义资源定义规则检验规则是否生效
sentinel入门
创建基础模块sentinel-boot-service
引入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-core/artifactIdversion1.8.0/version
/dependency创建主类
SpringBootApplication
public class SentinelBootServiceApplication {public static void main(String[] args) {SpringApplication.run(SentinelBootServiceApplication.class,args);}
}创建application.yml
server:port: 8080
资源定义
定义sentinel资源sentinel中的资源可以是一段代码一个方法甚至是整个应用。sentinel可以保护整个应用
资源必须定义资源名称
创建service/SentinelTestService
SphU(抛出异常方式)
public String sphuTest(){// 1.5.0 版本开始可以直接利用 try-with-resources 特性try(Entry ignored SphU.entry(sphuTest)) {// 被保护的逻辑return hello world;} catch (BlockException ex) {// 处理被流控的逻辑return blocked;}
}Sphu.entry(resourceName) 可以定义一段sentinel资源当访问超出sentinel定义的规则后Sphu.entry方法会抛出BlockException。
所以可以在try语句块中定义sentinel的资源也就是被保护的逻辑。在catch块中捕获BlockException用来对降级进行处理
SphO(布尔类型方式)
public String sphoTest() {if(SphO.entry(sphoTest)){try {return hello world;} finally{SphO.exit();}}else{//被限流了,逻辑处理return 被限流了;}
}SphO.entry(resourceName) 方法可以返回一个boolean值用于是否通过sentinel管控必须与SphO.exit()搭配使用
SentinelResource(注解的方式定义)
SentinelResource使用前置条件
由于SentinelResource是通过aop方式进行管控所以需要引入aspectj模块
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-annotation-aspectj/artifactIdversion1.8.0/version
/dependency将SentinelResourceAspect注入到Spring容器中
sentinel-boot-service 的config包下创建SentinelConfig
Configuration
public class SentinelConfig {//注入Beanpublic SentinelResourceAspect sentinelResourceAspect(){return new SentinelResourceAspect();}
}使用SentinelResource定义资源
private static AtomicInteger atomicInteger new AtomicInteger(0);SentinelResource(value annotationTest,blockHandler blockHandler,fallback fallback)public String annotationTest(){int i atomicInteger.incrementAndGet();if(i % 2 0){return hello world;}else{throw new RuntimeException(something wrong);}}blockHandler和fallback是两个概念
blockHandler是sentinel对资源进行限流或者熔断时的处理规则
fallback是资源抛出异常后对异常捕获后的处理
定义blockHandler和fallback方法(方法名称和sentinelResoruce的参数名称需要一直并且方法必须是public修饰) public String blockHandler(BlockException ex){ex.printStackTrace();return block handler;}public String fallback(){return fallback;}所以上述代码有三段逻辑第一段正常逻辑返回hello world第二段抛出异常被fallback捕获返回fallback,第三段被sentinel限流被blockHandler处理返回block handler
SentinelResoruce参数释义
参数名称类型描述valueString必需项用于指定资源的名称。这个名称通常用于在 Sentinel Dashboard 中进行监控和管理。entryTypeEntryType可选项指定 entry 的类型。默认为 EntryType.OUT表示这是一个出站调用。blockHandlerString可选项指定处理 BlockException 的函数名称。当原方法被限流或熔断时会调用这个函数进行异常处理。该函数的访问范围需要是 public返回类型需要与原方法相匹配参数类型需要和原方法相匹配并且最后加一个额外的参数类型为 BlockException。默认情况下这个函数需要和原方法在同一个类中。blockHandlerClassClass?可选项当希望使用其他类的函数作为 blockHandler 时可以指定这个类的 Class 对象。注意对应的函数必须为 static 函数。fallbackString可选项指定回退函数名称当原方法调用失败时会调用这个回退函数。回退函数的签名返回值类型、参数类型需要与原方法一致。fallbackClassClass?可选项当希望使用其他类的函数作为回退函数时可以指定这个类的 Class 对象。同样对应的函数必须为 static 函数。resourceTypeint可选项用于指定资源的类型。这通常用于区分不同类型的资源以便进行更精细的控制。exceptionsToIgnoreClass? extends Throwable[]可选项指定需要忽略的异常类型。当原方法抛出这些异常时Sentinel 不会触发限流或熔断逻辑。
这个注解允许开发者对方法进行细粒度的流量控制同时提供了异常处理和回退机制确保系统的可靠性和稳定性。通过合理配置这些参数可以更加精准地控制应用的流量避免由于流量过大或异常导致的系统崩溃。
完整的service代码(资源定义)
Service
public class SentinelTestService {private static AtomicInteger atomicInteger new AtomicInteger(0);public String sphuTest(){// 1.5.0 版本开始可以直接利用 try-with-resources 特性try(Entry ignored SphU.entry(sphuTest)) {// 被保护的逻辑return hello world;} catch (BlockException ex) {// 处理被流控的逻辑return blocked;}}public String sphoTest() {if(SphO.entry(sphoTest)){try {return hello world;} finally{SphO.exit();}}else{//被限流了,逻辑处理return 被限流了;}}SentinelResource(value annotationTest,blockHandler blockHandler,fallback fallback)public String annotationTest(){int i atomicInteger.incrementAndGet();if(i % 2 0){return hello world;}else{throw new RuntimeException(something wrong);}}public String blockHandler(BlockException ex){ex.printStackTrace();return block handler;}public String fallback(){return fallback;}
}定义sentinel规则
sentinel的规则有很多种此文章示例仅用流控来测试sentinel定义的资源信息
在config/SentinelRuleConfig下分别创建上面三个资源的流控规则如下
Configuration
public class SentinelRuleConfig {PostConstructpublic void init(){ListFlowRule rules new ArrayList();FlowRule rule new FlowRule();//定义规则适用的资源名称rule.setResource(sphuTest);rule.setGrade(RuleConstant.FLOW_GRADE_QPS);//定义每秒被保护的代码块最多运行2次rule.setCount(2);rules.add(rule);FlowRule sphoTestRule new FlowRule();//定义规则适用的资源名称sphoTestRule.setResource(sphoTest);sphoTestRule.setGrade(RuleConstant.FLOW_GRADE_QPS);//定义每秒被保护的代码块最多运行2次sphoTestRule.setCount(2);rules.add(sphoTestRule);FlowRule annotationTestRule new FlowRule();//定义规则适用的资源名称annotationTestRule.setResource(annotationTest);annotationTestRule.setGrade(RuleConstant.FLOW_GRADE_QPS);//定义每秒被保护的代码块最多运行2次annotationTestRule.setCount(2);rules.add(annotationTestRule);FlowRuleManager.loadRules(rules);}
}setResource设置资源名称setGrade设置流控的类型此处通过QPS即每秒的访问量setCount: 设置每秒访问量的阈值当大于2时触发sentinel流控
校验规则是否生效
新建入口SentinelTestController并分别创建三个uri对应三个资源的访问
public class SentinelTestController {Resourceprivate SentinelTestService sentinelTestService;GetMapping(/sphuTest)public String sphuTest(){return sentinelTestService.sphuTest();}GetMapping(/sphoTest)public String sphoTest(){return sentinelTestService.sphoTest();}GetMapping(/annotationTest)public String annotationTest(){return sentinelTestService.annotationTest();}
}启动sentinel-boot-service项目模块并通过浏览器访问
sphuTest
访问sphuTest,可以看到正常返回hello world
http://localhost:8080/sentinel/sphuTest
- hello world快速刷新sphuTest可以看到blocked和hello world出现
http://localhost:8080/sentinel/sphuTest
- hello world
- blocked sphotest
访问sphOTest,可以看到正常返回hello world
http://localhost:8080/sentinel/sphoTest
- hello world快速刷新sphoTest可以看到被限流了和hello world出现
http://localhost:8080/sentinel/sphoTest
- hello world
- 被限流了annotationTest
低频率刷新annotationTest,可以看到hello world和fallback交替出现这里由于业务逻辑是执行一次抛一次异常异常捕获被fallback执行所以交替出现
http://localhost:8080/sentinel/annotationTest
- hello world
- fallback
- hello world
- fallback
- hello world
- fallback高频率刷新可以看到hello world、fallback、block handler都会出现。
在高频率刷新中可能会执行正常逻辑未限流未异常情况可能执行到fallback逻辑异常情况block handler达到设定的流控QPS阈值
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/86836.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!