A microservice code generation method and system

Through the microservice code generation method, the template information and architecture diagrams entered by the user are used to generate microservice code, solving the problem of surge in the number of services and complex relationships in the microservice design, and achieving rapid code generation and simplified debugging.

CN114756220BActive Publication Date: 2025-02-14ZHEJIANG GEELY HLDG GRP CO LTD +1
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210398761.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-04-15
Publication Date
2025-02-14
Estimated Expiration
2042-04-15

AI Technical Summary

Technical Problem

The microservice design strategy has led to a surge in the number of services, the relationship between services is complex and difficult to debug, affecting the further improvement of microservices.

Method used

Provide a microservice code generation method, which analyzes the architecture diagram, generates corresponding code blocks, and simplifies the relationship between microservices by receiving template information and architecture diagrams input by users.

Benefits of technology

It realizes fast microservice code generation, reduces the time for manually creating services in the early stage of the project, simplifies service dependency and middleware component access, and reduces the time for debugging complex relationships.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114756220B_ABST
    Figure CN114756220B_ABST
Patent Text Reader

Abstract

The present invention provides a microservice code generation method and system, comprising the following steps: receiving template information and an architecture diagram input by a user; parsing the architecture diagram, and obtaining architecture information according to the result of parsing the architecture diagram; generating code by calling a code block corresponding to a corresponding identifier according to the architecture information and template information; wherein the code block is stored in a database, and the code block is mapped to the corresponding identifier. The present invention can quickly build the entire microservice.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of electronic digital data processing, and in particular to a microservice code generation method and system. Background Art

[0002] Microservices is a design strategy that splits a large single application and service into dozens of supporting microservices. Since a microservice only solves one business problem, splitting a large monolithic application into several or even dozens of microservices will lead to a blowout in the number of services, and the various relationships between services are intricate, interdependent and difficult to debug, which is not conducive to the further improvement of microservices. Summary of the invention

[0003] An object of the present invention is to provide a quick method for generating microservice codes.

[0004] A further object of the present invention is to simplify the relationship between microservices to implement a fast microservice code generation method.

[0005] Another further object of the present invention is to provide a quick microservice code generation system.

[0006] In particular, the present invention provides a microservice code generation method, comprising the following steps:

[0007] Receive template information and architecture diagram input by the user;

[0008] Parse the architecture diagram and obtain architecture information based on the results of parsing the architecture diagram;

[0009] According to the architecture information and the template information, the code is generated by calling the code block corresponding to the corresponding identifier; wherein the code block is stored in the database, and the code block is mapped with the corresponding identifier.

[0010] Furthermore, in the step of receiving the template information and the architecture diagram input by the user, the architecture diagram provided by the user represents the information flow direction of the architecture information in the form of a block diagram.

[0011] Furthermore, the architecture information includes: service type, service quantity, middleware type, service call relationship and custom components, wherein the service call relationship includes the gateway, database and subject corresponding to each service.

[0012] Furthermore, in the step of generating code by calling the code block corresponding to the corresponding identifier according to the architecture information and the template information, the following steps are included:

[0013] Get target information based on the architecture diagram provided by the user;

[0014] Retrieve and call the code block in the database according to the target information;

[0015] The code blocks of the combined database form the basic code;

[0016] Test the basic code and package the code after it passes the test.

[0017] Furthermore, in the step of retrieving and calling the code block in the database according to the target information, the following steps are included:

[0018] According to the service call relationship, retrieve and call the service call layer code block;

[0019] According to the middleware layer information, retrieve and call the middle layer code block;

[0020] Retrieve and call logical code blocks based on the logical relationship between services;

[0021] According to the message sending and receiving relationship between services, retrieve and call the consumption code block.

[0022] Furthermore, in the step of combining the code blocks of the database, multiple types of services are broadcast through the gateway to facilitate the call relationship between the multiple services, and the logic code blocks and consumption code blocks connected to the corresponding database are called during the broadcast.

[0023] Furthermore, before generating the code step by calling the code block corresponding to the corresponding identifier according to the architecture information and the template information, the microservice skeleton is first generated.

