Interface current limiting method, system and device based on Kong gateway and medium

The method enhances Kong gateway's flow control by reading rules from a MySQL database and using Redis cache to enforce user-specific flow rules, addressing the lack of user differentiation in existing Kong gateway implementations.

CN119922136APending Publication Date: 2025-05-02BEIJING HUANENG XINRUI CONTROL TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510071307.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-16
Publication Date
2025-05-02

AI Technical Summary

Technical Problem

Kong gateway's rate-limiting plugin lacks the ability to set different flow rules for different users, failing to meet the diverse requirements of real-world scenarios where individual user-specific flow rules are necessary.

Method used

Implement a method that reads flow rules from a MySQL database, stores them in a HashMap, and uses Redis cache to track user access, employing custom Kong gateway plugins to manage user identities and interface information, allowing for user-specific flow control.

Benefits of technology

Enables differentiated flow control for various users by setting and enforcing user-specific flow rules, ensuring appropriate access management and preventing excessive requests.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119922136A_ABST
    Figure CN119922136A_ABST
Patent Text Reader

Abstract

The invention discloses an interface current limiting method, system and device based on a Kong gateway and a medium, and the method comprises the steps: reading a current limiting rule, and writing the read current limiting rule into a HashMap; the method comprises the following steps of: when a user accesses an interface, customizing a plug-in part-control by using a Kong gateway, and acquiring identity information consumerId and interface information serviceId of the user; judging whether a key value pair taking consumerIdserviceId as a key exists in the redis cache or not, if yes, judging whether a value in the redis cache conforms to a traffic limiting rule or not, if yes, enabling the value to be automatically increased by using a redTemplate.opsForValue.incruption method, then normally accessing an interface, and if not, putting back an error prompt; and when the current limiting rule does not exist, a redTemplate.opsForValue.set () method is used, a key value pair is added in the redis cache, then the interface is normally accessed, and the method, the system, the equipment and the medium can set different current limiting rules for different users.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of gateway applications and relates to an interface current limiting method, system, device and medium based on Kong gateway. Background Art

[0002] Kong Gateway is an open source, scalable, microservice API gateway that has been widely used in organizations of all sizes around the world. The design principle of Kong Gateway is to handle service-to-service communication and achieve comprehensive management of API interfaces by providing a feature-rich platform.

[0003] In Kong Gateway, the rate limiting function is implemented by configuring the rate-limiting plugin. When a client requests an API, Kong Gateway checks the configured rate limiting rules. According to these rules, if the request rate exceeds the limit, the request will be blocked or rejected.

[0004] However, when many different users access the same interface, the rate-limiting plug-in can only perform unified flow limiting on the interface, and cannot set different flow limiting rules for different users. In actual business, there are many situations where different flow limiting rules need to be set for different users. Summary of the invention

[0005] The purpose of the present invention is to overcome the shortcomings of the above-mentioned prior art and provide an interface current limiting method, system, device and medium based on Kong gateway. The method, system, device and medium can set different current limiting rules for different users.

[0006] In order to achieve the above object, the present invention adopts the following technical scheme:

[0007] In one aspect, the present invention provides an interface current limiting method based on Kong gateway, comprising:

[0008] Read the current limiting rules and write the read current limiting rules into HashMap;

[0009] When the user accesses the interface, the kong gateway custom plug-in param-constriant is used to obtain the user's identity information consumerId and interface information serviceId;

[0010] Determine whether there is a key-value pair with consumerId_serviceId as the key in the redis cache. If it exists, determine whether the value in the redis cache meets the current limiting rules. If it does, use the redisTemplate.opsForValue.increment() method to increment the value and then access the interface normally. If it does not, return an error prompt; if it does not exist, use the redisTemplate.opsForValue.set() method to add a key-value pair in the redis cache and then access the interface normally.

[0011] The interface current limiting method based on Kong gateway described in the present invention is further improved in that:

