Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of loading SpringBoot-Mysql template from multiple data sources

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the method of loading multiple data sources of SpringBoot-Mysql template". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Brief introduction of SpringBoot-Mysql template Multi-data Source loading

Mysql multiple data source operations are commonly used in java projects. It is very convenient to use the original one in combination with springboot. However, you need to configure the configuration of multiple sets of data sources.

In microservices, configurations such as database connections are taken apart and read separately. It's equivalent to a template.

The following mysql:

Spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/$ {config.mysql.name}? useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=truespring.datasource.username=rootspring.datasource.password=rootspring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.driverClassName=com.mysql.jdbc.Driver

The connection configuration of middleware such as redis rabbitmq mongodb is separated by separate configuration so that the connection information such as ip can be easily modified later.

Of course, springboot needs to read the data configuration of the corresponding prefix when injecting multiple data sources.

Code:

@ Bean@ConfigurationProperties (prefix = "spring.datasource.onemysql") public DataSource originalDataSource (DataSourceFactory dataSourceFactory) {return DataSourceBuilder.create () .build ();}

Configuration:

Spring.datasource.onemysql.driver-class-name=com.mysql.jdbc.Driverspring.datasource.onemysql.url=jdbc:mysql://127.0.0.1:3306/$ {config.mysql.name}? useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=truespring.datasource.onemysql.username=rootspring.datasource.onemysql.password=rootspring.datasource.onemysql.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.onemysql.driverClassName=com.mysql.jdbc.Driver

Load the data source configuration that starts with onemysql in the above way. If there are multiple, configure multiple bean and configuration files accordingly.

According to the above method, we can use a mysql template to load and create the corresponding data source in a certain way. Only one mysql configuration template needs to be maintained under micro-service.

Feature use to add dependencies

Ps: please use the latest version of the actual version version

Click to view the latest version

Com.purgeteam mysql-datasource-spring-boot-starter 0.1.0.RELEASE1 configuration template

Let's configure the mysql data source configuration in the original way.

Config.mysql.name=userconfig.mysql.hosturl=127.0.0.1:3306# mysqlspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://$ {config.mysql.hosturl} / ${config.mysql.name}? useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=truespring.datasource.jdbc-url=$ {spring.datasource.url} spring.datasource.username=rootspring.datasource.password=rootspring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.filters=statspring.datasource. MaxActive=20spring.datasource.initialSize=1spring.datasource.maxWait=60000spring.datasource.minIdle=1spring.datasource.timeBetweenEvictionRunsMillis=60000spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=select 'x'spring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=falsespring.datasource.poolPreparedStatements=truespring.datasource.maxOpenPreparedStatements=20

Config.mysql.name is the name of the database in preparation for the following code injection.

2 database name combined with template configuration

You can inject a specified database through the DataSourceFactory#createDataSource method. The template is configured as the previous configuration, and the database name is replaced by ${config.mysql.name}.

/ * Database name replacement generation {@ link DataSource} * @ author purgeyao * @ since 1.0 * / @ Configurationpublic class OneDataSourceConfiguration {... / * DataSourceFactory#createDataSource loading one_mysql database * @ param dataSourceFactory dataSourceFactory * @ return {@ link DataSource} * / @ Primary @ Bean public DataSource oneDataSource (DataSourceFactory dataSourceFactory) {return dataSourceFactory.createDataSource ("one_mysql") }}

This allows you to load the database data source named one_mysql.

2 database information combined with configuration template

Of course, only the above method is only suitable for the case where the database address and password are consistent, the library name is inconsistent, and multiple data sources are injected.

The following ways support the use of templates to inject multiple data sources when the database information is inconsistent.

You need to configure mysql (1 configuration template) first, and then add the following configuration.

Configuration:

Config.datasource.mysql.source-info-map contains 4 pieces of information: host-url database address, name database name, username database user name, password database password.

The tow_mysql in config.datasource.mysql.source-info-map.tow_mysql is the key of the map collection.

# mysql-datasource-spring-boot-starterconfig.datasource.mysql.source-info-map.tow_mysql.host-url=127.0.0.1:3306config.datasource.mysql.source-info-map.tow_mysql.name=tow_mysqlconfig.datasource.mysql.source-info-map.tow_mysql.username=rootconfig.datasource.mysql.source-info-map.tow_mysql.password=root

Code:

First get the corresponding SourceInfoMap configuration in the DataSourceConfigProperties object.

You can create the DataSourceConfigProperties.SourceInfo towMysq configuration through the DataSourceFactory#createDataSource method.

/ * {@ link DataSourceConfigProperties} configuration generation {@ link DataSource} * * @ author purgeyao * @ since 1.0 * / @ Configurationpublic class TowDataSourceConfiguration {/ * DataSourceFactory#createDataSource loading tow_mysql database * * @ param dataSourceFactory dataSourceFactory * @ return {@ link DataSource} * / @ Bean public DataSource towDataSource (DataSourceFactory dataSourceFactory DataSourceConfigProperties properties) {DataSourceConfigProperties.SourceInfo towMysql = properties.getSourceInfoMap () .get ("tow_mysql") If (towMysql = = null) {throw new IllegalArgumentException ("corresponding configuration not obtained");} return dataSourceFactory.createDataSource (towMysql);}} 4 Annotation selector combined with template configuration (recommended)

The @ DataSourceSelector annotation can use the configuration template to generate the corresponding data source object.

DataSourceSelector#value configures key for the database (configuration of detail 2), and the other writing methods are the same as the original database creation method.

Just replace the original @ ConfigurationProperties (prefix = "spring.datasource.onemysql") with @ DataSourceSelector ("tow_mysql")

/ * Annotation injection {@ link DataSource} * * @ author purgeyao * @ since 1.0 * / @ Configurationpublic class AnnotationsDataSourceConfiguration {. / * {@ link DataSourceSelector} choose to inject {@ link DataSource} * * @ return {@ link DataSource} * / @ DataSourceSelector ("tow_mysql") public DataSource selectorDataSource () {return DataSourceBuilder.create () .build () }} Summary

@ DataSourceSelector ("tow_mysql") is recommended. It is basically the same as the previous method, and it is more simple and convenient.

After the separation of mysql configuration files, it is convenient for multiple services to use.

This is the end of the content of "multiple data source loading method of SpringBoot-Mysql template". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report