Method and system for local function calling between different Web applications under the same Tomcat instance

By deploying shared service components and business services under the same Tomcat instance, and using methods of dynamically creating classes and instances and registering called class object instances, local function calls between different web applications are implemented, which solves the performance overhead problems brought by RPC and significantly improves calling efficiency and system performance.

CN119292710BActive Publication Date: 2025-04-01BEIJING FENYANG TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411825351.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-12
Publication Date
2025-04-01
Estimated Expiration
2044-12-12

AI Technical Summary

Technical Problem

In the microservice architecture, since different web applications in Tomcat are managed by independent class loaders, local function calls cannot be made directly, resulting in the network latency and performance overhead brought by RPC seriously affecting system performance in high-frequency multilingual query scenarios.

Method used

By deploying the shared service component as an independent web application and sharing the same Tomcat instance with other business services, local function calls between different web applications are implemented by dynamically creating classes and instances and registering called class object instances. The specific steps include dynamically generating the called class, creating an instance of the called class object, registering it with the system class loader, and the main web application calls the method of the called class object through the proxy class, and returning the result.

Benefits of technology

Through local function calls, network latency and performance overhead brought by RPC are avoided, calling between applications is significantly improved, memory consumption is reduced, and deployment and maintenance processes are simplified.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119292710B_ABST
    Figure CN119292710B_ABST
Patent Text Reader

Abstract

The invention discloses a method and system for local function calling between different Web applications under the same Tomcat instance, and relates to the technical field of computer software. A plurality of Web applications are deployed under the same Tomcat instance, and an application that needs to provide a local function call is set to be started first; in the Web application started first, a new class is generated, and an object instance is created; the new class and the object instance are loaded into a system class loader, so that other Web applications can globally access the new class and the object instance; when other Web applications in Tomcat are started, the object instance is obtained from the system class loader, and is assigned to a target instance variable in a proxy class; other Web applications call the object instance through the proxy class; the proxy class returns the calling result of the target instance variable, thereby avoiding the network delay and performance overhead caused by the traditional remote procedure call, and thus greatly improving the calling efficiency between applications.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of computer software, and in particular relates to a method and system for implementing local function calls between different Web applications. Background Art

[0002] In a microservice architecture, communication between different services usually relies on remote procedure calls (RPCs). If some shared service components (such as i18n-client) are deployed as an independent Web application and deployed in the same Tomcat instance with other business services, since different Web applications in Tomcat are managed by independent class loaders, and there is no inheritance relationship between these class loaders, local function calls cannot be made directly and can only rely on RPCs. However, the network latency and performance overhead brought by RPCs seriously affect the performance of the system, especially in scenarios with high-frequency multi-language queries. Summary of the invention

[0003] Therefore, the technical problem to be solved by the present invention is to provide a method and system for local function calling between different Web applications under the same Tomcat instance, so as to improve the calling efficiency between Web applications.

[0004] In a first aspect, the present invention provides a method for local function calling between different Web applications under the same Tomcat instance, comprising:

[0005] The step of deploying a Web application, wherein the master Web application and the slave Web application are deployed in the same Tomcat instance, and the slave Web application is set to be started first; the slave Web application is used to provide a called class object, and the master Web application is used to directly call the called class object provided by the slave Web application;

[0006] Dynamically create a class and an instance step, when the slave Web application is started, dynamically generate a called class and create an object instance of the called class;

[0007] Registering a called class object instance step, loading the called class object into the system class loader;

[0008] The step of starting the main Web application is to start the main Web application in the Tomcat after the slave Web application is successfully started;

[0009] The step of obtaining a registered called class object instance, after the main Web application is started, obtaining the registered called class object instance from the system class loader, creating a proxy class of the called class, and assigning the called class object instance to a target instance variable in the proxy class of the called class, wherein the target instance variable is a static variable;

[0010] A local function calling step, wherein the main Web application calls the called class object through the proxy class of the called class; the proxy class of the called class obtains the method in the called class object instance and executes the method through the target instance variable;

[0011] In the result returning step, the proxy class of the called class returns the target instance variable execution result to the main Web application.