[0012] Furthermore, before reading the current limiting rules, the following steps are also included:

[0013] Set the current limiting rules for users corresponding to different interfaces in the MySql database.

[0014] Furthermore, the current limiting rules include user id, interface id, current limiting period rateUnit and maximum number of access times rate within the period.

[0015] Furthermore, the key is set to the form of consumerId_serviceId, the value is set to 1, and the expiration time is set to the value of rateUnit read from the HashMap.

[0016] In a second aspect of the present invention, the present invention provides an interface current limiting system based on Kong gateway, comprising:

[0017] The writing module is used to read the current limiting rules and write the read current limiting rules into the HashMap;

[0018] The acquisition module is used when the user accesses the interface. The kong gateway custom plug-in param-constriant is used to obtain the user's identity information consumerId and interface information serviceId;

[0019] The current limiting module is used to determine whether there is a key-value pair with consumerId_serviceId as the key in the redis cache. If it exists, it determines whether the value in the redis cache meets the current limiting rules. If it does, the redisTemplate.opsForValue.increment() method is used to increase the value automatically, and then the interface is accessed normally. If it does not meet the requirements, an error prompt is returned; if it does not exist, the redisTemplate.opsForValue.set() method is used to add a key-value pair in the redis cache, and then the interface is accessed normally.

[0020] The interface current limiting system based on Kong gateway described in the present invention is further improved in that:

[0021] Furthermore, it also includes:

[0022] The setting module is used to set the current limiting rules for users corresponding to different interfaces in the MySql database.

[0023] Furthermore, the current limiting rules include user id, interface id, current limiting period rateUnit and maximum number of access times rate within the period.

[0024] Furthermore, the key is set to the form of consumerId_serviceId, the value is set to 1, and the expiration time is set to the value of rateUnit read from the HashMap.

[0025] In a third aspect of the present invention, the present invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the interface current limiting method based on the Kong gateway when executing the computer program.

[0026] In a fourth aspect of the present invention, the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the interface current limiting method based on the Kong gateway are implemented.

[0027] The present invention has the following beneficial effects:

[0028] The interface current limiting method, system, device and medium based on Kong gateway described in the present invention set the current limiting rules for users corresponding to different interfaces in the MySql database during specific operation, and use HashMap to store them, then use a custom Kong gateway plug-in to obtain the user identity information of the request interface, and use redis cache to store the number of times the user accesses the interface within a cycle, so as to achieve the purpose of setting different current limiting rules for different users. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] The accompanying drawings constituting a part of the present invention are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the accompanying drawings:

[0030] Figure 1 The figure is a flow chart of the method of the present invention. DETAILED DESCRIPTION

[0031] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0032] In the description of the present invention, it should be understood that the terms “include” and “comprises” indicate the presence of described features, wholes, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or collections thereof.

[0033] It should also be understood that the terms used in the present specification are only for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in the present specification and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include plural forms.

[0034] It should be further understood that the term "and / or" used in the present specification and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, and includes these combinations. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in the present invention generally indicates that the associated objects are in an "or" relationship.

[0035] It should be understood that, although the terms first, second, third, etc. may be used to describe preset ranges, etc. in the embodiments of the present invention, these preset ranges should not be limited to these terms. These terms are only used to distinguish preset ranges from each other. For example, without departing from the scope of the embodiments of the present invention, the first preset range may also be referred to as the second preset range, and similarly, the second preset range may also be referred to as the first preset range.

[0036] The word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrases "if it is determined" or "if (stated condition or event) is detected" may be interpreted as "when it is determined" or "in response to determining" or "when detecting (stated condition or event)" or "in response to detecting (stated condition or event)", depending on the context.

