A micro-service port management method and device
By determining the port range and collecting attribute information in the microservice cluster, and using encryption algorithms to generate unique legal ports, the problem of microservice port conflicts is solved, and intelligent port management and conflict-free allocation are achieved.
Patent Information
- Application Number
- CN202111661756.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-30
- Publication Date
- 2025-10-10
- Estimated Expiration
- 2041-12-30
AI Technical Summary
The increase in the number of microservices leads to port conflicts and management difficulties, affecting joint debugging and operation and maintenance, especially when services drift, which may cause system paralysis.
By determining the port range of microservices in the cluster, collecting the used port list and storing the attribute information, and using encryption algorithms to generate unique and legal ports, conflicts can be avoided.
It implements intelligent dynamic management of microservice ports, ensures orderly and unique allocation, avoids port conflicts, and ensures successful service startup.
Smart Images

Figure CN114327952B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the computer software industry, more particularly to a port management technique for microservices. BACKGROUND
[0002] In recent years, with the increase in the number of microservices, some problems in management details have gradually emerged. These problems have little impact when the number of business services is small, but with the further rapid growth of the number of microservices in recent years, these problems have gradually emerged and brought great difficulties to the joint debugging and operation of front-end and back-end.
[0003] Firstly, the microservice publishing port is defined by the operation and development personnel, lacking a unified convention, and when multiple services are started on the same server, port conflicts between services are likely to occur, resulting in service startup failure.
[0004] Secondly, port conflicts are formed after service drift, causing services to be unusable, and in severe cases, the system will be paralyzed.
[0005] Therefore, how to manage the chaotic microservice ports, confirm whether the ports have been used, and systematically manage and allocate the microservice ports has become a problem to be solved. SUMMARY
[0006] The present application provides the following technical solutions, a microservice port management method, the method comprising:
[0007] determining the port range used by the microservice in the cluster;
[0008] collecting a used port list in the cluster and storing attribute information of the microservice using each port;
[0009] According to the port allocation request of the microservice, the attribute information uniquely identifying the microservice is spliced and encrypted, and a port is generated in combination with the port range;
[0010] determining whether the generated port is legal according to the used port list;
[0011] returning the generated legal port to the corresponding microservice.
[0012] Further, the method of generating a port by splicing and encrypting the attribute information uniquely identifying the microservice according to the port allocation request of the microservice is specifically:
[0013] parsing the port allocation request to obtain the microservice attribute information in the port allocation request;
[0014] string splicing the attribute information uniquely identifying the microservice in the microservice attribute information;
[0015] Encrypt the spliced string by using an encryption algorithm;
[0016] Take the modulus of the encrypted string on the upper limit of the port range;
[0017] According to the lower limit of the port range, the result of the modulus is adjusted and confirmed to generate a port.
[0018] Further, according to the lower limit of the port range, the result of the modulus is adjusted and confirmed to generate a port. The method is specifically:
[0019] Verify that the result of the modulus is greater than the lower limit of the port range, and confirm that the result of the modulus is the generated port;
[0020] Verify that the result of the modulus is less than the lower limit of the port range, and confirm that the sum of the result of the modulus and the lower limit of the port range is the generated port.
[0021] Preferably:
[0022] The attribute information of each port microservice at least includes the name, version number, configuration instance name, and port unique identifier of the microservice.
[0023] Preferably:
[0024] At least two attribute information of the unique microservice are spliced.
[0025] For an illegal port, the spliced string is spliced again until a legal port is obtained.
[0026] The application also discloses a microservice port management device, which comprises:
[0027] A port range determination unit is configured to determine a port range used by microservices in a cluster.
[0028] A port collection and storage unit is configured to collect a used port list in the cluster and store attribute information of microservices using each port.
[0029] A port generation unit is configured to splice attribute information of a unique microservice according to a port allocation request of the microservice, encrypt the attribute information, and generate a port in combination with a port range determined by the port range determination unit.
[0030] A port legality confirmation unit is configured to determine whether a port generated by the port generation unit is legal according to a used port list stored by the port collection and storage unit.
[0031] A port returning unit returns the legal port confirmed by the port legality confirming unit to the corresponding micro service.
[0032] Specifically, the port generating unit further comprises:
[0033] An allocation request parsing module is configured to parse a port allocation request and acquire micro service attribute information in the port allocation request.
[0034] A string concatenating module is configured to concatenate attribute information for uniquely identifying the micro service in the micro service attribute information acquired by the allocation request parsing module.
[0035] An encrypting module is configured to encrypt the concatenated string by using an encryption algorithm.
[0036] A module for taking a modulus is configured to take a modulus of the string encrypted by the encrypting module on an upper limit of the port range.
[0037] A port confirming module is configured to confirm a generated port by verifying and adjusting the result of the taking of the modulus according to a lower limit of the port range.
[0038] Specifically, the port confirming module further comprises:
[0039] A verifying module is configured to verify whether the result of the taking of the modulus is greater than the lower limit of the port range.
[0040] An adjusting module is configured to, when the verifying module verifies that the result of the taking of the modulus is less than the lower limit of the port range, adjust the result of the taking of the modulus to a sum of the result of the taking of the modulus and the lower limit of the port range.
[0041] A confirming module is configured to confirm the result of the taking of the modulus greater than the lower limit of the port range as the generated port.
[0042] Preferably,
[0043] The attribute information of each port micro service at least includes a name, a version number, a configuration instance name and a port unique identifier of the micro service.
[0044] Preferably,
[0045] The string concatenating module selects at least two attribute information for uniquely identifying the micro service to concatenate.
[0046] As can be seen from the above technical solution, the present invention discloses a microservice port management technology that determines a list of used ports, obtains occupied and free port ranges, generates a port for the microservice based on the port ranges in the cluster and the microservice's attribute information, and then determines the legality of the generated port based on the occupied port list. This technology implements intelligent dynamic port management, ensuring that port allocation will not conflict. Port generation, implemented through a port generation algorithm, also eliminates the need for developers to customize ports, ensuring the orderliness and uniqueness of port allocation. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0048] Figure 1 A schematic diagram of a process flow of a microservice port management method provided by an embodiment of the present invention;
[0049] Figure 2 A flow chart of the method provided in Example 2 of the present invention;
[0050] Figure 3 This is a schematic diagram of a method according to embodiment 3 of the present invention;
[0051] Figure 4 This is a flow chart of a method according to a fourth embodiment of the present invention;
[0052] Figure 5 This is a structural diagram of a microservice port management device provided in Example 5 of the present invention;
[0053] Figure 6 This is a detailed structural diagram of the port generation unit 3 in the device provided in Example 5 of the present invention. DETAILED DESCRIPTION
[0054] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. All other embodiments obtained by ordinary technicians in this field based on the embodiments of the present invention without creative work are within the scope of protection of the present invention.
[0055] With the increase of business, more and more microservices are developed in the back-end research and development, and more and more IPs and ports are released. Since the developers of the microservices are different, the ports are defined by the corresponding developers, which may cause multiple microservices to use the same port. Since the microservices are deployed based on yarn, the microservice instances may drift in the cluster. When two services using the same port drift to the same node, the ports will conflict, resulting in the failure of service startup. In order to solve this problem, a microservice port management method and device are provided.
[0056] Referring to Figure 1 , an embodiment of the present application is given, as Figure 1 shown, a microservice port management method in the present application is described in detail, which comprises the following steps:
[0057] Step S1: determining the port range used by the microservices in the cluster.
[0058] The determined port range can be used to allocate ports for the microservices in the port range, so as to ensure that the ports of each microservice are definitely inconsistent.
[0059] Usually, the port range used by the microservices is agreed before the system is deployed, and if there is no agreement, the port range is automatically set. The value of the port range can be set according to the system situation and the use demand. The port range can be written into the unmodifiable node of the system, such as the / UCMP / public / port-range node in the zookeeper.
[0060] Step S2: collecting the used port list in the cluster and storing the attribute information of the microservices using each port.
[0061] The used port list is collected, which is prepared for the subsequent port allocation verification.
[0062] There are multiple methods for collecting the used port list, which can be collected by using tools in the cluster.
[0063] The attribute information of the microservices is the information that can identify the microservices, including but not limited to the name, version number and configuration instance name of the microservices.
[0064] The collected attribute information of the microservices is stored through a storage structure. It should be noted that the microservices and the port numbers can be associated in the form of 1:n, that is, a process can have multiple port numbers, and each port has different functions. Each port has a unique port identifier.
[0065] The storage structure can also store more port information according to the demand, such as the creator, creation time, modifier, modification time, etc. The storage structure can be a port definition table.
[0066] Step S3: According to the port allocation request of the microservice, the attribute information that uniquely identifies the microservice is spliced and encrypted, and the port is generated in combination with the port range.
[0067] In order to make the generated port unique and avoid conflicts with other microservice ports, it is necessary to select attribute information that can uniquely identify the microservice for splicing, such as the microservice name, microservice version number, and microservice application instance name.
[0068] After concatenating these attributes that uniquely identify the microservice, an encryption algorithm is used to calculate a value, which is then combined with the port range to generate the port.
[0069] Further ensure the uniqueness, legitimacy and availability of the port.
[0070] Step S4: Determine whether the generated port is legal according to the used port list.
[0071] If the generated port is in the used port list, it means that the port is already occupied and cannot be allocated, which is illegal;
[0072] If the generated port is not in the used port list, it is marked as legal and can be allocated.
[0073] Step S5: Return the generated legal port to the corresponding microservice.
[0074] The present invention provides a tool for microservice port management and dynamic allocation. Microservice ports allocated using this tool will not conflict with each other and will not conflict with ports used by third-party programs that are not microservices. Developers do not need to define the ports published for service startup, and service startup failure will not occur due to port conflicts after service drift.
[0075] For the second embodiment of the present invention, please refer to the attached Figure 2 , step S3 is described in detail, according to the port allocation request of the microservice, the attribute information that uniquely identifies the microservice is spliced and encrypted, and the method for generating the port in combination with the port range is specifically as follows:
[0076] Step S31: Parse the port allocation request and obtain microservice attribute information in the port allocation request.
[0077] The attribute information of each port microservice includes at least the name of the microservice, version number, configuration instance name, and port unique identifier.
[0078] When a microservice starts, it initiates a port allocation request, which contains the attribute information of the microservice.
[0079] The unique identifier of the port is calculated based on the microservice attribute information using the method steps of this embodiment. When the port generation is completed, the generated port will be stored in the storage space.
[0080] Step S32: concatenate the attribute information that uniquely identifies the microservice in the microservice attribute information.
[0081] Select at least two pieces of attribute information that uniquely identify the microservice and join them.
[0082] For illegal ports, the concatenated strings are concatenated again until a legal port is obtained.
[0083] You can also select three, four or more microservice attribute information for splicing, as long as the spliced information can be used as the unique identifier of the microservice, to maximize the uniqueness of the microservice port allocation.
[0084] These attributes can be concatenated into a string using the method of attribute 1 + # + attribute 2 + # + attribute 3. The concatenated string can be represented by S.
[0085] Step S33: Encrypt the concatenated character string using an encryption algorithm.
[0086] The concatenated character strings are operated using an encryption algorithm to obtain the value of the character strings.
[0087] The encryption algorithm can be selected according to actual needs, such as CRC, SHA, etc.
[0088] Step S34: taking the modulo of the encrypted character string with the upper limit of the port range.
[0089] Modulo represents the remainder obtained by dividing the string value by the upper limit of the port.
[0090] Step S35: According to the lower limit of the port range, the modulo result is verified and adjusted to confirm the generated port.
[0091] The modulo result needs to be compared with the lower limit of the port range. If it is greater than the lower limit, it means that the result is within the port range. If it is less than the lower limit, it means that the result is not within the port range. Therefore, the two situations need to be confirmed respectively. Step S35 is specifically as follows:
[0092] Step S3501: Verify that the modulo result is greater than the lower limit of the port range, and confirm that the modulo result is the generated port.
[0093] Step S3502: Verify that the modulo result is less than the lower limit of the port range, and confirm that the sum of the modulo result and the lower limit of the port range is the generated port.
[0094] In order to more intuitively illustrate the execution steps of the method of the present invention, the following embodiment 3 is given in conjunction with an example. Figure 3 shown.
[0095] ① When the Microservice starts, it initiates a port allocation request to the PortManager module. The request contains the service name, version number, and instance name of the Microservice.
[0096] ②After receiving the request, the controller component of the Portmanager port management module parses the request, obtains the service name, version number, and instance name in the request, and sends a query instruction to the MySQL data storage structure based on these three parameters (i.e., step ③) to check whether the microservice has been assigned a port (i.e., whether there is data in the application port definition table). If it exists, it directly executes ⑥; if not, it executes ④.
[0097] ③MySQL receives the query instruction, queries the data according to the service name, version number, and instance name in the instruction, and returns the query results.
[0098] ④ Call the port-algrithm component to generate a port, using the algorithm described in Example 2.
[0099] ⑤Port-algrithm returns a valid port that does not conflict with the existing program port to the controller.
[0100] ⑥Controller returns the port to microservice.
[0101] In order to more clearly illustrate the execution method of the algorithm in Example 2, the algorithm is described below in conjunction with Example 4. Figure 4 shown.
[0102] Step S401: The caller passes in parameters appName, appVersion, and appInstance.
[0103] appName indicates the application name, appVersion indicates the application version, and appInstance indicates the application instance.
[0104] Step S402: concatenate the parameters.
[0105] s=${appName}#${appVersion}#${appInstance}; ${appName} represents the specific value of the variable. For example, if appName=test, appVersion=1, and appInstance=Instance-1, the specific value of s in the figure is test#1#Instance-1.
[0106] Step S403: Calculate the port value port according to S using the CRC32 algorithm.
[0107] Step S404: Modulo the port value and the port upper limit.
[0108] port=port%maxPort means that the result of port is the remainder obtained by dividing port by maxPort. For example, port=15, maxPort=10, after calculation, port=5; maxport indicates the upper limit of the port.
[0109] Step S405: Determine whether the remainder is greater than the port lower limit minport. If so, proceed to step S407; if not, proceed to step S406.
[0110] Step S406: The port is the sum of the port value and the port lower limit.
[0111] Step S407: Determine whether the port already exists. If so, proceed to step S408; if not, proceed to step S409.
[0112] Step S408: concatenate the port character strings again and proceed to step S402.
[0113] s=s+#+s represents string concatenation. For example, s=test becomes s=test#test after this calculation. If the port already exists, the string concatenation is repeated until an available port is calculated.
[0114] Step S409: Store into the warehouse and return to the caller.
[0115] The present invention also discloses a microservice port management device. The fifth embodiment is given below. Figure 5 , the structure of the device is described in detail. The device includes a port range determination unit 1, a port collection and storage unit 2, a port generation unit 3, a port legitimacy confirmation unit 4, and a port return unit 5, wherein:
[0116] The port range determination unit 1 is used to determine the port range used by the microservices in the cluster.
[0117] The port collection and storage unit 2 is used to collect a list of used ports in the cluster and store attribute information of the microservices using each port.
[0118] The port generation unit 3 is used to encrypt and concatenate the attribute information that uniquely identifies the microservice according to the port allocation request of the microservice, and generate a port based on the port range determined by the port range.
[0119] like Figure 6 As shown, the port generation unit 3 further includes:
[0120] The allocation request parsing module 31 is used to parse the port allocation request and obtain the microservice attribute information in the port allocation request;
[0121] A string concatenation module 32 is configured to concatenate the attribute information that uniquely identifies the microservice in the microservice attribute information obtained by the allocation request parsing module;
[0122] An encryption module 33 is used to encrypt the concatenated character strings using an encryption algorithm;
[0123] A modulo module 34, configured to take the modulo of the upper limit of the port range for the character string encrypted by the encryption module;
[0124] The port confirmation module 35 is used to verify and adjust the modulo result according to the lower limit of the port range and then confirm the generated port.
[0125] The port confirmation module 35 further includes:
[0126] A verification module 351 is configured to verify whether a modulo result obtained by the modulo module is greater than a lower limit of the port range;
[0127] an adjusting module 352 configured to adjust the modulo result to the sum of the modulo result and the lower limit of the port range when the modulo result verified by the verification module is less than the lower limit of the port range;
[0128] The confirmation module 353 is used to confirm that the modulo result greater than the lower limit of the port range is the generated port.
[0129] The port legitimacy confirmation unit 4 is configured to determine whether the port generated by the port generation unit is legal based on the used port list stored in the port collection storage unit.
[0130] The port returning unit 5 returns the legal port confirmed by the port legitimacy confirmation unit to the corresponding microservice.
[0131] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the corresponding processes in the device embodiments described above can refer to the specific working processes of the foregoing methods, and will not be described again.
[0132] The various embodiments are described in the specification by way of progression, each building on the last to facilitate ease of understanding. The embodiments are illustrative of the application and should not be construed as limiting the application. Stated otherwise, the embodiments disclosed in the specification are only for illustrative purposes and are not to be construed as limiting the scope of the application. The description of the embodiments disclosed in the specification is not exhaustive and does not limit the application to the precise embodiments disclosed. Modifications and variations are possible in light of the above teachings or can be acquired from practice of the application. As will be apparent, other modifications and changes can be accommodated without departing from the scope of the present application as set forth in the appended claims.
[0133] The steps of a method or algorithm described in connection with the embodiments disclosed herein can be embodied directly in hardware, in a software module executed by a processor, or in a combination of the two. A software module can reside in random access memory (RAM), flash memory, read-only memory (ROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), registers, hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0134] The terms "first", "second", "third", "fourth" and the like in the description and in the claims, if any, are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the use of these terms is interchangeable under appropriate circumstances such that the embodiments of the present application described herein are capable of accomplishing the same object and are therefore within the scope of the present application.
[0135] The above description of disclosed embodiments is not intended to be exhaustive or to be limited to the precise embodiments disclosed herein. Although specific embodiments of, and examples for, the application are described herein for illustrative purposes only, various equivalent modifications are possible within the scope of the application, as those skilled in the relevant art will recognize. The teachings of the application should not be limited, however, to those embodiments and examples described herein, but should be able to be practiced or carried out in various ways within the scope of the application as defined by the patent claims below.
Claims
1. A microservice port management method, characterized in that: The method comprises: Determine the port range used by microservices in the cluster; Collect the list of used ports in the cluster and store the attribute information of the microservices using each port; According to the port allocation request of the microservice, the attribute information that uniquely identifies the microservice is spliced and encrypted, and the port is generated in combination with the port range. The specific method is as follows: Parse the port allocation request and obtain microservice attribute information in the port allocation request; Concatenate the attribute information that uniquely identifies the microservice in the microservice attribute information into character strings; Use encryption algorithm to encrypt the concatenated character strings; Taking the modulo of the encrypted string with the upper limit of the port range; According to the lower limit of the port range, the modulo result is checked and adjusted to confirm the generated port, and the specific method is as follows: Verifying that the modulo result is greater than a lower limit of the port range, and confirming that the modulo result is a generated port; Verify that the modulo result is less than the lower limit of the port range, and confirm that the sum of the modulo result and the lower limit of the port range is the generated port; Determining whether the generated port is legal according to the used port list; Return the generated legal port to the corresponding microservice.
2. The method according to claim 1, wherein: The attribute information of each port microservice includes at least the name of the microservice, version number, configuration instance name, and port unique identifier.
3. The method according to any one of claims 1-2, characterized in that The method further comprises: Select at least two pieces of attribute information that uniquely identify the microservice and join them together; For illegal ports, the concatenated strings are concatenated again until a legal port is obtained.
4. A microservice port management device, characterized in that: The device comprises: The port range determination unit is used to determine the port range used by microservices in the cluster; The port collection and storage unit is used to collect the list of used ports in the cluster and store the attribute information of the microservices using each port; The port generation unit is used to encrypt and concatenate attribute information uniquely identifying the microservice according to the port allocation request of the microservice, and generate a port based on the port range determined by the port range, further comprising: An allocation request parsing module is used to parse the port allocation request and obtain microservice attribute information in the port allocation request; a string concatenation module, configured to concatenate the attribute information that uniquely identifies the microservice in the microservice attribute information obtained by the allocation request parsing module; An encryption module, used to encrypt the concatenated character strings using an encryption algorithm; a modulo module, configured to take the modulo of the upper limit of the port range on the character string encrypted by the encryption module; A port confirmation module, configured to verify and adjust the modulo result according to the lower limit of the port range and then confirm the generated port, further comprising: A verification module, configured to verify whether a result of the modulo operation performed by the modulo module is greater than a lower limit of the port range; an adjusting module, configured to adjust the modulo result to the sum of the modulo result and the lower limit of the port range when the result of the modulo verification by the verification module is less than the lower limit of the port range; A confirmation module, configured to confirm that a modulo result greater than a lower limit of the port range is a generated port; a port legitimacy confirmation unit, configured to determine whether the port generated by the port generation unit is legal based on the used port list stored in the port collection storage unit; The port returning unit returns the legal port confirmed by the port legitimacy confirmation unit to the corresponding microservice.
5. The device according to claim 4, characterized in that: The attribute information of each port microservice includes at least the name of the microservice, version number, configuration instance name, and port unique identifier.
6. The device according to claim 5, characterized in that: The string concatenation module selects at least two pieces of attribute information that uniquely identify the microservice for concatenation.
Citation Information
Patent Citations
Port detection method, device and equipment and storage medium
CN111200522A
Method for realizing non-invasive secure calling of interface between micro-services
CN112416528A