[0024] In particular, the present invention also discloses a microservice code generation system, including a control device, the control device including a memory and a processor, the memory storing a control program, and the control program is used to implement the microservice code generation method when executed by the processor.

[0025] The code generation method of the present invention is a construction platform based on the microservice ecosystem. It receives template information and an architecture diagram input by the user and parses the architecture diagram to obtain the information required for building the microservice, and then the entire microservice can be quickly built, which solves the need to manually create multiple services in the early stage of the project and solves the service dependency. Users no longer need to spend a lot of time solving the access of various middleware components and processing the calling relationship between services.

[0026] Furthermore, in the code generation method of the present invention, a graphical modeling method is creatively adopted to finally generate a set of core codes of the microservice system by identifying the architecture diagram, so that users only need to focus on the business layer processing logic. The modeling method is similar to the system architecture diagram in daily work, and the learning cost is low.

[0027] Furthermore, the code generation method of the present invention not only generates the code for one service, but generates a whole set of microservice framework codes. At the same time, users can maintain their own code style by DIYing the template, which is in line with user habits.

[0028] Based on the following detailed description of specific embodiments of the present invention in conjunction with the accompanying drawings, those skilled in the art will become more aware of the above and other objects, advantages and features of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] Hereinafter, some specific embodiments of the present invention will be described in detail in an exemplary and non-limiting manner with reference to the accompanying drawings. The same reference numerals in the accompanying drawings indicate the same or similar components or parts. It should be understood by those skilled in the art that these drawings are not necessarily drawn to scale. In the accompanying drawings:

[0030] Figure 1 is a metadata information mapping relationship diagram of one or more embodiments of the present invention;

[0031] Figure 2 is an architectural diagram in the form of a block diagram of one or more embodiments of the present invention;

[0032] Figure 3 It is a step diagram of a method for generating microservice code according to one or more embodiments of the present invention. DETAILED DESCRIPTION

[0033] Figure 1 : is a block diagram of a microservice code generation method according to an embodiment of the present invention. The microservice code generation method provided in this embodiment is not for a specific engineering project, but for a general microservice development project. Using the method provided in this embodiment, a set of initial codes can be automatically generated, thereby reducing the time for code development. The microservice code generation method can generally include the following steps:

[0034] S1. Store various code blocks in the database.

[0035] S2. Receive template information selected by the user according to the data source type.

[0036] S3. Map the database with the corresponding identifier for easy calling.

[0037] S4. Receive the architecture diagram provided by the user and parse the architecture diagram, and obtain architecture information according to the result of parsing the architecture diagram.

[0038] S5. According to the architecture information and the template information provided by the user, the code block is called through the corresponding identifier to generate code.

[0039] Specifically, in step S1, the operation and maintenance module needs to uniformly configure the project-related data sources. The data source refers to a common code, which generally refers to a middleware product, which can be a database (mysql, db2, oracle), a distributed cache (redis), a message middleware (kafka, rocketmq and other middleware products), or a distributed microservice framework.

[0040] More specifically, taking the usage scenario of the MySql database as an example, the traditional configuration method includes the following steps:

[0041] S1.1. Add dependencies and MySql driver to the Spring Boot project.

[0042] S1.2. Configure the application.properties file to modify some default configuration values.

[0043] S1.3. Create a new entity class corresponding to the database field. This entity class can optimize the code and reduce unnecessary code.

[0044] S1.4. Create a new Mapper (xml) mapping file.

[0045] S1.5. Create a new DAO (Database Access Object) interface, and then create a new DAO layer.

[0046] S1.6. Create a new Controller layer:

[0047] In one or more embodiments of the present invention, a concise template is formulated, and the variable part is extracted and combined with the variables through the Mustache template engine to finally load the required code.

