很多新手不知道如何在阿里巴巴设置开源哨兵限流方案。为了帮助大家解决这个问题,下面小编就为大家详细讲解一下。需要的人可以从中学习,希望你能有所收获。
Sentinel是阿里开源的限流方案框架,具有以下特点:
丰富的应用场景:哨兵承担了阿里巴巴双十一近10年流量提升的核心场景,如尖峰(即突发流量控制在系统容量范围内)、消息削峰填谷、集群流量控制、下游不可用应用实时融合等。
完备的实时监控:哨兵还提供实时监控功能。可以在控制台看到单机访问应用的二级数据,甚至500台以下集群的汇总操作。
广泛的开源生态:哨兵提供了与其他开源框架/库的现成集成模块,例如与Spring Cloud、Dubbo和gRPC的集成。您只需引入相应的依赖项并简单地配置它们,就可以快速访问Sentinel。
完善的 SPI 扩展点:哨兵提供简单完善的SPI扩展接口。您可以通过实现扩展接口来快速定制逻辑。例如,定制规则管理、适配动态数据源等。
第一,搭建监控平台。
1.下载相应的jar包,地址在https://github.com/alibaba/Sentinel/releases.
2.start Java-dserver . port=8080-dcsp . sentinel . dashboard . server=localhost :8080-dpproject . name=sentinel-dashboard-jar-dsen tiel . dashboard . auth . usernam . e=sentinel-dsen tiel . dashboard . auth . password=123456 sentinel-dashboard-1 . 6 . 3 . jar
-dsen tiel . dashboard . auth . username指定登录名。
-dsen tiel . dashboard . auth . password指定登录密码。
3.访问localhost:8080并输入用户密码。
4.看到以下效果,构建成功。
第二,客户端使用。
以注释的使用为例。
1.maven依赖。
?xmlversion='1.0 '编码='UTF-8 '?
project xmlns=' http://aven . Apache . org/POM/4 . 0 . 0 '
xmlns : xsi=' http://www . w3 . org/2001/XMLSchema-instance '
xsi : schema location=' http://aven . Apache . org/POM/4 . 0 . 0http://aven . Apache . org/xsd/maven-4 . 0 . 0 . xsd '
模型版本4 . 0 . 0/模型版本
artifactIdsentinel-演示-注释-spring-aop/artifactId
父母
groupIdorg.springframework.boot/groupId
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.配置限流规则方案
/* * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.csp.sentinel.demo.annotation.aop.config; import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import java.util.ArrayList; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author Eric Zhao */ @Configuration public class AopConfiguration { @Bean public SentinelResourceAspect sentinelResourceAspect() { //配置固定限流规则,每秒为20qps,真对test List<FlowRule> rules = new ArrayList<>(); FlowRule rule = new FlowRule(); rule.setResource("test"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // Set limit QPS to 20. rule.setCount(20); rules.add(rule); FlowRuleManager.loadRules(rules); return new SentinelResourceAspect(); } }
3.实际应用
/* * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.csp.sentinel.demo.annotation.aop.service; import org.springframework.stereotype.Service; import com.alibaba.csp.sentinel.annotation.SentinelResource; /** * @author Eric Zhao */ @Service public class TestServiceImpl implements TestService { @Override @SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class}) public void test() { System.out.println("Test"); } @Override @SentinelResource(value = "hello", fallback = "helloFallback") public String hello(long s) { if (s < 0) { throw new IllegalArgumentException("invalid arg"); } return String.format("Hello at %d", s); } @Override @SentinelResource(value = "helloAnother", defaultFallback = "defaultFallback", exceptionsToIgnore = {IllegalStateException.class}) public String helloAnother(String name) { if (name == null || "bad".equals(name)) { throw new IllegalArgumentException("oops"); } if ("foo".equals(name)) { throw new IllegalStateException("oops"); } return "Hello, " + name; } public String helloFallback(long s, Throwable ex) { // Do some log here. ex.printStackTrace(); return "Oops, error occurred at " + s; } public String defaultFallback() { System.out.println("Go to default fallback"); return "default_fallback"; } }
/* * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.csp.sentinel.demo.annotation.aop.controller; import com.alibaba.csp.sentinel.demo.annotation.aop.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @author Eric Zhao */ @RestController public class DemoController { @Autowired private TestService service; @GetMapping("/foo") public String apiFoo(@RequestParam(required = false) Long t) throws Exception { if (t == null) { t = System.currentTimeMillis(); } service.test(); return service.hello(t); } @GetMapping("/baz/{name}") public String apiBaz(@PathVariable("name") String name) { return service.helloAnother(name); } }
4.启动应用,-Dproject.name=test -Dcsp.sentinel.dashboard.server=localhost:8080 访问接口就可以看到监控信息了
-Dproject.name=test 指定监控名称
-Dcsp.sentinel.dashboard.server 指定监控平台地址
三、简单基于注解原理分析
1.sentinel 主要的核心功能由sentinel-core 提供,基于注解的应用主要是依赖于切面的使用,具体的实现如下
/* * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.csp.sentinel.annotation.aspectj; import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import java.lang.reflect.Method; /** * Aspect for methods with {@link SentinelResource} annotation. * * @author Eric Zhao */ @Aspect public class SentinelResourceAspect extends AbstractSentinelAspectSupport { @Pointcut("@annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)") public void sentinelResourceAnnotationPointcut() { } @Around("sentinelResourceAnnotationPointcut()") public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable { Method originMethod = resolveMethod(pjp); SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class); if (annotation == null) { // Should not go through here. throw new IllegalStateException("Wrong state for SentinelResource annotation"); } String resourceName = getResourceName(annotation.value(), originMethod); EntryType entryType = annotation.entryType(); int resourceType = annotation.resourceType(); Entry entry = null; try { entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs()); Object result = pjp.proceed(); return result; } catch (BlockException ex) { return handleBlockException(pjp, annotation, ex); } catch (Throwable ex) { Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore(); // The ignore list will be checked first. if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) { throw ex; } if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) { traceException(ex, annotation); return handleFallback(pjp, annotation, ex); } // No fallback function can handle the exception, so throw it out. throw ex; } finally { if (entry != null) { entry.exit(1, pjp.getArgs()); } } } }
与单机的应用其实是一样的,初始化规则,调用SphU的entry方法,判断限流机制
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/38491.html