Configuration initialization and updating method, device, equipment, medium and program product

By injecting configuration items in the configuration file at the start of the program and registering a change callback function, the inefficiency problem caused by frequent reading of configuration files during hot configuration is solved, and more efficient configuration updates are achieved.

CN120010943APending Publication Date: 2025-05-16TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

In the prior art, frequent reading of configuration files leads to low program operation efficiency when implementing configuration hot updates.

Method used

By injecting configuration items in the configuration file into program variables at program startup and registering the change callback function of the configuration item. When the configuration file changes, only update the configuration values ​​in the program variables if necessary.

Benefits of technology

Reduces frequent reading of configuration files, improves program operation efficiency, and improves configuration update efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120010943A_ABST
    Figure CN120010943A_ABST
Patent Text Reader

Abstract

The invention discloses a configuration initialization and updating method and device, equipment, a medium and a program product, and relates to the technical field of computers. The method comprises the steps that under the condition that a first configuration file is changed, a second configuration file is analyzed, and a configuration item corresponding to the second configuration file is obtained; matching a configuration item corresponding to the first configuration file with a configuration item corresponding to the second configuration file, and triggering a change callback function of the first configuration item in response to the fact that a first configuration value of the first configuration item corresponding to the first configuration file is different from a second configuration value of the first configuration item corresponding to the second configuration file; and updating the first configuration value injected into the first variable based on the second configuration value. The configuration information injected into the program is updated by paying attention to the change event of the configuration file, that is, the configuration file only needs to be read again when the configuration file is changed, the influence of frequent reading of the configuration file on the program is avoided, and the running efficiency of the program is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a configuration initialization and update method, apparatus, device, medium, and program product. Background Art

[0002] During the program's operation, it is usually necessary to load configuration information, which contains the configuration values ​​required for the program to run, such as database address, service log, etc. When the configuration information changes, hot updating the configuration without stopping the program can avoid the cost of restarting the program.

[0003] In the related art, configuration information is not loaded when the program is started, and when the configuration is needed in the program, the configuration information is obtained from the configuration file in real time, thereby realizing hot update of configuration.

[0004] However, the above method for implementing hot configuration update requires frequent reading of configuration files to obtain configuration information, and the program operation efficiency is low. Summary of the invention

[0005] The embodiments of the present application provide a configuration initialization and update method, apparatus, device, medium and program product, which improve the running efficiency of the program when implementing configuration update. The technical solution is as follows:

[0006] On the one hand, a configuration initialization and update method is provided, the method comprising:

[0007] In response to the first program being started, injecting a configuration value of a configuration item corresponding to the first configuration file into a variable of the first program, and the first program runs based on the configuration parameters provided by the variable of the first program;

[0008] Registering a change callback function of the configuration item corresponding to the first configuration file, wherein the change callback function is used to update the configuration value injected into the variable of the first program when being triggered;

[0009] When the first configuration file is changed, the second configuration file is parsed to obtain configuration items corresponding to the second configuration file, where the second configuration file refers to the file after the first configuration file is changed;

[0010] Matching a configuration item corresponding to the first configuration file with a configuration item corresponding to the second configuration file, and in response to a first configuration value of a first configuration item corresponding to the first configuration file being different from a second configuration value of the first configuration item corresponding to the second configuration file, triggering a change callback function of the first configuration item, wherein the first configuration value is a configuration value injected into a first variable of the first program;

[0011] The first configuration value injected into the first variable is updated based on the second configuration value.

[0012] On the other hand, a configuration initialization and update device is provided, the device comprising:

[0013] A startup module, configured to, in response to a first program being started, inject a configuration value of a configuration item corresponding to a first configuration file into a variable of the first program, so that the first program runs based on the configuration parameters provided by the variable of the first program;

[0014] A registration module, used to register a change callback function of a configuration item corresponding to the first configuration file, wherein the change callback function is used to update the configuration value injected into the variable of the first program when triggered;

[0015] a parsing module, configured to parse a second configuration file to obtain configuration items corresponding to the second configuration file when the first configuration file is changed, wherein the second configuration file refers to a file after the first configuration file is changed;

[0016] a matching module, configured to match a configuration item corresponding to the first configuration file with a configuration item corresponding to the second configuration file, and trigger a change callback function of the first configuration item in response to a first configuration value of the first configuration item corresponding to the first configuration file being different from a second configuration value of the first configuration item corresponding to the second configuration file, wherein the first configuration value is a configuration value injected into a first variable of the first program;

[0017] An updating module is used to update the first configuration value injected into the first variable based on the second configuration value.

[0018] On the other hand, a computer device is provided, comprising a processor and a memory, wherein the memory stores at least one instruction, at least one program, a code set or an instruction set, and the at least one instruction, the at least one program, the code set or the instruction set is loaded and executed by the processor to implement any of the above-mentioned configuration initialization and update methods.

[0019] On the other hand, a computer-readable storage medium is provided, wherein at least one instruction, at least one program, a code set or an instruction set is stored in the storage medium, and the at least one instruction, the at least one program, the code set or the instruction set is loaded and executed by a processor to implement any of the configuration initialization and update methods described above.

[0020] On the other hand, a computer program product or a computer program is provided, the computer program product or the computer program comprising computer instructions, the computer instructions being stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device performs any of the configuration initialization and update methods described above.

[0021] The beneficial effects brought by the technical solution provided by the embodiment of the present application include at least:

[0022] After starting the program, the configuration items in the configuration file are actively injected into the program variables, and the change callback function corresponding to the configuration items is registered; when the configuration file changes, the latest configuration file is obtained, and the configuration items injected in the current program are matched with the configuration items in the latest configuration file; if the configuration value of one of the configuration items changes, the change callback function corresponding to the configuration item is triggered, thereby reassigning the program variables corresponding to the configuration item. On the one hand, the configuration information injected into the program is updated by paying attention to the change events of the configuration file, that is, only the configuration file needs to be re-read when the configuration file changes, avoiding the impact of frequent reading of the configuration file on the program, thereby improving the running efficiency of the program; on the other hand, by registering the change callback function corresponding to the configuration item, the program variables associated with the configuration item are bound to the change callback event, thereby sensing the change of a single configuration item, and updating the configuration injected into the program based on the changed configuration item, thereby improving the configuration update efficiency of the program during operation. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0024] Figure 1 It is a schematic diagram of an implementation environment provided by an exemplary embodiment of the present application;

[0025] Figure 2 is a flowchart of a configuration initialization and update method provided by an exemplary embodiment of the present application;

[0026] Figure 3 is a flowchart of a configuration initialization and update method provided by another exemplary embodiment of the present application;

[0027] Figure 4 is a schematic diagram of a node structure provided by an exemplary embodiment of the present application;

[0028] Figure 5 is a schematic diagram of a tree structure provided by an exemplary embodiment of the present application;

[0029] Figure 6 is a flowchart of a configuration initialization and update method provided by another exemplary embodiment of the present application;

[0030] Figure 7 is a workflow diagram of a configuration component provided by an exemplary embodiment of the present application;

[0031] Figure 8 is a schematic diagram of a web page provided by an exemplary embodiment of the present application;

[0032] Fig. 9 is a structural block diagram of a configuration initialization and update device provided by an exemplary embodiment of the present application;

[0033] Fig.10 is a structural block diagram of a configuration initialization and update device provided by another exemplary embodiment of the present application;

[0034] Fig.11 It is a structural block diagram of a computer device provided by an exemplary embodiment of the present application. DETAILED DESCRIPTION

[0035] In order to make the purpose, technical solution and advantages of the present application clearer, the implementation mode of the present application will be further described in detail below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present application.

[0036] In the present application, the terms "first", "second", etc. are used to distinguish identical or similar items with substantially the same effects and functions. It should be understood that there is no logical or temporal dependency between "first" and "second", nor are there any limitations on quantity and execution order.

[0037] In the related art, the configuration information is not loaded when the program is started. During the program's operation, the configured variables are used to read the configuration file in real time when the configuration is needed, thereby obtaining the configuration information to implement hot configuration update. However, in the above-mentioned method of implementing hot configuration update, the configuration file is read as long as the variable needs to be configured, and the configuration file needs to be re-read regardless of whether the configuration file is changed. On the one hand, the configuration file reading operation is relatively frequent, which makes the program's running efficiency low; on the other hand, when the configuration file has not changed, the configuration file reading operation is still performed, resulting in a large waste of read and write resources.

[0038] The embodiment of the present application provides a configuration initialization and update method. On the one hand, the configuration information injected into the program is updated by paying attention to the change event of the configuration file, that is, only the configuration file needs to be re-read when the configuration file changes, avoiding the impact of frequent reading of the configuration file on the program, thereby improving the running efficiency of the program; on the other hand, by registering the change callback function corresponding to the configuration item, the program variable associated with the configuration item is bound to the change callback event, thereby perceiving the change of a single configuration item, and updating the configuration injected into the program based on the changed configuration item, thereby improving the configuration update efficiency of the program during operation. The configuration initialization and update method provided by the present application can be applied to the configuration update scenario of the back-end program, such as: HTTP (Hyper Text Transfer Protocol, Hypertext Transfer Protocol) service, message queue consumer, scheduled task and other back-end program configuration update scenarios; it can also be applied to the configuration update scenario of the front-end program, such as: the website navigation bar of the online web page, the online and offline time of the game activity, the manuscript solicitation rules and other configuration update scenarios. The embodiment of the present application does not limit this.

