This is the seventh day of my participation in the First Challenge 2022
preface
- Caching, fusing, and current limiting are very important concepts in high concurrency architecture. In many companies, relevant experience is considered during the interview.
- Cache mainly improves the system access speed and concurrency. Fuse A measure to prevent a service from being unavailable due to a heavy service load. Traffic limiting is a measure to control the system access volume to protect the entire system.
- The common limiting algorithms are: counter, leaky bucket algorithm, token bucket algorithm.
counter
- Counter flow limiting means that a certain number of access requests are allowed within a fixed period of time, and subsequent requests are rejected if access requests reach the upper limit within a certain period of time.
- The advantage of counter current limiting is that the algorithm implementation is relatively simple. The disadvantage is that the allocation is uneven and there are critical problems: for example, 100 requests per minute are allowed, and the maximum number of requests is reached in the first 10 seconds, resulting in all requests being rejected in the next 50 seconds.
Leakage bucket traffic limiting algorithm
- Leaky bucket flow limiting algorithm means that a leaky bucket of a fixed size flows out water at a fixed rate. If there is no water in the leaky bucket, the water does not flow out (no request is made). If too much water is in circulation, the excess water is discarded (too many requests are made).
- The advantage is that request resources can be evenly distributed. The disadvantage is that the average allocation of request resources leads to the inability to handle the burst of request traffic.
Token bucket traffic limiting algorithm
- Token bucket traffic limiting algorithm refers to that there is a bucket with a fixed size and the number of tokens is put into the bucket at a fixed rate. When the number of tokens in the bucket is full, new tokens are rejected. When a request comes in, it goes to the bucket and gets a token. If it gets a token, the request is approved. If it doesn’t get a token, the request is rejected.
- Compared with the leak bucket traffic limiting algorithm, the token bucket traffic limiting algorithm can not only limit the average request resources, but also cope with the unexpected request traffic.
Gateway + Redis implements the token bucket traffic limiting algorithm
- The token bucket traffic limiting algorithm is implemented in Guava and Sentinel.
- This demo uses Gateway + Redis to achieve the token bucket algorithm, first through idea to create a Springboot project, and the introduction of Gateway and Redis related to maven configuration.
<! --> <dependency> <groupId>com.alibaba.cloud</groupId> < artifactId > spring - the cloud - starter - alibaba - nacos - discovery < / artifactId > < version > 2.2.3. RELEASE < / version > < / dependency > <! - the gateway configuration - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - the cloud - starter - gateway < / artifactId > < version > 2.2.1. RELEASE < / version > < / dependency > <! - springCloud configuration - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - cloud - starter < / artifactId > < version > 2.2.2. RELEASE < / version > < / dependency > <! - the client load balancing loadbalancer - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - the cloud - starter - loadbalancer < / artifactId > < version > 2.2.2. RELEASE < / version > < / dependency > <! -- redis --> <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - starter - data - redis < / artifactId > < version > 2.2.4. RELEASE < / version > < / dependency >Copy the code
- Note: This demo registry uses NACOS, so the @enableDiscoveryClient annotation needs to be added to the project startup class to register the service with NACOS.
- Note: The reason for adding maven configuration to loadbalancer is that Alibaba deleted the Ribbon module in NacOS in 2021, so the Gateway cannot route to the specified module through LB, causing 503 error.
- Configure the registry NACOS and Gateway service access routing configuration in the configuration file bootstrap.yml. The configuration actually goes into the nacOS configuration center, which is written directly in the configuration file for the convenience of this article.
Server: port: 80 Spring: Application: name: gateway #redis: database: 0 host: localhost port: 6379 jedis: pool Max-active: -1 # Maximum number of connections in the connection pool (negative value indicates no limit) max-idle: 500 # Maximum number of idle connections in the connection pool min-idle: 50 # Minimum number of idle connections in the connection pool max-wait: -1 # Maximum waiting time for connection pool blocking (negative value indicates no limit) timeout: 30000 Cloud: nacos: discovery: server-addr: localhost:8848 Namespace: 6920d843-2f88-4e3e-8532-4e5ce04b5eed group: DEV_GROUP gateway: discovery: # Whether to combine with the service discovery component and forward to the specific service instance through serviceId (must be capitalized). Default is false. If this parameter is set to true, route creation using the automatic serviceId function of the service center is enabled. True # Route access mode: http://Gateway_HOST:Gateway_PORT/ Capitalized serviceId/**. The default value of the micro service application name is capitalized routes. Demo # unique serviceId URI: lb:// Demo # lb:// This parameter is fixed, indicating that load balancing is enabled. Predicates: -path =/demo/** # Match the filter route predicates: -stripprefix =1 # Skip the specified Path. Name: RequestRateLimiter args: The average replenishment rate of the token bucket per second, i.e., line equivalent to the average number of requests allowed to be processed by the user per second redis-rate-limiter.replenishrate: 1 # The capacity of the token bucket, the maximum number of requests allowed to be completed in one second redis-rate-limiter.burstCapacity: 2 # The name of the Bean object that parsed the key used to limit the flow. It uses SpEL expressions to get Bean objects from the Spring container based on #{@beanname}. key-resolver: "#{@apiKeyResolver}" #======================================================================Copy the code
- You also need to configure the filtering criteria for the gateway, such as the access URL, IP address, and user information.
/** * @Author: ZRH * @Date: 2022/2/21 20:45 */ @Configuration public class KeyResolverConfig { @Bean public KeyResolver apiKeyResolver () { // Return exchange -> Mono. Just (exchange.getrequest ().getPath().toString())); return exchange -> Mono. } // @bean public KeyResolver userKeyResolver () {return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user")); } // @bean public KeyResolver ipKeyResolver () {return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName()); }}Copy the code
- Then create a Springboot service named Demo and register it in NACOS. Open a controller interface. The final project structure is as follows:
- Access interface: http://localhost/demo/demo/index
- If the interface access frequency is too high, the following conditions occur: Traffic is restricted
The last
- The next article will cover the source code parsing performed by its Gateway+Redis limited flow filter.
- Learn with an open mind and make progress together