[0037] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of the embodiments. The components of the embodiments of the present invention described and shown in the drawings here can usually be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present invention provided in the drawings is not intended to limit the scope of the claimed invention, but merely represents selected embodiments of the present invention. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0038] Various structural schematic diagrams of the embodiments disclosed in the present invention are shown in the accompanying drawings. These figures are not drawn to scale, and some details are magnified and some details may be omitted for the purpose of clear expression. The shapes of various regions and layers shown in the figures and the relative sizes and positional relationships therebetween are only exemplary, and may deviate in practice due to manufacturing tolerances or technical limitations, and those skilled in the art may additionally design regions / layers with different shapes, sizes, and relative positions according to actual needs.

[0039] Embodiment 1

[0040] refer to Figure 1 The interface current limiting method based on Kong gateway described in the present invention comprises the following steps:

[0041] Use MySql relational database to store the current limiting rules for different users corresponding to each interface, read the current limiting rules into memory at regular intervals, use HashMap for storage, use custom kong gateway plug-in to obtain user identity information of the request interface, and use redis cache to store the number of times the user accesses the interface within a cycle.

[0042] When a user accesses the interface, the corresponding current limiting rules are read from the HashMap based on the obtained user identity information, and the number of times the interface is accessed in the current cycle is read from redis to compare whether it complies with the current limiting rules. If it does, the interface is accessed normally. If it does not, an error message is returned, indicating that the interface is accessed too frequently, please try again later, thereby realizing the current limiting function for different users.

[0043] Use MySql relational database to store the current limiting rules for each interface corresponding to different users, mainly storing 4 pieces of information, namely: user id, interface id, interface access current limiting period rateUnit and the maximum number of accesses rate within the current limiting period. Among them, rateUnit can be set to 1 second, 1 minute or 1 hour. By storing these 4 pieces of information, the interface current limiting rules for a specific interface of a user can be implemented.

[0044] The current limiting rules are read from the MySql database into the memory every 5 minutes and stored in HashMap. The key of HashMap is set in the format of consumerId_serviceId, where consumerId is the user ID, serviceId is the interface ID, and value is set in the json object format, which contains two attributes: rateUnit and rate. RateUnit is the current limiting period, and rate is the maximum number of visits within the current limiting period. HashMap can be used to quickly query the corresponding current limiting rules by key for subsequent judgment.

[0045] By using the param-constraint custom plug-in of the kong gateway, you can obtain the user's identity information and interface information, and get the consumerId and serviceId when the user accesses the interface.

[0046] Use redis cache to store the number of user visits within a current limiting period. Use the key expiration mechanism of redis to implement the current limiting function of the interface. When a user accesses the interface, first determine whether the user id and the access interface id are the key values ​​in redis. If not, it indicates that the user is accessing the interface for the first time within the current limiting period. At this time, use the redisTemplate.opsForValue.set() method to add a key-value pair to the redis cache, where the key is set in the form of consumerId_serviceId, the value is set to 1, and the expiration time is set to the value of rateUnit read from the HashMap, that is, 1 second, 1 minute or 1 hour; if it exists, compare whether the read value is greater than the rate value in the HashMap. If it is greater, an error prompt is returned, and the request is too frequent, please try again later. If it is less than or equal to, use the redisTemplate.opsForValue.increment() method to increase the value of consumerId_serviceId as the key in redis by 1.

[0047] The specific steps include:

[0048] 1) Set the current limiting rules for users corresponding to different interfaces in the MySql database, and the current limiting rules include user id, interface id, current limiting cycle rateUnit and maximum number of access times rate within the cycle;

[0049] 2) Write the read current limiting rules into the HashMap;

[0050] 3) User access interface;

[0051] 4) Use the kong gateway custom plug-in param-constriant to obtain the user's identity information consumerId and interface information serviceId;

[0052] 5) Determine whether there is a key-value pair with consumerId_serviceId as the key in redis. If it exists, determine whether the value of the read redis cache meets the current limiting rules. If it does, use the redisTemplate.opsForValue.increment() method to increment the value. If it does not, return an error prompt, that is, the request is too frequent, please try again later; if it does not exist, use the redisTemplate.opsForValue.set() method to add a key-value pair to the redis cache, where the key is set to consumerId_serviceId, the value is set to 1, and the expiration time is set to the value of rateUnit read from the HashMap, that is, 1 second, 1 minute or 1 hour;