[0039] The following describes the implementation environment provided by the embodiments of the present application. Figure 1 This is a schematic diagram of an implementation environment provided by an exemplary embodiment of the present application. The configuration initialization and update method provided in the embodiment of the present application can be implemented by the terminal alone, or by the server, or by the terminal and the server through data interaction, which is not limited in the embodiment of the present application. Optionally, the configuration initialization and update method is described by taking the terminal and the server interacting to perform the configuration initialization and update method as an example.

[0040] For illustration, please refer to Figure 1 The implementation environment involves a terminal 110 and a server 120, and the terminal 110 and the server 120 are connected via a communication network 130. Optionally, the communication network 130 may be a wired network or a wireless network, which is not limited in the present embodiment.

[0041] In some embodiments, the terminal 110 refers to a device running a first program. Optionally, the first program is a program that implements a backend service (e.g., HTTP service, etc.); or, the first program is a program that implements a frontend service (e.g., web page, etc.). This embodiment of the application does not limit this.

[0042] Illustratively, when the first program is started, the server 120 sends the first configuration file to the terminal 110; after receiving the first configuration file, the terminal 110 parses the first configuration file to obtain the configuration items corresponding to the first configuration file, and caches the configuration items corresponding to the first configuration file into the memory as the configuration information of the first program; when the first program is running, when the variables therein require configuration information, the configuration values ​​of the configuration items corresponding to the first configuration file are injected into the variables of the first program, and the change callback function of the configuration items corresponding to the first configuration file is registered.

[0043] During the running of the first program, if the first configuration file changes, the configuration item corresponding to the changed configuration file, that is, the configuration item corresponding to the second configuration file, is obtained, and the configuration item corresponding to the first configuration file is matched with the configuration item in the second configuration file. When the first configuration value of the first configuration item corresponding to the first configuration file is different from the second configuration value of the first configuration item corresponding to the second configuration file, the change callback function of the first configuration item is triggered, thereby updating the first configuration value injected into the first program based on the second configuration value.

[0044] In some embodiments, the first configuration file may also be a file obtained locally, that is, the first configuration file is stored in the terminal 110 .

[0045] Optionally, the second configuration file is a file obtained by a developer directly modifying the first configuration file on the terminal 110 ; or, the second configuration file is a configuration file obtained by the terminal 110 from the server 120 .

[0046] It is worth noting that the terminal 110 is a smart phone, a tablet computer, a laptop computer, a desktop computer, etc., but is not limited thereto. The server 120 can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms.

[0047] Among them, cloud technology refers to a hosting technology that unifies hardware, software, network and other series of resources in a wide area network or local area network to realize data calculation, storage, processing and sharing. Cloud technology is a general term for network technology, information technology, integration technology, management platform technology, application technology, etc. based on the cloud computing business model application, which can form a resource pool, which is used on demand and flexible and convenient. Cloud computing technology will become an important support. The background services of the technical network system require a large amount of computing and storage resources, such as video websites, picture websites and more portal websites. With the high development and application of the Internet industry, each item may have its own identification mark in the future, and all need to be transmitted to the background system for logical processing. Data of different levels will be processed separately, and all kinds of industry data require strong system backing support, which can only be achieved through cloud computing. Optionally, the server 120 can also be implemented as a node in the blockchain system.

[0048] It should be noted that before collecting relevant data of users and during the process of collecting relevant data of users, this application can display a prompt interface, pop-up window or output voice prompt information, and the prompt interface, pop-up window or voice prompt information is used to prompt the user that its relevant data is currently being collected, so that this application only starts to execute the relevant steps of obtaining relevant data of users after obtaining the confirmation operation issued by the user to the prompt interface or pop-up window, otherwise (that is, when the confirmation operation issued by the user to the prompt interface or pop-up window is not obtained), the relevant steps of obtaining relevant data of users are terminated, that is, the relevant data of users are not obtained. In other words, all user data collected by this application are collected with the consent and authorization of the user, and the collection, use and processing of relevant user data need to comply with relevant laws, regulations and standards.

[0049] Combined with the above introduction and implementation environment, the configuration initialization and update method provided by this application is described, and the method is applied to Figure 1 The terminal shown in FIG. 1 is used as an example to illustrate. Figure 2 As shown, the method includes the following steps 210 to 250.

[0050] Step 210: In response to the first program starting, inject the configuration value of the configuration item corresponding to the first configuration file into the variable of the first program.

[0051] Illustratively, a program refers to a pre-written set of instructions for performing a specific task. The first program is written in a programming language, and the programming language includes at least one of C, C++, Java, Python, Go, etc., which is not limited in the embodiments of the present application.

[0052] The first program is run based on configuration parameters provided by variables of the first program.

[0053] After the schematic program is started, if there are variables in the program code, it is usually necessary to assign configuration values ​​to the variables in the program code. After the variable assignment, the assigned variables represent the configuration parameters in the program, and the program code can be executed normally.

[0054] Optionally, the first program is an object-oriented program. After starting the first program, a first object is created, wherein the first object includes a plurality of variables to be configured for use; and configuration values ​​of configuration items corresponding to the first configuration file are injected into the variables of the first object.

[0055] Optionally, the first program being run is used to implement backend services. Schematically, backend services include database management, server programs (e.g., HTTP server programs), scheduled tasks, etc. Alternatively, the first program being run is used to implement frontend services. Schematically, frontend services include web applications, mobile applications, etc. This embodiment of the application does not limit this.

[0056] The first configuration file is used to provide the configuration information required when the first program is running, that is, the assignment information of the configured variables to be used in the first program.

[0057] Optionally, the first configuration file may be a locally saved configuration file, or a configuration file obtained from a remote configuration center, which is not limited in the embodiment of the present application.

[0058] In some embodiments, the first configuration file includes multiple configuration items, and the multiple configuration items correspond to configuration values. Schematically, if the first program is implemented as a database management program, the configuration items include host name, port number, database name, etc.; if the first program is implemented as an HTTP server program, the configuration items include home directory path, log file path, etc.; if the first program is implemented as an application program, the configuration items include interface layout, data source type, etc., which is not limited in the embodiments of the present application.

[0059] Optionally, the first program includes a plurality of variables to be configured for use, wherein the configuration items correspond to the variables one-to-one; or, a single configuration item corresponds to a plurality of variables.

[0060] Illustratively, after starting the first program, the configuration file of the first program (i.e., the first configuration file) is parsed to obtain configuration items and configuration values ​​corresponding to the configuration file, and the configuration items and configuration values ​​are stored in the memory as configuration information of the first program; after the first program obtains the configuration information in the memory, it can run normally.

[0061] In the embodiment of the present application, when the first program is running, if it needs to use the configuration, the idea of ​​Inversion of Control (IOC) can be used to actively inject the configuration value corresponding to the configuration item in the memory into the variable using the configuration. Schematically, the configuration file is managed by a configuration component, and the configuration component corresponds to the first program. After the first program creates an object, the configuration item processed by the configuration component is injected into the variable in the first program, and the first program is run.

[0062] Step 220: register a change callback function of the configuration item corresponding to the first configuration file.

[0063] The change callback function is used to update the configuration value injected into the variable of the first program when it is triggered.

[0064] Illustratively, a callback function is a function called through a function pointer, which can be passed as a parameter to other functions or as one of the parameter types. In the embodiment of the present application, the callback function is used to process variable change events, and the corresponding callback function is called according to the configuration item to process the variable change event corresponding to the configuration item, that is, the configuration item is used as the parameter type in the callback function.

[0065] After the configuration value corresponding to the configuration item is injected into the variable, the change callback function corresponding to each configuration item is registered. Assuming that the configuration value a corresponding to the current configuration item 1 is the assignment of variable 1, a change callback function is registered for configuration item 1. After the change callback function is triggered, a variable change event will be triggered, that is, the assignment of variable 1 will be changed.

[0066] Optionally, a single configuration item may correspond to one change callback function, that is, the variable change event corresponding to the configuration item is an independent event.

[0067] Alternatively, a single configuration item corresponds to multiple change callback functions, that is, a certain configuration item may include multiple change callback functions, and the multiple change callback functions correspond to different variable change events.

[0068] Indicatively, for configuration item a, including change callback function 1 and change callback function 2, when change callback function 1 is triggered, the configuration value injected into variable A is replaced; when change callback function 2 is triggered, the configuration value injected into variable A is +1 operated, etc. Alternatively, in the case where a single configuration item corresponds to multiple variables, when change callback function 1 is triggered, the configuration value injected into variable A is replaced; when change callback function 2 is triggered, the configuration value injected into variable B is replaced, wherein variable A and variable B are different variables.

