Introduction to the

The current article demonstrates several configurations of active traffic limiting and custom bottom-pocket methods. The current environment has completed the integration of the NACOS registry with the Sentinel console. For details, see my previous articleCopy the code

Active current limiting

The Sentinel console limits traffic by resource name

/ * * *@SentinelResourceThe * value attribute of the annotation must be the same as the resource name of the configured hotspot rule. * The blockHandler attribute configates the current bottom-pocket method@param username
 * @return* /
@GetMapping("/testE")
@SentinelResource(value = "testE",blockHandler = "dealTestE")
public String testE(String username){
    return "Call to method E";
}

// If the interface is requested with parameters and more than two requests are made within 10 seconds, the bottom-feeding method is executed
public String dealTestE(String username,BlockException blockException){
    return "------------ Run bottom method --------------";
}
Copy the code

The Sentinel console configures traffic limiting based on the requested resource path

/** * The current flow control rule will trigger the default backpocket method * if the specified threshold is exceeded within one second@param username
 * @return* /
@GetMapping("/user/login")
@SentinelResource(value = "login")
public String login(String username){
    return "Url Traffic Limiting Test";
}
Copy the code

Custom bottom pocket method

In addition to the single pocket method, you can also configure the pocket to be placed in the class and the common pocket method.


// Fallback for downscaling and fusing
@GetMapping("/user/login")
@SentinelResource(value = "login",fallback = "deal_login")
public String login(String username){
    return "Url Traffic Limiting Test";
}

// The bottom line
public String deal_login (String username){
    return "------ URL traffic limiting successfully performed";
}




// Limit traffic and block blockHandler
@GetMapping("/testE")
@SentinelResource(value = "testE",blockHandler = "dealTestE")
public String testE(String username){
    return "Call to method E";
}

// The bottom line
public String dealTestE(String username,BlockException blockException){
    return "------------ Run bottom method --------------";
}
Copy the code

Configure the class and method of the bottom – pocket method

/** * Set the name of the class in which the current pocket method is sitting and the name of the method in the class@return* /
@GetMapping("/user/register")
@SentinelResource(value = "register",fallbackClass = FalbakcHandler.class,fallback = "dealRegister", blockHandlerClass = BlockHandler.class,blockHandler = "dealRegister")
public String dealRegister(a){
    return "Running successfully";
}
Copy the code