[0048] Figure 1 It is a metadata information mapping relationship diagram. According to an embodiment of the present invention, in step S3, the database is mapped with the corresponding identifier for easy calling, specifically, the metadata information is mapped with the corresponding identifier. The metadata information refers to the attributes of the middleware product used by the service. This part of information is maintained in the system background. By marking each set of resources, specific resource information is defined, such as MySql. The resource information includes user name, password, connection address, driver, etc., and a tag with a specific meaning is identified at the same time. When a service uses MySql, it only needs to introduce the MySql model on the interface and identify the tag. The system can automatically identify and generate code related to operating MySql. Figure 1In serviceA, the DB-B database is needed. When code generation is executed, the logic for operating the relevant tables in the library will be automatically generated. For serviceB, the code logic for accessing the DB-C database is generated. For serviceC, the code logic for accessing the DB-A database is generated. In this embodiment, MySql is a relational database management system commonly used in the database field. Relational databases store data in different databases instead of putting all data in one large database, which increases speed and flexibility. In this embodiment, serviceA, serviceB, and serviceC are different services. In this embodiment, the BD-A database, the BD-B database, and the BD-C database are different databases.

[0049] According to one embodiment of the present invention, in step S4, an architecture diagram provided by a user is received and parsed, and architecture information is obtained according to the result of parsing the architecture diagram. The architecture diagram provided by the user includes information such as service type, number of services, middleware type, message sending and receiving, service dependency, service call relationship and custom components, wherein the service call relationship includes the gateway, database and subject corresponding to each service. The architecture diagram provided by the user is in the form of a block diagram. More specifically, the architecture diagram in the form of a block diagram marks the relationship between each service and the type of related technology. For example, a rectangular graphic is used to represent a service, a horizontal cylinder graphic is used to represent logic, an arrow is used on a service to represent the relationship between A and B services, or an arrow is used on a message to represent message sending and consumption. In this embodiment, middleware refers to a type of software between a database and an operating platform.

[0050] Figure 2 This is a typical architecture diagram in the form of a block diagram. According to an embodiment of the present invention, the code generated by the serviceA service includes: providing dubbo interface service to the gateway layer, providing dubbo interface to serviceB for calling, generating connection to DB-A database layer operation, and consuming topicC messages. The code generated by the serviceB service includes: calling the service logic of serviceA, sending messages to topicB, and accessing DB-B database layer operations. The code generated by the serviceB service includes: providing dubbo interface service to the gateway layer, consuming topicB messages. Sending messages to topicC, generating connection to DB-C database layer operations. By analyzing the above steps, the system can automatically generate most of the code for the user. From the perspective of use, the user only needs to focus on implementing the business code, and make some adjustments to unnecessary functions and interfaces.

[0051] According to an embodiment of the present invention, in step S3, the database is mapped to the corresponding identifier for easy calling. The database stores various code blocks, so step S3 actually calls the code block through the corresponding identifier, which specifically includes the following steps:

[0052] S3.1. Obtain the number of services, middleware types, service call relationships, and custom components based on the architecture diagram information provided by the user. The method used to parse the architecture diagram in this step is generally automatic resolution, or manual resolution. In the prior art, the automatically parsed architecture diagram can use a UML-based visual modeling tool, such as Rational Rose, which will not be repeated here.

[0053] S3.2, search and call the code block in the database. In this step, the search formula is automatically edited using the parsing result in step 3.1 as the keyword for search.

[0054] S3.3. The code blocks of the combined database form the basic code.

[0055] S3.4. Test the basic code and package the code after passing the test.

[0056] According to one embodiment of the present invention, in the above step S3,2, when retrieving and calling the relevant code block in the database, the following steps are included:

[0057] S3.2.1. According to the service call relationship, retrieve and call the service call layer code block.

[0058] S3.2.2. According to the middleware layer information, retrieve and call the middle layer code block.

[0059] S3.2.3. Retrieve and call logical code blocks based on the logical relationship between services.

[0060] S3.2.4. Retrieve and call the consumption code block based on the message sending and receiving relationship between services.

[0061] More specifically, in a specific implementation scenario, Java language is used for project development, and Alibaba company specifications are used to layer the Java language. The above embodiment of the present invention abstracts common middleware products and extracts the common parts to facilitate code generation. In step S3.3, when the code blocks of the database are combined to form the basic code, the following steps are included:

[0062] S3.3.1. Generate multiple service codes, pom files, packages, etc. involved in the microservice to form the entire microservice code skeleton. It should be noted that this stage does not involve the specific generation of code.

[0063] S3.3.1. Generate DAO layer and Manager layer code.