[0012] Furthermore, in the step of dynamically creating classes and instances, the called class is dynamically generated through bytecode enhancement.

[0013] Furthermore, in the step of registering the called class object instance, the system class loader adopts the JVM class loader and the parent delegation mechanism.

[0014] Furthermore, in the step of dynamically creating classes and instances, the called class object instance is assigned to a static variable in the called class.

[0015] Furthermore, in the local function calling step, the proxy class of the called class obtains the method in the called class object instance through a reflection mechanism.

[0016] Furthermore, in the step of obtaining the registered called class object instance, the proxy class of the called class encapsulates each public method of the called class object instance and provides a function with an equivalent signature.

[0017] Furthermore, there are multiple master Web applications, and when the slave Web application is successfully started, each master Web application is started in sequence.

[0018] On the other hand, the present invention also provides a system for local function calls between different Web applications under the same Tomcat instance, comprising:

[0019] Deploy the Web application module, the main Web application and the slave Web application are deployed in the same Tomcat instance, and the slave Web application is set to start first; the slave Web application is used to provide the called class object, and the main Web application is used to directly call the called class object provided by the slave Web application;

[0020] Dynamically create a class and instance module, when the slave Web application is started, dynamically generate a called class and create a called class object instance;

[0021] Registering a called class object instance module, and loading the called class object into the system class loader;

[0022] Start the main Web application module, and when the slave Web application is successfully started, start the main Web application in the Tomcat;

[0023] Obtaining a registered called class object instance module, after the main Web application is started, obtaining the registered called class object instance from the system class loader, creating a proxy class of the called class, and assigning the called class object instance to a target instance variable in the proxy class of the called class, wherein the target instance variable is a static variable;

[0024] A local function calling module, wherein the main Web application calls the called class object through the proxy class of the called class; the proxy class of the called class obtains the method in the called class object instance and executes the method through the target instance variable;

[0025] In the result return module, the proxy class of the called class returns the target instance variable execution result to the main Web application.

[0026] Beneficial effects:

[0027] The method for local function calling between different Web applications under the same Tomcat instance disclosed in the present invention implements local function calling by deploying a shared service component as an independent Web application and sharing the same instance with other business services, thereby achieving the following technical effects:

[0028] ‌Performance improvement‌: By calling local functions, the network delay and performance overhead caused by RPC are avoided, and the calling efficiency between applications is significantly improved.

[0029] ‌Memory optimization‌: Deploy shared service components as a separate web application and share the same instance with other business services, significantly reducing memory consumption.

[0030] ‌Easy to deploy and maintain‌: All Web applications are deployed under the same Tomcat instance, which simplifies the deployment and maintenance process.

[0031] The system for local function calling between different Web applications under the same Tomcat instance provided by the present invention has the same beneficial effects as the above method. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] Figure 1 is a flow chart of the method of embodiment 1 of the present invention;

[0033] Figure 2 This is a diagram of the registration and discovery mechanism architecture implemented by the object instance of Embodiment 1 of the present invention under the Tomcat class loader framework;

[0034] Figure 3 This is a flow chart of registration and discovery of an I18nClient object instance on a JVM system class loader according to Embodiment 2 of the present invention;

[0035] Figure 4 This is a flowchart of performing a local function call in a cross-Web application scenario according to Embodiment 2 of the present invention. DETAILED DESCRIPTION

[0036] In the prior art, a business system consists of hundreds of microservices, which query multilingual entries through local functions of the I18n-client component. In order to improve query efficiency, the I18n-client component caches a large amount of entry data into memory when it starts. Although this "space for time" solution improves query speed, when the service scale is large, memory consumption increases sharply, making the hardware cost of the system unbearable.

[0037] In order to solve the memory overhead problem, if the I18n-client component is transformed into an independently deployable Web application and deployed in the same Tomcat instance with other business services, multiple business services can share the same I18n-client instance through remote procedure calls (RPC), significantly reducing memory consumption. However, since the codes of different Web applications are managed by independent class loaders and there is no inheritance relationship between these class loaders, local function calls cannot be made directly and can only rely on RPC. Although RPC solves the memory problem, the network overhead it introduces, especially in the scenario of high-frequency multi-language queries, seriously affects the performance of the system.