[0069] Step 230: When the first configuration file is changed, the second configuration file is parsed to obtain configuration items corresponding to the second configuration file.

[0070] The second configuration file refers to a file after the first configuration file is changed.

[0071] Optionally, the change in the first configuration file refers to a new configuration item being added to the first configuration file; or a configuration item being deleted from the first configuration file; or a configuration value of a configuration item in the first configuration file being changed.

[0072] Optionally, the second configuration file is a file modified based on the first configuration file; or, the second configuration file is a new configuration file.

[0073] In some embodiments, the second configuration file includes multiple configuration items, and the multiple configuration items respectively correspond to configuration values.

[0074] Step 240, matching the configuration item corresponding to the first configuration file with the configuration item corresponding to the second configuration file, and in response to the first configuration value of the first configuration item corresponding to the first configuration file being different from the second configuration value of the first configuration item corresponding to the second configuration file, triggering a change callback function of the first configuration item.

[0075] The first configuration value is a configuration value injected into the first variable of the first program.

[0076] Illustratively, after parsing and obtaining the configuration items corresponding to the second configuration file, the configuration items corresponding to the second configuration file are traversed, and each configuration item corresponding to the second configuration file is matched with all configuration items corresponding to the first configuration file.

[0077] For the first configuration item corresponding to the second configuration file, if the first configuration item exists in all the configuration items corresponding to the first configuration file, the configuration value of the first configuration item corresponding to the first configuration file is compared with the configuration value of the first configuration item corresponding to the second configuration file. If the two configuration values ​​are different, it means that the configuration value of the first configuration item has changed, and the change callback function of the first configuration item is triggered, that is, the assignment of the first variable is modified.

[0078] In some embodiments, if the first configuration item does not exist in all configuration items corresponding to the first configuration file, it means that the first configuration item is a newly added configuration item. At this time, the first configuration item needs to be added to the memory, and the configuration item corresponding to the first configuration item needs to be injected into the variable using the first configuration item, and the change callback function of the first configuration item needs to be registered at the same time.

[0079] In some embodiments, if all configuration items corresponding to the first configuration file are traversed, each configuration item corresponding to the first configuration file is matched with all configuration items corresponding to the second configuration file. For the second configuration item corresponding to the first configuration file, if the second configuration item does not exist in all configuration items corresponding to the second configuration file, it means that the second configuration item is the currently deleted configuration item, and at this time, the second configuration item needs to be deleted from the memory.

[0080] Step 250: Update the first configuration value injected into the first variable based on the second configuration value.

[0081] Illustratively, after the change callback function corresponding to the first configuration item is triggered, the variable change event corresponding to the first configuration item is executed, that is, the change event of the assignment of the first variable. It should be noted that during the above process of updating the first configuration value injected into the first program based on the second configuration value, the first program is in a running state.

[0082] Optionally, the first configuration value injected into the first variable is updated to the second configuration value. Schematically, assume that the configuration value corresponding to the current configuration item 1 is the assignment of variable 1, where the configuration value corresponding to configuration item 1 is a; after the configuration file is changed, the configuration value corresponding to configuration item 1 is b; after the change callback function corresponding to configuration item 1 is triggered, a variable change event will be triggered, that is, the assignment of variable 1 is changed from a to b.

[0083] By triggering the change callback function bound to the first variable, the change of the configuration item corresponding to the first variable is sensed, thereby realizing hot update of the configuration information loaded into the first program, ensuring the real-time nature of the configuration information changes in the program while improving the updating efficiency of the configuration information in the program.

[0084] In some embodiments, the first program is connected to a first database, and the change callback function of the first configuration item is also used to update the database connected to the first program after the first configuration item is changed. Then, after updating the first configuration value injected into the first variable based on the second configuration value, it also includes: disconnecting the connection between the first program and the first database; and creating a connection between the first program and the second database in the first variable.

[0085] Indicatively, for the change callback function of the first configuration item, the developer can also set a custom event for the change callback function according to business needs, such as setting a database change event, that is, when the first configuration item is changed, the database connected to the program is changed. For example: for the configuration item "product name", the database corresponding to the "product name" can be implemented as a product information database. When the configuration value of the "product name" configuration item is updated, the change callback function corresponding to the "product name" configuration item is triggered, and the variables related to the "product name" in the program are assigned and updated, and then the previously connected product information database is disconnected, and a connection with the latest updated product information database is created.

[0086] By triggering the change callback function bound to the first variable, the change of the database corresponding to the first variable is sensed, thereby realizing hot update of the database loaded into the first program, thereby improving the update efficiency of the database in the program.

[0087] It should be noted that the custom event in the above-mentioned change callback function can also be implemented as other events, for example: when the value of a specified variable in the program is updated, a corresponding monitoring event is triggered, that is, a prompt message is sent to the relevant monitoring system to prompt that the value of the specified variable in the program has been changed. This embodiment of the application is not limited to this.

[0088] Optionally, when the first configuration item corresponds to multiple variables, the change callback functions corresponding to the multiple variables are triggered, and different change callback functions indicate different variable change events. For example, when the first configuration item corresponds to variable A and variable B, when the configuration value of the configuration item changes, the assignment of variable A and variable B will also change.

[0089] It should be noted that the loading process of the above-mentioned first configuration file (that is, the process of injecting the configuration items corresponding to the first configuration file into the first program) is a synchronous process, that is, after starting the first program, the first configuration file is loaded, and after the first configuration file is loaded, the first program runs; during the running of the first program, if the configuration file changes, for example: when the second configuration file needs to be loaded, the loading process of the second configuration file is an asynchronous process, that is, the loading process of the second configuration file can be executed during the running of the first program, that is, the configuration update process will not interrupt the running of the first program.

[0090] In summary, the configuration initialization and update method provided by the embodiment of the present application actively injects the configuration items in the configuration file into the program variables after starting the program, and registers the change callback function corresponding to the configuration item; when the configuration file changes, the latest configuration file is obtained, and the configuration items injected in the current program are matched with the configuration items in the latest configuration file; if the configuration value of one of the configuration items changes, the change callback function corresponding to the configuration item is triggered, thereby reassigning the program variable corresponding to the configuration item. On the one hand, the configuration information injected into the program is updated by paying attention to the change event of the configuration file, that is, only the configuration file needs to be re-read when the configuration file changes, avoiding the impact of frequent reading of the configuration file on the program, thereby improving the running efficiency of the program; on the other hand, by registering the change callback function corresponding to the configuration item, the program variable associated with the configuration item is bound to the change callback event, thereby sensing the change of a single configuration item, and updating the configuration injected into the program based on the changed configuration item, thereby improving the configuration update efficiency of the program during operation.

[0091] In some embodiments, during the running of the first program, the configuration file is stored in the form of a tree structure. Figure 3 As shown, Figure 2 The illustrated embodiment may also be implemented as the following steps 311 to 350 .

[0092] Step 311: In response to the first program being started, a first configuration file is parsed to obtain a first tree structure corresponding to the first configuration file.

[0093] The nodes of the first tree structure are used to indicate configuration items corresponding to the first configuration file, and the configuration values ​​of the configuration items corresponding to the first configuration file are stored in the nodes of the first tree structure.

[0094] Illustratively, the format of the first configuration file includes yaml, json, ini and other formats, which are not limited in the embodiments of the present application. The first configuration file is parsed to obtain a tree structure, which uses the file name of the configuration file as a root node, and the tree structure is added as a branch to the configuration root node and saved in the memory. In the case where there are multiple first configuration files, multiple first configuration files are parsed to obtain multiple tree structures, which are added as multiple branches to the configuration root node and saved in the memory.

[0095] Optionally, the method for obtaining the first tree structure corresponding to the first configuration file further includes the following steps:

[0096] Step 1: Parse the first configuration file to obtain node structures corresponding to n configuration items, where n is a positive integer.

[0097] The node structure includes configuration values ​​corresponding to the configuration items.

[0098] Schematically, the node structure can be implemented as a value structure, please refer to Figure 4 , which shows a schematic diagram of a value structure, in which the value structure 400 includes a node flag, a node type, and a node value.

[0099] The node flag is used to uniquely indicate the configuration item. For example, if the configuration item is user 1 in configuration file 1, the node flag can be represented as "configuration file 1.user 1". The node type refers to the data type of the configuration item, which includes map type, string type, list type, etc. The node value represents the configuration value corresponding to the configuration item. When the node type belongs to the map type, it means that the node can have child nodes.

[0100] Optionally, the n configuration items include multi-level configuration items. Figure 4 As shown, the value structure 400 also includes a child node (or referred to as child node indication information), and the child node indication information is used to point to the node corresponding to the next level configuration item indicated by the configuration item. For multi-level configuration items, assuming that there are currently configuration items a, b and c, configuration item a is a first-level configuration item, and configuration items b and c are second-level configuration items, then configuration item a contains configuration items b and c, and the child node indication information in the value structure corresponding to configuration item a is used to point to the nodes corresponding to configuration items b and c, respectively.