[0064] S3.3.1. Code generation of middleware layer related components such as kafka, redis, and oss.

[0065] S3.3.1. Dubbo calls between services to generate logic code.

[0066] S3.3.1. Messages are sent between services to generate consumption codes.

[0067] S3.3.1. Generate unit test code.

[0068] S3.3.1. Packaging.

[0069] S3.3.1. Output download.

[0070] It should be noted that in the above steps, pom file, package, DAO layer, Mmanager layer, kafka, redis, oss, and dubbo are all common terms in the field of Java development. The pom file is the working basis of the Maven project and exists in the project in the form of pom.xml. In this embodiment, the detailed information of the construction project is configured. It pre-configures some default values ​​for most projects, such as the build directory build, the source code directory src / main / java, the test source code directory src / test / java, etc. Kafka is an open source message middleware product. Redis is an open source distributed cache product. Oss is the upload component of Alibaba Cloud oss. Dubbo is a distributed service framework. The Manager layer is responsible for combining and reusing the database operations in the DAO layer, mainly some cache solutions, middleware processing, and the layer encapsulating the third-party platform. The service layer is the business processing layer, which pays more attention to business logic, combines the operations and business logic combined by the Manager, and then encapsulates them into business operations. The biz layer includes the service layer, which focuses on the processing of basic business, and the biz layer is the business layer of the complex application layer.

[0071] At this point, those skilled in the art should recognize that, although multiple exemplary embodiments of the present invention have been shown and described in detail herein, many other variations or modifications that conform to the principles of the present invention can still be directly determined or derived based on the content disclosed in the present invention without departing from the spirit and scope of the present invention. Therefore, the scope of the present invention should be understood and recognized as covering all these other variations or modifications.

Claims

1. A microservice code generation method, characterized in that: The following steps are involved: Receive template information and architecture diagram input by the user; Parse the architecture diagram and obtain architecture information based on the results of parsing the architecture diagram; Generate code by calling the code block corresponding to the corresponding identifier according to the architecture information and template information; The code blocks are stored in a database, and the code blocks are mapped to corresponding identifiers; In the step of generating code by calling the code block corresponding to the corresponding identifier according to the architecture information and the template information, the following steps are included: Get target information based on the architecture diagram provided by the user; Retrieve and call the code block in the database according to the target information; The code blocks of the combined database form the basic code; Test the basic code and package the code after it passes the test; The step of retrieving and calling the code block in the database according to the target information includes the following steps: According to the service call relationship, retrieve and call the service call layer code block; According to the middleware layer information, retrieve and call the middle layer code block; Retrieve and call logical code blocks based on the logical relationship between services; Retrieve and call the consumption code block based on the message sending and receiving relationship between services; In the step of combining the code blocks of the database to form the basic code, the following steps are included: Generate DAO layer and Manager layer code; Code generation for middleware layer related components: kafka, redis, oss, etc.; Dubbo calls between services to generate logic code; Send messages between services and generate consumption codes; Generate unit test code; Pack; Output download.

2. The microservice code generation method according to claim 1, characterized in that: In the step of receiving the template information and the architecture diagram input by the user, the architecture diagram provided by the user represents the information flow direction of the architecture information in the form of a block diagram.

3. The microservice code generation method according to claim 1 or 2, characterized in that: The architecture information includes: service type, service quantity, middleware type, service call relationship and custom components, wherein the service call relationship includes the gateway, database and subject corresponding to each service.

4. The microservice code generation method according to claim 1, characterized in that: When combining the code block steps of the database, multiple types of services are broadcast through the gateway to facilitate the call relationship between multiple services. During the broadcast, the logic code blocks and consumption code blocks connected to the corresponding database are called.

5. The microservice code generation method according to claim 1, characterized in that: Before generating the code step by calling the code block corresponding to the corresponding identifier according to the architecture information and the template information, a microservice skeleton is first generated.

6. A microservice code generation system, characterized in that: The invention comprises a control device, wherein the control device comprises a memory and a processor, wherein a control program is stored in the memory, and when the control program is executed by the processor, it is used to implement the microservice code generation method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Micro-service development method and system

    CN114115890A