An automatic application publishing system based on freemarker template engine
The application automated publishing system based on the FreeMarker template engine solves the problem of high complexity of traditional publishing methods, realizes automated deployment and upgrades, improves publishing efficiency and usage value, supports single-server and multi-server publishing, and reduces the workload of system publishing personnel. burden.
Patent Information
- Application Number
- CN202210213275.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-07
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2042-03-07
AI Technical Summary
In digital rural construction, the traditional CI continuous integration/CD continuous deployment integration method leads to high technical difficulty, high investment costs, and high complexity of platform construction. It cannot effectively integrate the application service technology systems provided by different cooperative vendors, resulting in an information service ecosystem. Construction efficiency is low.
An application automation publishing system based on the FreeMarker template engine is used to realize automatic installation, automatic configuration, automatic publishing and automatic opening of related services through the client, simplifying the system publishing process, generating startup scripts and configuring running ports, and supporting single server and multi-server publishing. , including gateway server, application server cluster and Linux server cluster, use FreeMarker engine to convert template files into script files, and upload and execute script files through JSch.
It greatly improves the efficiency of system release, reduces the workload of system release personnel, supports static resource release, realizes automated deployment and upgrade of applications, and simplifies the operation process of application developers.
Smart Images

Figure CN114675842B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of application data publishing, and in particular to an automatic application publishing system based on a FreeMarker template engine. Background Art
[0002] Based on the current situation of digital rural construction with many grassroots users, wide business scenarios and extensive functional requirements, it is urgent to establish an information service ecosystem, integrate related application services, and unify the informationization empowerment of digital rural construction. Since the application service technology systems provided by different cooperative manufacturers are not unified, if the traditional CI continuous integration / CD continuous deployment fusion method is adopted, the technical difficulty is high and the investment cost is high, resulting in high complexity of platform construction. Summary of the invention
[0003] In response to the above background problems, the present invention provides a new application publishing method and proposes an application automatic publishing system based on the FreeMarker template engine, which aims to complete a whole set of processes such as automatic installation, automatic configuration, automatic publishing, and automatic activation of related services through the client, thereby greatly improving the system publishing efficiency.
[0004] An automatic application publishing system based on the FreeMarker template engine includes a client and a server. The server includes a gateway server, an application server cluster, and a Linux application server cluster. The application server cluster processes application publishing logic, and the Linux server is responsible for deploying various applications.
[0005] Step 1: The server receives the version ID of the application transmitted by the client, retrieves the server and application information, and passes it into the FreeMarker template file, which is converted into the corresponding script file by the FreeMarker engine;
[0006] Step 2: Generate a temporary directory and save the script file to the local temporary directory to prepare for uploading to the server;
[0007] Step 3: Determine the service type and server information corresponding to the uploaded file, establish a server session control session and channel, and upload the script file in the temporary directory to the corresponding Linux server, poll to determine whether the upload is successful, and if the upload is successful, enable the command to execute the script file;
[0008] Step 4: If the integration is successful, delete the temporary directory and files.
[0009] Step 5: Check whether there is an integration log. If so, update the integration log; otherwise, store the integration log.
[0010] Step 6: If the integration fails, determine whether there is a previous version of the deployed application. If so, prompt whether to roll back. If the rollback is confirmed, re-execute the deployment process of the previous version, and so on.
[0011] The script file production is divided into the following steps:
[0012] Step 1: Define a template engine configuration object named fmconfigurer;
[0013] Step 2: Use the fmconfigurer object to obtain the ftl template object template;
[0014] Step 3: Define a template parameter set, which is consistent with the parameters to be replaced in the ftl template file;
[0015] Step 4: Define the output file format and output a shell script file with a .sh extension.
[0016] Step 5: Call the process method of the template object to replace the parameters and write the output text into the shell script file;
[0017] Step 6: Use the put method of the ChannelSftp object to upload the shell script file to the server temporary directory.
[0018] The present invention automatically adapts the compiled binary packaged file in the deployment cluster server and generates a startup script and configures the running port. When the application is deployed, the engine script is executed to successfully deploy the application in the specified location, which is convenient for application developers to deploy and upgrade application products multiple times. A new and lightweight application automatic publishing system is creatively proposed. The automatic publishing system in the present invention can support single-server publishing, multi-server publishing, and static resource publishing. It has high use value and can greatly reduce the workload of system publishers. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 This is a schematic diagram of the automatic publishing server architecture in an embodiment of the present invention;
[0020] Figure 2 A schematic diagram of request distribution in an embodiment of the present invention;
[0021] Figure 3 A schematic diagram of gateway routing and filtering in an embodiment of the present invention;
[0022] Figure 4 A timing diagram for configuring services in an embodiment of the present invention;
[0023] Figure 5This is a Freemarker implementation effect diagram in an embodiment of the present invention;
[0024] Figure 6 An execution flow chart is published in an embodiment of the present invention;
[0025] Figure 7 This is a schematic diagram of front-end script execution in an embodiment of the present invention;
[0026] Figure 8 This is a flowchart of the middle script execution process in an embodiment of the present invention;
[0027] Fig. 9 This is a flowchart of the nginx script execution process in an embodiment of the present invention;
[0028] Fig.10 This is a flowchart of the overall process of automatic deployment in an embodiment of the present invention;
[0029] Fig.11 This is a diagram of the working principle of FreeMarker in an embodiment of the present invention;
[0030] Fig.12 A schematic diagram of generating an automated publishing file in an embodiment of the present invention;
[0031] Fig.13 This is a schematic diagram of a file upload server according to an embodiment of the present invention;
[0032] Fig.14 This is a schematic diagram of the logic of publishing script execution in an embodiment of the present invention. DETAILED DESCRIPTION
[0033] An automatic application publishing system based on the FreeMarker template engine includes a client and a server. The server includes a gateway server, an application server cluster, and a Linux application server cluster. The application server cluster processes application publishing logic, and the Linux server is responsible for deploying various applications.
[0034] Step 1: The server receives the version ID of the application transmitted by the client, retrieves the server and application information, and passes it into the FreeMarker template file, which is converted into the corresponding script file by the FreeMarker engine;
[0035] Step 2: Generate a temporary directory and save the script file to the local temporary directory to prepare for uploading to the server;
[0036] Step 3: Determine the service type and server information corresponding to the uploaded file, establish a server session control session and channel, and upload the script file in the temporary directory to the corresponding Linux server, poll to determine whether the upload is successful, and if the upload is successful, enable the command to execute the script file;
[0037] Step 4: If the integration is successful, delete the temporary directory and files.
[0038] Step 5: Check whether there is an integration log. If so, update the integration log; otherwise, store the integration log.
[0039] Step 6: If the integration fails, determine whether there is a previous version of the deployed application. If so, prompt whether to roll back. If the rollback is confirmed, re-execute the deployment process of the previous version, and so on.
[0040] The script file production is divided into the following steps:
[0041] Step 1: Define a template engine configuration object named fmconfigurer;
[0042] Step 2: Use the fmconfigurer object to obtain the ftl template object template;
[0043] Step 3: Define a template parameter set, which is consistent with the parameters to be replaced in the ftl template file;
[0044] Step 4: Define the output file format and output a shell script file with a .sh extension.
[0045] Step 5: Call the process method of the template object to replace the parameters and write the output text into the shell script file;
[0046] Step 6: Use the put method of the ChannelSftp object to upload the shell script file to the server temporary directory.
[0047] Detailed description with reference to the embodiments:
[0048] The automated publishing process in the present invention is based on the B / S cluster architecture, and is completed by the collaboration of the client and multiple server ends. The user configures the server information and release package information on the client, and completes the entire publishing logic on the server end. The server end is the core of the entire application automated publishing system, including the gateway server, application server cluster, and Linux application server cluster (front-end server cluster, back-end server cluster, and single application server cluster). The application server cluster processes the application publishing logic, and the Linux server is responsible for deploying each application instance. The present invention adopts a front-end and back-end separated architecture model, and is compatible with the front-end and back-end mixed design (single application). It includes three types of server clusters, namely, the front-end server cluster, the back-end server cluster, and the single application server cluster. The front-end application server cluster is responsible for storing the front-end compressed package and nginx instance forwarding, and the back-end server is responsible for storing the back-end packaged files and running the back-end service instance. The single application server belongs to the back-end server cluster and is responsible for storing the single application and the single application running instance. The main file format is the war package format. The entire publishing example is as follows Figure 1 Shown:
[0049] The client is mainly divided into two roles: developers and administrators, who operate on different client terminals.
[0050] The application in the present invention is deployed on multiple servers, and the application exists in the form of a cluster to ensure reliable operation of the application. The client accesses the web server cluster of the intranet through the nginx load balancing hash strategy. When a request hits a web server, the request passes through the gateway layer and is forwarded to each service by the gateway routing. The routing and load balancing of each service of the present invention are implemented in the form of gateWay + nacos + config, and polling is adopted by default.
[0051] gateWay is responsible for filtering requests and forwarding requests. A set of routing rules is maintained in gateWay memory, such as: / xxx-api / **. If the request url path matches the routing rules, gateWay will release the request and forward the request to the real service; config is responsible for configuring the routing forwarding rules. config is configured in json array format. The json array maintains the path matching rules and request forwarding target addresses of each application; nacos is responsible for storing config configurations. nacos uniquely determines the config configuration through DataId and Group. Routing forwarding is implemented through route (routing). Route is a configuration rule that is implemented through the config configuration file. Route consists of ID, target URI, and a series of assertions and filters. If the assertion is true, the route is matched. Route URI can forward three request methods, namely http, ws, and lb + service name. The load balancing strategy of the present invention is implemented by randomly accessing a server application through "lb + service name".
[0052] The gateway layer runs as a separate service and routes by matching request path rules. The request path rules are stored as separate configuration files in the nacos registry. The specific implementation of request distribution is as follows: Figure 2 shown.
[0053] The filter is responsible for request filtering and permission authentication, such as Figure 3 shown.
[0054] The file server cluster is responsible for temporarily storing the program packages uploaded by users, which are requested and downloaded by the Linux server script.
[0055] The database server cluster is responsible for storing service configuration information, and the application server reads the database configuration information, generates script files and executes the publishing logic.
[0056] The premise of application deployment is service configuration, which is completed by the above-mentioned developers and administrators. First, the developer selects the application and version, then selects the corresponding service type and uploads the file to the file server on the configuration page. Then the administrator configures the server IP, port number and related startup configuration. The developer and administrator configuration information is saved in the database server. The specific timing diagram is as follows Figure 4 shown.
[0057] When the system user opens the application configuration page, the system will automatically trigger the configuration filling logic. The system will automatically select a low-load server as the deployment server. After automatically selecting the deployment server, the system will automatically randomly generate a port number. The port number is an unoccupied port number in the selected server cluster. To facilitate the management of port numbers, set the front-end server port range to 10000-20000 and the back-end port number range to 20000-30000.
[0058] After the configuration is completed, the publishing logic can be started. After the publishing is completed, the publishing status and publishing time will be automatically displayed. If another version of the same application is released, the server resources of the current version will be released and re-released. The so-called releasing server resources means deleting the files of the Linux server and closing the corresponding process. The configuration information of the application is still saved in the database server, and the administrator can still release other versions of the application again.
[0059] FreeMarker is a template engine that generates output files dynamically based on templates. It is also a Java class library that can be embedded in development applications. The system uses FreeMarker to generate script files. Figure 5 shown.
[0060] Template is a Freemarker core class, which is used to parse the ftl template file and generate an output file, which is converted into an executable shell script by the Freemarker execution program and stored in the temporary directory of the web server. The implementation process in the present invention is as follows:
[0061] Step 1: Define a template engine configuration object named fmconfigurer;
[0062] Step 2: Use the fmconfigurer object to obtain the ftl template object template;
[0063] Step 3: Define a template parameter set, which is consistent with the parameters to be replaced in the ftl template file;
[0064] Step 4: define the output file format, in the present invention, the output is a shell script file with a .sh extension;
[0065] Step 5: Call the process method of the template object to replace the parameters and write the output text into the shell script file;
[0066] Step 6: Use the put method of the ChannelSftp object to upload the shell script file to the server temporary directory.
[0067] The present invention designs five template files, the file suffix is in ftl format, which are respectively front-end template file, back-end template file, mime template file, nginx template file, and nginx configuration template file. The front-end template file is mainly responsible for downloading and decompressing the front-end package; the back-end template file is mainly responsible for file downloading and service startup, restart, shutdown, and monitoring startup status; the nginx template file is responsible for nginx instance creation, including verifying nginx service status, startup, restart, shutdown, and monitoring startup status; the nginx configuration template file is a configuration file corresponding to each nginx instance startup, and is a bridge for the front-end application to establish a connection with the gateway service. Under the conversion of the FreeMarker engine, the above template files finally generate shell script files, which are respectively front-end script (appFront.sh), back-end script (appBack.sh), mime configuration file (mime.types), nginx script file (nginx.sh), and nginx configuration file (nginx.conf), wherein mime.types is a necessary file for nginx service startup, and can be uploaded to the directory where the nginx configuration file is located. The back-end script file is responsible for starting the back-end application and a single application. Uploading it to the back-end server cluster requires the creation of multiple connection objects. Other script files are responsible for front-end application deployment and nginx instance generation. Uploading it to the front-end server cluster also requires the creation of multiple connection objects. Since the front-end application and nginx instance are uniformly deployed in the front-end server, uploading and script execution are unified in one connection object, and the connection resources are finally released. This operation prevents frequent creation and release of connections that cause system overhead. Before the release process is executed, the configuration information will be queried based on the application id and version id, including server configuration information and deployment package configuration information. Then, a JSch object will be instantiated based on the server configuration information, and a session will be created. Finally, a connection with the Linux server will be established. For example, Figure 6 shown.
[0068] The following takes the front-end script execution logic as an example. Figure 7 shown.
[0069] Specific steps:
[0070] Step 1: Secondly, use the ps –ef|grep port command to check whether the port number is occupied. If it is occupied, the user is prompted to reconfigure the port number on the service configuration page.
[0071] Step 2: Then, use the wget command to download the front-end package from the file server, and poll to monitor whether the download is successful (downloading the front-end package takes some time). If it cannot be detected within the specified time, the user will be prompted that the download failed.
[0072] Step 3: Finally, decompress the front-end tar.gz compressed package. The back-end script execution logic is as follows Figure 8 shown.
[0073] Specific steps:
[0074] Step 1: Poll at the logic layer to determine whether the script is uploaded successfully. If the script file exists within the valid time, execute the script file.
[0075] Step 2: Use the ps –ef|grep port command to determine whether the port number is occupied. If it is occupied, stop the script logic and prompt the user.
[0076] Step 3: Use the wget command to download the backend package from the file server, and poll to monitor whether the download is successful (downloading the backend package takes some time). If it cannot be detected within the specified time, the user will be prompted that the download failed.
[0077] Step 4: If the download is successful, start the backend application using the java –jar command.
[0078] Step 5: Check whether the port number exists according to the service name. If it exists, it means that the service is starting. You need to shut down the backend process first and then start it again.
[0079] Step 6: Poll to determine whether the application has a port number. If so, the backend application is started successfully. If the port number cannot be obtained within the specified time, the user is prompted that the startup failed. The execution logic of the nginx script file is as follows: Fig. 9 shown.
[0080] The nginx script is the core of the entire process. An nginx instance is created by starting the nginx script. The nginx instance associates the front-end application with the back-end application through the nginx.conf configuration file.
[0081] Specific steps:
[0082] Step 1: First, use the whereis command to determine whether nginx is installed. If it cannot be obtained, the user is prompted to install nginx.
[0083] Step 2: Use the ps –ef|grep port command to check whether the port number is occupied. If it is occupied, the user is prompted to reconfigure the port number. This step is mainly to prevent the useless nginx instance from being started when the front-end port number is occupied.
[0084] Step 3: Then check whether the port number exists according to the service name. If it does exist, the nginx instance will be stopped and restarted.
[0085] Step 4: Poll and monitor whether the nginx instance is started successfully. If the nginx instance cannot be obtained within the specified time, stop the nginx script and prompt the user that the startup failed.
[0086] Note: The "${object name}" identifier in the above script file is passed in by the map hash structure, and then parsed and mapped to the identifier by Freemarker. "$object name" is the identifier of the Linux Sheel script receiving variables. The variables of the Linux Sheel script can also be identified as "${object name}". In order to avoid conflicts between the two parsing, the variables of the Linux Sheel script use the "$object name" expression, which perfectly avoids the consequences of failure caused by parsing conflicts.
[0087] Freemarker maps the template file to the final script file by the Template class of the Freemarker dependency package. The template file is converted into the corresponding script file by the process method of the Template class.
[0088] JSch is the abbreviation of Java Secure Channel, which is a pure Java implementation of SSH2. It allows connecting to one or more Linux servers, transferring files and operating remote servers.
[0089] The system uses JSch to upload the script file generated by Freemarker to the corresponding directory of the server, and then sends a command or executes a script command to start the script file, ultimately achieving the purpose of automated application download, automated deployment, automated storage, automated monitoring, and automated verification.
[0090] JSch is a channel for Java programs to interact with servers remotely. It includes three objects: Connection, Session, and Channel. Connection is established by the factory mode. You need to enter the server IP, server port number, user name, and password to establish a connection, then instantiate a session Session, and then declare a channel Channel. There are three commonly used channels, including ChannelShell, ChannelExec, and ChannelSftp. The first two are used to execute commands (commands can be Linux shell commands), and the latter is used to upload and download files. This system uses these three channel types at the same time, that is, it uploads the final Freemarker script file and executes the script file, thereby achieving the purpose of automated integration.
[0091] Specific steps:
[0092] Step 1: Create a new JSch object;
[0093] Step 2: Get the Session from the JSch object for connection and set the connection information.
[0094] Step 3: Use the session object to call opnChannel("xxx") to open the communication channel and connect;
[0095] Step 4: The following are different channels for different usage scenarios.
[0096] The following is a brief introduction to the automatic publishing implementation in the present invention:
[0097] The user configures the deployment package to the file server cluster, and the configuration information (deployment package information and server information) is saved in the database server. After all preparations are completed, the automatic deployment process begins. If there is a successfully deployed version, the resources of that version must be released first. The so-called releasing resources means deleting all files of that version and closing the corresponding process. The overall logic of automated deployment is as follows: Fig.10 shown.
[0098] Specific steps:
[0099] Step 1: The server receives the version ID of the application sent by the client, retrieves the server and application information, and passes it into the FreeMarker template file, which is converted into the corresponding script file by the FreeMarker engine.
[0100] Step 2: Generate a temporary directory and save the script file to the local temporary directory in preparation for uploading to the server.
[0101] Step 3: Determine the service type and server information corresponding to the uploaded file, establish a server session and channel, and upload the script file in the temporary directory to the corresponding Linux server. Poll to determine whether the upload is successful. If the upload is successful, enable the command to execute the script file.
[0102] Step 4: If the integration is successful, delete the temporary directory and files.
[0103] Step 5: Check whether there is an integration log. If so, update the integration log; otherwise, store the integration log.
[0104] Step 6: If the integration fails, determine whether there is a previous version of the deployed application. If so, prompt whether to roll back. If the rollback is confirmed, re-execute the deployment process of the previous version, and so on.
[0105] After successful integration, an integration log will be generated. The integration log logic runs through the entire integration process and is stored in the web server memory through the append method of the StringBuilder class. After execution, the log string is stored in the database server.
[0106] Taking into account the multi-user and multi-request approach, the background will start multiple threads to integrate different applications at the same time. Global variables are stored in ThreadLocal to avoid thread insecurity. The ThreadLocal hash structure stores JSch's Connection, Channel, and Session objects, thereby solving the problem of integrating multiple applications at the same time.
[0107] The service configuration obtains the port number by using JSch to access the server. First, all the occupied port sets of the server cluster are obtained according to the configured server IP list. The occupied port set is obtained using the Linux command, and then the service type is determined to be front-end or back-end type (single application belongs to the back-end service type). The port number in the specified range is randomly hit to determine whether it is within the occupied port number list range. If it exists, the port number is randomly hit again until a free port number is obtained and returned to the front-end page. Since accessing multiple servers requires a lot of time to establish connections, which affects the experience, this interface uses multi-threaded simultaneous access and uses countdownLatch to wait and store the occupied port set.
[0108] A successfully released application may face removal or deletion. At this time, resources need to be released. The backend logic also accesses the server through JSch, finds the specified storage directory according to the application ID and version number, deletes all files through the "rm -rf" command, stops the process through the "kill -QUIT port number" command, and the backend service stops the process through the "kill -9 port number" command.
[0109] FreeMarker is an open source template engine tool developed based on the Apache License Version 2.0. It is developed using the popular Java language and can be used to generate and output text based on templates and data to be changed. FreeMarker is a Java class library, a component that programmers can embed into the products they develop. FreeMarker was originally designed to generate HTML pages in Web development in the MVC mode. It is not bound to Servlet or any Web-related things, so it can run in non-Web application environments.
[0110] In the traditional application release process, professionals are required to upload executable binary files to the server and start the binary files by manually entering startup commands or executing startup scripts. This will bring a lot of workload to operation and maintenance personnel in a situation where applications need to be released frequently, and full-time personnel need to be assigned to operate.
[0111] The automated publishing system in the present invention is very different from the currently popular CI / CD. The currently popular CI / CD tools (gitlab + jenkins) are pipeline deployment methods. From source code to compilation and packaging to the publishing stage, the resources are known, and the source code cannot be released in a third-party system. The automated publishing system in the present invention adopts a more flexible publishing method, requiring the publisher to provide a binary package file that can be run. The publishing system automatically adapts and generates a startup script based on the binary file, configures the application running port, and provides the application access address.
[0112] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described here are only used to explain the present invention and are not used to limit the present invention.
[0113] The embodiments of the present invention can be divided into static resource publishing embodiments and executable binary file publishing embodiments, which are described below. The instructions and expressions provided by FreeMarker are used to write template files, where instructions refer to special tags used by FreeMarker to identify conversions, and expressions refer to specific grammatical implementations in tags. In the template, we focus on how to display data, and outside the template, we focus on what data to display.
[0114] The static resources of the present invention refer to resources (such as js, html, css, image, etc.) that can be run in reverse proxy software (such as nginx) and can be proxied by reverse proxy software. Before publishing static resources, it is necessary to prepare a reverse proxy software configuration FreeMarker template file, and the template file extension uses the extension .ftl defined by FreeMarker to be used in the template file. In this embodiment, the configuration of the reverse proxy software (nginx) is generated in .conf file commands, and the final command mode of the file must match the requirements of the reverse proxy software used. The reverse proxy software used in this embodiment is nginx. In this embodiment, it is necessary to configure the configuration template file of the reverse proxy software, and the port number of the server used after the reverse proxy system is started is specified in the template file. The user can access the static resources mapped by the reverse proxy system through the port number. In order to ensure that the use of the server port does not conflict with other services, the port range that can be used by the reverse proxy system is specified in this embodiment as 10000~20000, and the server directory where the static resources are currently automatically published needs to be specified, and the static resources are decompressed and published under this directory. In order to uniformly plan and use publishing directories on the server, it is necessary to specify a unified publishing directory, which is also the parent directory used uniformly by multiple static resource publishing implementation examples. The subordinate directory of static resource publishing can be a project name or other value with special meaning, as long as it can be distinguished from other publishing implementation examples.
[0115] In the present invention, after the reverse proxy system configuration FreeMarker template file is prepared, the FreeMarker template file of the static resource is then prepared to be downloaded and decompressed, and the template file finally generates a shell script that can be directly run on a Linux server according to the parameters input by the user. In the FreeMarker template file, the real publishing directory of the static resource is imported from the outside of the program using FreeMarker Java API, and the acquisition URL of the static resource to be released is specified in the FreeMarker template file. The static resources automatically released in this implementation example all need to be downloaded from the network address, and the name of the static compressed package to be released is specified. The compressed package file extension used in this embodiment is a tar.gz format compressed package, and the compressed package name is imported from the outside of the program using FreeMarker API. The template file is configured to check whether the port number occupied when the reverse proxy software is started is occupied. If the port number required by the reverse proxy software to be started is checked to be occupied, the entire automatic publishing process needs to be terminated. In the generated .sh file, the static resources on the network are downloaded to the directory specified by the static resource server using the wget instruction. After the static resource release package is downloaded, the compressed package is decompressed using the linux tar instruction. In this embodiment, the operation instructions such as wget and tar on the Linux server have been installed on the server.
[0116] In the present invention, a FreeMarker template file is used to generate a startup script. In this embodiment, a FreeMarker template file for starting the reverse proxy software needs to be generated, and in the template, it is necessary to specify how to start the reverse proxy software.
[0117] In the FreeMarker sample template file, the Linux whereis instruction is used to obtain the installation directory of the reverse proxy software Nginx on the server. The template file also needs to check the occupancy of the currently specified port and specify a status code to indicate whether the port is occupied. The status code will be returned to the Java program, and the Java program will process it according to the actual situation. In this embodiment, this situation will be returned to the user page, and the user will decide the subsequent processing logic.
[0118] In the present invention, after the three FreeMarker template files described above are written, the FreeMarker Java API can be used to perform the automatic publishing process. In this embodiment, the FreeMarker SDK used is the jar package officially provided by FreeMarker. In this embodiment, the FreeMarker template is used to explain the automatic publishing process with key Java codes.
[0119] In this embodiment, for the convenience of demonstration and explanation, unified configuration is made in the Java program. In order to make the maintenance of the code and the resource management on the server clearer, the user ID of the current automatic publishing operation is defined, which corresponds to the unique user identity in the automatic publishing system, and the storage directory of the publishing log recorded in the entire automatic publishing process on the server is specified, which provides convenience for users to find related problems based on the publishing log later. After encapsulating some parameters into objects of the Map type, the key process of automatic publishing is carried out.
[0120] In the above key process, first assemble the array object of static resources to be published, and then loop through the array. In each loop, it is necessary to obtain the name of the static resource package published and the text extension output by the FreeMarker template engine from the published object variable. The encapsulation function queries the configured server list through the currently published application ID, and then finally executes the server through the executable script generated by FreeMarker. After obtaining the server, the resource file can be decompressed to call the Nginx function.
[0121] The above implementation illustrates how to generate the configuration file required when the reverse proxy software Nginx is started. In the configuration file generation function, the parameters required in the FreeMarker template and the name of the FreeMarker template file to be operated need to be specified.
[0122] In this embodiment, the final output result is a .sh script file that can be directly executed on Linux. Then, the template object is obtained according to the FreeMarker template name, and the FreeMarker configuration environment is obtained through the built-in configuration class provided by FreeMarker. Then, the template object is obtained from the configuration environment. The process function of the template object replaces the template content and outputs the replaced text content. This function is also the core function in the FreeMarker template engine API, such as Fig.12 shown.
[0123] After completing the above three Nginx configuration files and executable scripts in Java logic, the generated files need to be uploaded to the target publishing server for resource publishing operations. How to upload the generated files to the publishing server? The present invention uses the open source JSch framework to perform this operation.
[0124] The JSch framework is an SSH2 connection tool developed in pure Java language, which can be used for port forwarding, X11 status and file transfer, etc. The functions provided by the JSch framework can be integrated into the program for communication with the server, command forwarding and file creation. In this embodiment, the version number of the JSch framework used is: version 0.1.55. In the JSch framework, communication with the server uses the JSch Channel type object to communicate with the server and forward files, so before transferring the script to the server, it is necessary to obtain the Channel object.
[0125] To obtain the channel object, first define the JSch client, and pass the parameters required by the target publishing server in the Session acquisition function, including: the user name required to connect to the target server, the IP address of the target server, the port number required to connect to the template server, etc. After connecting to the server, JSch will return the Session object of the conversation with the server. After the connection is successful, the channel for file transfer with the server is obtained through the channel opening function. The channel protocol type is sftp (Secure File Transfer Protocol).
[0126] After converting the channel object to a ChannelSftp object, the put function of the ChannelSftp object is used to perform file upload operations. The input parameters include: the source directory of the transferred file, the template directory of the publishing server, whether to overwrite the same file, etc. After completing the acquisition of the above ChannelSftp object, the configuration file and executable script file generated by the FreeMarker engine can be uploaded to the publishing target server. The uploaded files include three: the first is to upload the script generated to execute the download and decompression of static resources to the target server; the second is to upload the configuration file required to start the reverse proxy software nginx to the target server; the third is to upload the script file to start the reverse proxy software nginx to the target server. In this embodiment, only after the above three files are fully uploaded can the necessary files required in the static resource publishing process be completed. It should be noted that the file upload in this embodiment is only to illustrate how to complete the upload of configuration files and script files. For unpredictable exceptions such as network errors and file errors that occur during the upload process, the sample code does not show how to handle them. However, in actual applications, such abnormal errors must be considered and the error information can be friendly notified to the user, such as Fig.13 shown.
[0127] After completing the above three configuration files and executable script files generated by FreeMarker, you can run these scripts on the target publishing server to perform the final static resource publishing operation.
[0128] When running a script, you must first open a communication channel from the JSchSession object and specify that the opened channel can be used to execute multiple commands on the target server. Specify the commands to be executed on the channel. In this embodiment, the sh command is sent to the target server to execute the .sh script that has been uploaded on the target server. The log output during the execution of the .sh script on the target server is output to the Java program by specifying a function setting, such as Fig.14 shown.
[0129] The specific implementation of the above automated publishing system logic can refer to the specific content in the above method embodiment, and this embodiment will not be described in detail above.
[0130] The technical details of the above-described embodiments have been omitted. To make the description concise, other code details in the above-described embodiments are not introduced, and only some key codes are introduced. However, the use of these key codes should be considered as the contents recorded in this specification.
[0131] The above-described embodiments only express part of the automated publishing process in the present invention, and the description thereof is relatively specific and detailed, but it cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for ordinary technicians in this field, several variations and improvements can be made without departing from the concept of the present invention, which all belong to the protection scope of the present invention. Therefore, the protection scope of the patent of the present invention shall be based on the attached claims.
[0132] The present invention relates to an application automatic publishing system based on a template engine. The system is based on the problems existing in the current traditional application publishing method. The compiled binary packaged file is automatically adapted in the deployment cluster server to generate a startup script and configure the running port. When the application is deployed, the engine script is executed to successfully deploy the application in a specified location, which is convenient for application developers to deploy and upgrade application products multiple times. A new and lightweight application automatic publishing system is creatively proposed. The automatic publishing system in the present invention can support single-server publishing, multi-server publishing, and static resource publishing. It has high use value and can greatly reduce the workload of system publishers.
Claims
1. An automatic application publishing system based on the FreeMarker template engine, Features include Client and server. The server includes gateway server, application server cluster and Linux application server cluster. The application server cluster processes application publishing logic and the Linux server is responsible for deploying various applications. Step 1: The server receives the version ID of the application uploaded by the client, retrieves the server and application information, and passes it into the FreeMarker template file, which is converted into the corresponding script file by the FreeMarker engine; Step 2: Generate a temporary directory and save the script file to the local temporary directory to prepare for uploading to the server; Step 3: Determine the service type and server information corresponding to the uploaded file, establish a server session control session and channel, and upload the script file in the temporary directory to the corresponding Linux server, poll to determine whether the upload is successful, and if the upload is successful, enable the command to execute the script file; Step 4: Delete the temporary directory and files if the integration is successful; Step 5: Check whether there is an integration log. If so, update the integration log. Otherwise, store the integration log. Step 6: If the integration fails, determine whether there is a previous version of the deployed application. If so, prompt whether to roll back. If the rollback is confirmed, re-execute the deployment process of the previous version, and so on.
2. According to the application automatic publishing system based on FreeMarker template engine described in claim 1, Features The script file production is divided into the following steps: Step 1: Define a template engine configuration object named fmconfigurer; Step 2: Use the fmconfigurer object to obtain the ftl template object template; Step 3: Define a template parameter set, which is consistent with the parameters to be replaced in the ftl template file; Step 4: Define the output file format and output a shell script file with a .sh extension. Step 5: Call the process method of the template object to replace the parameters and write the output text into the shell script file; Step 6: Use the put method of the ChannelSftp object to upload the shell script file to the server temporary directory.