[0053] 6) Access the interface normally.

[0054] Embodiment 2

[0055] The interface current limiting system based on Kong gateway of the present invention comprises:

[0056] The writing module is used to read the current limiting rules and write the read current limiting rules into the HashMap;

[0057] The acquisition module is used when the user accesses the interface. The kong gateway custom plug-in param-constriant is used to obtain the user's identity information consumerId and interface information serviceId;

[0058] The current limiting module is used to determine whether there is a key-value pair with consumerId_serviceId as the key in the redis cache. If it exists, it determines whether the value in the redis cache meets the current limiting rules. If it does, the redisTemplate.opsForValue.increment() method is used to increase the value automatically, and then the interface is accessed normally. If it does not meet the requirements, an error prompt is returned; if it does not exist, the redisTemplate.opsForValue.set() method is used to add a key-value pair in the redis cache, and then the interface is accessed normally.

[0059] The interface current limiting system based on Kong gateway described in the present invention is further improved in that:

[0060] Furthermore, it also includes:

[0061] The setting module is used to set the current limiting rules for users corresponding to different interfaces in the MySql database.

[0062] Furthermore, the current limiting rules include user id, interface id, current limiting period rateUnit and maximum number of access times rate within the period.

[0063] Furthermore, the key is set to the form of consumerId_serviceId, the value is set to 1, and the expiration time is set to the value of rateUnit read from the HashMap.

[0064] The division of modules in the embodiments of the present application is schematic and is only a logical function division. There may be other division methods in actual implementation. In addition, each functional module in each embodiment of the present application may be integrated into a processor, or may exist physically separately, or two or more modules may be integrated into one module. The above-mentioned integrated modules may be implemented in the form of hardware or in the form of software functional modules.

[0065] Embodiment 3

[0066] A computer device comprises a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of implementing the interface current limiting method based on Kong gateway are implemented, for example, comprising: reading the current limiting rules and writing the read current limiting rules into a HashMap; when a user accesses the interface, the kong gateway custom plug-in param-constriant is used to obtain the user's identity information consumerId and interface information serviceId; it is determined whether there is a key-value pair with consumerId_serviceId as the key in the redis cache. If it exists, it is determined whether the value in the redis cache complies with the current limiting rules. If it complies, the redisTemplate.opsForValue.increment() method is used to increment the value, and then the interface is accessed normally. If it does not comply, an error prompt is returned; if it does not exist, the redisTemplate.opsForValue.set() method is used to add a key-value pair in the redis cache, and then the interface is accessed normally. The memory may include a memory, such as a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk memory, etc. The processor, network interface, and memory are interconnected through an internal bus, and the internal bus may be an industrial standard architecture bus, a peripheral component interconnection standard bus, an extended industrial standard architecture bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. The memory is used to store programs. Specifically, the program may include a program code, and the program code includes computer operation instructions. The memory may include a memory and a non-volatile memory, and provide instructions and data to the processor.

[0067] Embodiment 4

[0068] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of implementing the interface current limiting method based on Kong gateway are, for example, including: reading the current limiting rules, and writing the read current limiting rules into a HashMap; when a user accesses the interface, using the Kong gateway custom plug-in param-constriant to obtain the user's identity information consumerId and interface information serviceId; judging whether there is a key-value pair with consumerId_serviceId as the key in the redis cache, and if so, judging whether the value in the redis cache complies with the current limiting rules, and if so, using the redisTemplate.opsForValue.increment() method to increment the value, and then access the interface normally, and if not, returning an error prompt; and if not, using the redisTemplate.opsForValue.set() method to add a key-value pair in the redis cache, and then access the interface normally. Specifically, the computer-readable storage medium includes, but is not limited to, for example, volatile memory and / or non-volatile memory. The volatile memory may include a random access memory (RAM) and / or a cache memory (cache), etc. The non-volatile memory may include a read-only memory (ROM), a hard disk, a flash memory, an optical disk, a magnetic disk, etc.