[0038] To this end, the present invention discloses a method for calling local functions between different Web applications under the same Tomcat instance, such as Figure 1 As shown, including:

[0039] In the step of deploying the Web application, the main Web application and the slave Web application are deployed in the same Tomcat instance, and the slave Web application is set to start first; the slave Web application is used to provide the called class object, and the main Web application is used to directly call the called class object provided by the slave Web application;

[0040] Dynamically create classes and instances. When the Web application is started, a called class is dynamically generated and an object instance of the called class is created.

[0041] Register the called class object instance step and load the called class object into the system class loader;

[0042] Start the main Web application step. After the slave Web application is successfully started, start the main Web application in Tomcat;

[0043] The step of obtaining the registered called class object instance is as follows: after the main Web application is started, the registered called class object instance is obtained from the system class loader, a proxy class of the called class is created, and the called class object instance is assigned to the target instance variable in the proxy class of the called class. The target instance variable is a static member variable of the Object type.

[0044] In the local function calling step, the main Web application calls the called class object through the proxy class of the called class; the proxy class of the called class obtains the method in the called class object instance and executes the method through the target instance variable;

[0045] In the result return step, the proxy class of the called class returns the execution result of the target instance variable to the main Web application.

[0046] Through the method of the present invention, a class object in a Web application can directly call a method of a class object in another Web application, avoiding the network delay and performance overhead caused by the traditional RPC (Remote Procedure Call), thereby greatly improving the calling efficiency between applications. The present invention is particularly suitable for multi-microservice architectures that need to frequently call shared services, which helps to improve the overall performance and response speed of the system. It realizes direct calling of local functions between different Web applications under the same Tomcat instance, completely avoiding the network delay and performance bottleneck caused by RPC. The present invention not only reduces memory consumption, but also greatly improves the service calling efficiency, and is particularly suitable for high-concurrency, multi-language query scenarios.

[0047] The present invention will be described in detail below with reference to the accompanying drawings and in combination with embodiments. The principles and features of the present invention are described below in combination with the accompanying drawings. It should be noted that the embodiments in this application and the features in the embodiments can be combined with each other without conflict. The embodiments are only used to explain the present invention and are not used to limit the scope of the present invention.

[0048] Example 1

[0049] 1. Mixed deployment of multiple web applications: Deploy multiple web applications (webapp) under the same Tomcat instance, and set the i18n webapp that needs to provide local function calls to start first.

[0050] 2. Dynamically create classes and instances: When the i18n webapp is started, a new class (I18nDynamicWrapper) is dynamically generated using Java bytecode enhancement technology, and an I18nClient object instance is created. This instance is assigned to the static variable INSTANCE in the I18nDynamicWrapper class to reference the real I18nClient instance.

[0051] 3. Registration of object instance in class loader: Load the I18nDynamicWrapper class object into the system class loader to ensure that other web applications can globally access the class and its instances.

[0052] 4. Start other business Web applications: After the i18n webapp is successfully started, start other business service Web applications in Tomcat one by one.

[0053] 5. Get the registered object instance from the class loader: When the business service starts, it gets the previously registered I18nClient object instance from the system class loader and assigns it to the target instance variable (TARGET_INSTANCE) in the proxy class I18nClientProxy. Figure 2 As shown in the figure, the registration and discovery mechanism architecture of the object instance implemented in the Tomcat class loader framework is shown using I18nClient as an example.

[0054] 6. Local function call: The business service calls the local function of I18nClient through the I18nClientProxy proxy class. The I18nClientProxy proxy class uses the reflection mechanism to obtain the method in the I18nClient class and executes the method through the real instance TARGET_INSTANCE.

[0055] 7. Return result: The I18nClientProxy proxy class returns the call result to the upper-level caller of the business service, completing efficient local function calls between different Web applications.

