In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how sentinel integrates spring cloud current restrictions. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Spring cloud invokes services based on http, and the general process is as follows:
Service provider: provides http interface and registers service information with the service center
Service consumer: take the http interface of the server as a local service, read the information of the service provider from the registry, and initiate remote calls using feign
Related dependency com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-sentinel example
In order to simplify the processing, we directly limit the flow of the url interface without making service calls.
Application.yml
Spring: application: name: hello-sentinel cloud: nacos: discovery: server-addr: localhost:8848 sentinel: transport: dashboard: localhost:8081
Current restriction can be configured locally or sentinel dashboard
HelloController
@ RestControllerpublic class HelloController {@ SentinelResource (value = "hello", blockHandler = "blockHandle") @ RequestMapping ("/ hello") public String hello () {return "hello";} public String blockHandle (BlockException e) {e.printStackTrace (); return "is restricted";}
************
Local current limit configuration
CustomFlowRule
Public class CustomFlowRule implements InitFunc {@ Override public void init () throws Exception {List flowRules = new ArrayList (); FlowRule flowRule = new FlowRule (); flowRule.setResource ("hello"); flowRule.setCount (1); flowRule.setControlBehavior (RuleConstant.CONTROL_BEHAVIOR_DEFAULT); flowRules.add (flowRule); FlowRuleManager.loadRules (flowRules);}}
META-INF/services/com.alibaba.csp.sentinel.init.InitFunc
Com.example.demo.rule.CustomFlowRule
Jmeter test
2022-03-27 22 INFO 30 Tomcat started on port 50.534 INFO 1791-[main] o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat started on port (s): 8080 (http) with context path '2022-03-27 22 22 30 INFO 50.547 INFO 1791-[main] c.a.c.n.registry.NacosServiceRegistry: nacos registry DEFAULT_GROUP hello-sentinel 192.168.5.11 main 8080 register finished2022-03-27 22 INFO 30 main 50.557 INFO 1791-[main] com.example.demo.DemoApplication: Started DemoApplication in 2.227 seconds (JVM running for 2.824) 2022-03-27 22 22 31V Vol 04.044 INFO 1791-[nio-8080-exec-1] o.a.c..Tomcat. [localhost]. : Initializing Spring DispatcherServlet 'dispatcherServlet'2022-03-27 22 nio-8080-exec-1 31lo 04.044 INFO 1791-[nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Initializing Servlet' dispatcherServlet'2022-03-27 2222 31V 04.049 INFO 1791-[nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Completed initialization in 5 msINFO: Sentinel log output type is: fileINFO: Sentinel log charset is: utf-8INFO: Sentinel log Base directory is: / Users/huli/logs/csp/INFO: Sentinel log name use pid is: falsecom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow .FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowException
************
Sentinel dashboard configuration current limit
Start sentinel dashboard
Java-Dserver.port=8081-Dcsp.sentinel.dashboard.server=localhost:8081-Dproject.name=sentinel-dashboard-jar sentinel-dashboard.jar parameter description:-Dserver.port=8081: specify console startup port-Dcsp.sentinel.dashboard.server: specify console address and port-Dproject.name=sentinel-dashboard: specify console project name
Localhost:8081, configure the flow control policy in the console
Note: to use the local degradation method, you need to configure the flow control rules in the hello below
Jmeter test
2022-03-28 08 INFO 50 Tomcat started on port 29.165 INFO 853-[main] o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat started on port (s): 8080 (http) with context path '2022-03-28 08 Tomcat started on port 50 553-[main] c.a.c.n.registry.NacosServiceRegistry: nacos registry DEFAULT_GROUP hello-sentinel 192.168.5.11 DEFAULT_GROUP hello-sentinel 192.168.5.11 INFO 8080 com.example.demo.DemoApplication-- [main] com.example.demo.DemoApplication: Started DemoApplication in 3.315 seconds (JVM running for 4.03) 2022-03-28 08 DEFAULT_GROUP hello-sentinel 5292 INFO 853-[nio-8080-exec-2] o.a.c.C. [tomcat]. [localhost]. [/] : Initializing Spring DispatcherServlet 'dispatcherServlet'2022-03-28 008 INFO 52INFO 853-[nio-8080-exec-2] o.s.web.servlet.DispatcherServlet: Initializing Servlet' dispatcherServlet'2022-03-28 00880 52Partition 05.802 INFO 853-[nio-8080-exec-2] o.s.web.servlet.DispatcherServlet: Completed initialization in 9 msINFO: Sentinel log output type is: fileINFO: Sentinel log charset is: utf-8INFO: Sentinel log Base directory is: / Users/huli/logs/csp/INFO: Sentinel log name use pid is: falsecom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow FlowExceptioncom.alibaba.csp.sentinel.slots.block.flow.FlowException on "how sentinel integrates spring cloud flow restrictions" ends here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
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.