Springboot + DruidDataSource 实现不重启项目加载修改后的数据源

背景

由于公司项目需求,研究了一下如何在项目不重启的情况下,修改了数据源配置后能正常使用新的数据源配置。在百度查找解决方案时找到了动态修改数据源和动态刷新配置两种方式,但是都不适合我的实际场景。百度找不到解决方案,自己尝试解析springboot自动加载配置数据源的源码,依然没有找到。最后无意间看DruidDataSource 的源码时发现里面有个restart方法就可以实现,只需要监听到配置有改动过,刷新数据源配置,执行restart方法即可实现不重启项目加载最新的数据源配置,闲话说到这,接下来实际操作。

Spring动态修改数据源和动态刷新配置

  1. 使用spring 动态修改数据源需要继承AbstractRoutingDataSource类,实现determineCurrentLookupKey方法就能动态的修改数据源,具体的实现流程感兴趣的朋友可以自行去了解下
  2. 不重启项目动态刷新配置我们需要spring-boot-starter-actuator提供刷新接口,spring-cloud-starter-config提供动态监测注解,具体实现流程可以参考链接:https://www.jianshu.com/p/230af40377cf
    由于公司项目使用的是Apollo配置中心,已经提供了监听配置修改的方式

引入maven依赖

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>io.shardingsphere</groupId>
			<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
			<version>3.0.0.M3</version>
		</dependency>

		<!-- 阿里的druid 依赖log4j -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.0</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

		<!-- 引入mybatis-spring -->
		<dependency>
			<groupId>org.mybatis
您可以通过创建一个配置类来配置springboot+DruidDataSource+shardingsphere的数据库配置,而是将配置信息放在yml文件中。以下是一个示例配置类的代码: ```java @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DruidDataSourceBuilder.create().build(); } @Bean public DataSourceProxy dataSourceProxy(DataSource dataSource) { return new DataSourceProxy(dataSource); } @Bean public DataSourceRule dataSourceRule(DataSourceProxy dataSourceProxy) { DataSourceRule dataSourceRule = new DataSourceRule(dataSourceProxy); // 配置分库分表规则 // dataSourceRule.addDataSourceRule(...) // dataSourceRule.addTableRule(...) return dataSourceRule; } @Bean public ShardingRule shardingRule(DataSourceRule dataSourceRule) { ShardingRule shardingRule = ShardingRule.builder() .dataSourceRule(dataSourceRule) // 配置分片规则 // shardingRule.getTableRuleConfigs().add(...) .build(); return shardingRule; } @Bean public DataSource dataSource(ShardingRule shardingRule) throws SQLException { return ShardingDataSourceFactory.createDataSource(shardingRule); } @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); // 配置其他属性 // sessionFactory.setMapperLocations(...) return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 您可以根据自己的需求在配置类中添加适当的配置,比如配置数据源、分库分表规则、分片规则等。这样,您就可以将数据库配置信息集中在一个配置类中,而是放在yml文件中。 #### 引用[.reference_title] - *1* *2* [springboot+druid连接池+mybatisplus+shardingsphere5.0alpha实现分库分表实战案例](https://blog.youkuaiyun.com/qq_40726812/article/details/120594172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Springboot+mybatis+shardingsphere 分库分表](https://blog.youkuaiyun.com/l_ian/article/details/120547449)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值