Sentinel preliminary preparation
- Download address:
https://github.com/alibaba/Sentinel/releases
Note: to download Jar package
- In the jar directory of sentinel, cmd executes the following command:
java -Dserver.port=8180 -Dcsp.sentinel.dashboard.server=localhost:8180 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar
**Note: * * execute the command according to your jar version number
- Access Sentinal services
Default web address:
http://localhost:8180
Default account and password: sentinel
1, Sentinel current limiting primer
1. Add dependency
<!--sentinel rely on,After adding this dependency,An interceptor object is added to the project,This object will Requests to this service,Intercept,After the request is intercepted, it will communicate with sentinel Compare the current limiting rules defined on the console, If within the allowable range,Then continue to visit,Otherwise, conduct current limiting or degradation--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
2. Modify the configuration file
- Add configuration at provider
spring: cloud: sentinel: transport: dashboard: localhost:8180 #n described here is the address of the sentinel console eager: true #After the service is started, a heartbeat message will be sent to the sentinel console
3. Create a Controller object for current limiting operation
- Add at provider
package com.jt.provider.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/provider") public class ProviderSentinelController { @GetMapping("/sentinel01") public String doSentinel01(){ return "sentinel 01 test ..."; } }
4. Access services
- Start the SCA provider service, and then access the specified service
- Refresh sentinel console to monitor information in real time, as shown in the figure
5. Set flow control
- Under the corresponding path, select flow control
- Set current limiting policy: direct flow first (default)
- Repeatedly refresh and access the consumer service to check whether the limited flow information output is available, as shown in the figure
2, Sentinel flow control rules
Set current limiting mode
1. Associated current limiting
- Add a method in ProviderSentinelController
@GetMapping("/sentinel02") public String doSentinel02(){ return "sentinel 02 test ..."; }
- The current limiting design in sentinel is as follows:
3. Open two test windows, access / provider/sentinel02 and check the status of / provider/sentinel01 (hand speed should be fast!)
2. Link current limiting
- Create a ResourceService class in the specified package
package com.jt.provider.service; @Service public class ResourceService{ @SentinelResource("doGetResource") public String doGetResource(){ return "doGetResource"; } }
- Add a method in ProviderSentinelController
@Autowired private ResourceService resourceService; @GetMapping("/sentinel03") public String doSentinel03() throws InterruptedException { resourceService.doGetResource(); return "sentinel 03 test"; }
- Configure the current limiting rules in sentinel, as shown in the figure
Setting modes and parameters
- After setting the link flow control rules, frequently access the current limiting link to detect whether 500 exceptions will occur
- Add configuration
**Note: * * when the flow control mode is link mode, if sentinel is later than 1.7.2, Sentinel Web filter will aggregate all URLs by default. The entry is sentinel_spring_web_context. Therefore, limiting the current of the specified link alone will not take effect. You need to add the following statement in application.yml to turn off URL PATH aggregation, as follows:
3, Sentinel downgrade rule
1. Write slow call method
- Add the doSentinel04 method in the ProviderController class to demonstrate the current limit under the slow call process based on this method
//AtomicLong class supports thread safe self increment and self decrement operations private AtomicLong atomicLong=new AtomicLong(1); @GetMapping("/sentinel04") public String doSentinel04() throws InterruptedException { //Get the value of the self incrementing object, and then add 1 long num=atomicLong.getAndIncrement(); if(num%2==0){//Simulate 50% slow call ratio Thread.sleep(200); } return "sentinel 04 test"; }
**Note: * * we set sleep in this method to demonstrate slow call (long response time)
2. Configuration degradation
Select the link to downgrade
**Prompt: * * this indicates that "slow call proportion" is selected for the fusing strategy, which means that when the number of requests exceeds 3, if 30% of the average response time exceeds 200 milliseconds, the request will be fusing for 10 seconds, and it will return to normal after 10 seconds.
3. Test
Refresh the specified link, conduct multiple access tests, and check whether the Blocked By Sentinel (flow Limiting) content appears on the page
Sentinel exception handling
1. Create interceptor object
- Time access control of control methods based on interceptors in Spring MVC?
package com.jt.provider.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.time.LocalTime; /** * Spring MVC Interceptor object in, * This object can be executed before you execute the target Controller method */ public class TimeInterceptor implements HandlerInterceptor { /** * This method is executed before the target Controller method you execute * @return The return value of true indicates that the request is released and the subsequent business of the request can be continued */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("===preHandle==="); //Business: allow access after 8:00 and before 9:00 p.m LocalTime now = LocalTime.now();//Get the current time, int hour = now.getHour();//Gets the hour unit of the current time if(hour<8||hour>=21) throw new RuntimeException("Please visit 8 at the specified time~21"); return true; } }
2. Configure interceptor
package com.jt; import com.jt.provider.interceptor.TimeInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class SpringWebConfig implements WebMvcConfigurer { //Register interceptor @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new TimeInterceptor()) .addPathPatterns("/provider/sentinel01"); } }
3. Detect the execution of interceptor
Access the / provider/sentinel01 path by time period to detect the execution of the interceptor