Dynamic execution method and device based on groover script in Java project
By adopting the dynamic execution method of groovy scripts in Java projects, the problems of slow response speed and high resource consumption in traditional Java projects are solved, and rapid iteration and stable operation are achieved, reducing operation and maintenance costs.
Patent Information
- Application Number
- CN202411174430.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-26
- Publication Date
- 2024-12-06
AI Technical Summary
In traditional Java projects, Java services need to be redeployed when issuing new business requests, resulting in high consumption of resources and operation and maintenance, especially for small and micro enterprises, it is difficult to achieve rapid iteration and stable operation.
Using a dynamic execution method based on groovy scripts, obtain business requests of upper-level services through the execution interface, perform matching processing of groovy scripts, determine the target groovy scripts, and send it to execution class execution, instead of Java-type business code to respond to business requests. Reduce resource consumption and improve response speed.
It significantly improves the response speed of business requests, reduces resource consumption, realizes the ability to quickly iterate and operate stably, and reduces the operation and maintenance costs for small and micro enterprises.
Smart Images

Figure CN119088479A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of Java services, and in particular to a dynamic execution method and device based on Groovy scripts in Java projects. Background Art
[0002] With OpenAI adding new function calling capabilities to the Chat Completions API, developers can use the API to implement data interaction capabilities similar to the ChatGPT plug-in, allowing models to call customized business interfaces and services through OpenAI. Currently, related technologies have proposed that in traditional Java projects, Java-based business code is required to respond to business requests from upper-level services. Therefore, when new business requests are issued, Java services need to be redeployed. However, for small and micro enterprises, this solution requires both stable service operation and rapid iteration, which will result in large resource and operation and maintenance consumption. Summary of the Invention
[0003] In view of this, the purpose of the present invention is to provide a dynamic execution method and device based on Groovy scripts in Java projects, which can significantly improve the response speed to business requests and reduce resource consumption.
[0004] In a first aspect, an embodiment of the present invention provides a dynamic execution method based on groovy scripts in a Java project. The method is applied to a Java microservice. The Java microservice includes an execution interface and an execution class. The method includes: obtaining a business request sent by an upper-level service through the execution interface; performing groovy script matching processing on the business request, determining a target groovy script, and sending the target groovy script to the execution class, wherein the groovy script is a business-type code, and the groovy script is used to replace the Java-type business code in the Java project; executing the target groovy script through the execution class to execute the business corresponding to the business request, and feeding back the execution result to the upper-level service through the execution interface.
[0005] In one embodiment, a step of matching a groovy script for a business request and determining a target groovy script includes: matching the business request in a preset first database to match a target script path of the groovy script corresponding to the business request, wherein the preset first database includes a script path corresponding to each business request; sending the target script path to an execution class, matching a target script address corresponding to the target script path from a preset second database through the execution class, and determining the target script path and the target script address as the target groovy script, wherein the preset second database includes a script address corresponding to each script path.
[0006] In one embodiment, the method includes: if the target script path is not matched in the preset first database, or the target script address is not matched in the preset second database, then feeding back the matching failure result to the upper layer service through the execution interface.
[0007] In one embodiment, before performing groovy script matching processing on business requests and determining the target groovy script, the method includes: obtaining historical business deployment data; determining business requests in the historical business deployment data whose request times are greater than a preset threshold as target business requests, and establishing a third database using the groovy script corresponding to the target business request.
[0008] In one embodiment, after the step of establishing the third database using the groovy script corresponding to the target business request, the method includes: when detecting that the business request sent by the upper-layer service is the target business request, preferentially matching the target business request with the third database to determine the target groovy script.
[0009] In one embodiment, the step of executing the target Groovy script through the execution class includes: using the target script address corresponding to the target script path, updating the initial script engine in the execution class, and inputting the target Groovy script into the updated script engine so that the updated script engine executes the target Groovy script.
[0010] In one embodiment, the Java microservice also includes: a management interface and a management class, and the method includes: obtaining a data management request sent by an upper-layer service through the management interface, and sending the data management request to the management class, wherein the data management request includes: adding a groovy script, deleting a groovy script, changing a groovy script, and viewing and deleting a groovy script; executing data management processing corresponding to the data management request on a preset first database and a preset second database through the management class.
[0011] In the second aspect, an embodiment of the present invention also provides a dynamic execution device based on groovy scripts in a Java project. The device is applied to a Java microservice. The Java microservice includes: an execution interface and an execution class. The device includes: a data acquisition module, which obtains the business request sent by the upper-level service through the execution interface; a script matching module, which performs groovy script matching processing on the business request, determines the target groovy script, and sends the target groovy script to the execution class, wherein the groovy script is a business-type code, and the groovy script is used to replace the Java-type business code in the Java project; a dynamic execution module, which executes the target groovy script through the execution class to execute the business corresponding to the business request, and feeds back the execution result to the upper-level service through the execution interface.
[0012] In a third aspect, an embodiment of the present invention further provides a server, comprising a processor and a memory, wherein the memory stores computer-executable instructions that can be executed by the processor, and the processor executes the computer-executable instructions to implement any one of the methods provided in the first aspect.
[0013] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, which stores computer-executable instructions. When the computer-executable instructions are called and executed by a processor, the computer-executable instructions prompt the processor to implement any one of the methods provided in the first aspect.
[0014] The embodiments of the present invention bring the following beneficial effects:
[0015] An embodiment of the present invention provides a dynamic execution method and device based on Groovy scripts in a Java project. After obtaining a business request sent by an upper-level service through an execution interface, the method performs Groovy script matching processing on the business request, determines a target Groovy script, and sends the target Groovy script to an execution class. Finally, the execution class executes the target Groovy script to execute the business corresponding to the business request, and feeds back the execution result to the upper-level service through the execution interface. The embodiment of the present invention can significantly improve the response speed to business requests and reduce resource consumption.
[0016] Other features and advantages of the present invention will be described in the following description, and in part will become apparent from the description, or understood by practicing the present invention. The purposes and other advantages of the present invention are realized and obtained by the structures particularly pointed out in the description, claims and drawings.
[0017] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the specific embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0019] Figure 1 A schematic diagram of a flow chart of a dynamic execution method based on Groovy scripts in a Java project provided by an embodiment of the present invention;
[0020] Figure 2 A schematic diagram of a specific process of a dynamic execution method based on Groovy scripts in a Java project provided by an embodiment of the present invention;
[0021] Figure 3 A schematic diagram of the structure of a dynamic execution device based on Groovy scripts in a Java project provided by an embodiment of the present invention;
[0022] Figure 4 A schematic diagram of the structure of a server provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0023] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions of the present invention will be clearly and completely described below in conjunction with the embodiments. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of them. All other embodiments obtained by ordinary technicians in this field based on the embodiments of the present invention without making any creative efforts shall fall within the scope of protection of the present invention.
[0024] At present, as OpenAI has added a new function calling (FunctionCalling) capability to the Chat Completions API, it helps developers to implement data interaction capabilities similar to the ChatGPT plug-in through API, so that the model can call customized business interfaces and services through OpenAI. Related technologies have proposed that in traditional Java projects, it is necessary to respond to business requests of upper-level services through Java-type business codes. Therefore, when a new business request is issued, the Java service needs to be redeployed. However, for small and micro enterprises, this solution requires both stable service operation and rapid iteration, which will bring about large resource and operation and maintenance consumption. Based on this, the present invention implements a dynamic execution method and device based on groovy scripts in Java projects, which uses groovy scripts to execute business requests of upper-level services, which can significantly improve the response speed to business requests and reduce resource consumption.
[0025] See also Figure 1 The flowchart of a dynamic execution method based on a Groovy script in a Java project is shown, and the method mainly includes the following steps S102 to S106:
[0026] Step S102, obtain the business request sent by the upper-layer service through the execution interface. In one embodiment, the execution interface is used to communicate with the upper-layer service. When the upper-layer service adds or adjusts functions (that is, the upper-layer service requires the execution of a certain business), the Java microservice needs to quickly adapt to the functions defined by the upper-layer service to implement the corresponding business.
[0027] Step S104, matching processing of the Groovy script is performed for the business request, a target Groovy script is determined, and the target Groovy script is sent to the execution class, wherein the Groovy script is a business code, and the Groovy script is used to replace the Java-type business code in the Java project. In one embodiment, the business code is called in response to the business request sent to the upper-level service, and the business code is separated from Java through the Groovy script, which can successfully replace the function of the Java-type business code in the traditional Java project.
[0028] Step S106, execute the target groovy script through the execution class to execute the business corresponding to the business request, and feed back the execution result to the upper-level service through the execution interface. In one embodiment, the execution interface is used to call the upper-level service request. First, it searches the preset first database to see whether groovy exists. If it does not exist, the result is directly returned. If it exists, the groovy script is found and loaded for execution, and the result is returned after waiting for the return result.
[0029] The above-mentioned dynamic execution method based on Groovy scripts in Java projects provided by the embodiment of the present invention adds dynamic calls to Groovy scripts in Java microservices, achieves the purpose of rapid iteration in microservices, and ensures the ability of stable service operation, which can significantly improve the response speed to business requests and reduce resource consumption.
[0030] See also Figure 2 The specific flow diagram of a dynamic execution method based on a groovy script in a Java project is shown. The embodiment of the present invention also provides an implementation method for quickly calling a groovy script to dynamically execute a business request. For details, see (1) to (3) below:
[0031] (1) Matching script paths: matching business requests in a preset first database, matching the target script path of the groovy script corresponding to the business request, wherein the preset first database includes the script path corresponding to each business request, the execution interface is defined by Java, and the implementation method is written in groovy, so that the groovy script can be called in the java microservice to achieve the purpose of rapid iteration and ensure the ability of the service to run stably. By adopting the groovy script, the method in the groovy script can be dynamically called at any time, so that the function (function) that is constantly added and modified by the upper-level service can be responded to at any time. After writing a usable groovy script, it can be called by the upper-level service immediately.
[0032] (2) Matching script address: Send the target script path to the execution class, match the target script address corresponding to the target script path from the preset second database through the execution class, and determine the target script path and the target script address as the target Groovy script, wherein the preset second database includes the script address corresponding to each script path. The script address can be any location on the server as long as the service can access it. The corresponding path is saved in the database (the preset first database). When updating the script data, only the Groovy script needs to be added and updated. In one embodiment, if the target script path is not matched in the preset first database, or the target script address is not matched in the preset second database, the matching failure result will be fed back to the upper-level service through the execution interface.
[0033] (3) Execute the groovy script: Use the target script address corresponding to the target script path to update the initial script engine in the execution class, and input the target groovy script into the updated script engine so that the updated script engine executes the target groovy script. Specifically, if a method class for calling the groovy script is implemented, all upper-level calls will call this method, find the corresponding groovy script in this method according to the parameters passed in by the upper-level service, call the method in the script, and return the result to the upper layer. In addition, in this method class, the GroovyScriptEngine script engine method can be used to load the groovy script, find the corresponding script address url through the upper-level passed function, and then instantiate new GroovyScriptEngine(url), and finally execute the groovy script and return the result. Among them, the results returned by the Groovy script are all defined as JSONObject, which is more convenient and universal in the final parsing.
[0034] In one embodiment, a compiled Groovy basic class library (a basic class library for executing programs in classes) and a custom Groovy package in a third database may be added to the Java microservice packaging. This step is to enable the classes and methods therein to be directly used in dynamic Groovy scripts (scripts matched in the preset first database). Classes and interfaces (execution interfaces and management interfaces) are all defined in Java, and implementation methods are written in Groovy. Specifically, after obtaining historical business deployment data, business requests with a request count greater than a preset threshold in the historical business deployment data are identified as target business requests, and a third database is established using the Groovy script corresponding to the target business request. Subsequently, when it is detected that the business request sent by the upper-layer service is the target business request, the target business request is preferentially matched with the third database to determine the target Groovy script. That is, general Java classes or Groovy classes (usually defining public entity classes and tool classes) are defined in the Java microservice, and these commonly used codes and tools are used to establish a third database. When the corresponding business request is obtained, the script stored in the third database is preferentially called, thereby achieving the purpose of faster function and tool calling.
[0035] In one embodiment, the Java microservice also includes: a management interface and a management class, which obtains data management requests sent by the upper-level service through the management interface, and sends the data management requests to the management class, so as to perform data management processing corresponding to the data management requests on the preset first database and the preset second database through the management class, wherein the data management requests include: adding groovy scripts, deleting groovy scripts, changing groovy scripts, and viewing and deleting groovy scripts. That is to say, the execution interface and the management interface are both used to provide external calls. The management interface can be used for the usual operations of adding, deleting, modifying and checking groovy, and also includes a groovy script upload interface. In order to better maintain the groovy script, the information of the groovy script and the information corresponding to the functional method can be stored in the database through the management class to facilitate management and maintenance.
[0036] In summary, the implementation of the present invention is simple. Since Groovy scripts can be well combined with Java and their syntax is similar to that of the Java language, the learning cost is low and one can get started quickly. In addition, the method provided by the present invention can iterate quickly and respond quickly to the addition and modification of upper-level service functions, thereby making dynamic adjustments without the need to redeploy services. Therefore, it can significantly save resources and operation and maintenance costs.
[0037] Regarding the dynamic execution method based on groovy script in Java project provided in the above embodiment, the embodiment of the present invention provides a dynamic execution device based on groovy script in Java project, which is applied to Java microservice. Java microservice includes: execution interface and execution class, see Figure 3 The structure diagram of a dynamic execution device based on Groovy script in a Java project is shown, and the device includes the following parts:
[0038] The data acquisition module 302 acquires the service request sent by the upper layer service through the execution interface;
[0039] The script matching module 304 performs Groovy script matching processing on the business request, determines the target Groovy script, and sends the target Groovy script to the execution class. The Groovy script is a business code that replaces the Java business code in the Java project.
[0040] The dynamic execution module 306 executes the target Groovy script through the execution class to execute the business corresponding to the business request, and feeds back the execution result to the upper-layer service through the execution interface.
[0041] The above-mentioned dynamic execution device based on Groovy scripts in Java projects provided in the embodiments of the present application can significantly improve the response speed to business requests and reduce resource consumption.
[0042] In one embodiment, when performing groovy script matching processing for business requests and determining the target groovy script, the above-mentioned script matching module 304 is also used to: match the business request in a preset first database, match the target script path of the groovy script corresponding to the business request, wherein the preset first database includes the script path corresponding to each business request; send the target script path to the execution class, match the target script address corresponding to the target script path from the preset second database through the execution class, and determine the target script path and the target script address as the target groovy script, wherein the preset second database includes the script address corresponding to each script path.
[0043] In one embodiment, the script matching module 304 is further configured to: if the target script path is not matched in the preset first database, or the target script address is not matched in the preset second database, feedback the matching failure result to the upper layer service through the execution interface.
[0044] In one embodiment, before performing the step of matching groovy scripts for business requests and determining target groovy scripts, the script matching module 304 is further used to: obtain historical business deployment data; determine business requests in the historical business deployment data whose request times are greater than a preset threshold as target business requests, and establish a third database using the groovy scripts corresponding to the target business requests.
[0045] In one embodiment, after the step of establishing the third database using the groovy script corresponding to the target business request, the script matching module 304 is further used to: when it is detected that the business request sent by the upper-layer service is the target business request, preferentially match the target business request with the third database to determine the target groovy script.
[0046] In one embodiment, when executing the target Groovy script through the execution class, the dynamic execution module 306 is further used to: update the initial script engine in the execution class using the target script address corresponding to the target script path, and input the target Groovy script into the updated script engine so that the updated script engine executes the target Groovy script.
[0047] In one embodiment, the Java microservice also includes: a management interface and a management class. The above-mentioned dynamic execution module 306 is also used to: obtain the data management request sent by the upper-level service through the management interface, and send the data management request to the management class, wherein the data management request includes: adding a groovy script, deleting a groovy script, changing a groovy script, and viewing and deleting a groovy script; and executing data management processing corresponding to the data management request on the preset first database and the preset second database through the management class.
[0048] The device provided in the embodiment of the present invention has the same implementation principle and technical effects as those in the aforementioned method embodiment. For the sake of brief description, for matters not mentioned in the device embodiment, reference can be made to the corresponding content in the aforementioned method embodiment.
[0049] An embodiment of the present invention provides a server. Specifically, the server includes a processor and a storage device. The storage device stores a computer program, and when the computer program is executed by the processor, it executes the method described in any one of the above-mentioned embodiments.
[0050] Figure 4A structural diagram of a server provided in an embodiment of the present invention is provided, wherein the server 100 includes: a processor 40, a memory 41, a bus 42 and a communication interface 43, wherein the processor 40, the communication interface 43 and the memory 41 are connected via the bus 42; the processor 40 is used to execute an executable module stored in the memory 41, such as a computer program.
[0051] The memory 41 may include high-speed random access memory (RAM) and may also include non-volatile memory, such as at least one disk storage. The system network element communicates with at least one other network element via at least one communication interface 43 (which may be wired or wireless), and may utilize the Internet, a wide area network, a local area network, a metropolitan area network, or the like.
[0052] The bus 42 may be an ISA bus, a PCI bus, or an EISA bus. The bus may be divided into an address bus, a data bus, a control bus, and the like. For ease of representation, Figure 4 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or one type of bus.
[0053] Among them, the memory 41 is used to store programs, and the processor 40 executes the program after receiving the execution instruction. The method executed by the device for flow process definition disclosed in any embodiment of the above-mentioned embodiment of the present invention can be applied to the processor 40 or implemented by the processor 40.
[0054] Processor 40 may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method may be completed by hardware integrated logic circuits or software instructions in processor 40. The above processor 40 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It may implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present invention. The general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in conjunction with the embodiments of the present invention may be directly implemented and executed by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software module may be located in a storage medium well-known in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or the like. The storage medium is located in the memory 41 , and the processor 40 reads the information in the memory 41 and completes the steps of the above method in combination with its hardware.
[0055] The computer program product of the readable storage medium provided in the embodiment of the present invention includes a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the method described in the previous method embodiment. The specific implementation can be referred to the previous method embodiment and will not be repeated here.
[0056] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.
[0057] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The scope of protection of the present invention is not limited thereto. Although the present invention has been described in detail with reference to the above-described embodiments, those skilled in the art should understand that any person skilled in the art can modify or easily conceive of changes to the technical solutions described in the above-described embodiments within the technical scope disclosed by the present invention, or replace some of the technical features therein with equivalents. Such modifications, changes, or replacements do not deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the scope of protection of the present invention. Therefore, the scope of protection of the present invention shall be subject to the scope of protection of the claims.
Claims
1. A dynamic execution method based on groovy script in a Java project, characterized in that: The method is applied to a Java microservice, the Java microservice includes: an execution interface and an execution class, and the method includes: Obtaining the business request sent by the upper layer service through the execution interface; Performing groovy script matching processing on the business request, determining a target groovy script, and sending the target groovy script to the execution class, wherein the groovy script is a business code, and the groovy script is used to replace the Java-type business code in the Java project; The target groovy script is executed through the execution class to execute the business corresponding to the business request, and the execution result is fed back to the upper-layer service through the execution interface.
2. The dynamic execution method based on groovy script in Java project according to claim 1, characterized in that: The step of performing groovy script matching processing on the business request and determining the target groovy script comprises: Performing matching processing on the business request in a preset first database to match a target script path of a groovy script corresponding to the business request, wherein the preset first database includes a script path corresponding to each business request; The target script path is sent to the execution class, and the target script address corresponding to the target script path is matched from a preset second database through the execution class, and the target script path and the target script address are determined as the target groovy script, wherein the preset second database includes the script address corresponding to each script path.
3. The dynamic execution method based on groovy script in Java project according to claim 2, characterized in that: The method comprises: If the target script path is not matched in the preset first database, or the target script address is not matched in the preset second database, the matching failure result is fed back to the upper layer service through the execution interface.
4. The dynamic execution method based on groovy script in Java project according to claim 1, characterized in that: Before the step of matching the groovy script for the business request and determining the target groovy script, the method includes: Obtain historical business deployment data; A business request whose request times are greater than a preset threshold in the historical business deployment data is determined as a target business request, and a third database is established using a groovy script corresponding to the target business request.
5. The dynamic execution method based on groovy script in Java project according to claim 4, characterized in that: After the step of establishing the third database by using the groovy script corresponding to the target business request, the method includes: When it is detected that the service request sent by the upper layer service is the target service request, the target service request is preferentially matched with the third database to determine the target groovy script.
6. The dynamic execution method based on groovy script in Java project according to claim 1, characterized in that: The step of executing the target groovy script through the execution class comprises: The initial script engine in the execution class is updated using the target script address corresponding to the target script path, and the target groovy script is input into the updated script engine so that the updated script engine executes the target groovy script.
7. The dynamic execution method based on groovy script in Java project according to claim 1, characterized in that: The Java microservice also includes: a management interface and a management class, and the method includes: Obtaining a data management request sent by an upper layer service through the management interface, and sending the data management request to the management class, wherein the data management request includes: adding a groovy script, deleting a groovy script, changing a groovy script, and viewing and deleting a groovy script; The data management processing corresponding to the data management request is performed on the preset first database and the preset second database through the management class.
8. A dynamic execution device based on groovy script in a Java project, characterized in that: The device is applied to a Java microservice, the Java microservice includes: an execution interface and an execution class, and the device includes: A data acquisition module, which acquires the business request sent by the upper layer service through the execution interface; A script matching module performs groovy script matching processing on the business request, determines a target groovy script, and sends the target groovy script to the execution class, wherein the groovy script is a business code and is used to replace the Java-type business code in the Java project; The dynamic execution module executes the target groovy script through the execution class to execute the business corresponding to the business request, and feeds back the execution result to the upper layer service through the execution interface.
9. A server, characterized in that: The method comprises a processor and a memory, wherein the memory stores computer executable instructions that can be executed by the processor, and the processor executes the computer executable instructions to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are called and executed by a processor, the computer-executable instructions prompt the processor to implement the method according to any one of claims 1 to 7.
