Disclosure of Invention
The invention provides a resource flow limiting method, a device, electronic equipment and a storage medium, which are used for solving the problems of missing code resources and non-unique labeling caused by manually labeling the code resources in the current resource flow limiting method.
In a first aspect, the present invention provides a resource flow limiting method, including:
acquiring a resource name of a target resource from a byte code by using a packet path, wherein the packet path is a path appointed by the acquisition of the target resource, and the byte code is the content after code compilation with business logic;
acquiring configuration information corresponding to the target resource by utilizing the resource name, wherein the configuration information stores a current limiting strategy and a current limiting parameter related to the target resource, and the configuration information is stored in one-to-one correspondence with the resource name;
and executing the content corresponding to the target resource by utilizing the configuration information.
With reference to the first aspect, in an implementation manner of the first aspect, the step of using the packet path to obtain the resource name of the target resource from the byte code includes:
scanning all corresponding contents under the packet path in the byte code according to the packet path;
acquiring all classes in all contents under the packet path;
traversing all classes, and obtaining the method names of all methods as the resource names.
With reference to the first aspect, in an implementation manner of the first aspect, before the acquiring, by using the packet path, a resource name of the target resource from the byte code, the method further includes:
determining each method requiring customizing the current limiting strategy;
a configuration message is generated having a current limit policy and current limit parameters for each method requiring a customized current limit policy.
With reference to the first aspect, in an implementation manner of the first aspect, the determining each method that needs to customize the current limiting policy includes:
executing all methods in the byte code to obtain test traffic corresponding to each method, wherein the test traffic is used for representing the quantity of the services processed by executing the method;
comparing the test traffic with the real traffic in the real traffic scene, and if the test traffic is smaller than the real traffic, determining that the method needs to customize a current limiting strategy, wherein the current limiting strategy is used for indicating that the target resource needs to perform current limiting operation when executing.
With reference to the first aspect, in an implementation manner of the first aspect, the step of generating, for each method requiring customizing the current limiting policy, configuration information including the current limiting policy and the current limiting parameter includes:
if the method requires customizing a flow limiting strategy, determining the test traffic as a flow limiting parameter of the method, wherein the flow limiting parameter is used for indicating the maximum traffic which can be processed by the target resource when the target resource is executed.
With reference to the first aspect, in an implementation manner of the first aspect, the step of determining each method that needs to customize the current limiting policy further includes:
and if the test traffic is zero, determining that the method needs to perform a fusing operation, wherein the fusing operation is used for indicating that a preset execution result is provided for the target resource.
In a second aspect, the present invention provides a resource flow limiting device, comprising:
the resource name acquisition module is used for acquiring the resource name of the target resource from the byte code by utilizing a packet path, wherein the packet path is a path designated by the acquisition target resource, and the byte code is the content after the code with business logic is compiled;
the configuration information acquisition module is used for acquiring configuration information corresponding to the target resource by utilizing the resource name, wherein the configuration information stores a current limiting strategy and a current limiting parameter related to the target resource, and the configuration information is stored in a one-to-one correspondence with the resource name;
and the execution module is used for executing the content corresponding to the target resource by utilizing the configuration information.
With reference to the second aspect, in an implementation manner of the second aspect, the resource name obtaining module includes:
the scanning unit is used for scanning all corresponding contents under the packet path in the byte code according to the packet path;
a class acquisition unit, configured to acquire all classes in all contents under the packet path;
and the resource name acquisition unit is used for traversing all the classes and acquiring the method names of all the methods as the resource names.
With reference to the second aspect, in an implementation manner of the second aspect, the resource current limiting device further includes:
the strategy determining module is used for determining each method requiring customizing the current limiting strategy;
and the strategy customizing module is used for generating configuration information with the current limiting strategy and the current limiting parameters for each method needing to customize the current limiting strategy.
With reference to the second aspect, in an implementation manner of the second aspect, the policy determining module further includes:
the execution unit is used for executing all the methods in the byte code to obtain test traffic corresponding to each method, wherein the test traffic is used for representing the quantity of the services processed by executing the method;
the judging unit is used for comparing the test traffic with the real traffic in the real traffic scene, and determining that the method needs to customize a current limiting strategy when the test traffic is smaller than the real traffic, wherein the current limiting strategy is used for indicating that the target resource needs to carry out current limiting operation when executing.
With reference to the second aspect, in an implementation manner of the second aspect, the policy customization module is further configured to determine, when the method needs to customize a current limiting policy, the test traffic as a current limiting parameter of the method, where the current limiting parameter is used to indicate a maximum traffic volume that can be processed by the target resource when the target resource is executed.
With reference to the second aspect, in an implementation manner of the second aspect, the policy determining module is further configured to determine that, when the test traffic is zero, the method needs to perform a fusing operation, where the fusing operation is used to indicate that a preset execution result is provided for the target resource.
In a third aspect, the present invention provides an electronic device, comprising: a memory for storing program instructions; and the processor is used for calling and executing the program instructions in the memory so as to realize the resource flow limiting method in the first aspect.
In a fourth aspect, the present invention provides a storage medium having stored therein a computer program which, when executed by at least one processor of a resource flow limiting device, performs the resource flow limiting method of the first aspect.
According to the resource flow limiting method, the device, the electronic equipment and the storage medium, the resource name of the target resource can be obtained from the byte code in a reflection mode by utilizing the packet path, wherein the byte code is the content compiled by the code with service logic, corresponding configuration information is obtained according to the resource name of the target resource, and finally the content corresponding to the target resource is executed according to the flow limiting strategy in the configuration information, so that resource flow limiting is realized, the service realized by the target resource can bear preset flow pressure, and meanwhile, the problem of resource omission caused by manually marking the target resource and the problem of poor uniqueness of manual marking are avoided by automatically extracting the target resource.
Detailed Description
At present, when current limiting operation is performed on code resources of a server, a technician usually manually finds out codes to be protected from the codes and marks the codes; when the server starts, the marked code resource is found out from the configuration file, and the marked code resource is executed according to the corresponding current limiting strategy. At present, the current limiting mode requires technicians to manually label code resources, the phenomenon of missing resources can occur, and when the code resources to be protected are too many, the uniqueness of the labels is difficult to ensure by manual labeling. Therefore, the invention provides a resource flow limiting method, which does not need to label the code resource manually, can determine the resource name of the code resource only by means of the compiling content of the code resource, can improve the accuracy of identifying the protected code resource, and can also avoid the problem of poor uniqueness of the manually labeled code resource.
Fig. 1 is a flowchart of a resource flow limiting method according to an embodiment of the present invention. As shown in fig. 1, the resource current limiting method provided by the embodiment of the invention includes:
s101, acquiring a resource name of a target resource from a byte code by using a packet path, wherein the packet path is a path designated by the acquisition target resource, and the byte code is compiled content of codes with business logic and is a binary file containing an execution program.
In the embodiment of the invention, in order to avoid manually marking the code resource and extracting the target resource from the byte code through the marked resource name, the target resource is obtained by directly utilizing the content compiled by the code resource. Typically, the compiled bytecode includes classes that provide a way to combine data and functionality; creating a new class means creating an object of a new type, allowing creation of a new instance of that type; each instance of a class may have properties that preserve its own state; an instance of a class may also have a method (defined in the class) of changing its own state. The methods in different classes are different from each other, so that the names of the methods can be directly identified to obtain the content corresponding to the methods from the byte codes. In the embodiment of the invention, the method content used in the stage of resource flow restriction by using the configuration information is the target resource, and the target resource is a part or all of the method resources in the byte code.
The embodiment of the invention can also use a reflection mechanism to acquire the resource name of the target resource from the byte code, wherein the reflection mechanism is a function of dynamically acquiring the object information and calling the object method, any class in the byte code content can be called through reflection, and the method and the attribute in the class can be acquired.
In addition, the packet path in the embodiment of the present invention is stored in a configuration file, where configuration information or an address of a remote configuration center is also stored, and if the configuration information is not stored in the configuration file, the configuration information may also be stored in the remote configuration center, for example, in a database. The packet path in the embodiment of the invention is used for indicating the position of the target resource, such as a certain folder, and after the packet path is determined, the content corresponding to the packet path can be found from the byte code, so that the target resource is obtained from the content.
S102, acquiring configuration information corresponding to the target resource by utilizing the resource name, wherein the configuration information stores a current limiting strategy and current limiting parameters related to the target resource, and the configuration information is stored in one-to-one correspondence with the resource name.
The configuration information in the embodiment of the invention is a preset current limiting strategy obtained when the target resource is tested in advance, the test process is the process of executing the method, the result of executing the method is used as the test result, the test result is compared with the real service volume needed to be executed on the server side, whether the execution of the method can bear the expected service pressure is judged, if the execution of the method can not bear the expected service pressure, a current limiting parameter executed is set for the method, and the maximum service flow which can be processed by the method cannot exceed the current limiting parameter. After testing, for the method that cannot bear the expected service pressure, a corresponding current limiting policy and current limiting parameter need to be stored in the memory, so that the current limiting policy and current limiting parameter can be called from the memory through the resource name when the target resource is actually executed.
Because the service functions which can be realized by each method are different, the service flow and the service type which can be processed by the method are also different, in the embodiment of the invention, whether the method needs to carry out the current limiting treatment or not is considered according to the test result of the method, and the setting of the current limiting parameters is considered in multiple aspects by combining the functions and the functions which are specifically realized by the method. For example, the method is performed to display a specified picture, but the server providing the picture fails and cannot be used, and then the test result of the method may be that the picture cannot be displayed, but considering that a blank picture cannot be provided for the user, a specific display picture needs to be set for the method, and the specific display picture can be used as the current limiting parameter of the method.
S103, executing the content corresponding to the target resource by utilizing the configuration information.
In the embodiment of the invention, the aspectJ tool can be adopted to weave the current limiting strategy into the content corresponding to the target resource, so that the target resource can be executed according to the current limiting strategy.
It can be seen that, in the resource flow limiting method provided in the embodiment of the present invention, the resource name of the target resource can be obtained from the bytecode by using the packet path in a reflection manner, where the bytecode is the content compiled by the code with service logic, and corresponding configuration information is obtained according to the resource name of the target resource, and finally, the content corresponding to the target resource is executed according to the flow limiting policy in the configuration information, so as to implement the method resource flow limiting, ensure that the service implemented by the code can bear the predetermined flow pressure, and meanwhile, the method of automatic extraction also avoids the problems of resource omission and poor uniqueness of manual labeling caused by manual labeling of the method.
In addition, the resource flow limiting method provided by the embodiment of the invention can perform flow limiting operation on the code resources in the server, and is also applicable to other web sites with the same functions as the server or service architectures with the functions of realizing the code resources.
Fig. 2 is a flowchart of a method for obtaining a resource name according to an embodiment of the present invention. As shown in fig. 2, in the embodiment of the present invention, the step of obtaining the resource name of the target resource from the bytecode by using the configuration file specifically includes:
s201, scanning all corresponding contents under the packet path in the byte code according to the packet path. For example, if the packet path specified in the configuration file is folder x of windows, then the content corresponding to folder x needs to be found in the bytecode.
S202, acquiring all classes in all contents under the packet path. That is, all the classes in all the files corresponding to the folder x can be found out.
As can be seen from the above embodiments, any class can be called by the reflection mechanism, and therefore, in the embodiment of the present invention, all classes in the bytecode resource are called by the reflection mechanism.
S203, traversing all classes, and obtaining the method names of all methods as the resource names, wherein the methods are the target resources.
After all the classes in all the files corresponding to the folder x are found out, all the methods are found out by traversing all the classes, and all the methods are marked as resources, so that the position of a certain method is not needed to be manually determined in a certain line of a certain file in the folder x, and then the line is manually marked with resources. Each class may contain a method, and the code itself distinguishes different methods when writing, so that the method can be directly called through a reflection mechanism, and a method name, namely a resource name, is obtained.
Therefore, in the resource flow limiting method provided by the embodiment of the invention, the class and the method in the byte code can be called by utilizing the reflection mechanism, so that the resource name is obtained, manual resource marking operation by a technician is not needed, and the phenomenon of omission in the process of obtaining the resource is avoided.
Fig. 3 is a flowchart of another resource flow limiting method according to an embodiment of the present invention. As shown in fig. 3, before the obtaining, by using the packet path, the resource name of the target resource from the byte code, the embodiment of the present invention further includes:
s301, determining each method requiring customizing the current limiting strategy. Specifically, fig. 4 is a flowchart of a method for determining that a current limiting policy needs to be customized according to an embodiment of the present invention. As shown in fig. 4, in the embodiment of the present invention, the steps of determining each method that needs to customize the current limiting policy include:
s401, executing the method in the byte code, and obtaining test traffic corresponding to each method, wherein the test traffic is used for representing the quantity of the business processed by executing the method.
In the embodiment of the invention, in the test stage of the method, no current limiting measures are taken for the execution of the method, so that the test result after the execution of the method is obtained, and is different from the real traffic to be processed by the method in actual situations, therefore, whether the current target resource needs to be subjected to current limiting operation or not is judged according to the comparison situation of the test result and the real traffic to be processed by the method. In addition, the test results in the embodiments of the present invention are expressed in terms of test traffic.
Because the resource throttling method in the embodiment of the invention is specific to all methods in the byte code, each method also needs to be tested in the test stage.
S402, comparing the test traffic with the real traffic in the real traffic scene, and if the test traffic is smaller than the real traffic, determining that the method needs to customize a current limiting strategy, wherein the current limiting strategy is used for indicating that the target resource needs to perform current limiting operation when executing. If the test traffic is greater than or equal to the real traffic, it is determined that the method does not require custom throttling policies.
S302, generating configuration information with the current limiting strategy and the current limiting parameters for each method needing to customize the current limiting strategy. If the method requires customizing a flow limiting strategy, determining the test traffic as a flow limiting parameter of the method, wherein the flow limiting parameter is used for indicating the maximum traffic which can be processed by the target resource when the target resource is executed.
In the embodiment of the invention, the test traffic and the real traffic specifically comprise the quantity, the flow and the like of the traffic. For example, when a method can process 500 pieces/s of traffic during testing, and when the service end executing the method is executing 800 pieces/s of traffic to be truly processed, the test traffic of the method is smaller than the actual traffic, and if the method is not in current limiting processing, when the service end executes the method, excessive traffic can jointly compete for the resources of the method, which may cause the problem that the method cannot be executed and further causes failure of service processing. Therefore, the method needs to be subjected to the current limiting treatment, and the current limiting parameter can be set to be 500 pieces/s, so that when the method is executed in the server, the method processes 500 pieces of business at most each time, and the normal operation of the method is ensured. If the real traffic is only 300 pieces/s, the test traffic is larger than the real traffic, which means that the method can be normally executed and the corresponding traffic in the server can be normally processed, and the method does not need to be subjected to current limiting processing.
It should be noted that, in the embodiment of the present invention, if the method needs to customize the current limiting policy, the test traffic is determined as the current limiting parameter of the method, which is only a preferred current limiting parameter setting manner in the embodiment, and in general, in order to further ensure the normal operation of the method, the current limiting parameter may be set smaller than the test traffic, for example, the test traffic is 500 pieces/s, and the current limiting parameter is 400 pieces/s.
In addition, in another embodiment of the present invention, if the test traffic is zero, it is determined that the method needs to perform a fusing operation, where the fusing operation is used to indicate that a preset execution result is provided for the target resource. That is, a fusing operation may also be included in the current limiting strategy. As in the foregoing embodiment, if the method is performed to display a specified picture, but the server providing the picture fails and cannot be used, and the test result for the method may be that the picture cannot be displayed, the test traffic of the method should be represented as zero, because the method is not performed successfully. In this case, the current limiting operation of the method cannot control the traffic of the processing of the method, but performs the fusing operation in the executing process of the method, that is, provides a preset processing result for the executing process of the method. If the above is taken as an example, when the picture requested by the method is not available, it is necessary to set a specific display picture for the method as an execution result. Therefore, the fusing operation in the embodiment of the invention is also used for ensuring the normal operation of the method.
As can be seen from the above, in the resource flow limiting method provided in the embodiment of the present invention, the resource name of the target resource can be obtained from the bytecode by using the packet path in a reflection manner, where the bytecode is the content compiled by the code with service logic, and corresponding configuration information is obtained according to the resource name of the target resource, and finally, the content corresponding to the target resource is executed according to the flow limiting policy in the configuration information, so as to implement resource flow limiting, and ensure that the service implemented by the target resource can bear a predetermined flow pressure.
Fig. 5 is a block diagram of a resource flow-limiting device according to an embodiment of the present invention. As shown in fig. 5, the resource current limiting device provided by the embodiment of the invention includes:
the resource name acquisition module 51 is configured to acquire a resource name of a target resource from a bytecode by using a packet path, where the packet path is a path specified by the acquisition target resource, and the bytecode is a content after compiling a code with service logic; a configuration information obtaining module 52, configured to obtain configuration information corresponding to the target resource by using the resource name, where a current limiting policy and a current limiting parameter related to the target resource are stored in the configuration information, and the configuration information is stored in a one-to-one correspondence with the resource name; and an execution module 53, configured to execute the content corresponding to the target resource by using the configuration information.
Wherein, the resource name acquisition module comprises: the scanning unit is used for scanning all corresponding contents under the packet path in the byte code according to the packet path; a class acquisition unit, configured to acquire all classes in all contents under the packet path; and the resource name acquisition unit is used for traversing all the classes and acquiring the method names of all the methods as the resource names.
The resource flow limiting device further comprises: the strategy determining module is used for determining each method requiring customizing the current limiting strategy; and the strategy customizing module is used for generating configuration information with the current limiting strategy and the current limiting parameters for each method needing to customize the current limiting strategy.
The policy determination module further includes: the execution unit is used for executing all the methods in the byte code to obtain test traffic corresponding to each method, wherein the test traffic is used for representing the quantity of the services processed by executing the method; the judging unit is used for comparing the test traffic with the real traffic in the real traffic scene, and determining that the method needs to customize a current limiting strategy when the test traffic is smaller than the real traffic, wherein the current limiting strategy is used for indicating that the target resource needs to carry out current limiting operation when executing.
The policy customization module is further configured to determine, when the method needs to customize a current limiting policy, the test traffic as a current limiting parameter of the method, where the current limiting parameter is used to indicate a maximum traffic that can be processed by the target resource when the target resource is executed.
The policy determining module is further configured to determine that, when the test traffic is zero, the method needs to perform a fusing operation, where the fusing operation is used to indicate that a preset execution result is provided for the target resource.
Fig. 6 is a schematic diagram of a hardware structure of an electronic device according to an embodiment of the present invention. As shown in fig. 6, an electronic device provided by an embodiment of the present invention includes: a memory 61 for storing program instructions; a processor 62 for invoking and executing the program instructions in the memory to implement the resource throttling method described in the above embodiments.
In embodiments of the present invention, processor 62 and memory 61 may be connected by a bus or other means. The processor may be a general-purpose processor, such as a central processing unit, a digital signal processor, an application specific integrated circuit, or one or more integrated circuits configured to implement embodiments of the present invention. The memory may include volatile memory, such as random access memory; the memory may also include non-volatile memory, such as read-only memory, flash memory, a hard disk, or a solid state disk.
The present invention also provides a storage medium having a computer program stored therein, which when executed by at least one processor of a resource flow limiting device performs the resource flow limiting method described in the above embodiments.
The storage medium may be a magnetic disk, an optical disk, a read-only memory (ROM), a random-access memory (random access memory, RAM), or the like.
It will be apparent to those skilled in the art that the techniques of embodiments of the present invention may be implemented in software plus a necessary general purpose hardware platform. Based on such understanding, the technical solutions in the embodiments of the present invention may be embodied in essence or what contributes to the prior art in the form of a software product, which may be stored in a storage medium, such as a ROM/RAM, a magnetic disk, an optical disk, etc., including several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the method described in the embodiments or some parts of the embodiments of the present invention.
The same or similar parts between the various embodiments in this specification are referred to each other. In particular, for the service building apparatus and the service loading apparatus embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and reference is made to the description in the method embodiments for the matters.
The embodiments of the present invention described above do not limit the scope of the present invention.