Method for realizing fine-grained control of dynamic data source during operation based on micro-service system
By building a microservice system, the addition and switching of dynamic data sources are realized, and the scope of data sources is controlled in a fine-grained manner, the problem of difficulty in dynamically managing data sources in the existing technology is solved, and the flexibility and reliability of the application are improved.
Patent Information
- Application Number
- CN202411589923.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-08
- Publication Date
- 2025-02-25
AI Technical Summary
The existing technology is difficult to dynamically add or switch data sources when the application is running, and cannot control the scope of the data sources in a fine-grained manner, and cannot meet the needs of complex business logic and multi-tenant.
By building a microservice system, including configuration center module, monitoring module, adapter, application module, database access layer and database, new and switching of dynamic data sources can be achieved. The configuration center module is used to centrally manage and dynamically update the data source configuration, monitor the configuration changes of the monitor module, convert the configuration file content of the adapter module, handle the addition and switching of data source requests, and the database access layer realizes fine-grained data sources through dynamic proxy. Access control.
It realizes the function of dynamically adding and switching data sources when the application is running, and can control the scope of the data sources in a fine-grained manner, improves the flexibility, expansion and reliability of microservice applications, and supports zero downtime for applications. Time and multi-tenant needs.
Smart Images

Figure CN119513176A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of dynamic switching of data sources, and in particular, relates to a method and system for realizing fine-grained control of dynamic data sources at runtime based on a microservice system. Background Art
[0002] As system functions increase and traffic increases, business logic becomes more complex. Traditional single, large applications become increasingly difficult to maintain and expand, and stability is challenged. The microservice architecture solves the problems faced by the traditional monolithic architecture by breaking down a single large application into multiple groups of small, independently deployed services. At the same time, as business data increases, the performance of a single data source no longer meets program requirements, and there may be situations where data sources need to be dynamically added or switched without stopping the application. Existing technical conditions generally specify multiple data sources for switching when the application starts, and it is not possible to continue to add data sources according to actual conditions after the application goes online to meet the requirements of dynamically adding data sources; at the same time, it is not possible to specify the scope of effectiveness of the data source, and to access different data sources according to different permissions or users to achieve fine-grained control. Summary of the invention
[0003] Purpose of the invention: The purpose of the present invention is to provide a method and system for implementing fine-grained control of dynamic data sources at runtime based on a microservice system, while being able to specify the scope of the data source.
[0004] Technical solution: A method for implementing fine-grained control of runtime dynamic data sources based on a microservice system of the present invention comprises the following steps:
[0005] Step 1: Build a microservice system, including the configuration center module, monitoring configuration module, adapter, application module, database access layer and database;
[0006] Step 2: Create four data sources in the database, namely master, slave1, slave2, and slave3;
[0007] Step 3: Start the configuration center program in the configuration center module and open the server registration port and external API port;
[0008] Step 4: Configure the configuration center registration address through the microservice system application module to connect to the configuration center program; configure the master data source connection address to implement the default database access of the application module; start the microservice application module and register the application module in the configuration center;
[0009] Step 5. Call the configuration center module API interface or enter a new data source configuration file on the console page to add four new data source information. Each data source information includes: database type, connection information, key value of the data source stored in the application module, scope includedMethod, and invalid scope excludedMethod.
[0010] Step 6: Call the microservice application module interface to query the currently registered data source set information, and return all the data source information dynamically added in step 5, indicating that the data source is dynamically added successfully;
[0011] Step 7: The interface accesses the database and returns the data of the master data source because the data source is not switched;
[0012] Step 8. Edit the configuration file for switching the data source, including the key value of the target data source to be switched. Change the key value to the key value slave1 corresponding to the data source returned in step 6.
[0013] Step 9. In the listening configuration module, the adapter listens to the updated content after creating or editing the configuration file in the configuration center module, distinguishes between creating a new data source and switching a data source according to the DataId and Group of the configuration file, and pushes it to the adapter through the Spring event mechanism;
[0014] Step 10: Access the interface of the business layer method name includedMethod to return the data of the slave1 database; access the interface of the business layer method name excludedMethod to return the data of the master primary data source;
[0015] Step 11. Continue to change the key value in the switch data source configuration file. In the database access layer, use the mapper method of the Mytbatis framework or use the annotation DataSourceInterface in the business service layer method to implement fine-grained data source access control for microservice applications as needed.
[0016] Furthermore, step 2 specifically includes the following steps:
[0017] Step 2.1, start the configuration center server, and register the microservice application as a client to the configuration center;
[0018] Step 2.2, agree on the configuration file attributes, and use DataId and Group to determine the uniqueness. For example, the DataId of a new data source configuration file is dynamic-add, and the Group is Panda_CONFIG. The DataId of a switch data source configuration file is dynamic-change, and the Group is Panda_CONFIG. The configuration file format is defined as json;
[0019] Step 2.3, the configuration center provides a console page, through which users can create or edit corresponding configuration files; at the same time, the configuration center can provide corresponding API interfaces to provide communication calling functions between the configuration center and other programs;
[0020] Step 2.4: After the user completes the configuration, the configuration center pushes the changed configuration to all clients that listen to the DataId and Group of this configuration file.
[0021] Furthermore, step 9 specifically includes the following steps:
[0022] Step 9.1, use the event notification mechanism of the Spring framework; define AddDataSourceEvent to inherit ApplicationEvent, call the publisthEvent method of applicationContext in the addListener method after listening to the configuration changes of the newly added data source, and send the AddDataSourceEvent event;
[0023] Step 9.2, define ChangeDataSourceEvent to inherit ApplicationEvent, call the publisthEvent method of applicationContext in the addListener method after listening to the change of switching data source configuration, and send the ChangeDataSourceEvent event;
[0024] Step 9.3, define AddDataSourceListener to implement the onApplicationEvent method of the ApplicationListener class to listen to the AddDataSourceEvent event and convert the data in the json format of the configuration file into an array of the DataSourceAddReq entity class to unify the input format for the subsequent application module to process the business logic of adding new data sources;
[0025] Step 9.4, define ChangeDataSourceListener to implement the onApplicationEvent method to listen to the ChangeDataSourceEvent event and convert the data in the json format of the configuration file into the DataSourceChangeReq entity class to unify the input format for the subsequent application module to process the business logic of switching data sources.
[0026] Furthermore, in step 11, using the annotation DataSourceInterface in the business service layer method specifically includes the following steps:
[0027] Step 11.1, obtain the point of intersection through ProceedingJoinPoint;
[0028] Step 11.2, determine whether the method has the DataSourceInterface annotation; if there is no DataSourceInterface annotation, it means that this method does not need to switch the data source, so the default data source is used, and the string master is set to the ThreadLocal in the DataSourceContextHolder to switch to the default data source; if there is a DataSourceInterface annotation, it is processed according to the value of the attribute name;
[0029] Step 11.3, the name value is the string dynamic, indicating that a dynamic data source is used. The data source mapped by hashMap1 can be found according to the dynamicDataSource value; at the same time, the DataSourceAddReq class stored in hashMap2 is found, and the attributes of this class, includeMethods and excludeMethods, are used to determine whether the scope of the data source includes the current method; the judgment principle is that includeMethods represents the included method name, * or missing represents all, excludeMethods represents the excluded method name, and includeMethods has a higher priority than excludeMethods; similarly, the database can be switched according to the class name;
[0030] Step 11.4, if the current method is included, set the value of the string variable dynamicDataSource to ThreadLocal in DataSourceContextHolder to indicate the data source key to be switched; realize dynamic switching of data sources;
[0031] Step 11.5: If the current method is not included, it means that the data source is not valid for the current method, and the default data source is used;
[0032] Step 11.6, the name value is the string paramDataSource, indicating the use of dynamic parameter data source, which means selecting the data source according to the input parameter value; obtain the method input parameter through ProceedingJoinPoint, the last parameter of the method represents the key of the data source, and set this key to ThreadLocal in DataSourceContextHolder;
[0033] Step 11.7: In all other cases, since there are no special requirements, the default data source is used.
[0034] The present invention also discloses a microservice system, the system comprising: a configuration center module for centrally managing and dynamically updating the data source configuration of microservice application programs;
[0035] The configuration monitoring module monitors the configuration changes of the configuration center, distinguishes the event type, and divides the type into adding a new data source or switching a data source, and passes the event to the next step for processing;
[0036] The adapter converts the data into the external interface format provided by the program application module and pushes it to the application module;
[0037] The application module receives adapter requests and handles adding new data sources or switching data source functions;
[0038] Database access layer and database, the database access layer provides application and database interaction functions.
[0039] Furthermore, the system provides a unified entrance to store the configuration information of each microservice application in a central location for centralized management and dynamic updating of data source configuration, and provides two modes of operation: API interface or console page to realize the functions of adding new data sources and switching data sources; after calling the configuration center API interface or entering the configuration on the console page, a configuration update event is sent to notify the listening end for processing.
[0040] Furthermore, the monitoring configuration module provides a monitoring configuration function, configures two monitoring events, respectively monitors the newly added data source and the switched data source events, monitors the data source configuration changes from the configuration center, and pushes the monitored content to the adapter module for processing.
[0041] Furthermore, the application module provides a dynamic proxy function to meet the needs of adding new data sources and switching data sources in a fine-grained manner at runtime, receiving and processing requests from the adapter, and the main methods include:
[0042] Process new data source requests, test the correctness of database connection according to the general entity class parameters, discard the request and record the exception if it is abnormal, and establish a database connection pool if it is correct, and put the connection pool into the storage container; store the scope of the database to distinguish whether this data source works globally, for a class, or for a method; at the same time, to avoid the situation where no data source is available for the application due to no active new data source, the data source configuration is obtained from the local configuration file when the microservice application is started to establish a default data source connection pool, which is used as the global application data source when no new data source is actively created;
[0043] Process the request to switch the data source. According to the data source identifier to be switched in the entity class parameters, find the target data source from the storage container. If there is no target data source, use the default data source and record the exception information.
[0044] Furthermore, the database access layer connects the microservice application and the database instance to provide application and database interaction functions; enhances the database access layer through dynamic proxy; uses the default data source connection if the data source is not switched; and when accessing the database again after switching the data source, determines whether the business layer method corresponding to the accessed SQL is within the scope of the data source. If so, the request is released; if not, the default data source connection is used and log information is recorded.
[0045] Beneficial effects: Compared with the prior art, the present invention has the following significant advantages:
[0046] (1) By changing the configuration of the configuration center, you can dynamically add or switch data sources for microservice applications. At the same time, you can specify the scope of the new data source, such as whether it is effective or not for the global, specified class name or method name, to achieve more fine-grained management of data sources. Based on this, you can dynamically add or switch data sources in a fine-grained manner according to different business needs to achieve application read-write separation, multi-tenancy, data sharding, database fault switching and other functions, greatly improving the flexibility, scalability and reliability of microservice applications in complex environments.
[0047] (2) Adding or switching data sources is performed while the program is running, which avoids restarting the application and achieves zero downtime, thus meeting the requirements of a continuously available production environment. Users will not experience service interruptions due to application restarts, providing a smoother user experience. At the same time, it can reduce the resource waste caused by restarting the application and avoid unstable factors that may be introduced by restarting the application, such as restart failure and resource leakage.
[0048] (3) When launching new features or migrating databases, part of the traffic can be dynamically switched to a new data source for A / B testing. At the same time, based on fine-grained (system level, class level, method level) dynamic data source control, the data source can be dynamically selected according to different business logics, different users, and different role permissions, providing more convenient and flexible control.
[0049] (4) Ease of operation. Whether adding a new data source or switching a data source at any granularity (system level, class level, method level), you only need to modify the configuration file in the configuration center, and the modification will take effect immediately. The system is non-invasive. The operations of adding and switching data sources are not directly related to the application, and there is no need to modify the application code, which reduces maintenance costs and error risks. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Figure 1 It is a schematic diagram of the overall structure of the module of the present invention;
[0051] Figure 2 Execution flow chart of an example of the present invention. DETAILED DESCRIPTION
[0052] The technical solution of the present invention is further described below in conjunction with the accompanying drawings.
[0053] The present invention aims to realize dynamic addition and switching of data sources at runtime for a microservice architecture, and can also specify the scope of the data source.
[0054] This is achieved through the following technical solutions:
[0055] Configuration center module:
[0056] 1. Start the configuration center server, and register the microservice application as a client to the configuration center;
[0057] 2. Agreed configuration file attributes, uniqueness is determined by DataId and Group. For example, the DataId of a new data source configuration file is dynamic-add, and the Group is Panda_CONFIG. The DataId of a switch data source configuration file is dynamic-change, and the Group is Panda_CONFIG. The configuration file format is defined as json;
[0058] 2.1. The format of the new data source configuration file is as follows:
[0059] {dataSourceList":[{"datasourceKey":"one","ip":"127.0.0.1","dataName":"dataName1","username":"root","password":"***","sqlType":"mysql","includeMethods:":"method1"},{"datasourceKey":"two","ip":"172.21.11.33","dataName":"dataName2","username":"root","password":"***","sqlType":"mysql","excludeMethods":"selectByOne"}]}; This means the data type, connection information, key of the data source stored in the application, and the scope of effectiveness or ineffectiveness of the newly added data source;
[0060] 2.2. The format of the configuration file for switching data sources is as follows:
[0061] {"datasourceKey":"one"}; means that the data source to be switched is stored in the key of the application (the key is defined when adding a new data source), so that the corresponding data source can be found and switched according to this key;
[0062] 3. The configuration center provides a console page, through which users can create or edit corresponding configuration files; at the same time, the configuration center can provide corresponding API interfaces to provide communication calling functions between the configuration center and other programs;
[0063] 4. After the user completes the configuration, the configuration center pushes the changed configuration to all clients that listen to the DataId and Group of this configuration file.
[0064] Monitoring module
[0065] The adapter implements the listening function through the addListener method of NacosConfigService, listens to the updated content after creating or editing the configuration file in the configuration center, distinguishes between new data sources and switched data sources based on the DataId and Group of the configuration file, and pushes them to the adapter module through Spring's event mechanism;
[0066] Adapter modules
[0067] The adapter provides the conversion and adaptation function of the configuration file content, converting the data in the json format of the configuration file into the DataSourceAddReq and DataSourceChangeReq entity classes. The method is as follows:
[0068] 1. Sending events
[0069] 1.1. Added new data source events
[0070] Use the event notification mechanism of the Spring framework. Define AddDataSourceEvent to inherit ApplicationEvent, call the publisthEvent method of applicationContext in the addListener method after listening to the configuration changes of the newly added data source, and send the AddDataSourceEvent event.
[0071] 1.2. Switching data source events
[0072] Define ChangeDataSourceEvent to inherit ApplicationEvent, call the publisthEvent method of applicationContext in the addListener method after listening to the change of switching data source configuration, and send the ChangeDataSourceEvent event.
[0073] 2. Listen for Spring events
[0074] 2.1、Define AddDataSourceListener to implement the onApplicationEvent method of the ApplicationListener class, which is used to listen to the AddDataSourceEvent event and convert the data in the json format of the configuration file into an array of DataSourceAddReq entity classes, so as to unify the input format for the subsequent application modules to process the business logic of adding new data sources;
[0075] 2.2. Define ChangeDataSourceListener to implement the onApplicationEvent method of the ApplicationListener class to listen to the ChangeDataSourceEvent event and convert the data in the json format of the configuration file into the DataSourceChangeReq entity class to unify the input format for the subsequent application module to process the business logic of switching data sources.
[0076] Application Module
[0077] The application module is used to process the requests for adding and switching data sources converted by the adapter module. It will establish a connection with the data source and save it in the memory container, and change the data source connection of the database access layer according to different conditions. The specific rules are as follows:
[0078] 1. Add new data source
[0079] Define the DynamicDataSourceFactory factory class to generate database data sources. Get the data source by using the DruidDataSourceFactory.createDataSource method from the received adapter module DataSourceAddReq entity class array, and use the dataSourceKey attribute of DataSourceAddReq as the key of the map, and store the data source as the value in the local cache HashMap. This HashMap is used to switch the data source by specifying the key in the future. For the convenience of subsequent reference, it is called hashMap1. At the same time, use the dataSourceKey attribute of DataSourceAddReq as the key of the map, and store the DataSourceAddReq entity class as the value in another HashMap. This HashMap is used to query the scope of this data source by key in the future, and is called hashMap2.
[0080] 2. Switch data source
[0081] Define a string type variable dynamicDataSource, assign the attribute datasourceKey in the DataSourceChangeReq entity class of the adapter module to dynamicDataSource, and use this variable to find the data source in hashMap1 in step 1. Find the corresponding data source by changing the value of dynamicDataSource.
[0082] 3. Dynamic Proxy
[0083] This function is the main implementation of fine-grained control of running data sources. It uses the AOP technology of the Spring framework to implement dynamic proxy. The implementation idea is to dynamically proxy the database access layer and automatically switch different data source connections according to preset values. The steps are as follows:
[0084] 3.1. Define the DataSourceContextHolder class, which contains ThreadLocal to access the key value corresponding to the data source that the current thread needs to use;
[0085] 3.2. Define the class DynamicDataSource to inherit AbstractRoutingDataSource to implement data source switching; AbstractRoutingDataSource has two important properties:
[0086] Private Map<Object,Object> targetDataSources;
[0087] private Object defaultTargetDataSource;
[0088] targetDataSources represents the target data source collection, storing the mapping relationship between key and data source; defaultTargetDataSource represents the default connection;
[0089] Based on this, we can think of how to access the specified data source by changing the values of targetDataSources and defaultTargetDataSource; determineCurrentLookupKey and determineTargetDataSource methods provide help; determineCurrentLookupKey means returning the key attribute corresponding to the data source. Here we return the value stored in ThreadLocal in DataSourceContextHolder, and return different keys by manually changing the value of ThreadLocal; determineTargetDataSource returns the data source actually needed, and finds the corresponding data source from the data source collection hashMap1 in DynamicDataSourceFactory in step 1 according to the value returned by determineCurrentLookupKey; if the corresponding data source cannot be found, the primary data source is used. The primary data source obtains the connection information from the configuration file and loads it when the application starts, and stores it in hashMap1. The key is master and the value is the primary data source dataSource, so as to avoid errors caused by not finding the data source information.
[0090] 3.3. Define the annotation DataSourceInterface and the annotation attribute name, with the default value of the string dynamic. Use Spring's AOP aspect technology to define the DataSourceAspect aspect class. The aspect point is the method using the annotation DataSourceInterface, and the aspect notification is the surround notification. The specific steps are as follows:
[0091] a. Method to obtain the tangent point through ProceedingJoinPoint;
[0092] b. Determine whether the method has the DataSourceInterface annotation;
[0093] c. If there is no DataSourceInterface annotation, it means that this method does not need to switch the data source, so the default data source is used and the string master is set to ThreadLocal in DataSourceContextHolder; according to the functional description of 3.2, the default data source can be switched;
[0094] d. If there is a DataSourceInterface annotation, it will be processed according to the value of the attribute name;
[0095] e. The value of name is the string dynamic, indicating that a dynamic data source is used. The data source mapped by hashMap1 can be found according to the dynamicDataSource value in step 2. At the same time, the DataSourceAddReq class stored in hashMap2 is found. According to the properties of this class, includeMethods and excludeMethods, it is judged whether the scope of the data source includes the current method. The judgment principle is that includeMethods represents the included method name, '*' or missing represents all, excludeMethods represents the excluded method name, and includeMethods has a higher priority than excludeMethods. Similarly, the database can be switched according to the class name.
[0096] f. If the current method is included, set the value of the string variable dynamicDataSource in step 2 to ThreadLocal in DataSourceContextHolder to indicate the data source key to be switched; according to the functional description of 3.2, dynamic switching of the data source can be achieved;
[0097] g. If the current method is not included, it means that the data source is not valid for the current method, and the default data source is used;
[0098] h. The name value is the string paramDataSource, indicating that a dynamic parameter data source is used, which means that the data source is selected according to the input parameter value; the method input parameter is obtained through ProceedingJoinPoint, and the last parameter of the method represents the key of the data source, which is set to the ThreadLocal in DataSourceContextHolder;
[0099] i. In all other cases, since there are no special requirements, the default data source is used.
[0100] Database access layer and database module
[0101] The framework used in the database access layer is Mybatis. The DataSourceInterface annotation is used in the Mytbatis mapper method or in the business service layer method. Through the above dynamic proxy implementation, fine-grained switching of the database data source can be completed.
[0102] The method flow described above generally includes two ports; one is the triggering endpoint, i.e. the configuration center, which is triggered by the user editing the configuration file to start the process; the other is the receiving endpoint, i.e. the microservice application, which receives the content from the configuration center and completes the dynamic addition and fine-grained dynamic switching of data sources through a series of processes.
[0103] Example
[0104] A method and system for fine-grained control of dynamic data sources at runtime based on a microservice system, as shown in the attached Figure 1 As shown, it includes a configuration center module, a monitoring module, an adapter module, an application module, a database access layer and a database. The configuration center service and the microservice application service are deployed and constructed on the computer system, and configuration management is performed through the configuration center to trigger the addition and switching of the microservice application data source.
[0105] Configuration center module, as attached Figure 2 The figure shows the starting point of the entire process. Users can modify the configuration files of adding a new data source and switching a data source in accordance with the agreed format through the web page or open API interface provided by the configuration center. The configuration file of adding a new data source stores the connection information of the data source, and the configuration file of switching a data source stores the mapping value of the data source;
[0106] The monitoring module is the bridge between the configuration center program and the microservice application program. Configure two monitoring events, namely, adding a new data source and switching a data source, to monitor changes in the configuration file from the configuration center and send the changes to the adapter;
[0107] The adapter module receives the request information from the monitoring module and checks whether the content format conforms to the agreed format. If it conforms, the content is converted into a general entity class to provide a unified entry parameter for the application module. If it does not conform, the current content is discarded and an abnormal log is recorded, which can be fed back to the designated personnel through warnings and messages.
[0108] The application module is used to process the request content of adding and switching data sources converted by the adapter module. Figure 2 As shown in the figure, after being processed by the adapter module, different requests are distinguished, and a connection with the newly added data source is established through a dynamic proxy and saved in a memory container, and the data source connection of the database access layer is changed according to the conditions. Dynamic addition of data sources and fine-grained dynamic switching are realized.
[0109] The database access layer and database, after the connection request sent to the database is enhanced by the dynamic proxy of the application module, access to different databases is implemented according to the rules. Different databases can be accessed according to the method name and class name to execute operation instructions in the target data source to obtain execution results.
[0110] The method of the present invention is described below by a specific embodiment.
[0111] A method and system for fine-grained control of runtime dynamic data sources based on a microservice system, comprising the following steps:
[0112] 1. Create multiple data sources in the MySQL database, namely master, slave1, slave2, and slave3;
[0113] 2. Start the configuration center program and open the server registration port and external API port;
[0114] 3. The microservice application configures the configuration center registration address for connecting to the configuration center program; configures the master data source connection address to implement the default database access for the application; starts the microservice application and registers the application in the configuration center;
[0115] 4. Call the configuration center API interface or enter a new data source configuration file on the console page to dynamically add four data source information, including: database type, IP address, port, account, password, corresponding key value (the four data sources in this instance are defined as master, slave1, slave2, and slave3), scope of application (this instance is defined as includedMethod to indicate that this method name is valid. If it is not defined or defined as *, it is valid for all methods), invalid scope (this instance is defined as excludedMethod to indicate that this method name is invalid);
[0116] 5. Call the microservice application program interface to query the currently registered data source collection information. Here, all the data sources dynamically added in step 4 can be returned, indicating that the data source has been dynamically added successfully;
[0117] 6. The interface accesses the database and returns the data of the master data source because the data source is not switched;
[0118] 7. Edit the configuration file for switching the data source, including the key value of the target data source to be switched. Change the key value to the key value slave1 corresponding to the data source returned in step 5.
[0119] 8. Access the interface of the business layer method name includedMethod to return the data of the slave1 database; access the interface of the business layer method name excludedMethod to return the data of the master primary data source;
[0120] 9. Through the above operations, continue to change the key value in the switch data source configuration file to implement fine-grained data source access control for microservice applications as needed.
[0121] It is to be understood that the above embodiments are merely exemplary embodiments used to illustrate the principles of the present application, but the present application is not limited thereto. For those skilled in the art, various modifications and improvements can be made without departing from the spirit and substance of the present application, and these modifications and improvements are also considered to be within the scope of protection of the present application.
Claims
1. A method for implementing fine-grained control of runtime dynamic data sources based on a microservice system, characterized in that: The steps include: Step 1: Build a microservice system, including the configuration center module, monitoring configuration module, adapter, application module, database access layer and database; Step 2: Create four data sources in the database, namely master, slave1, slave2, and slave3; Step 3: Start the configuration center program in the configuration center module and open the server registration port and external API port; Step 4: Configure the configuration center registration address through the microservice system application module to connect to the configuration center program; configure the master data source connection address to implement the default database access of the application module; Start the microservice application module and register the application module in the configuration center; Step 5. Call the configuration center module API interface or enter a new data source configuration file on the console page to add four new data source information. Each data source information includes: database type, connection information, key value of the data source stored in the application module, scope includedMethod, and invalid scope excludedMethod. Step 6: Call the microservice application module interface to query the currently registered data source set information, and return all the data source information dynamically added in step 5, indicating that the data source is dynamically added successfully; Step 7: The interface accesses the database and returns the data of the master data source because the data source is not switched; Step 8. Edit the configuration file for switching the data source, including the key value of the target data source to be switched. Change the key value to the key value slave1 corresponding to the data source returned in step 6. Step 9. In the listening configuration module, the adapter listens to the updated content after creating or editing the configuration file in the configuration center module, distinguishes between creating a new data source and switching a data source according to the DataId and Group of the configuration file, and pushes it to the adapter through the Spring event mechanism; Step 10: Access the interface of the business layer method name includedMethod to return the data of the slave1 database; access the interface of the business layer method name excludedMethod to return the data of the master primary data source; Step 11. Continue to change the key value in the switch data source configuration file. In the database access layer, use the mapper method of the Mytbatis framework or use the annotation DataSourceInterface in the business service layer method to implement fine-grained data source access control for microservice applications as needed.
2. According to claim 1, a method for implementing fine-grained control of dynamic data sources at runtime based on a microservice system is characterized in that: Step 2 specifically includes the following steps: Step 2.1, start the configuration center server, and register the microservice application as a client to the configuration center; Step 2.2, agree on the configuration file attributes, and use DataId and Group to determine the uniqueness. For example, the DataId of a new data source configuration file is dynamic-add, and the Group is Panda_CONFIG. The DataId of a switch data source configuration file is dynamic-change, and the Group is Panda_CONFIG. The configuration file format is defined as json; Step 2.3, the configuration center provides a console page, through which users can create or edit corresponding configuration files; at the same time, the configuration center can provide corresponding API interfaces to provide communication calling functions between the configuration center and other programs; Step 2.4: After the user completes the configuration, the configuration center pushes the changed configuration to all clients that listen to the DataId and Group of this configuration file.
3. According to claim 1, a method for implementing fine-grained control of dynamic data sources at runtime based on a microservice system is characterized in that: Step 9 specifically includes the following steps: Step 9.1, use the event notification mechanism of the Spring framework; define AddDataSourceEvent to inherit ApplicationEvent, call the publisthEvent method of applicationContext in the addListener method after listening to the configuration changes of the newly added data source, and send the AddDataSourceEvent event; Step 9.2, define ChangeDataSourceEvent to inherit ApplicationEvent, call the publisthEvent method of applicationContext in the addListener method after listening to the change of switching data source configuration, and send the ChangeDataSourceEvent event; Step 9.3, define AddDataSourceListener to implement the onApplicationEvent method of the ApplicationListener class to listen to the AddDataSourceEvent event and convert the data in the json format of the configuration file into an array of the DataSourceAddReq entity class to unify the input format for the subsequent application module to process the business logic of adding new data sources; Step 9.4, define ChangeDataSourceListener to implement the onApplicationEvent method to listen to the ChangeDataSourceEvent event and convert the data in the json format of the configuration file into the DataSourceChangeReq entity class to unify the input format for the subsequent application module to process the business logic of switching data sources.
4. According to claim 1, a method for implementing fine-grained control of dynamic data sources at runtime based on a microservice system is characterized in that: In step 11, the use of the annotation DataSourceInterface in the business service layer method specifically includes the following steps: Step 11.1, obtain the point of intersection through ProceedingJoinPoint; Step 11.2, determine whether the method has the DataSourceInterface annotation; if there is no DataSourceInterface annotation, it means that this method does not need to switch the data source, so the default data source is used, and the string master is set to the ThreadLocal in the DataSourceContextHolder to switch to the default data source; if there is a DataSourceInterface annotation, it is processed according to the value of the attribute name; Step 11.3, the name value is the string dynamic, indicating that a dynamic data source is used. The data source mapped by hashMap1 can be found according to the dynamicDataSource value; at the same time, the DataSourceAddReq class stored in hashMap2 is found, and the attributes of this class, includeMethods and excludeMethods, are used to determine whether the scope of the data source includes the current method; the judgment principle is that includeMethods represents the included method name, * or missing represents all, excludeMethods represents the excluded method name, and includeMethods has a higher priority than excludeMethods; similarly, the database can be switched according to the class name; Step 11.4, if the current method is included, set the value of the string variable dynamicDataSource to ThreadLocal in DataSourceContextHolder to indicate the data source key to be switched; Realize dynamic switching of data sources; Step 11.5: If the current method is not included, it means that the data source is not valid for the current method, and the default data source is used; Step 11.6, the name value is the string paramDataSource, indicating the use of dynamic parameter data source, which means selecting the data source according to the input parameter value; obtain the method input parameter through ProceedingJoinPoint, the last parameter of the method represents the key of the data source, and set this key to ThreadLocal in DataSourceContextHolder; Step 11.7: In all other cases, since there are no special requirements, the default data source is used.
5. A microservice system, characterized in that: To implement the method as claimed in claim 1, the system includes: a configuration center module for centrally managing and dynamically updating the data source configuration of the microservice application; The configuration monitoring module monitors the configuration changes of the configuration center, distinguishes the event type, and divides the type into adding a new data source or switching a data source, and passes the event to the next step for processing; The adapter converts the data into the external interface format provided by the program application module and pushes it to the application module; The application module receives adapter requests and handles adding new data sources or switching data source functions; Database access layer and database, the database access layer provides application and database interaction functions.
6. The system according to claim 5, characterized in that The system provides a unified entry to store the configuration information of each microservice application in a central location for centralized management and dynamic update of data source configuration. It provides two modes of operation: API interface or console page operation to realize the functions of adding new data sources and switching data sources. After calling the configuration center API interface or entering the configuration on the console page, a configuration update event is sent to notify the listening end for processing.
7. The system according to claim 5, characterized in that The monitoring configuration module provides a configuration monitoring function, configures two monitoring events, monitors the newly added data source and the switched data source events respectively, monitors the data source configuration changes from the configuration center, and pushes the monitored content to the adapter module for processing.
8. The system according to claim 5, characterized in that The application module provides a dynamic proxy function to meet the needs of adding new data sources and switching data sources in a fine-grained manner at runtime, receiving and processing requests from the adapter. The main methods include: Process new data source requests, test the correctness of database connection according to the general entity class parameters, discard the request and record the exception if it is abnormal, and establish a database connection pool if it is correct, and put the connection pool into the storage container; store the scope of the database to distinguish whether this data source works globally, for a class, or for a method; at the same time, to avoid the situation where no data source is available for the application due to no active new data source, the data source configuration is obtained from the local configuration file when the microservice application is started to establish a default data source connection pool, which is used as the global application data source when no new data source is actively created; Process the request to switch the data source. According to the data source identifier to be switched in the entity class parameters, find the target data source from the storage container. If there is no target data source, use the default data source and record the exception information.
9. The system according to claim 5, characterized in that The database access layer connects the microservice application and the database instance to provide the application and database interaction function; the database access layer is enhanced through dynamic proxy; if the data source is not switched, the default data source connection is used; When accessing the database again after switching the data source, determine whether the business layer method corresponding to the accessed SQL is within the scope of the data source. If it is, release the request. If not, use the default data source connection and record log information.