Online interface code generation method based on SpringbootTechnical Field
The invention relates to the technical field of universal interface code generation and hot update in data center open service, in particular to an online interface code generation method based on a Springboot.
Background
In the data center open service, an interface for changing the data center is released after being tested and packaged after being developed by developers, and the flow is long. Especially, when developing some common interfaces, it is time consuming and labor consuming.
The existing hot update technology such as devtool is based on a single JVM process, and source codes are compiled into IDE (integrated development environment), so that hot loading is carried out after class, and the hot loading technology is mainly used for accelerating development and reducing the time for repeatedly restarting the JVM process.
Disclosure of Invention
The invention aims to provide an online interface code generation method based on a Springboot, which can quickly generate source codes and send the source codes to a server according to a prefabricated code module under the environment of a micro-service distributed system without the processes of redevelopment, test and deployment; and the hot update supports the distributed micro-service system, the program only restarts the SpringBoot application context, and the process is not restarted, so that the deployment speed is accelerated.
In order to achieve the purpose, the technical scheme of the invention is as follows: an online interface code generation method based on a Springboot provides a distributed micro-service system based on the Springboot, and comprises a main control end and a plurality of micro-service clients; wherein,
the main control end is used for generating source codes, distributing the source codes to the micro-service clients, initiating operation including hot updating, and receiving processing results of the micro-service clients;
and the micro-service client is used for receiving the source code, pre-compiling the check code to check whether the code is compiled successfully or not, finally executing hot update to reload the Spring boot context, loading the Class with the source code compiled successfully into IoC of Spring, and registering the Class as Spring service for calling.
In an embodiment of the present invention, the method specifically includes the following steps:
step 1, starting TCP connection service:
the method comprises the steps that when a main control end is started, a TCP service port is automatically started;
when the micro-service client is started, the micro-service client is automatically connected with a TCP (transmission control protocol) service port of the main control end and registers own information to the main control end, and a communication link is provided for RPC (remote procedure call) calling between the main control end and the micro-service client;
step 2, after the TCP connection service is established between the main control end and the micro-service client, the main control end generates source codes:
generating a universal source code at the main control end according to the code template or compiling a code on line at the main control end, and generating a source code at the main control end;
and step 3, grouping source codes:
the master control end groups the generated source codes according to the category of the micro service client and packs the codes belonging to the same micro service client;
step 4, source code distribution:
the master control end sends RPC requests for distributing source codes to the micro-service client end in sequence according to the dependency relationship of the micro-service, and waits for the return of a result;
step 5, when the micro-service client receives an RPC request for distributing source codes, sending the received source code data into a custom ClassLoader for compiling, obtaining a class object after successful compiling, verifying the correctness of the source codes, finally caching the source codes into a memory, and returning an execution result to the main control end;
step 6, the master control end receives RPC request results of the distribution source codes returned by all the microservice clients, judges whether the execution of the distribution source codes is successful or not according to the returned results, and if one of the results is failed, the flow is stopped and the failure is returned; if all the operations are successful, the next operation is carried out;
step 7, the master control end sequentially sends hot update RPC requests to the micro-service client end according to the sequence required by the source code module, and waits for the result to return;
step 8, when the micro-service client receives the RPC request of the hot update, triggering and executing a hot update function module, and preparing the system to execute the hot update;
and 9, the micro-service client executes hot updating, closes the context of the Spring boot application, closes all the components injected into the Spring, and closes the thread and the thread pool.
In an embodiment of the present invention, the method further includes a step of clearing the cache, that is, all the cache data in the static class is cleared in a reflection manner, so as to avoid memory leakage.
In an embodiment of the present invention, the step of clearing the cache is specifically implemented as follows:
step 10, the microservice client checks all current threads to confirm the threads needing to be closed, and finally executes destruction operation on all found threads to be closed to release resources;
step 11, the micro-service client starts an application process, writes all the issued source codes into a cache file from a cache, and automatically compiles and loads all the codes during cold start; creating a self-defined ClassLoader and setting the self-defined ClassLoader as the ClassLoader of the current thread so as to take over the loading of jar packages and class files when the system is started, and distributing the class Loader used by the system according to rules to ensure that the system is normally started;
step 12, reading all source codes from the source code cache by using the currently self-defined ClassLoader, and compiling the source codes into class to be written to a local path, so that the ClassLoader can correctly load the class;
step 13, starting the SpringBoot application context, loading class compiled from a source code and injecting the class into a Spring container in a service mode, wherein a module of the system can directly call the code; when the micro service client finishes the hot updating, returning a result of executing the hot updating to the main control end;
and step 14, the main control end receives the hot updating results of all the microservice clients, if one microservice client fails, the flow is stopped, the microservice client fails to return, and if all the microservice client fails, the microservice client succeeds in operation.
In one embodiment of the invention, in step 1, the master control end can call the micro-service client through RPC; the micro-service client can also call the master control end through the RPC, and the RPC calls at the two sides of the master control end and the micro-service client share one TCP link, so that the deployment difficulty of the system is reduced.
In an embodiment of the present invention, in step 5, the ClassLoader is implemented based on Groovy.
Compared with the prior art, the invention has the following beneficial effects:
1. under the environment of a micro-service distributed system, according to a prefabricated code module, a source code can be quickly generated and sent without the processes of redevelopment, test and deployment;
2. the hot update supports the distributed micro-service system, the program only restarts the SpringBoot application context, and the process is not restarted, so that the deployment speed is accelerated.
Drawings
FIG. 1 is a flow chart of the method of the present invention.
Detailed Description
The technical scheme of the invention is specifically explained below with reference to the accompanying drawings.
The invention relates to an online interface code generation method based on a Springboot, which provides a distributed micro-service system based on the Springboot, and comprises a master control end and a plurality of micro-service client ends; wherein,
the main control end is used for generating source codes, distributing the source codes to the micro-service clients, initiating operation including hot updating, and receiving processing results of the micro-service clients;
and the micro-service client is used for receiving the source code, pre-compiling the check code to check whether the code is compiled successfully or not, finally executing hot update to reload the Spring boot context, loading the Class with the source code compiled successfully into IoC of Spring, and registering the Class as Spring service for calling.
The following are specific implementation examples of the present invention.
As shown in fig. 1, in the present invention, a distributed micro-service system based on SpringBoot is divided into a master control end and a plurality of micro-service clients. Wherein,
and the master control end is used for generating source codes, distributing the source codes to the micro service clients, initiating operations such as hot update and the like, and receiving the processing results of the micro service clients.
And the micro-service client is used for receiving the source code, pre-compiling the check code to check whether the code is compiled successfully or not, finally executing hot update to reload the Spring boot context, loading the Class with the source code compiled successfully into IoC of Spring, and registering the Class as Spring service for calling.
The technical scheme provided by the invention is realized by adopting the following scheme: the online interface code generation method based on the SpringBoot distributed micro-service system specifically comprises the following steps:
step 1, starting TCP connection service.
The method comprises the steps that when a main control end is started, a TCP server is automatically started;
when the micro-service client is started, the micro-service client is automatically connected with a TCP (transmission control protocol) service port of the main control end and registers own information to the main control end, and a communication link is provided for RPC (remote procedure call) calling between the main control end and the micro-service client.
The main control end can call the micro-service client through RPC; the micro-service client can also call the master control end through the RPC, the scheme provided by the proposal is summarized, and the two-sided RPC calls share one TCP link, so that the deployment difficulty of the system can be well reduced.
And 2, after the TCP connection service is established between the main control end and the micro-service client, the main control end generates source codes.
And generating a universal source code at the main control end according to the code template or writing a code on line at the main control end, and generating the source code at the main control end.
And step 3, grouping the source codes.
And the master control end groups the generated source codes according to the category of the source codes and the category of the micro service end and packs the codes belonging to the same micro service end.
And 4, distributing source codes.
And the master control end sends the RPC requests for distributing the source codes to the micro-service client end in sequence according to the dependency relationship of the micro-service, and waits for the return of the result.
And 5, when the micro-service client receives an RPC request for distributing the source code, sending the received source code data to a custom ClassLoader (based on Groovy) for compiling, obtaining a class object after successful compiling, verifying the correctness of the source code, finally caching the source code into a memory, and returning an execution result to the main control terminal.
And 6, the master control end receives RPC request results of the distributed source codes returned by all the microservice clients, judges whether the execution of the distributed source codes is successful or not according to the returned results, if one of the RPC request results fails, the flow is stopped, the RPC request results are returned, and if all the RPC request results are successful, the next operation is carried out.
And 7, the master control end sequentially sends hot update RPC requests to the micro-service client end according to the sequence required by the source code module, and waits for the result to be returned.
And 8, when the micro-service client receives the RPC request of the hot update, triggering to execute the hot update function module, and preparing the system to execute the hot update.
And 9, the micro-service client executes hot updating, closes the context of the Spring boot application, closes all the components injected into the Spring, and closes the thread and the thread pool.
In the technical solution of the present disclosure, in a preferred implementation manner, the method further includes clearing the cache.
Because some static classes cache data and the memory leakage is caused by hot updating, the cache data in the static classes are completely cleared by adopting a reflection mode, and the memory leakage is avoided.
Step 10, the micro-service client does not close the opened threads or thread pools when some components are closed, and needs to check all current threads again to confirm which threads need to be closed, and finally executes destroy operation on all found threads to be closed, and releases resources.
And step 11, the micro-service client starts an application process, writes all the issued source codes into a cache file from a cache, and automatically compiles and loads all the codes during cold start. And creating a self-defined ClassLoader and setting the self-defined ClassLoader as the ClassLoader of the current thread so as to take over the loading of the jar packet and the class file when the system is started, and distributing the class Loader used by the system according to rules so as to normally start the system.
And step 12, reading all source codes from the source code cache by using the currently self-defined ClassLoader, and compiling the source codes into class to be written to a local path, so that the ClassLoader can correctly load the class.
And step 13, starting the SpringBoot application context, loading class compiled from the source code and injecting the class into a Spring container in a service mode, wherein a module of the system can directly call the code. And returning the result of executing the hot update to the main control terminal.
And step 14, the main control end receives the hot updating results of all the microservice clients, if one microservice client fails, the flow is stopped, the microservice client fails to return, and if all the microservice client fails, the microservice client succeeds in operation.
The invention has the following characteristics:
1. the main control end is connected with the micro-service client through a TCP to complete the two-way request of the RPC.
2. The generated source codes are grouped according to the functions of the micro-service client, and the source codes are sequentially sent to the micro-service client to be precompiled so as to verify the correctness of the source codes.
3. The main control end sends hot update requests to the micro service client end in sequence, the micro service client end executes a hot update processing program and returns results to the main control end, and finally the main control end manages the whole process.
The above are preferred embodiments of the present invention, and all changes made according to the technical scheme of the present invention that produce functional effects do not exceed the scope of the technical scheme of the present invention belong to the protection scope of the present invention.