国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

SpringBoot集成nacos動態(tài)刷新數(shù)據(jù)源的實現(xiàn)示例

瀏覽:93日期:2023-04-04 09:31:30
前言

因為項目需要,需要在項目運行過程中能夠動態(tài)修改數(shù)據(jù)源(即:數(shù)據(jù)源的熱更新)。這里以com.alibaba.druid.pool.DruidDataSource數(shù)據(jù)源為例

第一步:重寫DruidAbstractDataSource類

這里為什么要重寫這個類:因為DruidDataSource數(shù)據(jù)源在初始化后,就不允許再重新設置數(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)先加載重寫后的類

SpringBoot集成nacos動態(tài)刷新數(shù)據(jù)源的實現(xiàn)示例

第二步:配置動態(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@Datapublic 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

到此這篇關于SpringBoot集成nacos動態(tài)刷新數(shù)據(jù)源的實現(xiàn)示例的文章就介紹到這了,更多相關SpringBoot nacos動態(tài)刷新數(shù)據(jù)源內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Spring
相關文章:
主站蜘蛛池模板: 温泉县| 浮山县| 黄平县| 内乡县| 南平市| 靖宇县| 南皮县| 稻城县| 龙泉市| 华安县| 济宁市| 桂平市| 中江县| 闻喜县| 成安县| 信丰县| 恩平市| 于都县| 楚雄市| 麻城市| 西盟| 柏乡县| 马公市| 绥阳县| 五大连池市| 越西县| 中山市| 灵寿县| 云南省| 舒城县| 兴隆县| 拜城县| 邵阳市| 衡南县| 江油市| 云林县| 庐江县| 辉县市| 江油市| 乡城县| 渝中区|