本文主要是介绍Sentinel入门(一) Sentinel在Dubbo的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- Demo
- Dubbo Consumer Sentinal Filter
- Dubbo Provider Sentinal
- Sentinel实现原理
Demo
Consumer.java
public static void main(String[] args) {FlowRule flowRule = new FlowRule();flowRule.setResource("com.alibaba.cloud.examples.FooService:hello(java.lang.String)");// 设置1s内只能调用10次flowRule.setCount(10);flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);flowRule.setLimitApp("default");// 注册限流规则FlowRuleManager.loadRules(Collections.singletonList(flowRule));SpringApplicationBuilder consumerBuilder = new SpringApplicationBuilder();ApplicationContext applicationContext = consumerBuilder.web(WebApplicationType.NONE).sources(SentinelDubboConsumerApp.class).run(args);FooServiceConsumer service = applicationContext.getBean(FooServiceConsumer.class);// 测试调用15次,期望应该是有5调用失败的for (int i = 0; i < 15; i++) {try {String message = service.hello("Jim");System.out.println((i + 1) + " -> Success: " + message);}catch (SentinelRpcException ex) {System.out.println("Blocked");}catch (Exception ex) {ex.printStackTrace();}}}
FooService服务在Sentinel下对应的资源名是 com.alibaba.cloud.examples.FooService:hello(java.lang.String) 。详细见下面的Filter的getResourceName
, 定义该资源名对应的限流规则:
public static String getResourceName(Invoker<?> invoker, Invocation invocation) {StringBuilder buf = new StringBuilder(64);buf.append(invoker.getInterface().getName()
这篇关于Sentinel入门(一) Sentinel在Dubbo的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!