[0069] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0070] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0071] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0072] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0073] Those skilled in the art will readily appreciate other embodiments of the present invention after considering the specification and disclosure of the invention. This application is intended to cover any variations, uses or adaptations of the present invention that follow the general principles of the present invention and include common knowledge or customary techniques in the art that are not disclosed by the present invention. The specification and examples are to be considered exemplary only, and the true scope and spirit of the present invention are indicated by the following claims.

[0074] It should be understood that the present invention is not limited to the exact construction that has been described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the present invention is limited only by the appended claims.

[0075] The above description is only a preferred embodiment of the present invention and does not limit the present invention in any way. Any simple modification, change and equivalent structural change made to the above embodiment based on the technical essence of the present invention still falls within the protection scope of the technical solution of the present invention.

Claims

1. An interface current limiting method based on Kong gateway, characterized in that: include: Read the current limiting rules and write the read current limiting rules into HashMap; When the user accesses the interface, the kong gateway custom plug-in param-constriant is used to obtain the user's identity information consumerId and interface information serviceId; Determine whether there is a key-value pair with consumerId_serviceId as the key in the redis cache. If it exists, determine whether the value in the redis cache meets the current limiting rules. If it does, use the redisTemplate.opsForValue.increment() method to increment the value and then access the interface normally. If it does not, return an error prompt; if it does not exist, use the redisTemplate.opsForValue.set() method to add a key-value pair in the redis cache and then access the interface normally.

2. According to claim 1, the interface current limiting method based on Kong gateway is characterized in that: The reading current limiting rule also includes: Set the current limiting rules for users corresponding to different interfaces in the MySql database.

3. According to claim 2, the interface current limiting method based on Kong gateway is characterized in that: The current limiting rules include user id, interface id, current limiting period rateUnit and maximum number of access times rate within the period.

4. According to claim 1, the interface current limiting method based on Kong gateway is characterized in that: The key is set in the form of consumerId_serviceId, the value is set to 1, and the expiration time is set to the value of rateUnit read from the HashMap.

5. An interface current limiting system based on Kong gateway, characterized in that: include: The writing module is used to read the current limiting rules and write the read current limiting rules into the HashMap; The acquisition module is used when the user accesses the interface. The kong gateway custom plug-in param-constriant is used to obtain the user's identity information consumerId and interface information serviceId; The current limiting module is used to determine whether there is a key-value pair with consumerId_serviceId as the key in the redis cache. If it exists, it determines whether the value in the redis cache meets the current limiting rules. If it does, the redisTemplate.opsForValue.increment() method is used to increase the value automatically, and then the interface is accessed normally. If it does not meet the requirements, an error prompt is returned; if it does not exist, the redisTemplate.opsForValue.set() method is used to add a key-value pair in the redis cache, and then the interface is accessed normally.

6. The interface current limiting system based on Kong gateway according to claim 5 is characterized in that: Also includes: The setting module is used to set the current limiting rules for users corresponding to different interfaces in the MySql database.

7. The interface current limiting system based on Kong gateway according to claim 6 is characterized in that: The current limiting rules include user id, interface id, current limiting period rateUnit and maximum number of access times rate within the period.

8. The interface current limiting system based on Kong gateway according to claim 5 is characterized in that: The key is set in the form of consumerId_serviceId, the value is set to 1, and the expiration time is set to the value of rateUnit read from the HashMap.

9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the interface current limiting method based on the Kong gateway are implemented as described in any one of claims 1-4.

10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by the processor, the steps of the interface current limiting method based on the Kong gateway as described in any one of claims 1-4 are implemented.