[0101] Step 2: Based on the hierarchical relationship between the n configuration items, the n node structures are combined to obtain a first tree structure with the node structures as nodes.

[0102] The first tree structure indicates n configuration items. Optionally, the hierarchical relationship between the n configuration items is an inclusion relationship between the n configuration items, and the included configuration item 1 is a lower-level configuration item of the configuration item 2 that includes configuration item 1.

[0103] Schematically, if the first configuration file includes five configuration items, the five configuration items are configuration file name (config), user (user), user name (name), information (property) and user list (list), then the configuration file name is the first-level configuration item, the user is the second-level configuration item, and the user name, information and user list are the third-level configuration items.

[0104] According to the hierarchical relationship between the above five configuration items, the first tree structure can be obtained. For schematic diagram, please refer to Figure 5 , which shows a schematic diagram of a tree structure, such as Figure 5As shown, for the configuration file 501 (config1.yml), a tree structure 510 can be obtained, wherein the tree structure 510 has the configuration file name (config1) as a root node, the user (user) as a child node of the configuration file name (config1), and the user name (name), information (property) and user list (list) as child nodes of the user (user).

[0105] After obtaining the tree structure 510, it is also necessary to add the tree structure 510 as a leaf node to the configuration root node 520 corresponding to the first program. It is worth noting that there may be multiple corresponding configuration files in the first program. For example, the first program also corresponds to a configuration file 502 (config2.yml). Then, according to the above processing method for the configuration file 501, the configuration file 501 is parsed to obtain the tree structure 530. Similarly, the tree structure 530 needs to be added as a leaf node to the configuration root node 520 corresponding to the first program.

[0106] Step 312: inject the configuration values ​​stored in the nodes of the first tree structure into the variables of the first program.

[0107] Illustratively, the first program executes after acquiring configuration information stored in a node of a first tree structure in a memory.

[0108] In some embodiments, the variable type of the second variable of the first program is parsed to obtain a type tag corresponding to the second variable; based on the type tag corresponding to the second variable, the first tree structure is queried to obtain a first node that matches the type tag corresponding to the second variable; and the configuration value stored in the first node is injected into the second variable.

[0109] The second variable and the first variable are the same or different variables.

[0110] It should be noted that the second variable refers to the variable that needs to be assigned in the first program when the first program is configured based on the first configuration file, and the first variable refers to the variable that needs to be assigned and updated in the first program when the first program is configured based on the second configuration file. The first variable and the second variable can be implemented as the same variable or as different variables, and the embodiment of the present application does not limit this. For example: the first program includes variables A and variable B. When the program is initialized, the second variable in the first program is assigned based on configuration file 1. If configuration file 1 indicates that variables A and variable B are assigned, then the second variable refers to at least one of variables A and variable B, for example, the second variable is variable A; when configuration file 1 is changed to configuration file 2, the first variable in the first program is assigned based on configuration file 2. If configuration file 2 indicates that the configuration of variable B is updated, then the first variable refers to variable B.

[0111] Optionally, when the type tag corresponding to the second variable is a specified type tag, the first tree structure is queried, wherein the specified type tag may be implemented as a key-value tag, wherein the key indicates the configuration root node corresponding to the first tree structure, and the value is used to indicate a node flag.

[0112] Schematically, the first program is implemented as a golang program as an example for explanation, and IOC is performed through the reflection mechanism of the golang language to implement object creation and injection.

[0113] That is, after the first program is started, the first object is created and the variable type in the structure variable of the object is parsed. When the variable type has a type tag and the format is value: "key1.key2.key3" (where value indicates the configuration root node and key1.key2.key3 indicates the node flag), the first tree structure is automatically recursively queried to find the configuration value of key1 node -> key2 node -> key3 node and assign it to the variable.

[0114] In the golang program, the variable types of the structures defined for the Host configuration and Port configuration are as follows:

[0115] type Redis struct {

[0116] Host string`value:〝cache.redis.host〝` / / Automatically inject Redis Host configuration

[0117] Port int`value:〝cache.redis.port〝` / / Automatically inject Redis Port configuration

[0118] }

[0119] Then, create a target object based on the structure, and the target object will automatically inject the corresponding Host configuration and Port configuration.

[0120] In this embodiment, when assigning values ​​to variables in the first program based on the first configuration file, it is necessary to query the first tree structure corresponding to the first configuration file to obtain the configuration value in the corresponding node. Usually, when querying the first tree structure, depth-first traversal is adopted, that is, traversing from the root node to the leaf node, and only when a subtree is completely traversed will it return to a layer. In order to improve the efficiency of assigning values ​​to certain variables in the program, the variable can be assigned first by establishing an index.

[0121] Optionally, when the query priority of the second variable is greater than a preset priority, node position information corresponding to the second variable is obtained, and the node position information is used to record the node position that matches the type label corresponding to the second variable; based on the node position information, the first node that matches the type label corresponding to the second variable is obtained from the first tree structure.

[0122] Illustratively, when the second variable is a variable that is frequently used in a program, the query priority of the second variable can be set to be greater than a preset priority, and a node index corresponding to the second variable is established, in which the node position corresponding to the second variable is stored; when injecting a configuration value into the variable, first search whether the variable exists in the node index, and if so, directly retrieve the node position corresponding to the variable from the node index, and directly obtain the node configuration value corresponding to the node position from the tree structure based on the node position.

[0123] For example: for a web page program running in a browser, if the web page program corresponds to a product page, and the real-time transaction quantity of the products in the product page is a variable that needs to be updated frequently, then when assigning values ​​to the variables of the web page program, an index can be established for the variables related to the real-time transaction quantity, so that the variables can be assigned and updated first.

[0124] Step 320: register a change callback function of the configuration item corresponding to the first configuration file.

[0125] The change callback function is used to update the configuration value injected into the variable of the first program when it is triggered.

[0126] Optionally, a callback function list of configuration items corresponding to the first configuration file is stored in the node of the first tree structure, and the change callback function of the configuration item corresponding to the first configuration file is registered in the callback function list.

[0127] Indicatively, the callback function lists corresponding to different nodes correspond to different configuration items, that is, the callback function list of configuration item 1 is stored in node a; that is, the callback function list of configuration item 2 is stored in node b.

[0128] Indicatively, Figure 4 As shown, the value structure 400 also includes a callback function list, which is used to register a change callback function. The change callback function is used to be triggered when the configuration value of the configuration item is changed; after the change callback function is triggered, the newly acquired configuration value will be reassigned to the variable corresponding to the configuration item.

[0129] Step 330: When the first configuration file is changed, the second configuration file is parsed to obtain configuration items corresponding to the second configuration file.

[0130] The second configuration file refers to a file after the first configuration file is changed.

[0131] Optionally, the change in the first configuration file refers to a new configuration item being added to the first configuration file; or a configuration item being deleted from the first configuration file; or a configuration value of a configuration item in the first configuration file being changed.

[0132] Step 340, matching the configuration item indicated by the node of the first tree structure with the configuration item corresponding to the second configuration file, and in response to the first configuration value of the first configuration item indicated by the first node of the first tree structure being different from the second configuration value of the first configuration item corresponding to the second configuration file, triggering a change callback function of the first configuration item.

[0133] The first configuration value is a configuration value injected into the first variable of the first program.

[0134] Optionally, the method for triggering the change callback function of the first configuration item further includes the following steps:

[0135] Step 1: Traverse the configuration items corresponding to the second configuration file.

[0136] Optionally, the second configuration file is parsed, each configuration item in the second configuration file is traversed, and each configuration item in the second configuration file is matched with a configuration item indicated by a node of the first tree structure.

[0137] Illustratively, the second configuration file includes m configuration items, where m is a positive integer; the first tree structure indicates n configuration items, where n is a positive integer. The m configuration items are traversed to obtain matching results corresponding to the m configuration items.

[0138] The matching result corresponding to the i-th configuration item among the m configuration items refers to the matching relationship between the i-th configuration item and the n configuration items, where i≤m and i is a positive integer.

[0139] Step 2: In response to the existence of a first configuration item in the configuration items indicated by the first node of the first tree structure, compare a first configuration value of the first configuration item indicated by the first node with a second configuration value of the first configuration item corresponding to the second configuration file.

[0140] Indicatively, m configuration items in the second configuration file are traversed, and if the i-th configuration item is the same as the j-th configuration item among the n configuration items, configuration values ​​are compared, where j≤m and j is a positive integer.

[0141] Step three: in response to the first configuration value being different from the second configuration value, updating the first configuration value stored in the first node based on the second configuration value; and triggering a change callback function of the first configuration item.

[0142] Illustratively, when it is determined that the configuration values ​​are different, the configuration value of the j-th configuration item is updated to the configuration value of the ith configuration item to obtain an updated first tree structure, and at the same time, the change callback function of the first configuration item is triggered.

[0143] In some embodiments, in response to the fact that the first configuration item does not exist in the configuration items indicated by the nodes of the first tree structure; the nodes of the first tree structure are updated based on the first configuration item to obtain a second tree structure; and the configuration values ​​stored in the nodes of the second tree structure are injected into the variables of the first program.