[0056] This embodiment solves the problem of local function calls between different Web applications by deploying i18n webapp together with other business services in the same Tomcat instance. First, i18n webapp is the first startup item, and the I18nClient object instance is registered in the system class loader of JVM at startup. Subsequently, when other business Web applications are started, the I18nClient instance is obtained from the system class loader and injected into the TARGET_INSTANCE variable of the proxy class I18nClientProxy. I18nClientProxy encapsulates the public functions in I18nClient and calls the corresponding methods of the target instance I18nClient through the reflection mechanism. This design allows different Web applications to efficiently share multi-language cache data through local function calls, significantly improving system performance.

[0057] Example 2

[0058] The steps for registering and discovering the proxy object instance in this embodiment are as follows:

[0059] 1. Proxy class packaging: Provide a proxy class (I18nClientProxy) for I18n access to the business service. It contains a static member variable TARGET_INSTANCE of type Object. It also encapsulates each public method of I18nClient and provides functions with equivalent signatures.

[0060] 2. Modify Tomcat configuration: Use the Context tag in Tomcat's server.xml to configure i18nwebapp, and put the war package of the business service into Tomcat's appBase directory. Since Tomcat starts the webapp configured with Context first, the i18n webapp will be started first. The configuration example is as follows:

[0061] XML

[0062] <host name="localhost" appbase="webapps" unpackwars="true" autodeploy="false" startstopthreads="1">

[0063] <context docbase="i18n-client" path=" / i18n-client" debug="0">

[0064] < / context>

[0065] < / host>

[0066] 3. Start the I18n service: Tomcat will first start the i18n Web application.

[0067] 4. Dynamic class creation: When the i18n webapp is started, the I18nDynamicWrapper class is dynamically generated through bytecode enhancement technology. This class contains a static Object type variable OBJ_INSTANCE.

[0068] 5. Create an object instance of the I18nClient class and assign it to the I18nDynamicWrapper.OBJ_INSTANCE variable

[0069] 6. Instance creation and assignment: Create an object instance of the I18nClient class and assign it to the OBJ_INSTANCE variable in the I18nDynamicWrapper class.

[0070] 7. Register to the system class loader: Load the I18nDynamicWrapper class to the system class loader of the JVM, making it accessible to all web applications in the entire Tomcat.

[0071] 8. Start business services: Start other business service Web applications in Tomcat one by one.

[0072] 9. Instance injection: When the business service is started, it obtains the I18nDynamicWrapper class registered by the i18n web application from the system class loader, and then injects its OBJ_INSTANCE variable value into the TARGET_INSTANCE variable of I18nClientProxy. Figure 3 The figure shows the registration and discovery process of the I18nClient object instance on the JVM system class loader.

[0073] like Figure 4 As shown, the implementation steps of local function calls between different webapps in this embodiment are as follows:

[0074] 1. Calling functions: The business service calls the functions of I18nClient through I18nClientProxy. Here we take the multilingual query function as an example;

[0075] 2. Reflection call: I18nClientProxy uses the reflection mechanism to obtain the corresponding Method class instance in the TARGET_INSTANCE class and calls the method of TARGET_INSTANCE;

[0076] 3. Result return: I18nClientProxy returns the call result of TARGET_INSTANCE to the upper-level business service to complete the call process.

[0077] This embodiment uses Java bytecode enhancement technology, combined with the design of JVM class loader and parent delegation mechanism, to achieve the function of direct local method calls between different WebApps under the same Tomcat. Compared with the traditional RPC method, this method significantly improves the call performance, especially in large concurrency scenarios, and can efficiently meet the needs of interface call performance across WebApps under ultra-large traffic.

[0078] Example 3

[0079] This embodiment provides a system for local function calls between different Web applications under the same Tomcat instance, including:

[0080] Deploy the Web application module, the main Web application and the slave Web application are deployed in the same Tomcat instance, and the slave Web application is set to start first; the slave Web application is used to provide the called class object, and the main Web application is used to directly call the called class object provided by the slave Web application;

[0081] Dynamically create a class and instance module, when the slave Web application is started, dynamically generate a called class and create a called class object instance;

[0082] Registering a called class object instance module, and loading the called class object into the system class loader;

[0083] Start the main Web application module, and when the slave Web application is successfully started, start the main Web application in the Tomcat;

