This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Use Sentinel Dashboard for flow control configuration

Flow control rules are configured on Sentinel Dashboard to achieve dynamic configuration of flow control. The steps are as follows:

  1. Start the Sentinel Dashboard server
  2. Add the following configuration to application.yml:
spring:
	application:
		name: spring-cloud-sentinel
	cloud:
		sentinel:
			transport:	
				dashboard: 127.0. 01.: 8080
Copy the code

Spring. Cloud. Sentinel. Transport. Dashboard configuration to sentinel dashboard server address, used to implement the distribution of flow monitoring and control flow control rules.

Define a REST interface that needs to limit the flow as follows:

@RestController
@RequestMapping("/sentinel")
public class SentinelController {

    @GetMapping("limit")
    public String limit(a){
        return "Sentinel DashBoard Flow Limit"; }}Copy the code

There is no need to add any resource burying points, and Sentinel by default limits the flow of all requests.

Access the corresponding interface. Because no flow control rule is configured, the traffic is not restricted by Sentinel

Enter the Sentinel Dashboard to configure flow control rules. The steps are as follows:

  1. Start the Sentinel Dashboard – 1.3.0. Jar
  2. Go to http://127.0.0.1:8888 to configure flow control rules for Sentinel Dashboard
  3. Go to the menu corresponding to spring.application.name and access the cluster point link. In this list, you can see the resource name of the /limit interface.
  4. For the /limit resource name, click the “Flow Control” button to set the flow control rule

All the configuration information in the new rule is actually the corresponding property Settings in FlowRole. The single-machine threshold is set to 1 in the test effect

Visit http://127.0.0.1:8080/sentinel/limit again, after the completion of the new interface, when the QPS is greater than 1, can see the corresponding current limiting effect

Blocked By Sentinel(flow limiting)
Copy the code

The user-defined URL traffic limiting is abnormal

By default, the URL trigger flow limit returns directly

Blocked By Sentinel(flow limiting)
Copy the code

In practice, most of the data is in JSON format, so the data returned in JSON format when traffic limiting is triggered can be modified by customizing traffic limiting exceptions to implement UrlBlockHandler and rewrite the Blocked method.

@Service
public class CustomUrlBlockHandler implements UrlBlockHandler{
    	@Override
    	public void blocked(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,BlockException e) throws IOException{
            httpServletResponse.setHeader("Content-Type"."application/json; charset=UTF-8");
            String msg="Number of requests exceeded the maximum, please try again later!"; httpServletResponse.getWirter().write(msg); }}Copy the code

In another case, after traffic limiting is triggered, the corresponding degraded page is directly redirected. You can add the following configuration to achieve this

spring:
	cloud:
		sentinel:
			serlvet:
				block-page: Degrade the page URL
Copy the code