SpringBoot集成nacos動態(tài)刷新數(shù)據(jù)源的實現(xiàn)示例
前言
因為項目需要,需要在項目運行過程中能夠動態(tài)修改數(shù)據(jù)源(即:數(shù)據(jù)源的熱更新)。這里以com.alibaba.druid.pool.DruidDataSource數(shù)據(jù)源為例
第一步:重寫DruidAbstractDataSource類
這里為什么要重寫這個類:因為DruidDataSource數(shù)據(jù)源在初始化后,就不允許再重新設(shè)置數(shù)據(jù)庫的url和userName
public void setUrl(String jdbcUrl) { if (StringUtils.equals(this.jdbcUrl, jdbcUrl)) { return; } // 重寫的時候,需要將這個判斷注釋掉,否則會報錯 // if (inited) { // throw new UnsupportedOperationException(); // } if (jdbcUrl != null) { jdbcUrl = jdbcUrl.trim(); } this.jdbcUrl = jdbcUrl; // if (jdbcUrl.startsWith(ConfigFilter.URL_PREFIX)) { // this.filters.add(new ConfigFilter()); // } } public void setUsername(String username) { if (StringUtils.equals(this.username, username)) { return; } // 重寫的時候,需要將這個判斷注釋掉,否則會報錯 // if (inited) { // throw new UnsupportedOperationException(); // } this.username = username; }
重寫的時候包路徑不能變,只有這樣類加載的時候才會優(yōu)先加載重寫后的類
第二步:配置動態(tài)獲取nacos配置信息
package com.mp.demo.config; import com.alibaba.druid.pool.DruidDataSource; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Slf4j @Configuration @RefreshScope @Data public class DruidConfiguration { @Value("${spring.datasource.url}") private String dbUrl; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Bean @RefreshScope public DruidDataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); return datasource; } }
這里要注意增加@RefreshScope
注解
第三步:手動刷新數(shù)據(jù)源
@GetMapping("/refresh") public String refresh() throws SQLException { DruidDataSource master = SpringUtils.getBean("dataSource"); master.setUrl(druidConfiguration.getDbUrl()); master.setUsername(druidConfiguration.getUsername()); master.setPassword(druidConfiguration.getPassword()); master.setDriverClassName(druidConfiguration.getDriverClassName()); master.restart(); return userName + "<>" + jdbcUrl+"----------"+druidConfiguration.getDbUrl(); }
源碼地址:https://gitee.com/jackson_hou/RefreshDataSource.git
到此這篇關(guān)于SpringBoot集成nacos動態(tài)刷新數(shù)據(jù)源的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot nacos動態(tài)刷新數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用異或?qū)崿F(xiàn)變量互換和異或加密解密示例
這篇文章主要介紹了使用異或?qū)崿F(xiàn)變量互換和異或加密解密示例,需要的朋友可以參考下2014-02-02JAVA中 redisTemplate 和 jedis的配合使用操作
這篇文章主要介紹了JAVA中 redisTemplate 和 jedis的配合使用操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Mybatis工具類JdbcTypeInterceptor運行時自動添加jdbcType屬性
今天小編就為大家分享一篇關(guān)于Mybatis工具類JdbcTypeInterceptor運行時自動添加jdbcType屬性,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12如何使用spring?boot的程序主線程中異步訪問外部接口
CompletableFuture.supplyAsync提供了一種強大的工具,使您能夠以異步方式執(zhí)行操作,充分利用多核處理器和提高程序性能,同時保持代碼的清晰性和可維護性,本文給大家介紹使用spring?boot的程序主線程中異步訪問外部接口,感興趣的朋友一起看看吧2023-10-10java用靜態(tài)工廠代替構(gòu)造函數(shù)使用方法和優(yōu)缺點
這篇文章主要介紹了java用靜態(tài)工廠代替構(gòu)造函數(shù)使用方法和優(yōu)缺點,需要的朋友可以參考下2014-02-02