A Java Web interface request statistics method, computer equipment and readable storage medium
By maintaining temporary and permanent objects in Java Web programs, the problem that the existing technology cannot count the data requested by the backend interface in real time is solved, and the method of efficiently counting the number of interface requests in Servlets is realized, which simplifies the development process and improves the system Real-time.
Patent Information
- Application Number
- CN202010603561.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-29
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2040-06-29
AI Technical Summary
The prior art cannot count data requested by the backend interface in Java Web programs in real time, and the request statistics function is cumbersome in implementing the framework, which increases the burden on the backend.
Using a Java Web interface request statistics method, it uses timed updates and thread processing to count the number of interface requests, and provides a statistical solution implemented in Servlets, including parsing URLs, maintaining data objects, and processing Concurrency and storage of data.
It realizes the data that is convenient to obtain the number of interface requests in the code, reduces development complexity, can be directly integrated into the existing framework without secondary development, and provides real-time and efficient.
Smart Images

Figure CN111782182B_ABST
Abstract
Description
[Technical field]
[0001] The present invention relates to the field of computer technology, and in particular to a Java Web interface request statistics method, a computer device and a readable storage medium. [Background technology]
[0002] In Java Web programs, they can be mainly divided into the front-end and the back-end. The front-end can be roughly summarized as the part that users can see and operate, as the initiator of the request. In simple terms, the back-end is invisible to users and is the receiver of the request. The number of times the back-end or a specific interface in the back-end is requested (HTTP request), the source, parameters and other traffic data need to be counted. There are two existing technologies:
[0003] 1. The backend usually has log information, which records the relevant information of the request and then saves it in the form of a file within a certain period, usually once a day. The log file statistics can be used to record the requests of each backend and interface. This method has the following shortcomings: By using the log statistics method, only past data can be counted to a large extent, and real-time statistical data cannot be obtained.
[0004] 2. Write a function in the form of code to implement statistics on requests. This method has the following shortcomings: Currently, the development of a backend is based on various frameworks. If the statistical function is implemented in code on the framework, it is not only cumbersome, but also makes the entire backend more cumbersome. [Summary of the invention]
[0005] To solve the aforementioned problem, the present invention provides a Java Web interface request statistics method, which can easily obtain data such as the number of times the backend and each interface are requested.
[0006] In order to achieve the above object, the present invention adopts the following technical solution:
[0007] A Java Web interface request statistics method is used to count data objects, the data objects include temporary objects and permanent objects, the temporary objects are the accumulated times after the current service is enabled, and the permanent objects are not limited to the accumulated times of the current service startup, the Java Web interface request statistics method comprises the following steps:
[0008] Parse the URL of the received request to obtain the required path to locate the specific node in the data object. If the received request includes a name other than the URL, parse it according to the actual situation of the received request.
[0009] Maintain data objects and regularly update and store permanent objects.
[0010] Optionally, maintaining the data object specifically includes:
[0011] The counter of the interface request corresponding to the located specific node is incremented by 1.
[0012] Optionally, each specific node has three member variables, namely the node name, value, and children of the specific node; the value of the interface is the number of times the interface is requested, and the value of the controller is the sum of the values of all interfaces under the controller.
[0013] Optionally, maintaining data objects includes maintaining temporary objects and maintaining permanent objects, and maintaining temporary objects and maintaining permanent objects includes: calling an increase method, wherein the increase method increases a counter by 1, and calling a get method, wherein the get method obtains the value of a specific node.
[0014] Optionally, the interface requests to generate a statistical thread, which copies the global variable as a local variable. The local variable calls the increase method and determines whether the local variable is equal to the global variable before accumulation. If they are not equal, the global variable is copied again and the determination is repeated. If they are equal, the global variable is locked and the accumulation operation is performed.
[0015] Optionally, when the increase method is called on a temporary object, the interface requests the generation of a statistical thread, each statistical thread and its tasks are added to the queue, and the queue executes tasks in the order in which they are added. When executing tasks, only one task accumulates the global variable at a time.
[0016] Optionally, when maintaining a data object, if a call to the increase method on a transient object or a temporary object fails, the state is rolled back to the state before the maintenance regardless of whether the call to the increase method on the other object succeeds.
[0017] Optionally, a statistics thread is started in the init method of the Servlet, and the statistics thread executes the Java Web interface request statistics method.
[0018] The present invention has the following beneficial effects:
[0019] The present invention provides a solution for constructing and maintaining an object of the number of interface requests, which allows developers to easily obtain this data and can be directly called in the form of variables in the code, making it easy to integrate into the existing framework without the need for secondary development.
[0020] At the same time, the present invention also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements any of the above-mentioned methods when executing the computer program.
[0021] At the same time, the present invention also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, any of the above-mentioned methods is implemented.
[0022] These features and advantages of the present invention will be disclosed in detail in the following specific embodiments and drawings. The best embodiments or means of the present invention will be described in detail in conjunction with the drawings, but they are not intended to limit the technical solutions of the present invention. In addition, there are multiple features, elements, and components that appear in each of the following texts and drawings, and different symbols or numbers are marked for convenience, but they all represent components with the same or similar structures or functions.
Brief Description of the Drawings
[0023] The present invention will be further described below in conjunction with the accompanying drawings:
[0024] Figure 1 This is a call graph of the first embodiment of the present invention;
[0025] Figure 2 This is a flow chart of Embodiment 1 of the present invention;
[0026] Figure 3 This is a flow chart of using the comparison method to solve high concurrency in Embodiment 1 of the present invention;
[0027] Figure 4 This is a flow chart of using a queue method to solve high concurrency in Embodiment 2 of the present invention. [Specific implementation method]
[0028] The technical solutions of the embodiments of the present invention are explained and described below in conjunction with the drawings of the embodiments of the present invention, but the following embodiments are only preferred embodiments of the present invention, not all. Based on the embodiments in the implementation mode, other embodiments obtained by those skilled in the art without creative work are all within the protection scope of the present invention.
[0029] References in this specification to "one embodiment" or "an example" or "an example" mean that a particular feature, structure, or characteristic described in conjunction with the embodiment itself may be included in at least one embodiment of the present patent disclosure. The appearance of the phrase "in one embodiment" in various places in the specification does not necessarily refer to the same embodiment.
[0030] Embodiment 1:
[0031] This embodiment provides a Java Web interface request statistics method, which implements the statistics of requests in the init method of the Servlet. Specifically, the statistics function implemented in this embodiment maintains two data objects, including a temporary object and a permanent object. The temporary object is the number of times accumulated after the service is enabled, and the permanent object is not limited to the number of times accumulated after the service is started. The method provided in this embodiment starts a child thread in the init method of the Servlet, that is, the statistics thread, and the Java Web interface request statistics method is executed by the statistics thread, such as Figure 1 shown.
[0032] like Figure 2 As shown, the Java Web interface request statistics method provided in this embodiment includes the following steps:
[0033] Parse the URL of the received request to obtain the required path to locate the specific node in the data object. The path parameter here refers to the absolute path of the node in the tree, for example: service name / module 2 / controller 4 / interface 3, similar to the key-value format, and query the corresponding value through the key value. The key value here, that is, the required path, can usually be directly defined as the corresponding URL, which can save work. If the received request includes a name other than the URL, such as an alias, it will be parsed according to the actual situation of the received request;
[0034] Maintain data objects, including:
[0035] The counter of the interface request corresponding to the located specific node is incremented by 1. Each specific node has three member variables, namely the node name, value and children of the specific node; the value of the interface is the number of interface requests, and the value of the controller is the sum of the values of all interfaces under the controller. Maintaining data objects includes two sub-steps: maintaining temporary objects and maintaining permanent objects. Maintaining temporary objects and permanent objects includes: calling the increase method, the increase method accumulates the counter by 1, and calling the get method, calling the get method to obtain the value of the specific node.
[0036] like Figure 3As shown, in this embodiment, when the increase method is called, the comparison method is used to solve the high concurrency situation. The interface request generates a statistical thread, and the child thread opened in the init method of the Servlet mentioned above is the statistical thread here. The statistical thread copies a copy of the global variable as a local variable, and the local variable calls the increase method. Before accumulation, it determines whether the local variable is equal to the global variable. If they are not equal, it means that the global variable has been operated by other threads during this period, then the global variable is copied again and the judgment is repeated; if they are equal, the global variable is locked and the accumulation operation is performed again. Due to the particularity of the data structure, each accumulation operation actually only needs to add 1 to the value of a node, so when comparing the local variable with the global variable, it only needs to determine whether the corresponding nodes are the same.
[0037] The two sub-steps of maintaining temporary objects and permanent objects are required to be serial and atomic, that is, the entire step is successful only when both sub-steps are successful. When maintaining data objects, if the increase method fails to be called on a temporary object or a permanent object, then regardless of whether the increase method is successful on the other object, the state before maintenance is rolled back to ensure the unity of the two objects during maintenance. If the atomicity of two objects is not guaranteed during a maintenance, the data is meaningless.
[0038] The statistics function is an independent sub-thread, and its success or failure will not affect the main thread, so as to ensure that the normal process of the main thread and the designed statistical function do not affect each other. However, due to the atomic design of the statistical function, there is a situation of accumulation failure. The function on the main thread is executed normally, but the maintenance fails, that is, this request does not add 1 to the value in the corresponding node, so the data of the two objects does not match the actual data. However, the first thing to ensure is that the designed statistical function cannot affect the normal process, so the absolute accuracy of the statistics is relaxed to ensure availability. In addition, from the actual needs, the number of requests does not need to be accurate to the single digit. Under big data, it is often sufficient to know its magnitude. Therefore, it is necessary to relax the absolute accuracy of the statistics compared to ensuring the normal process.
[0039] Finally, update the stored permanent objects regularly. Periodically write the data of the permanent objects into the file to replace the old data and update it. The cycle can be once a day, once an hour, or once every five minutes. Make a choice based on actual needs. The shorter the cycle, the greater the load on the system, but at the same time the real-time performance is higher.
[0040] This embodiment provides a solution for constructing and maintaining an object of the number of interface requests, which allows developers to easily obtain this data and can be directly called as a variable in the code, making it easy to integrate into the existing framework without the need for secondary development.
[0041] Embodiment 2
[0042] The difference between this embodiment and the first embodiment is that when the increase method is called on a temporary object, a queue method is used to solve the high concurrency situation, such as Figure 4 As shown, the interface request generates a statistical thread, each statistical thread and its tasks are added to the queue, and the queue executes tasks in sequence according to the order of joining. When executing tasks, only one task accumulates the global variables at a time.
[0043] Embodiment 3
[0044] The present embodiment provides a computer device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the method in any of the above embodiments when executing the computer program. It can be understood by a person of ordinary skill in the art that all or part of the processes in the above embodiment method can be completed by instructing the relevant hardware through a computer program. Accordingly, the computer program can be stored in a non-volatile computer-readable storage medium, and the computer program can implement the method described in any of the above embodiments when executed. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0045] The above are only specific embodiments of the present invention, but the protection scope of the present invention is not limited thereto. Those skilled in the art should understand that the present invention includes but is not limited to the contents described in the drawings and the above specific embodiments. Any modification that does not deviate from the functional and structural principles of the present invention will be included in the scope of the claims.
Claims
1. A Java Web interface request statistics method, characterized in that: The Java Web interface request statistics method is used to collect data objects, which include temporary objects and permanent objects. The temporary objects are the accumulated times after the service is enabled, and the permanent objects are not limited to the accumulated times of the service startup. The Java Web interface request statistics method includes the following steps: Parse the URL of the received request to obtain the required path to locate the specific node in the data object. If the received request includes a name other than the URL, parse it according to the actual situation of the received request. Maintain data objects and regularly update and store permanent objects; The two sub-steps of maintaining temporary objects and maintaining permanent objects are required to be serial; Maintaining data objects includes maintaining temporary objects and maintaining permanent objects. Maintaining temporary objects and maintaining permanent objects includes: calling an increase method, the increase method accumulates 1 for a counter, calling a get method, the get method obtains the value of a specific node; The interface requests to generate a statistics thread. The statistics thread copies a copy of the global variable as a local variable. The local variable calls the increase method. Before accumulation, it determines whether the local variable is equal to the global variable. If they are not equal, the global variable is copied again and the determination is repeated. If they are equal, the global variable is locked and the accumulation operation is performed again. When maintaining data objects, if calling the increase method on a temporary object or a permanent object fails, the state will be rolled back to the state before the maintenance regardless of whether the other increase method is successful.
2. The Java Web interface request statistics method according to claim 1, characterized in that: Maintenance of data objects specifically includes: The counter of the interface request corresponding to the located specific node is incremented by 1.
3. The Java Web interface request statistics method according to claim 2, characterized in that: Each specific node has three member variables, namely the node name, value and children of the specific node; the value of the interface is the number of times the interface is requested, and the value of the controller is the sum of the values of all interfaces under the controller.
4. The Java Web interface request statistics method according to claim 1, characterized in that: When the increase method is called on a temporary object, the interface requests the generation of a statistical thread. Each statistical thread and its tasks are added to the queue. The queue executes tasks in the order in which they are added. When executing tasks, only one task accumulates the global variables at a time.
5. The Java Web interface request statistics method according to claim 1, characterized in that: A statistics thread is started in the init method of the Servlet, and the statistics thread executes the Java Web interface request statistics method.
6. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 5 is implemented.
7. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.