[0144] Illustratively, the m configuration items in the second configuration file are traversed. If the i-th configuration item is not included in the n configuration items, it means that the i-th configuration item is a newly added configuration item. Then, a node corresponding to the i-th configuration item is added to the first tree structure to obtain a second tree structure. The second tree structure is saved in the memory. Optionally, the first tree structure in the memory is deleted; and the configuration values ​​stored in the nodes of the second tree structure are injected into the variables of the first program. The injection method can refer to the method in step 312 and will not be repeated here.

[0145] In some embodiments, in response to the second configuration item not existing in the configuration items corresponding to the second configuration file; the nodes of the first tree structure are updated based on the second configuration item to obtain a third tree structure; and the configuration values ​​stored in the nodes of the third tree structure are injected into the variables of the first program.

[0146] Illustratively, if the m configuration items do not include the j-th configuration item among the n configuration items, it means that the j-th configuration item is a deleted configuration item, then the node corresponding to the j-th configuration item is deleted in the first tree structure to obtain a third tree structure, and the third tree structure is saved in the memory. Optionally, the first tree structure in the memory is deleted; and the configuration values ​​stored in the nodes of the third tree structure are injected into the variables of the first program. The injection method can refer to the method in step 312, which will not be repeated here.

[0147] Step 350: Update the first configuration value injected into the first variable based on the second configuration value.

[0148] Indicatively, after the change callback function corresponding to the first configuration item is triggered, the variable change event corresponding to the first configuration item is executed, that is, the change event of the assignment of the first variable. For example: the configuration value of the jth configuration item injected into the first variable is modified to the configuration value of the ith configuration item.

[0149] In summary, the configuration initialization and update method provided by the embodiment of the present application actively injects the configuration items in the configuration file into the program variables after starting the program, and registers the change callback function corresponding to the configuration item; when the configuration file changes, the latest configuration file is obtained, and the configuration items injected in the current program are matched with the configuration items in the latest configuration file; if the configuration value of one of the configuration items changes, the change callback function corresponding to the configuration item is triggered, thereby reassigning the program variable corresponding to the configuration item. On the one hand, the configuration information injected into the program is updated by paying attention to the change event of the configuration file, that is, only the configuration file needs to be re-read when the configuration file changes, avoiding the impact of frequent reading of the configuration file on the program, thereby improving the running efficiency of the program; on the other hand, by registering the change callback function corresponding to the configuration item, the program variable associated with the configuration item is bound to the change callback event, thereby sensing the change of a single configuration item, and updating the configuration injected into the program based on the changed configuration item, thereby improving the configuration update efficiency of the program during operation.

[0150] In an embodiment of the present application, the configuration information is stored in a tree structure. When the configuration information is updated, the configuration items in the configuration file are matched by matching the tree structure with the latest configuration file, and the configuration information is updated based on the changed configuration items, thereby improving the efficiency of updating the configuration information during the operation of the program.

[0151] In an embodiment of the present application, node structures are combined based on the hierarchical relationship between configuration items to obtain a first tree structure with node structures as nodes. A large amount of configuration information is classified and organized based on the first tree structure, so that the structure and hierarchy of the configuration information are clearer, which is conducive to the update of the configuration information and improves the updating efficiency of the configuration information.

[0152] In an embodiment of the present application, when creating an object corresponding to the first program, the variable type and the corresponding type tag of the object are parsed, and the first tree structure is queried through the type tag, thereby realizing active injection of the configuration and improving the efficiency of the first program in obtaining configuration information.

[0153] In an embodiment of the present application, when the second variable query priority is higher, the node position matching the type label of the second variable is obtained, so that the node configuration value is directly obtained from the first tree structure according to the node position. When the first tree structure is more complex, the query complexity of the variable with higher priority can be reduced, thereby improving the efficiency of the first program in obtaining configuration information.

[0154] In the embodiment of the present application, a callback function list of the configuration item corresponding to the node is stored in the node of the tree structure. By registering the change callback function of the configuration item in the callback function list, the configuration item and the corresponding variable change event are bound. On the one hand, the variable and the configuration item are bound to the change event, and there is no need to query again when using the configuration, which improves the efficiency of obtaining the configuration in the program; on the other hand, the value of the variable is refreshed in real time through the callback method, which not only realizes the change notification of the configuration item dimension but also ensures the real-time nature of the configuration change.

[0155] In the embodiment of the present application, by traversing the configuration items of the second configuration file, the change of the configuration value of the current injection variable is obtained from the dimension of the configuration item, thereby improving the granularity and accuracy of the configuration update.

[0156] In the embodiment of the present application, when configuration items are added, the tree structure currently stored in the memory is updated to obtain a new tree structure for configuration injection, thereby improving the accuracy of the configuration update of the first program.

[0157] In some embodiments, the change event of the first program is triggered by a file change callback function. Figure 6 As shown, Figure 2 or Figure 3 The illustrated embodiment may also be implemented as steps 610 to 650 as follows.

[0158] Step 610: In response to the first program starting, inject the configuration value of the configuration item corresponding to the first configuration file into the variable of the first program.

[0159] Illustratively, after starting the first program, the configuration file of the first program (i.e., the first configuration file) is parsed to obtain configuration items and configuration values ​​corresponding to the configuration file, and the configuration items and configuration values ​​are stored in the memory as configuration information of the first program; after the configuration values ​​of the configuration items corresponding to the first configuration file are injected into the variables of the first program, the first program is run.

[0160] Step 620: register a change callback function of the configuration item corresponding to the first configuration file.

[0161] The change callback function is used to update the configuration value injected into the variable of the first program when it is triggered.

[0162] Indicatively, after the configuration value corresponding to the configuration item is injected into the variable, the change callback function corresponding to each configuration item is registered. Assuming that the configuration value a corresponding to the current configuration item 1 is the value assigned to variable 1, a change callback function is registered for configuration item 1. After the change callback function is triggered, a variable change event will be triggered, that is, the value assigned to variable 1 will be changed.

[0163] Step 631, obtaining the file change callback function corresponding to the first configuration file.

[0164] The file change callback function is used to update the configuration file corresponding to the first program when it is triggered.

[0165] Indicatively, a configuration watcher is used to watch configuration file change events and register a file change callback function (reload function). The file change callback function (reload function) is obtained and triggered when the configuration file is changed.

[0166] Optionally, the method for the configuration observer to determine whether the configuration file in the program has been changed includes at least one of the following methods:

[0167] Method 1: Determine whether the configuration file has been modified by comparing the size of the configuration file. If the size of the configuration file has changed, it is determined that the configuration file in the program has been changed.

[0168] Method 2: Determine whether the configuration file has been modified by determining whether the file timestamp of the configuration file has changed. When the configuration file is modified, the file timestamp will change; if the file timestamp of the configuration file has changed, it is determined that the configuration file in the program has been changed.

[0169] Method 3: Determine whether the configuration file has been modified by calculating the checksum of the configuration file (for example, MD5 or SHA256 hash value). If the checksum of the configuration file has changed, it is determined that the configuration file in the program has been changed.

[0170] It should be noted that the above examples are merely illustrative and are not limited to these examples in the present application.

[0171] Step 632: When the first configuration file is changed, trigger the file change callback function.

[0172] Illustratively, when the configuration observer determines that the first configuration file is changed, the file change callback function is triggered. After the file change callback function is triggered, the file after the first configuration file is changed (ie, the second configuration file) is read and the second configuration file is parsed.

[0173] Step 633: parse the second configuration file to obtain configuration items corresponding to the second configuration file.

[0174] The second configuration file refers to the changed first configuration file.

[0175] Schematically, the second configuration file is preprocessed to obtain key-value pair data (i.e., map type data) corresponding to the m configuration items in the second configuration file, wherein the key in the key-value pair data represents the configuration item, and the value in the key-value pair data represents the configuration value corresponding to the configuration item.

[0176] Step 640, matching the configuration item corresponding to the first configuration file with the configuration item corresponding to the second configuration file, and in response to the first configuration value of the first configuration item corresponding to the first configuration file being different from the second configuration value of the first configuration item corresponding to the second configuration file, triggering a change callback function of the first configuration item.

[0177] The first configuration value is a configuration value injected into the first variable of the first program.

[0178] Indicatively, after obtaining m key-value pair data, the m key-value pair data are depth-first traversed, and the key-value pair data are matched with n configuration item data corresponding to the first configuration file in turn to obtain matching results corresponding to the m key-value pair data.

[0179] If the configuration item indicated by the i-th key-value pair data among the m key-value pair data is the same as the j-th configuration item among the n configuration items, the configuration values ​​are compared. When it is determined that the configuration values ​​are different, the configuration value of the j-th configuration item is updated to the configuration value indicated by the i-th key-value pair data, an updated first tree structure is obtained, and the change callback function of the first configuration item is triggered.

[0180] Step 650: Update the first configuration value injected into the first variable based on the second configuration value.

