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

How to add and switch data sources dynamically by springboot+dynamicDataSource

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you how springboot+dynamicDataSource can dynamically add and switch data sources. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Springboot dynamicDataSource dynamically add switched data sources

There was a previous article about the method of switching data sources. Those who can configure several fixed data sources in yml to switch need to add, delete, change and check data sources in the database and then connect.

Forget about the previous configuration and check the https://www.yisu.com/article/233975.htm by yourself.

1. Modify the initially loaded data source map

The tagetData obtained in the previous upload directly reads multiple data sources in the yml. At this point, I wrote the dataTest method to call the master data to directly query a table in the database and load the contents of the table into data sources and put them into map.

two。 At the beginning of this time, the

The data information of a table is used as a data source.

3. But cannot operate when a new data source is found or the data source is modified

The service needs to be restarted. Later, it is found that there is a Map variable in DynamicDataSource to store the data source, which is loaded when its constructor is called.

As long as the map is modified, then the new or modified map can also take effect.

DynamicDataSource dynamically add and remove data source modelimport io.swagger.annotations.ApiModelProperty;import lombok.Data;import javax.validation.constraints.NotBlank;@Datapublic class DataSourceDTO {@ NotBlank @ ApiModelProperty (value = "connection pool name", example = "db1") private String poolName; @ NotBlank @ ApiModelProperty (value = "JDBC driver", example = "com.mysql.cj.jdbc.Driver") private String driverClassName @ NotBlank @ ApiModelProperty (value = "JDBC url address", example = "jdbc:mysql://x.x.x.x:3306/x?useUnicode=true&characterEncoding=utf-8") private String url; @ NotBlank @ ApiModelProperty (value = "JDBC username", example = "sa") private String username; @ ApiModelProperty (value = "JDBC password") private String password;} switch interface import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;import com.baomidou.dynamic.datasource.creator.* Import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;import com.lets.web.vo.common.DataSourceDTO;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.beans.BeanUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.*; import javax.sql.DataSource;import java.util.Set @ RestController@RequestMapping ("/ datasources") @ Api (tags = "add and remove data sources") public class DataSourceController {@ Autowired private DataSource dataSource; @ Autowired private DefaultDataSourceCreator dataSourceCreator; @ Autowired private DruidDataSourceCreator druidDataSourceCreator; @ Autowired private HikariDataSourceCreator hikariDataSourceCreator; @ GetMapping @ ApiOperation ("get all current data sources") public Set now () {DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource; return ds.getDataSources (). KeySet () } / / generic data sources are selected sequentially according to the connection pooling configured in maven. / / the default order is druid > hikaricp > beecp > dbcp > spring basic @ PostMapping ("/ add") @ ApiOperation ("Universal add data Source (recommended)") public Set add (@ Validated @ RequestBody DataSourceDTO dto) {DataSourceProperty dataSourceProperty = new DataSourceProperty (); BeanUtils.copyProperties (dto, dataSourceProperty); DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource; DataSource dataSource = dataSourceCreator.createDataSource (dataSourceProperty); ds.addDataSource (dto.getPoolName (), dataSource) Return ds.getDataSources (). KeySet ();} @ PostMapping ("/ addDruid") @ ApiOperation (basic Druid data Source) public Set addDruid (@ Validated @ RequestBody DataSourceDTO dto) {DataSourceProperty dataSourceProperty = new DataSourceProperty (); BeanUtils.copyProperties (dto, dataSourceProperty); dataSourceProperty.setLazy (true); DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource; DataSource dataSource = druidDataSourceCreator.createDataSource (dataSourceProperty) Ds.addDataSource (dto.getPoolName (), dataSource); return ds.getDataSources (). KeySet ();} @ PostMapping ("/ addHikariCP") @ ApiOperation ("basic HikariCP data Source") public Set addHikariCP (@ Validated @ RequestBody DataSourceDTO dto) {DataSourceProperty dataSourceProperty = new DataSourceProperty (); BeanUtils.copyProperties (dto, dataSourceProperty); dataSourceProperty.setLazy (true) / / if you have this attribute below version 3.4.0, you need to set it manually, otherwise the pointer will be null. DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource; DataSource dataSource = hikariDataSourceCreator.createDataSource (dataSourceProperty); ds.addDataSource (dto.getPoolName (), dataSource); return ds.getDataSources (). KeySet ();} @ DeleteMapping @ ApiOperation ("delete data source") public String remove (String name) {DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource; ds.removeDataSource (name); return "deleted successfully" }} above is all the content of the article "how to dynamically add and switch data sources in springboot+dynamicDataSource". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report