[0084] A module for obtaining a registered called class object instance. After the main Web application is started, the module obtains the registered called class object instance from the system class loader, creates a proxy class of the called class, and assigns the called class object instance to a target instance variable in the proxy class of the called class, where the target instance variable is a static member variable of the Object type.

[0085] A local function calling module, wherein the main Web application calls the called class object through the proxy class of the called class; the proxy class of the called class obtains the method in the called class object instance and executes the method through the target instance variable;

[0086] In the result return module, the proxy class of the called class returns the target instance variable execution result to the main Web application.

[0087] Obviously, the above embodiments are merely examples for the purpose of clear explanation, and are not intended to limit the implementation methods. For those skilled in the art, other different forms of changes or modifications can be made based on the above description. It is not necessary and impossible to list all the implementation methods here. The obvious changes or modifications derived therefrom are still within the scope of protection of the invention.

Claims

1. A method for calling local functions between different Web applications under the same Tomcat instance, characterized in that: include: The step of deploying a Web application, wherein the master Web application and the slave Web application are deployed in the same Tomcat instance, and the slave Web application is set to be started first; the slave Web application is used to provide a called class object, and the master Web application is used to directly call the called class object provided by the slave Web application; Dynamically create a class and an instance step, when the slave Web application is started, dynamically generate a called class and create an object instance of the called class; Registering a called class object instance step, loading the called class object into the system class loader; The step of starting the main Web application is to start the main Web application in the Tomcat after the slave Web application is successfully started; The step of obtaining a registered called class object instance, after the main Web application is started, obtaining the registered called class object instance from the system class loader, creating a proxy class of the called class, and assigning the called class object instance to a target instance variable in the proxy class of the called class, wherein the target instance variable is a static variable; A local function calling step, wherein the main Web application calls the called class object through the proxy class of the called class; the proxy class of the called class obtains the method in the called class object instance and executes the method through the target instance variable; In the result returning step, the proxy class of the called class returns the target instance variable execution result to the main Web application.

2. The method according to claim 1, characterized in that In the step of dynamically creating classes and instances, the called class is dynamically generated through bytecode enhancement.

3. The method according to claim 2, characterized in that In the step of registering the called class object instance, the system class loader adopts the JVM class loader and the parent delegation mechanism.

4. The method according to claim 1, characterized in that: In the step of dynamically creating classes and instances, the called class object instance is assigned to the static variables in the called class.

5. The method according to claim 1, characterized in that In the local function calling step, the proxy class of the called class obtains the method in the called class object instance through a reflection mechanism.

6. The method according to claim 1, characterized in that In the step of obtaining the registered called class object instance, the proxy class of the called class encapsulates each public method of the called class object instance and provides a function with an equivalent signature.

7. The method according to claim 1, characterized in that There are multiple master Web applications, and when the slave Web application is successfully started, each master Web application is started in sequence.

8. A system for local function calls between different Web applications under the same Tomcat instance, characterized in that: include: Deploy the Web application module, the main Web application and the slave Web application are deployed in the same Tomcat instance, and the slave Web application is set to start first; the slave Web application is used to provide the called class object, and the main Web application is used to directly call the called class object provided by the slave Web application; Dynamically create a class and instance module, when the slave Web application is started, dynamically generate a called class and create a called class object instance; Registering a called class object instance module, and loading the called class object into the system class loader; Start the main Web application module, and when the slave Web application is successfully started, start the main Web application in the Tomcat; Obtaining a registered called class object instance module, after the main Web application is started, obtaining the registered called class object instance from the system class loader, creating a proxy class of the called class, and assigning the called class object instance to a target instance variable in the proxy class of the called class, wherein the target instance variable is a static variable; A local function calling module, wherein the main Web application calls the called class object through the proxy class of the called class; the proxy class of the called class obtains the method in the called class object instance and executes the method through the target instance variable; In the result return module, the proxy class of the called class returns the target instance variable execution result to the main Web application.

Citation Information

Patent Citations

  • Method and device for class loading process

    CN101118497A

  • Method, equipment and device for dynamically deploying modularization and computer program product

    CN114064215A