[0181] Illustratively, after the change callback function of the first configuration item is triggered, the configuration value of the j-th configuration item injected into the first variable is modified to the configuration value indicated by the i-th key-value pair data.

[0182] In summary, the configuration initialization and update method provided by the embodiment of the present application actively injects the configuration items in the configuration file into the program variables after starting the program, and registers the change callback function corresponding to the configuration item; when the configuration file changes, the latest configuration file is obtained, and the configuration items injected in the current program are matched with the configuration items in the latest configuration file; if the configuration value of one of the configuration items changes, the change callback function corresponding to the configuration item is triggered, thereby reassigning the program variable corresponding to the configuration item. On the one hand, the configuration information injected into the program is updated by paying attention to the change event of the configuration file, that is, only the configuration file needs to be re-read when the configuration file changes, avoiding the impact of frequent reading of the configuration file on the program, thereby improving the running efficiency of the program; on the other hand, by registering the change callback function corresponding to the configuration item, the program variable associated with the configuration item is bound to the change callback event, thereby sensing the change of a single configuration item, and updating the configuration injected into the program based on the changed configuration item, thereby improving the configuration update efficiency of the program during operation.

[0183] In an embodiment of the present application, a configuration watcher is set to watch file change events and register a file change callback function; when the configuration file is changed, the file change callback function is triggered, thereby executing the loading process of the latest configuration file, realizing a solution for reading the configuration file when the configuration file is changed, and improving the running efficiency of the program.

[0184] In some embodiments, the above configuration information updating method is implemented by introducing a configuration component in the first program. Figure 7 A schematic diagram of the workflow of a configuration component is shown, Figure 7 As shown, the configuration component 700 includes a configuration change attention module 710, a configuration information assembly module 720 and an external interface module 730. The configuration component is used for configuration management, and the configuration component can implement the configuration first loading process and the subsequent configuration update process.

[0185] Optionally, when the first program is implemented as a golang program, a configuration component is introduced into the golang program for configuration management, and the component's Load method is used to specify the configuration file to be loaded. When the golang program is started, the configuration component will parse the specified configuration file and parse all configuration items into a tree structure with value structures as nodes (see Figure 4 and Figure 5 ), the structure takes the configuration file name as the root node, and then adds the tree structure as a leaf node to the configuration root node of the configuration component and saves it in memory.

[0186] Indicatively, Figure 7 As shown, the configuration (first time) loading process is:

[0187] 1. Load the configuration file → process the configuration file → preprocess it into map format data → traverse the map format data in depth-first order → determine whether the current key exists.

[0188] Indicatively, when a configuration file is loaded for the first time, the configuration file needs to be processed, that is, the loaded configuration file is preprocessed into map format data, and the map format data is traversed in a depth-first traversal manner.

[0189] The map format data stores configuration items in the configuration file and configuration values ​​corresponding to the configuration items. The current key indicates the configuration item in the currently traversed map format data. To determine whether the current key exists, it is to determine whether the configuration item indicated by the tree structure stored in the memory exists in the traversed map format data.

[0190] Optionally, since the loading process is executed when the program is started, and this loading is the first loading process, it is determined here that the current key does not exist.

[0191] 2. If the current key does not exist, assemble a new value structure → save it to the child of the parent node → determine whether the value is a map type.

[0192] Indicatively, when it is determined that the current key does not exist in the configuration item indicated by the tree structure stored in the memory, a value structure is assembled based on the configuration item indicated by the current key. The schematic diagram of the value structure can be referred to Figure 4 , no further details are given here. Then, the structure is saved to the node of the preset tree structure. When saving, if the configuration item corresponds to the previous level configuration item, the structure assembled by the configuration item is saved to the child node of the node corresponding to the previous level configuration item. If the configuration item does not have the previous level configuration item, the parent node of the structure assembled by the configuration item is the configuration file name. After saving, determine whether the node value in the newly assembled value structure is a map type.

[0193] 3. If the judgment value is a map type, recursively process the map data: Schematically, if the judgment value is a map type, it means that the value structure has child nodes, that is, the configuration item corresponding to the value structure has a next-level configuration item, then recursively process the map data, that is, continue to traverse the map format data in a depth-first traversal manner, and traverse the next-level configuration items contained in the configuration item corresponding to the value structure.

[0194] 4. If the judgment value is not of map type, determine whether the traversal is completed: Indicatively, if the judgment value is of map type, it means that the value structure does not have a child node, that is, the configuration item corresponding to the value structure does not have a next-level configuration item, then it can be determined whether all map format data has been traversed.

[0195] If it is determined that the traversal is not completed, continue to traverse the next key: Indicatively, if it is determined that the traversal is not completed, continue to traverse the next key, that is, continue to traverse the next configuration item in the map format data, determine whether the configuration item indicated by the tree structure stored in the memory exists in the traversed map format data, and loop the above process.

[0196] When it is determined that the traversal is completed, a tree structure with the value structure as a node is obtained → configuration information: Schematically, when it is determined that the traversal is completed, the current tree structure with the value structure as a node is obtained, and the tree structure is added as a leaf node to the configuration root node of the configuration component and saved in the memory to obtain the configuration information.

[0197] 5. Object injection → Create object → Variable injection.

[0198] In an illustrative manner, after obtaining the configuration information, IOC is performed through the reflection mechanism of the Golang language to implement object creation and variable injection. When creating an object, the structure variable and the corresponding structured tag of the object are parsed. When the variable has a structured tag and the format is `value:"key1.key2.key3"`, the tree structure in the configuration information is automatically recursively queried to find the configuration value of key1 node->key2 node->key3 node and assign it to the variable (i.e., the configuration acquisition step in the external interface module 730 in the figure).

[0199] Then, a change return function is automatically registered and added to the callback list of the node value structure (i.e., the registration callback function step in the external interface module 730 in the figure), and the variable and the node are bound to an event. When the callback method is triggered, the newly acquired variable value will be reassigned to the variable.

[0200] In addition, the configuration component will start a configuration watcher through an asynchronous coroutine to pay attention to configuration file change events, and register a change event callback function (Reload function) to be executed when the configuration file changes.

[0201] Indicatively, Figure 7 As shown, the configuration attention process is:

[0202] 1. Change the configuration file → Determine whether the watcher is created → If the watcher is created, add the file to the watcher list.

[0203] Schematically, triggering the configuration file change step, that is, executing the process in the configuration change watch module 710: first, it is necessary to determine whether the configuration watcher is created, that is, whether the configuration watcher is created in the configuration component. If the watcher has been created, the loaded configuration file is added to the watcher list.

[0204] 2. If the watcher is not created, create a watcher → add the file to the watcher list.

[0205] Indicatively, if the watcher is not created, the watcher is created in the configuration component. After the watcher is created, the loaded configuration file is added to the watcher list.

[0206] 3. Asynchronously monitor file changes → determine whether the file has changed → configure file processing if the file has changed.

[0207] Indicatively, after adding the configuration file to the watcher list, asynchronous attention is paid to the file changes. If the configuration file is determined to have changed, the changed configuration file is processed, i.e., the process in the configuration information assembly module 720 is triggered. If the file has not changed, the asynchronous attention to the file changes continues.

[0208] Among them, in the case of file changes, configuration file processing corresponds to the configuration update process, such as Figure 7 As shown in the figure, the configuration update process is:

[0209] 1. Configuration file processing → preprocessing into map format data → traversing map format data in depth-first order → determining whether the current key exists.

[0210] In schematic form, when it is determined that a configuration file has changed, the watcher triggers the change event callback function to reload the configuration file and parse the changed configuration file. That is, the changed configuration file is preprocessed into map format data, and the map format data is traversed in a depth-first traversal manner.

[0211] The map format data stores configuration items in the configuration file and configuration values ​​corresponding to the configuration items. The current key indicates the configuration item in the currently traversed map format data. To determine whether the current key exists, it is to determine whether the configuration item indicated by the tree structure stored in the memory exists in the traversed map format data.

[0212] 2. If the current key does not exist, assemble a new value structure → save it to the child of the parent node → determine whether the value is a map type.

[0213] Indicatively, when it is determined that the current key does not exist in the configuration item indicated by the tree structure stored in the memory, a value structure is assembled based on the configuration item indicated by the current key. The schematic diagram of the value structure can be referred to Figure 4 , no further details are given here. Then, the structure is saved to the node of the preset tree structure. When saving, if the configuration item corresponds to the previous level configuration item, the structure assembled by the configuration item is saved to the child node of the node corresponding to the previous level configuration item. If the configuration item does not have the previous level configuration item, the parent node of the structure assembled by the configuration item is the configuration file name. After saving, determine whether the node value in the newly assembled value structure is a map type.

[0214] If the judgment value is a map type, the map data is processed recursively: Schematically, if the judgment value is a map type, it means that the value structure has child nodes, that is, the configuration item corresponding to the value structure has a next-level configuration item, then the map data is processed recursively, that is, the map format data is continued to be traversed in a depth-first traversal manner, and the next-level configuration items contained in the configuration item corresponding to the value structure are traversed.

