A service stateful method, device, and storage medium
The method improves configuration efficiency in distributed systems by parsing and dynamically configuring service states across nodes, reducing database access loads and eliminating manual code changes.
Patent Information
- Application Number
- CN202411715381.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-07-15
- Estimated Expiration
- 2044-11-27
AI Technical Summary
In a distributed system, when the same service is deployed on different servers, the state of the service cannot be achieved, resulting in large database access and manual configuration modification, which is inefficient.
By obtaining the service configuration, parsing and filtering the service status configuration set, variable configuration is configured using dynamic variable configuration strategies, including unique values, global public parameters, local parameters or random value configurations, and stored in the target service memory to achieve service status.
It improves the efficiency of configuring services, reduces the need for manual modification, realizes differentiated configuration of corresponding databases for service connections in different computer rooms, and reduces the need for additional bandwidth.
Smart Images

Figure CN119668671B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of service configuration, and in particular, to a service stateful method, apparatus, and storage medium. Background Art
[0002] In the same distributed system, the same service is deployed on different servers, for example, in different computer rooms. Since the same service configuration is used, these servers will all connect to the same database, resulting in a large amount of database access. It is necessary to adjust the configuration so that each server connects to the database corresponding to its own computer room to share the access volume. Therefore, the service needs to have its own state, that is, when the same service is deployed to multiple different nodes, the service on each node can have different states to generate differences. The traditional method manually modifies the service configuration code of each server, resulting in low efficiency.
[0003] In summary, the technical problems existing in the related art need to be improved. Summary of the Invention
[0004] Embodiments of the present invention provide a service stateful method, apparatus, and storage medium, which effectively improve the efficiency of configuring services.
[0005] On the one hand, embodiments of the present invention provide a service stateful method, including the following steps:
[0006] Obtain a service configuration;
[0007] Parse the service configuration to obtain a service configuration set;
[0008] Filter the service configuration set to obtain a service state configuration set;
[0009] Use a dynamic variable configuration strategy to perform variable configuration on the service state configuration set to obtain a configuration value, where the dynamic variable configuration strategy includes a unique value configuration strategy, a global common parameter configuration strategy, a local parameter configuration strategy, or a random value configuration strategy;
[0010] Store the configuration value in the memory of the target service to implement service statefulness.
[0011] In some embodiments, the parsing the service configuration to obtain a service configuration set includes:
[0012] Extract the service attribute name from the service configuration as a key;
[0013] Extract the first attribute value corresponding to the key from the service configuration;
[0014] Combine the key and the first attribute value to obtain a first key-value pair;
[0015] Combine multiple of the first key-value pairs to obtain the service configuration set.
[0016] In some embodiments, filtering the service configuration set to obtain a service status configuration set includes:
[0017] Select a key-value pair from the service configuration set as the second key-value pair;
[0018] Extract a second attribute value from the second key-value pair;
[0019] If the second attribute value starts with a preset start symbol and ends with a preset end symbol, then use the second key-value pair as the service status configuration;
[0020] Combine multiple of the service status configurations to obtain the service status configuration set.
[0021] In some embodiments, when the dynamic variable configuration policy is a unique value configuration policy, using the dynamic variable configuration policy to perform variable configuration on the service status configuration set to obtain a configuration value includes:
[0022] Cut each attribute value in the service status configuration set to obtain a corresponding first cut value set, where the first cut value set includes an automatically generated keyword, a unique keyword, a first configuration item name, a first configuration item type, and a first configuration item value;
[0023] Send the first configuration item name to a preset configuration center to obtain a configuration item value query result;
[0024] If the configuration item value query result is that the configuration item value does not exist, then set the first configuration item value to 1;
[0025] If the configuration item value query result is that the configuration item value exists, then add 1 to the queried query configuration item value as the first configuration item value;
[0026] Combine the automatically generated keyword, the unique keyword, the first configuration item name, the first configuration item type, and the updated first configuration item value to obtain the configuration value.
[0027] In some embodiments, when the dynamic variable configuration policy is a global common parameter configuration policy, using the dynamic variable configuration policy to perform variable configuration on the service status configuration set to obtain a configuration value includes:
[0028] Cut each attribute value in the service status configuration set to obtain a corresponding second cut value set, where the second cut value set includes a matching keyword, a global keyword, a second configuration item name, and a second configuration item value;
[0029] Send the second configuration item name to a preset configuration center to obtain a global configuration item value;
[0030] Update the second configuration item value with the global configuration item value;
[0031] Combine the matching keyword, the global keyword, the second configuration item name, and the updated second configuration item value to obtain the configuration value.
[0032] In some embodiments, when the dynamic variable configuration policy is a local parameter configuration policy, the step of performing variable configuration on the service status configuration set using the dynamic variable configuration policy to obtain a configuration value includes:
[0033] Cut each attribute value in the service status configuration set to obtain a corresponding third cut value set, where the third cut value set includes a matching keyword, a local keyword, a third configuration item name, and a third configuration item value;
[0034] Match the third configuration item name with a local configuration item data set to obtain a local configuration item value;
[0035] Update the third configuration item value with the local configuration item value;
[0036] Combine the matching keyword, the local keyword, the third configuration item name, and the updated third configuration item value to obtain the configuration value.
[0037] In some embodiments, when the dynamic variable configuration policy is a random value configuration policy, the step of performing variable configuration on the service status configuration set using the dynamic variable configuration policy to obtain a configuration value includes:
[0038] Cut each attribute value in the service status configuration set to obtain a corresponding fourth cut value set, where the fourth cut value set includes an auto-generated keyword, a random keyword, a fourth configuration item name, a fourth configuration item type, and a fourth configuration item value;
[0039] Generate a random value using a random function according to a preset numerical range;
[0040] Update the fourth configuration item value with the random value;
[0041] Combine the automatically generated keyword, the random keyword, the fourth configuration item name, the fourth configuration item type, and the updated fourth configuration item value to obtain the configuration value.
[0042] On the other hand, an embodiment of the present invention provides a service state device, including:
[0043] A first module, configured to obtain a service configuration;
[0044] A second module, configured to parse the service configuration to obtain a service configuration set;
[0045] A third module, configured to filter the service configuration set to obtain a service status configuration set;
[0046] A fourth module, configured to perform variable configuration on the service status configuration set by using a dynamic variable configuration policy to obtain a configuration value, where the dynamic variable configuration policy includes a unique value configuration policy, a global common parameter configuration policy, a local parameter configuration policy, or a random value configuration policy;
[0047] A fifth module, configured to store the configuration value in the memory of the target service to implement service state.
[0048] On the other hand, an embodiment of the present invention provides a computer device, including:
[0049] At least one processor;
[0050] At least one memory, configured to store at least one program;
[0051] When the at least one program is executed by the at least one processor, the at least one processor implements the method.
[0052] On the other hand, an embodiment of the present invention provides a computer-readable storage medium, where the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method is implemented.
[0053] The beneficial effects of the present invention are as follows:
[0054] In the embodiment of the present invention, first, a service configuration is obtained, the service configuration is parsed to obtain a service configuration set, then the service configuration set is filtered to obtain a service status configuration set, then a variable configuration is performed on the service status configuration set by using a dynamic variable configuration policy to obtain a configuration value, and finally the configuration value is stored in the memory of the target service, so that service state can be realized through multiple configuration policies, and further the efficiency of configuring services is improved.
[0055] Other features and advantages of the present invention will be set forth in the following description, and in part will be obvious from the description, or may be learned by practice of the present invention. The objectives and other advantages of the present invention may be realized and attained by the structure particularly pointed out in the specification and the drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for the description of the embodiments. Obviously, the drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other drawings may be obtained based on these drawings.
[0057] Figure 1 It is a flowchart of a service state method according to an embodiment of the present invention;
[0058] Figure 2 It is a schematic diagram of the overall process of configuring the service state according to an embodiment of the present invention;
[0059] Figure 3 It is a schematic structural diagram of a service state device according to an embodiment of the present invention;
[0060] Figure 4 It is a schematic hardware structure diagram of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0061] In order to make the objectives, technical solutions and advantages of the present application more clear and understandable, the present application will be further described in detail below with reference to the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the embodiments of the present application. They are only examples of devices and methods consistent with some aspects of the embodiments of the present application detailed in the appended claims.
[0062] It can be understood that the terms "first", "second", etc. used in the present application may be used herein to describe various concepts, but unless otherwise specified, these concepts are not limited by these terms. These terms are only used to distinguish one concept from another. For example, without departing from the scope of the embodiments of the present application, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the words "if", "when" used herein may be interpreted as "when...", "when...", or "in response to determining".
[0063] The terms "at least one", "multiple", "each", "any one", etc. used in this application, "at least one" includes one, two or more than two, "multiple" includes two or more than two, "each" refers to each one in the corresponding multiple, and "any one" refers to any one in the multiple.
[0064] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by those skilled in the technical field to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.
[0065] Before elaborating on the embodiments of this application in detail, some nouns and terms involved in the embodiments of this application are first explained, and the nouns and terms involved in the embodiments of this application are applicable to the following explanations.
[0066] Nacos (Naming and Configuration Service): An open-source distributed service discovery and configuration management system open-sourced by Alibaba Group. Its main function is to help developers more conveniently perform service registration, discovery, configuration, and dynamic management in a microservices architecture.
[0067] Snowflake algorithm: A distributed unique ID generation algorithm that generates a globally unique ID by combining a timestamp, a machine ID, and a sequence number. It is commonly used in distributed systems to meet the requirement of generating unique identifiers under high concurrency. The generation of Snowflake IDs is determined by a series of configuration items, and the selection of each configuration item will affect the ID generation method and distribution characteristics.
[0068] Configuration center: A tool or system for centrally managing configuration information in a distributed system or microservices architecture. It can help simplify the management and maintenance of configurations, improve the availability and maintainability of the system. The core functions of the configuration center usually include centralized storage of configuration information, dynamic update, permission control, and high availability.
[0069] Service statefulization: Refers to that in a microservices or distributed system architecture, the service saves and manages its own state (or data), rather than relying on an external state storage. Service statefulization is opposite to statelessness. A stateless service does not retain any state information between requests, while a stateful service maintains a certain degree of data persistence between service instances.
[0070] Service state configuration item: In the configuration, as long as the value of the configuration item starts with "${" and ends with "}", this configuration item is called a service state configuration item.
[0071] In the related art, the application of this embodiment is a method for processing the respective configuration state among service nodes in a distributed system. It can be used in various different fields and scenarios to ensure that the configurations of each service have both commonalities and differences. In the same distributed system, different node services can use the same configuration while each node has some private configurations. Currently, when the same service is deployed on different servers, such as in different computer rooms, if we want the nodes in different computer rooms to connect to the databases in their corresponding computer rooms to reduce bandwidth, etc., but using the same configuration center, that is, using the same configuration, this effect cannot be achieved, and the service needs to be stateful to solve this problem. Currently, existing configuration centers include: ZooKeeper configuration center, Consul configuration center, Spring CloudConfig configuration center, etcd configuration center, Nacos configuration center, Apollo configuration center. Currently, relatively popular distributed configuration centers include Apollo, Nacos, Consul, etc. So far, none of these existing configuration centers have realized the stateful configuration of service nodes.
[0072] In view of this, this embodiment performs service statefulization through multiple configuration strategies to achieve deploying the same service on different servers (computer rooms) using the same configuration center, and different computer rooms use different configurations, such as services in different computer rooms connecting to the databases in their corresponding computer rooms, or other middleware services; and when using the snowflake ID algorithm, each service uses the same set of configurations, but their machine IDs (configuration items of the snowflake ID algorithm used to generate unique IDs) can also be different. This embodiment solves the problem that some configurations of distributed services cannot be privatized after using the configuration center (a service has its own state, that is, when the same service is deployed on multiple nodes, the service on each node can have different configurations). That is, after a service references the configuration center, if we want to use some private configurations among these service nodes (such as the workId in the snowflake ID, when each service uses the same workId, it may cause the generation of the same snowflake ID), there is no need for manual processing in the service code, nor is it necessary to restart the service.
[0073] A service state method provided by an embodiment of the present application relates to the technical field of service configuration. The service state method provided by the embodiment of the present application can be applied to a terminal, can also be applied to a server, or can be software running on a terminal or a server. In some embodiments, the terminal can be a smart phone, a tablet computer, a notebook computer, a desktop computer, a smart speaker, a smart watch, a vehicle-mounted terminal, etc., but is not limited thereto; the server side can be configured as an independent physical server, can also be configured as a server cluster or a distributed system composed of multiple physical servers, or can be configured as a cloud server providing 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, and big data and artificial intelligence platforms. The server can also be a node server in a blockchain network; the software can be an application that implements a service state method, etc., but is not limited to the above forms.
[0074] The present application can be used in many general or special computer system environments or configurations. For example: personal computers, server computers, handheld or portable devices, tablet-type devices, multi-processor systems, microprocessor-based systems, set-top boxes, programmable consumer electronic devices, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, and so on. The present application can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present application can also be practiced in a distributed computing environment where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices.
[0075] The following specifically explains the embodiments of the present application with reference to the accompanying drawings:
[0076] Figure 1 is an optional flowchart of a service state method provided by an embodiment of the present application, Figure 1 The method in can include but is not limited to steps S101 to S105.
[0077] Step S101, obtain service configuration;
[0078] Step S102, parse the service configuration to obtain a service configuration set;
[0079] Step S103, filter the service configuration set to obtain a service state configuration set;
[0080] Step S104: Configure variables for the service status configuration set using a dynamic variable configuration policy to obtain configuration values. The dynamic variable configuration policy includes a unique value configuration policy, a global common parameter configuration policy, a local parameter configuration policy, or a random value configuration policy;
[0081] Step S105: Store the configuration values in the memory of the target service to implement service statelessness.
[0082] Steps S101 to S105 shown in the embodiments of the present application achieve service statelessness, thereby improving the efficiency of configuring services.
[0083] In step S101 of some embodiments, service configurations can be obtained from a preset configuration center. Service configurations can also be obtained through other means, not limited to this. Among them, the preset configuration center can include a nacos configuration center.
[0084] In some embodiments, in step S102, parsing the service configurations to obtain a service configuration set may include, but is not limited to, the following steps:
[0085] Extract the service attribute name from the service configuration as the key;
[0086] Extract the first attribute value corresponding to the key from the service configuration;
[0087] Combine the key and the first attribute value to obtain a first key-value pair;
[0088] Combine multiple first key-value pairs to obtain a service configuration set.
[0089] In some embodiments, the service configurations are parsed into the form of key-value key-value pairs. First, the service attribute name can be extracted from the service configuration as the key, then the first attribute value corresponding to the key can be extracted from the service configuration, and then the key and the first attribute value are combined to obtain a first key-value pair. Finally, multiple first key-value pairs are combined to obtain a service configuration set. Exemplarily, in the configuration of the properties (performance) type, http.connectionRequestTimeout can be used as the key, and 2000 can be used as the first attribute value, and after combination, a first key-value pair is obtained. Further, http.comnectTimeout can be used as the key, 3000 can be used as the first attribute value, http.socketTimeout can be used as the key, and 20000 can be used as the first attribute value, and so on. Multiple first key-value pairs are combined to obtain a service configuration set, and the key-value key-value pairs parsed are present in the service configuration set.
[0090] In some embodiments, in step S103, filtering the service configuration set to obtain the service status configuration set may include, but is not limited to, the following steps:
[0091] Select a key-value pair from the service configuration set as the second key-value pair;
[0092] Extract a second attribute value from the second key-value pair;
[0093] If the second attribute value starts with a preset start symbol and ends with a preset end symbol, then use the second key-value pair as the service status configuration;
[0094] Combine multiple service status configurations to obtain the service status configuration set.
[0095] In some embodiments, a key-value pair can be first selected from the service configuration set as the second key-value pair, then the second attribute value is extracted from the second key-value pair. If the second attribute value starts with a preset start symbol and ends with a preset end symbol, then the second key-value pair is used as the service status configuration. Finally, multiple service status configurations are combined to obtain the service status configuration set. Exemplarily, the preset start symbol can be set to "${", and the preset end symbol can be set to "}". Traverse all the key-value pairs in the service configuration set, extract the corresponding second attribute value. If the second attribute value starts with "${" and ends with "}", then the second key-value pair is used as the service status configuration and stored in the service status configuration set, thereby realizing the filtering of the service configuration set. For example, if the second key-value pair is: dsap.userName=${fixed::localhost::dsap.userName::userName], it meets the preset requirements (starts with "${" and ends with "}") and can be used as the service status configuration; if the second key-value pair is: dsap.password=ENC(U2XW7CSt5XflViD / K7MhDicsoDVoX9Xs), it does not meet the preset requirements and this key-value pair is discarded.
[0096] In some embodiments, in step S104, when the dynamic variable configuration policy is the unique value configuration policy, using the dynamic variable configuration policy to perform variable configuration on the service status configuration set to obtain the configuration value may include, but is not limited to, the following steps:
[0097] Cut each attribute value in the service status configuration set to obtain the corresponding first cut value set, and the first cut value set includes an automatically generated keyword, a unique keyword, a first configuration item name, a first configuration item type, and a first configuration item value;
[0098] Send the first configuration item name to the preset configuration center to obtain the query result of the configuration item value;
[0099] If the query result of the configuration item value indicates that the configuration item value does not exist, set the first configuration item value to 1;
[0100] If the query result of the configuration item value indicates that the configuration item value exists, add 1 to the queried configuration item value as the first configuration item value;
[0101] Combine the automatically generated keyword, unique keyword, first configuration item name, first configuration item type, and the updated first configuration item value to obtain the configuration value.
[0102] In some embodiments, when the dynamic variable configuration policy is the unique value configuration policy, each attribute value in the service status configuration set can be first segmented to obtain a corresponding first segmentation value set, where the first segmentation value set includes an automatically generated keyword, a unique keyword, a first configuration item name, a first configuration item type, and a first configuration item value. Exemplarily, one of the attribute values in the service status configuration set is: ${dynamic::single::snowflake.config.workerId::integer::1-32}, and its format is: ${dynamic::single::first configuration item name::integer::min-max}. After removing '${' and '}', it is segmented by '::' to obtain five values, namely dynamic, single, snowflake.config.workerId, integer, and 1-32. Among them, dynamic represents the automatically generated keyword, single represents the unique keyword, snowflake.config.workerId represents the first configuration item name, integer (integer number) represents the first configuration item type, and 1-32 (min-max) represents the first configuration item value, which is the range of the generated value. Then, the first configuration item name is sent to the preset configuration center to obtain a configuration item value query result. If the configuration item value query result indicates that the configuration item value does not exist, the first configuration item value is set to 1; if the configuration item value query result indicates that the configuration item value exists, the queried configuration item value obtained by the query is incremented by 1 as the first configuration item value. Exemplarily, after obtaining the configuration item, it can be queried in the nacos configuration center whether the first configuration item name snowflake.config.workerId has a registered value. If not, the first configuration item value is set to 1. If it has been registered, the maximum value of the queried configuration item value obtained by the query is obtained and then incremented by 1 as the first configuration item value. Finally, the automatically generated keyword, the unique keyword, the first configuration item name, the first configuration item type, and the updated first configuration item value are combined to obtain a configuration value. At the same time, the configuration value is synchronized to the nacos configuration center for registration to be synchronized to other nodes. It can be understood that if a unique number needs to be generated, for example, an integer value that does not conflict within [min, max], and the values of a certain configuration item of all nodes in the cluster are not repeated, the value of the corresponding configuration item should have the keyword 'dynamic::single', and the unique value configuration policy can be used for configuration.
[0103] In some embodiments, in step S104, when the dynamic variable configuration policy is the global common parameter configuration policy, using the dynamic variable configuration policy to perform variable configuration on the service status configuration set to obtain a configuration value may include, but is not limited to, the following steps:
[0104] Cut each attribute value in the service status configuration set to obtain a corresponding second cut value set, where the second cut value set includes a matching keyword, a global keyword, a second configuration item name, and a second configuration item value;
[0105] Send the second configuration item name to a preset configuration center to obtain a global configuration item value;
[0106] Use the global configuration item value to update the second configuration item value;
[0107] Combine the matching keyword, the global keyword, the second configuration item name, and the updated second configuration item value to obtain a configuration value.
[0108] In some embodiments, when the dynamic variable configuration policy is a global common parameter configuration policy, each attribute value in the service status configuration set can be cut first to obtain a corresponding second cut value set, where the second cut value set includes a matching keyword, a global keyword, a second configuration item name, and a second configuration item value. Exemplarily, one of the attribute values in the service status configuration set is: ${fixed::global::server.workerId::workerId_config}, and its format is: ${fixed::global::second configuration item name::second configuration item value}. After removing the '${' and '}' and then cutting by '::', four values are obtained, namely fixed, global, server.workerId, and workerId_config. Among them, fixed represents the matching keyword, global represents the global keyword, server.workerId represents the second configuration item name, and workerId_config represents the second configuration item value (the name of the common configuration item dataId). Then send the second configuration item name to the preset configuration center to obtain the global configuration item value (the global configuration directory on nacos), and then use the global configuration item value to update the second configuration item value. Exemplarily, the global configuration item value can be obtained by matching in the nacos configuration center. Finally, combine the matching keyword, the global keyword, the second configuration item name, and the updated second configuration item value to obtain a configuration value. It can be understood that if the value of a certain configuration item needs to be replaced with a certain common configuration on the nacos global configuration, such as the database configuration of a certain computer room, when the common configuration is modified, all nodes can be synchronously modified, and the value of the corresponding configuration item should have the 'fixed::global' keyword, and the global common parameter configuration policy can be used for configuration.
[0109] In some embodiments, in step S104, when the dynamic variable configuration policy is the local parameter configuration policy, using the dynamic variable configuration policy to perform variable configuration on the service status configuration set to obtain a configuration value may include, but is not limited to, the following steps:
[0110] Cut each attribute value in the service status configuration set to obtain a corresponding third cut value set, where the third cut value set includes a matching keyword, a local keyword, a third configuration item name, and a third configuration item value;
[0111] Match the third configuration item name with the local configuration item data set to obtain a local configuration item value;
[0112] Update the third configuration item value with the local configuration item value;
[0113] Combine the matching keyword, the local keyword, the third configuration item name, and the updated third configuration item value to obtain a configuration value.
[0114] In some embodiments, when the dynamic variable configuration policy is the local parameter configuration policy, each attribute value in the service status configuration set may be cut first to obtain a corresponding third cut value set, where the third cut value set includes a matching keyword, a local keyword, a third configuration item name, and a third configuration item value. Exemplarily, one of the attribute values in the service status configuration set is: ${fixed::localhost::server.workerId::server.machine}, and its format is: ${fixed::localhost::third configuration item name::third configuration item value}. After removing '${' and '}', it is cut by '::' to obtain four values, namely fixed, localhost, server.workerId, server.machine, where fixed represents the matching keyword, localhost represents the local keyword, server.workerId represents the third configuration item name, and server.machine represents the third configuration item value (the full name of the local configuration item). Then, the third configuration item name is matched with the local configuration item data set to obtain a local configuration item value, and then the third configuration item value is updated with the local configuration item value. Finally, the matching keyword, the local keyword, the third configuration item name, and the updated third configuration item value are combined to obtain a configuration value. It can be understood that if it is necessary to make the value of a certain configuration item the same as another configuration item in the current service, for example, the same configuration is used in two places, but this configuration is declared separately. To avoid modifying multiple places when modifying, the value of the corresponding configuration item should have the 'fixed::localhost' keyword, and the local parameter configuration policy can be used for configuration.
[0115] In some embodiments, in step S104, when the dynamic variable configuration policy is a random value configuration policy, using the dynamic variable configuration policy to perform variable configuration on the service status configuration set to obtain a configuration value may include, but is not limited to, the following steps:
[0116] Cut each attribute value in the service status configuration set to obtain a corresponding fourth cut value set, where the fourth cut value set includes an automatically generated keyword, a random keyword, a fourth configuration item name, a fourth configuration item type, and a fourth configuration item value;
[0117] Generate a random value using a random function according to a preset numerical range;
[0118] Update the fourth configuration item value using the random value;
[0119] Combine the automatically generated keyword, the random keyword, the fourth configuration item name, the fourth configuration item type, and the updated fourth configuration item value to obtain a configuration value.
[0120] In some embodiments, when the dynamic variable configuration policy is a random value configuration policy, each attribute value in the service status configuration set can be sliced first to obtain a corresponding fourth sliced value set, where the fourth sliced value set includes an automatically generated keyword, a random keyword, a fourth configuration item name, a fourth configuration item type, and a fourth configuration item value. Exemplarily, one of the attribute values in the service status configuration set is: ${dynamic::random::snowflake.config.dataCenterId::integer::1-32}, and its format is: ${dynamic::random::fourth configuration item name::integer::min-max}. After removing the '${' and '}' and then slicing by '::', five values are obtained, namely dynamic, random, snowflake.config.dataCenterId, integer, and 1-32. Among them, dynamic represents the automatically generated keyword, random represents the random keyword, snowflake.config.dataCenterId represents the fourth configuration item name, integer (integer number) represents the fourth configuration item type, and 1-32 (min-max) represents the fourth configuration item value, which is the range of the generated value. Then, according to the preset numerical range, a random value is generated using a random function, and the fourth configuration item value is updated using the random value. Exemplarily, a random integer value in the range of 1 to 32 can be automatically generated using a random function to update the fourth configuration item value. Finally, the automatically generated keyword, the random keyword, the fourth configuration item name, the fourth configuration item type, and the updated fourth configuration item value are combined to obtain a configuration value. At the same time, the configuration value is synchronized to the nacos configuration center for registration to be synchronized to other nodes. It can be understood that if a random number needs to be generated within [min, max], for example, to prevent the same configuration value for all nodes in a cluster service, but some nodes' configuration values can be the same, then the value of the corresponding configuration item should have the 'dynamic::random' keyword, and the random value configuration policy can be used for configuration.
[0121] In some embodiments, in step S105, the configuration value can be stored in the memory of the target service to achieve service status. Exemplarily, after the corresponding configuration value is obtained through the configuration parsing engine (used to execute the dynamic variable configuration strategy), the configuration value can be saved in the memory of the corresponding target service, thereby achieving service status, so that the actual calculated configuration value is different from the configuration directly obtained from the nacos configuration center, and then the servers in different computer rooms can be connected to different databases to reduce the problem of additional bandwidth generated by the transmission of different computer rooms. In addition, the configuration value can also be stored in the nacos configuration center so that other nodes can obtain it to avoid conflicts in the generation of unique values.
[0122] In some embodiments, the overall process of configuring the service status is as follows: Figure 2 As shown, you can first get all service configurations from the nacos configuration center. The configuration center responds to the request and returns configuration data of string type. Then, it parses the value of each configuration item, traverses each configuration item for filtering, and obtains the service status configuration set. Then, it uses the dynamic variable configuration strategy to classify the service status configuration set to obtain the configuration value. Finally, the configuration value is stored in the content of the nacos configuration center and the target service. The configuration center will store the configuration value in a new configuration list to record the updated configuration value, thereby realizing service status and improving the efficiency of configuration services.
[0123] In some embodiments, the existing distributed configuration centers include Apollo, Nacos, Consul, etc., but there is currently no service state technology. If a cluster service is connected to the nacos configuration center, each service in the cluster needs to have some private configuration items, and the configuration file of the local service needs to be read by handwritten code to realize the service state, but the handwritten code needs to be manually maintained by the local configuration file. When there are too many service nodes, a lot of manpower costs are required to modify or add these private configurations, and if multiple services are not deployed on the same server, it takes more time to operate these configuration files on the corresponding servers, which is time-consuming, and after the modification, the service may need to be restarted to take effect, which is both time-consuming and reduces the high availability of the service. The present embodiment is based on the service status implemented by the configuration center, and the service node can be configured to have its own independent status. Some private configurations can be directly configured in the nacos configuration center and operated directly on the client page without the need to modify the corresponding server, saving the time of operation and maintenance personnel and the need to read local configuration files by hand-written code, saving coding time. In addition, the nacos configuration center supports hot configuration, so the modification does not need to be restarted for the service to take effect, thereby increasing the high availability of the service.
[0124] The beneficial effects of implementing the embodiments of the present invention include: First, the service configuration is obtained, the service configuration is parsed to obtain a service configuration set, then the service configuration set is filtered to obtain a service status configuration set, and then the dynamic variable configuration strategy is used to perform variable configuration on the service status configuration set to obtain a configuration value. Finally, the configuration value is stored in the memory of the target service, so that the service can be made stateful through various configuration strategies, thereby improving the efficiency of configuring the service. At the same time, this embodiment enables the use of a unified configuration center even if each service node in the distributed system has its own state, and reduces the additional human and material resources for maintaining the private configuration items of each service node.
[0125] As Figure 3 shown, the embodiments of the present invention also provide a service stateful device, including:
[0126] A first module 801 for obtaining a service configuration;
[0127] A second module 802 for parsing the service configuration to obtain a service configuration set;
[0128] A third module 803 for filtering the service configuration set to obtain a service status configuration set;
[0129] A fourth module 804 for performing variable configuration on the service status configuration set by using a dynamic variable configuration strategy to obtain a configuration value, and the dynamic variable configuration strategy includes a unique value configuration strategy, a global common parameter configuration strategy, a local parameter configuration strategy, or a random value configuration strategy;
[0130] A fifth module 805 for storing the configuration value in the memory of the target service to achieve service statefulness.
[0131] The content in the above method embodiments is applicable to the device embodiments of the present invention. The functions specifically implemented by the device embodiments of the present invention are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those of the above method embodiments.
[0132] As Figure 4 shown, the embodiments of the present invention also provide a computer device, including:
[0133] At least one processor 901;
[0134] At least one memory 902 for storing at least one program;
[0135] When at least one program is executed by at least one processor, at least one processor implements Figure 1 the method shown.
[0136] The content in the method embodiments described above is applicable to the device embodiments of the present invention. The functions specifically implemented in the device embodiments are the same as those in the method embodiments described above, and the beneficial effects achieved are also the same as those in the method embodiments described above.
[0137] The embodiments of the present invention further provide a computer-readable storage medium storing a computer program, and when the computer program is executed by a processor, it implements Figure 1 the method shown.
[0138] The content in the method embodiments described above is applicable to the storage medium embodiments of the present invention. The functions specifically implemented in the storage medium embodiments are the same as those in the method embodiments described above, and the beneficial effects achieved are also the same as those in the method embodiments described above.
[0139] The preferred embodiments of the embodiments of the present application have been described above with reference to the accompanying drawings. However, this does not limit the scope of the rights of the embodiments of the present application. Any modifications, equivalent replacements, and improvements made by those skilled in the art without departing from the scope and essence of the embodiments of the present application shall fall within the scope of the rights of the embodiments of the present application.
Claims
1. A service stateful method, characterized in that, It includes the following steps: Obtain service configurations; Parse the service configurations to obtain a service configuration set; Filter the service configuration set to obtain a service status configuration set; Perform variable configuration on the service status configuration set by using a dynamic variable configuration policy to obtain configuration values, where the dynamic variable configuration policy includes a unique value configuration policy, a global common parameter configuration policy, a local parameter configuration policy, or a random value configuration policy; Store the configuration values in the memory of the target service to implement service statefulness; Among them, the parsing of the service configurations to obtain a service configuration set includes: Extract service attribute names from the service configurations as keys; Extract the first attribute values corresponding to the keys from the service configurations; Combine the keys and the first attribute values to obtain first key-value pairs; Combine multiple first key-value pairs to obtain the service configuration set; The filtering of the service configuration set to obtain a service status configuration set includes: Select a key-value pair from the service configuration set as a second key-value pair; Extract a second attribute value from the second key-value pair; If the second attribute value starts with a preset start symbol and ends with a preset end symbol, then use the second key-value pair as a service status configuration; Combine multiple service status configurations to obtain the service status configuration set; Service statefulness means that in a microservice or distributed system architecture, a service saves and manages its own state without relying on an external state store; the stateful service after service statefulness is opposite to the stateless service without state, and the stateless service does not retain any state information between requests, and the stateful service maintains a part of data persistence between service instances; The service status configuration refers to a configuration item whose value starts with "${" and ends with "}".
2. The method according to claim 1, wherein When the dynamic variable configuration policy is a unique value configuration policy, the performing variable configuration on the service status configuration set by using the dynamic variable configuration policy to obtain configuration values includes: Cut each attribute value in the service status configuration set to obtain a corresponding first cut value set, where the first cut value set includes an automatically generated keyword, a unique keyword, a first configuration item name, a first configuration item type, and a first configuration item value; Send the first configuration item name to a preset configuration center to obtain a configuration item value query result; If the configuration item value query result is that the configuration item value does not exist, then set the first configuration item value to 1; If the configuration item value query result is that the configuration item value exists, then use the queried query configuration item value plus 1 as the first configuration item value; Combine the automatically generated keyword, the unique keyword, the first configuration item name, the first configuration item type, and the updated first configuration item value to obtain the configuration values.
3. The method according to claim 1, wherein When the dynamic variable configuration policy is a global common parameter configuration policy, the performing variable configuration on the service status configuration set by using the dynamic variable configuration policy to obtain configuration values includes: Cut each attribute value in the service status configuration set to obtain a corresponding second cut value set, where the second cut value set includes a matching keyword, a global keyword, a second configuration item name, and a second configuration item value; Send the second configuration item name to a preset configuration center to obtain a global configuration item value; Update the second configuration item value with the global configuration item value; Combine the matching keyword, the global keyword, the second configuration item name, and the updated second configuration item value to obtain the configuration value.
4. The method according to claim 1, wherein When the dynamic variable configuration policy is a local parameter configuration policy, the variable configuration of the service status configuration set using the dynamic variable configuration policy to obtain a configuration value includes: Cut each attribute value in the service status configuration set to obtain a corresponding third cut value set, where the third cut value set includes a matching keyword, a local keyword, a third configuration item name, and a third configuration item value; Match the third configuration item name with the local configuration item data set to obtain a local configuration item value; Update the third configuration item value with the local configuration item value; Combine the matching keyword, the local keyword, the third configuration item name, and the updated third configuration item value to obtain the configuration value.
5. The method according to claim 1, characterized in that When the dynamic variable configuration policy is a random value configuration policy, the variable configuration of the service status configuration set using the dynamic variable configuration policy to obtain a configuration value includes: Cut each attribute value in the service status configuration set to obtain a corresponding fourth cut value set, where the fourth cut value set includes an auto-generated keyword, a random keyword, a fourth configuration item name, a fourth configuration item type, and a fourth configuration item value; Generate a random value using a random function according to a preset numerical range; Update the fourth configuration item value with the random value; Combine the auto-generated keyword, the random keyword, the fourth configuration item name, the fourth configuration item type, and the updated fourth configuration item value to obtain the configuration value.
6. A service stateful device, characterized in that, Include: A first module for obtaining a service configuration; A second module for parsing the service configuration to obtain a service configuration set; A third module for filtering the service configuration set to obtain a service status configuration set; A fourth module for performing variable configuration on the service status configuration set using a dynamic variable configuration policy to obtain a configuration value, where the dynamic variable configuration policy includes a unique value configuration policy, a global common parameter configuration policy, a local parameter configuration policy, or a random value configuration policy; A fifth module for storing the configuration value in the memory of the target service to implement service stateification; Among them, the parsing of the service configuration to obtain a service configuration set includes: Extract the service attribute name from the service configuration as a key; Extract the first attribute value corresponding to the key from the service configuration; Combine the key and the first attribute value to obtain a first key-value pair; Combine multiple first key-value pairs to obtain the service configuration set; Filtering the service configuration set to obtain a service status configuration set includes: Selecting a key-value pair from the service configuration set as a second key-value pair; Extracting a second attribute value from the second key-value pair; If the second attribute value starts with a preset start symbol and ends with a preset end symbol, then use the second key-value pair as a service status configuration; Combining multiple service status configurations to obtain the service status configuration set; The service state refers to that in a microservice or distributed system architecture, the service saves and manages its own state without relying on an external state storage; the stateful service after service state is opposite to the stateless service, and the stateless service does not retain any state information between requests, and the stateful service maintains a part of data persistence between service instances; The service status configuration refers to a configuration item whose value starts with "${" and ends with "}".
7. A computer device, characterized in that, Including: At least one processor; At least one memory for storing at least one program; When the at least one program is executed by the at least one processor, the at least one processor implements the method according to any one of claims 1-5.
8. A computer-readable storage medium storing a computer program, characterized in that, The computer program implements the method according to any one of claims 1-5 when executed by a processor.
Citation Information
Patent Citations
System and method for dynamically publishing distributed application parameters
CN111562950A
Multi-cloud synchronization method, system and device based on Nacos configuration management and medium
CN118018560A