In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Alibaba open source Sentinel limit solution is how to build, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Sentinel is a current-limiting solution framework of Ali open source, which has the following characteristics:
Rich application scenarios: Sentinel takes on the core scenarios of Alibaba's Singles Day traffic promotion in the past 10 years, such as second kill (that is, burst traffic control within the system capacity), message peak cutting and valley filling, cluster flow control, real-time fuse downstream unavailable applications, and so on.
Complete real-time monitoring: Sentinel also provides real-time monitoring function. You can see the second-rate data of a single machine connected to the application in the console, and even the summary operation of less than 500 clusters.
Broad open source ecology: Sentinel provides out-of-the-box integration modules with other open source frameworks / libraries, such as Spring Cloud, Dubbo, and gRPC. You only need to introduce the appropriate dependencies and make a simple configuration to quickly access Sentinel.
Perfect SPI extension point: Sentinel provides easy-to-use and perfect SPI extension interface. You can quickly customize the logic by implementing an extension interface. Such as customized rule management, adapting to dynamic data sources, and so on.
First, set up a monitoring platform
1. Download the appropriate jar package at https://github.com/alibaba/Sentinel/releases
two。 Start java-Dserver.port=8080-Dcsp.sentinel.dashboard.server=localhost:8080-Dproject.name=sentinel-dashboard-jar-Dsentinel.dashboard.auth.username=sentinel-Dsentinel.dashboard.auth.password=123456 sentinel-dashboard-1.6.3.jar
-Dsentinel.dashboard.auth.username specifies the login name
-Dsentinel.dashboard.auth.password specifies the login password
3. Access localhost:8080 and enter user password
4. You can see the following effect: the construction is successful
II. Use by the client
Take the use of annotations as an example
1.maven dependence
4.0.0 sentinel-demo-annotation-spring-aop org.springframework.boot spring-boot-starter-parent 2.0.6.RELEASE com.alibaba.csp sentinel-core 1. 6.3 com.alibaba.csp sentinel-transport-simple-http 1.6.3 com.alibaba.csp sentinel-annotation-aspectj 1.6.3 org.springframework.boot spring-boot-starter-aop org.springframework.boot spring-boot-starter-web
two。 Configure current-limiting rule scheme
/ * * 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 "ASIS" 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 * / @ Configurationpublic class AopConfiguration {@ Bean public SentinelResourceAspect sentinelResourceAspect () {/ / configure a fixed current limit rule, 20qps per second, true to test List 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. Practical application
/ * * 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 "ASIS" 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 * / @ Servicepublic 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 "ASIS" 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 * / @ RestControllerpublic 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. Start the application, and the-Dproject.name=test-Dcsp.sentinel.dashboard.server=localhost:8080 access interface can see the monitoring information.
-Dproject.name=test specifies the monitoring name
-Dcsp.sentinel.dashboard.server specifies the address of the monitoring platform
Third, simple analysis based on the principle of annotation
The main core functions of 1.sentinel are provided by sentinel-core, and annotation-based applications mainly rely on the use of aspects, as follows
/ * * 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 "ASIS" 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 * / @ Aspectpublic 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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.