[0215] If the judgment value is not of map type, determine whether the traversal is completed: In schematic form, if the judgment value is of map type, it means that the value structure does not have a child node, that is, the configuration item corresponding to the value structure does not have a next-level configuration item, then it can be determined whether all map format data has been traversed.

[0216] If it is determined that the traversal is not completed, continue to traverse the next key: Indicatively, if it is determined that the traversal is not completed, continue to traverse the next key, that is, continue to traverse the next configuration item in the map format data, determine whether the configuration item indicated by the tree structure stored in the memory exists in the traversed map format data, and loop the above process.

[0217] 3. Determine if the current key exists → Determine whether the configuration value has changed → If the configuration value has changed, update the value structure value → Update to the child of the parent node → Trigger callback method → ​​Callback function → Configuration value.

[0218] Indicatively, if the current key exists, it is also necessary to determine whether the configuration value has changed. If the configuration value has changed, update the corresponding value structure value cached in the memory, and update the updated value structure value to the child of the parent node; it is also necessary to continue to determine whether the value structure value is a map type. If it is a map type, recursively process the map data; if it is not a map type, determine whether the traversal is completed. If the traversal is completed, the updated tree structure with the value structure as the node is cached in the memory, and the configuration information is obtained. At the same time, the callback function is triggered to obtain the new configuration value for variable injection.

[0219] If the configuration value has not changed, continue to traverse the next key: Indicatively, if the configuration value has not changed, continue to traverse the next key, that is, continue to traverse the configuration items in the next map format data, determine whether the configuration items indicated by the tree structure stored in the memory exist in the traversed map format data, and repeat the above process.

[0220] 4. When the traversal is completed, a tree structure with value structures as nodes is obtained → configuration information.

[0221] Indicatively, when it is determined that the traversal is completed, the current tree structure with the value structure as a node is obtained, and the tree structure is added as a leaf node to the configuration root node of the configuration component and saved in the memory to obtain the configuration information.

[0222] 5. Object injection → Create object → Variable injection.

[0223] In an illustrative manner, after obtaining the configuration information, IOC is performed through the reflection mechanism of the Golang language to implement object creation and variable injection. When creating an object, the structure variable and the corresponding structured tag of the object are parsed. When the variable has a structured tag and the format is `value:"key1.key2.key3"`, the tree structure in the configuration information is automatically recursively queried to find the configuration value of key1 node->key2 node->key3 node and assign it to the variable (i.e., the configuration acquisition step in the external interface module 730 in the figure).

[0224] Then, a change return function is automatically registered and added to the callback list of the node value structure (i.e., the registration callback function step in the external interface module 730 in the figure), and the variable and the node are bound to an event. When the callback method is triggered, the newly acquired variable value will be reassigned to the variable.

[0225] In addition, the configuration component will start a configuration watcher through an asynchronous coroutine to pay attention to configuration file change events, and register a change event callback function (Reload function) to be executed when the configuration file changes.

[0226] It should be noted that in addition to the callback function automatically registered by the structured tag, the user can also register a custom callback to implement more scenarios. For example, in the scenario of database connection, by customizing a callback function, the current connection is closed and re-created when the configuration changes to implement hot update of database configuration.

[0227] In summary, through this component solution, the running program can perceive the change of a single configuration item and perform customized hot update processing without restarting the program, while ensuring the performance of configuration acquisition. In terms of coding, the automatic injection of structured tags can also improve the developer's development efficiency and code readability. In addition, this component can be combined with the configuration management platform to achieve unified management of configuration, and remotely control the configuration and update of the program through online update release of configuration.

[0228] In addition, this solution can be used in common golang programs to implement service operation configuration management and hot update. It is mainly used in the operation configuration management of backend golang programs such as HTTP services, message queue consumers, and scheduled tasks, such as database addresses, account passwords, application identifiers and keys, service logs, etc., which can implement hot update of operation configuration without shutting down the service.

[0229] In addition to the backend service operation configuration, it can also be used in business scenario configuration, such as website navigation bar, game activity online and offline time, submission rules, etc., to achieve online update of site functions. Figure 8 A schematic diagram of an application scenario of configuration update is shown. In interface 800, the configuration information update method provided by this solution can update the content loaded in the interface, such as control layout, display data, etc., without refreshing.

[0230] Please refer to Fig. 9 , which shows a structural block diagram of a configuration information updating device provided by an exemplary embodiment of the present application, the device includes the following modules:

[0231] A starting module 910 is used to start a first program and inject a configuration value of a configuration item corresponding to a first configuration file into a variable of the first program;

[0232] A registration module 920, configured to register a change callback function of a configuration item corresponding to the first configuration file, wherein the change callback function updates the configuration value injected into the variable of the first program when the change callback function is triggered;

[0233] A parsing module 930, configured to parse a second configuration file to obtain configuration items corresponding to the second configuration file when the first configuration file is changed, wherein the second configuration file refers to the first configuration file after the change;

[0234] a matching module 940, configured to match a configuration item corresponding to the first configuration file with a configuration item corresponding to the second configuration file, and in response to a first configuration value of a first configuration item corresponding to the first configuration file being different from a second configuration value of the first configuration item corresponding to the second configuration file, triggering a change callback function of the first configuration item, wherein the first configuration value is a configuration value injected into a first variable of the first program;

[0235] The updating module 950 is configured to update the first configuration value injected into the first variable based on the second configuration value.

[0236] In some embodiments, please refer to Fig.10 The startup module 910 includes:

[0237] A parsing unit 911 is used to parse the first configuration file to obtain a first tree structure corresponding to the first configuration file, wherein the nodes of the first tree structure are used to indicate configuration items corresponding to the first configuration file, and the nodes of the first tree structure store configuration values ​​of the configuration items corresponding to the first configuration file;

[0238] The injection unit 912 is used to inject the configuration value stored in the node of the first tree structure into the variable of the first program.

[0239] In some embodiments, the parsing unit 911 is used to parse the first configuration file to obtain node structures corresponding to n configuration items respectively, where n is a positive integer; and combine the n node structures based on the hierarchical relationship between the n configuration items to obtain the first tree structure with the node structures as nodes.

[0240] In some embodiments, the injection unit 912 is used to parse the variable type of the second variable of the first program to obtain a type label corresponding to the second variable; based on the type label corresponding to the second variable, query the first tree structure to obtain a first node matching the type label corresponding to the second variable; inject the configuration value stored in the first node into the second variable; wherein the second variable and the first variable are the same or different variables.

[0241] In some embodiments, the injection unit 912 is used to obtain node position information corresponding to the second variable when the query priority of the second variable is greater than a preset priority, and the node position information is used to record the node position matching the type label corresponding to the second variable; based on the node position information, obtain the first node matching the type label corresponding to the second variable from the first tree structure.

[0242] In some embodiments, a callback function list of configuration items corresponding to the first configuration file is stored in the nodes of the first tree structure; the registration module 920 is used to register the change callback function of the configuration item corresponding to the first configuration file in the callback function list.

[0243] In some embodiments, the matching module 940 includes:

[0244] A traversal unit 941, configured to traverse configuration items corresponding to the second configuration file;

[0245] The matching module 940 is used to compare the first configuration value of the first configuration item indicated by the first node with the second configuration value of the first configuration item corresponding to the second configuration file in response to the presence of the first configuration item in the configuration items indicated by the first node of the first tree structure; in response to the first configuration value being different from the second configuration value, update the first configuration value stored in the first node based on the second configuration value; and trigger the change callback function of the first configuration item.

[0246] In some embodiments, the update module 950 is used to update the nodes of the first tree structure based on the first configuration item to obtain a second tree structure in response to the absence of the first configuration item in the configuration items indicated by the nodes of the first tree structure; and inject the configuration values ​​stored in the nodes of the second tree structure into the variables of the first program.

[0247] In some embodiments, the updating module 950 is used to update the first configuration value injected into the first variable to the second configuration value.

[0248] In some embodiments, the first program is connected to a first database, and the change callback function of the first configuration item is also used to update the database connected to the first program after the first configuration item is changed; the update module 950 is used to disconnect the connection between the first program and the first database; and create a connection between the first program and the second database in the first variable.

[0249] In summary, the configuration initialization and update device provided in the embodiment of the present application actively injects the configuration items in the configuration file into the program variables after starting the program, and registers the change callback function corresponding to the configuration item; when the configuration file changes, the latest configuration file is obtained, and the configuration items injected in the current program are matched with the configuration items in the latest configuration file; if the configuration value of one of the configuration items changes, the change callback function corresponding to the configuration item is triggered, thereby reassigning the program variables corresponding to the configuration item. On the one hand, the configuration information injected into the program is updated by paying attention to the change event of the configuration file, that is, only the configuration file needs to be re-read when the configuration file changes, avoiding the impact of frequent reading of the configuration file on the program, thereby improving the running efficiency of the program; on the other hand, by registering the change callback function corresponding to the configuration item, the program variables associated with the configuration item are bound to the change callback event, thereby sensing the change of a single configuration item, and updating the configuration injected into the program based on the changed configuration item, thereby improving the configuration update efficiency of the program during operation.

