Nacos multiple data source plug-in

Starting from version 2.2.0, Nacos can inject multiple data sources to implement plug-ins through the SPI mechanism. After the corresponding data source implementation is introduced, it can be read when Nacos starts.application.propertiesIn the configuration filespring.datasource.platform configuration item selects to load the corresponding multi-data source plug-in. This document details how to implement a multi-data source plug-in and how to make it effective.

Note: The multi-data source plug-in is currently in the Beta testing stage, and its API and interface method definitions may be significantly modified in subsequent version upgrades. Please pay attention to the applicable version of your plug-in.

Plug-in implementation

In the original Config module, all SQL operations are executed by directly using JdbcTemplate to execute fixed SQL statements, which makes SQL statements highly coupled with business logic, and only supports Derby and MySQL two data sources. The original Config module architecture as follows.

The current multi-data source plug-in uses the SPI mechanism to abstract SQL operations according to data tables and abstract multiple Mapper interfaces. The implementation class of the Mapper interface needs to write corresponding SQL dialect implementations according to different data sources; now the plug-in provides Derby and MySQL by default. Mapper implementation can be used directly; other data sources require users to use data source plug-ins to load. The architecture diagram after the transformation is as follows.

how to use

  1. Users can query whether the current Nacos supports the required data sources. Nacos provides Derby and MySQL implementations by default. If it is not supported yet, please refer to the following development steps for plug-in writers to develop plug-ins for your own use or contribution;
  2. Change to the corresponding data source name in theapplication.properties configuration file, and configure data source related parameters;spring.datasource.platform
  3. Then compile and run to support this data source;

How plugin writers develop

  1. Introductionnacos-datasource-pluginReliance
  2. Implementationcom.alibaba.nacos.plugin.datasource.mapperThe data table in the package corresponds to the special SQL method in the Mapper interface, which mainly involves dialect differences such as paging. Please refer to Derby and com.alibaba.nacos.plugin.datasource.impl To implement MySQL, you only need to implement the corresponding interface. The corresponding relationship between interfaces and tables is as follows:
Database Table Mapper
config_info_aggr ConfigInfoAggrMapper
config_info_beta ConfigInfoBetaMapper
config_info ConfigInfoMapper
config_info_tag ConfigInfoTagMapper
config_tags_relation ConfigTagsRelationMapper
his_config_info HistoryConfigInfoMapper
  1. Write an SPI configuration file with the name com.alibaba.nacos.plugin.datasource.mapper.Mapper and write a class that implements the Mapper interface. Please refer to the Derby and MySQL configuration files in the config module.
  2. Plug-in users can achieve the effect of corresponding data source operations by relying on this plug-in.
  3. Compile and run

How to compile

Before compiling the plug-in, you need to compile itnacos and install it into the local repository.

  1. git clone [email protected]:alibaba/nacos.git
  2. cd nacos && mvn -B clean package install -Dmaven.test.skip=true

Ifrevision the variable cannot be parsed, please updatemaven to the latest version

  1. git clone #{Corresponding data source plug-in implements Git address}
  2. mvn install

It is recommended to upload to the company's maven warehouse

future plans

Future version updates are as follows:

  •  Continue to subdivide SQL, and on the existing basis, while reducing SQL statements, it will be more friendly to the implementation of dynamic SQL;
  •  Extract the difference lists between different data sources and replace the difference lists through configuration files or configuration classes to facilitate plug-in writers to write plug-ins;

 

Original text from: nacos official website

Guess you like

Origin blog.csdn.net/leesinbad/article/details/134726101