Microservice - current limiting fuse

Sentinel preliminary preparation

  1. Download address:

Note: to download Jar package

  1. 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

  1. Access Sentinal services
    Default web address:

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-->

2. Modify the configuration file

  1. Add configuration at provider
         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

  1. 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;

public class ProviderSentinelController {
       public String doSentinel01(){
           return "sentinel 01 test  ...";

4. Access services

  1. Start the SCA provider service, and then access the specified service
  2. Refresh sentinel console to monitor information in real time, as shown in the figure

5. Set flow control

  1. Under the corresponding path, select flow control
  2. Set current limiting policy: direct flow first (default)
  3. 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

  1. Add a method in ProviderSentinelController
   public String doSentinel02(){
     return "sentinel 02 test  ...";
  1. 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

  1. Create a ResourceService class in the specified package
package com.jt.provider.service;
public class ResourceService{
    public String doGetResource(){
        return "doGetResource";
  1. Add a method in ProviderSentinelController
    private ResourceService resourceService;
    public String doSentinel03() throws InterruptedException {
        return "sentinel 03 test";
  1. Configure the current limiting rules in sentinel, as shown in the figure

    Setting modes and parameters
  2. After setting the link flow control rules, frequently access the current limiting link to detect whether 500 exceptions will occur
  3. 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

  1. 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);
    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
        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

  1. 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
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //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
            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;

public class SpringWebConfig implements WebMvcConfigurer {
    //Register interceptor
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TimeInterceptor())

3. Detect the execution of interceptor

Access the / provider/sentinel01 path by time period to detect the execution of the interceptor

4, Sentinel hotspot data current limiting

5, Sentinel system rule application

5, sentinel authorization rule application

Keywords: Java Spring Microservices

Added by idealbrain on Fri, 29 Oct 2021 14:37:44 +0300