[0250] It should be noted that the configuration initialization and update device provided in the above embodiment is only illustrated by the division of the above functional modules. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the configuration initialization and update device and the configuration initialization and update method embodiment provided in the above embodiment belong to the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.

[0251] Fig.11 The block diagram of a computer device 1100 provided by an exemplary embodiment of the present application is shown. The computer device 1100 may be: a smart phone, a tablet computer, a Moving Picture Experts Group Audio Layer III (MP3) player, a Moving Picture Experts Group Audio Layer IV (MP4) player, a laptop computer or a desktop computer. The computer device 1100 may also be called a user device, a portable computer device, a laptop computer device, a desktop computer device, or other names.

[0252] Typically, the computer device 1100 includes a processor 1101 and a memory 1102 .

[0253] The processor 1101 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 1101 may be implemented in at least one hardware form of digital signal processing (DSP), field-programmable gate array (FPGA), and programmable logic array (PLA). The processor 1101 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 1101 may be integrated with a graphics processing unit (GPU), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 1101 may also include an artificial intelligence (AI) processor, which is used to process computing operations related to machine learning.

[0254] The memory 1102 may include one or more computer-readable storage media, which may be non-transitory. The memory 1102 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In some embodiments, the non-transitory computer-readable storage medium in the memory 1102 is used to store at least one instruction, which is used to be executed by the processor 1101 to implement the configuration initialization and update method provided in the method embodiment of the present application.

[0255] Illustratively, the computer device 1100 also includes other components, which can be understood by those skilled in the art. Fig.11 The structure shown in the figure does not constitute a limitation on the computer device 1100, and the computer device 1100 may include more or less components than shown in the figure, or combine some components, or adopt a different component arrangement.

[0256] A person skilled in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing the relevant hardware through a program, and the program can be stored in a computer-readable storage medium, which can be a computer-readable storage medium contained in the memory in the above embodiments; or a computer-readable storage medium that exists independently and is not assembled into a computer device. The computer-readable storage medium stores at least one instruction, at least one program, code set or instruction set, and the at least one instruction, the at least one program, the code set or instruction set is loaded and executed by the processor to implement the configuration initialization and update method described in any of the above embodiments.

[0257] Optionally, the computer readable storage medium may include: a read-only memory (ROM), a random access memory (RAM), a solid state drive (SSD), or an optical disk. Among them, the random access memory may include a resistance random access memory (ReRAM) and a dynamic random access memory (DRAM). The serial numbers of the above embodiments of the present application are only for description and do not represent the advantages and disadvantages of the embodiments.

[0258] Those skilled in the art can understand that all or part of the steps of implementing the above embodiments can be completed by hardware, or by a program to instruct the relevant hardware to complete, and the program can be stored in a computer-readable storage medium, and the above-mentioned storage medium can be a read-only memory, a disk or an optical disk, etc. The above description is only an optional embodiment of the present application and is not intended to limit the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims

1. A configuration initialization and update method, characterized in that: The method comprises: In response to the first program being started, injecting a configuration value of a configuration item corresponding to the first configuration file into a variable of the first program, and the first program runs based on the configuration parameters provided by the variable of the first program; Registering a change callback function of the configuration item corresponding to the first configuration file, wherein the change callback function is used to update the configuration value injected into the variable of the first program when being triggered; When the first configuration file is changed, the second configuration file is parsed to obtain configuration items corresponding to the second configuration file, where the second configuration file refers to the file after the first configuration file is changed; Matching a configuration item corresponding to the first configuration file with a configuration item corresponding to the second configuration file, and in response to a first configuration value of a first configuration item corresponding to the first configuration file being different from a second configuration value of the first configuration item corresponding to the second configuration file, triggering a change callback function of the first configuration item, wherein the first configuration value is a configuration value injected into a first variable of the first program; The first configuration value injected into the first variable is updated based on the second configuration value.

2. The method according to claim 1, characterized in that The step of injecting the configuration value of the configuration item corresponding to the first configuration file into the variable of the first program includes: Parsing the first configuration file to obtain a first tree structure corresponding to the first configuration file, wherein nodes of the first tree structure are used to indicate configuration items corresponding to the first configuration file, and configuration values ​​of the configuration items corresponding to the first configuration file are stored in the nodes of the first tree structure; The configuration values ​​stored in the nodes of the first tree structure are injected into the variables of the first program.

3. The method according to claim 2, characterized in that The parsing of the first configuration file to obtain a first tree structure corresponding to the first configuration file includes: Parse the first configuration file to obtain node structures corresponding to n configuration items, where n is a positive integer; The n node structures are combined based on the hierarchical relationship between the n configuration items to obtain the first tree structure with the node structures as nodes.

4. The method according to claim 2, characterized in that: The step of injecting the configuration value stored in the node of the first tree structure into the variable of the first program includes: Parsing the variable type of the second variable of the first program to obtain a type label corresponding to the second variable; Based on the type label corresponding to the second variable, query the first tree structure to obtain a first node matching the type label corresponding to the second variable; Injecting the configuration value stored in the first node into the second variable; The second variable and the first variable are the same or different variables.

5. The method according to claim 4, characterized in that The querying the first tree structure based on the type label corresponding to the second variable to obtain a first node matching the type label corresponding to the second variable further includes: When the query priority of the second variable is greater than the preset priority, obtaining node position information corresponding to the second variable, where the node position information is used to record the node position matching the type label corresponding to the second variable; Based on the node position information, a first node matching the type label corresponding to the second variable is obtained from the first tree structure.

6. The method according to claim 2, characterized in that A callback function list of configuration items corresponding to the first configuration file is stored in the nodes of the first tree structure; The registering of the change callback function of the configuration item corresponding to the first configuration file includes: The change callback function of the configuration item corresponding to the first configuration file is registered in the callback function list.

7. The method according to any one of claims 2 to 6, characterized in that: The matching of the configuration item corresponding to the first configuration file and the configuration item corresponding to the second configuration file, and in response to a first configuration value of the first configuration item corresponding to the first configuration file being different from a second configuration value of the first configuration item corresponding to the second configuration file, triggering a change callback function of the first configuration item, includes: Traversing the configuration items corresponding to the second configuration file; In response to the presence of the first configuration item in the configuration items indicated by the first node of the first tree structure, comparing a first configuration value of the first configuration item indicated by the first node with a second configuration value of the first configuration item corresponding to the second configuration file; In response to the first configuration value being different from the second configuration value, updating the first configuration value stored in the first node based on the second configuration value; Trigger the change callback function of the first configuration item.

8. The method according to claim 7, characterized in that The method further comprises: In response to the first configuration item not existing in the configuration items indicated by the nodes of the first tree structure, updating the nodes of the first tree structure based on the first configuration item to obtain a second tree structure; The configuration values ​​stored in the nodes of the second tree structure are injected into the variables of the first program.

9. The method according to any one of claims 1 to 6, characterized in that: The updating of the first configuration value injected into the first variable based on the second configuration value includes: The first configuration value injected into the first variable is updated to the second configuration value.

10. The method according to any one of claims 1 to 6, characterized in that: The first program is connected to a first database, and the change callback function of the first configuration item is further used to update the database connected to the first program after the first configuration item is changed; After the first configuration value injected into the first variable is updated based on the second configuration value, the method further includes: disconnecting the first program from the first database; A connection between the first program and a second database is created in the first variable.

11. A configuration initialization and update device, characterized in that: The device comprises: A startup module, configured to, in response to a first program being started, inject a configuration value of a configuration item corresponding to a first configuration file into a variable of the first program, so that the first program runs based on the configuration parameters provided by the variable of the first program; A registration module, used to register a change callback function of a configuration item corresponding to the first configuration file, wherein the change callback function is used to update the configuration value injected into the variable of the first program when triggered; a parsing module, configured to parse a second configuration file to obtain configuration items corresponding to the second configuration file when the first configuration file is changed, wherein the second configuration file refers to a file after the first configuration file is changed; a matching module, configured to match a configuration item corresponding to the first configuration file with a configuration item corresponding to the second configuration file, and trigger a change callback function of the first configuration item in response to a first configuration value of the first configuration item corresponding to the first configuration file being different from a second configuration value of the first configuration item corresponding to the second configuration file, wherein the first configuration value is a configuration value injected into a first variable of the first program; An updating module is used to update the first configuration value injected into the first variable based on the second configuration value.

12. A computer device, characterized in that: The computer device includes a processor and a memory, wherein the memory stores at least one instruction, and the at least one instruction is loaded and executed by the processor to implement the configuration initialization and update method according to any one of claims 1 to 10.

13. A computer-readable storage medium, characterized in that: The storage medium stores at least one instruction, and the at least one instruction is loaded and executed by the processor to implement the configuration initialization and update method according to any one of claims 1 to 10.

14. A computer program product, characterized in that It comprises computer instructions, which, when executed by a processor, implement the configuration initialization and update method as claimed in any one of claims 1 to 10.