詳解spring cloud config實(shí)現(xiàn)datasource的熱部署
關(guān)于spring cloud config的基本使用,前面的博客中已經(jīng)說過了,如果不了解的話,請(qǐng)先看以前的博客
spring cloud config整合gitlab搭建分布式的配置中心
spring cloud config分布式配置中心的高可用
今天,我們的重點(diǎn)是如何實(shí)現(xiàn)數(shù)據(jù)源的熱部署。
1、在客戶端配置數(shù)據(jù)源
@RefreshScope
@Configuration// 配置數(shù)據(jù)源
public class DataSourceConfigure {
@Bean
@RefreshScope// 刷新配置文件
@ConfigurationProperties(prefix="spring.datasource") // 數(shù)據(jù)源的自動(dòng)配置的前綴
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
}
通過上面的幾個(gè)步驟,就可以實(shí)現(xiàn)在gitlab上修改配置文件,刷新后,服務(wù)器不用重啟,新的數(shù)據(jù)源就會(huì)生效。
2、自定義數(shù)據(jù)源的熱部署
當(dāng)我們使用spring boot集成druid,我們需要手動(dòng)來(lái)配置數(shù)據(jù)源,代碼如下:
package com.chhliu.springcloud.config;
import java.sql.SQLException;
import javax.sql.DataSource;
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;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
/**
*
* 描述:如果不使用代碼手動(dòng)初始化DataSource的話,監(jiān)控界面的SQL監(jiān)控會(huì)沒有數(shù)據(jù)("是spring boot的bug???")
* @author chhliu
* 創(chuàng)建時(shí)間:2017年2月9日 下午7:33:08
* @version 1.2.0
*/
@Slf4j
@Configuration
@RefreshScope
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.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
private String filters;
@Value("${spring.datasource.connectionProperties}")
private String connectionProperties;
@Value("${spring.datasource.useGlobalDataSourceStat}")
private boolean useGlobalDataSourceStat;
@Bean //聲明其為Bean實(shí)例
@Primary //在同樣的DataSource中,首先使用被標(biāo)注的DataSource
@RefreshScope
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
//configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
log.error("druid configuration initialization filter: "+ e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}
通過上面的示例,也可以實(shí)現(xiàn)數(shù)據(jù)源的動(dòng)態(tài)刷新。接下來(lái),我們就來(lái)看看,spring cloud config是怎么來(lái)實(shí)現(xiàn)數(shù)據(jù)源的熱部署的。
從前面的博客中,我們不難發(fā)現(xiàn),要想實(shí)現(xiàn)動(dòng)態(tài)刷新,關(guān)鍵點(diǎn)就在post refresh的請(qǐng)求上,那我們就從刷新配置文件開始。
當(dāng)我們post刷新請(qǐng)求的時(shí)候,這個(gè)請(qǐng)求會(huì)被actuator模塊攔截,這點(diǎn)從啟動(dòng)的日志文件中就可以看出
Mapped "{[/refresh || /refresh.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()
接下來(lái),我們就來(lái)看actuator定義的EndPoint,然后我們就找到了RefreshEndpoint這個(gè)類,該類的源碼如下:
@ConfigurationProperties(prefix = "endpoints.refresh", ignoreUnknownFields = false)
@ManagedResource
public class RefreshEndpoint extends AbstractEndpoint<Collection<String>> {
private ContextRefresher contextRefresher;
public RefreshEndpoint(ContextRefresher contextRefresher) {
super("refresh");
this.contextRefresher = contextRefresher;
}
@ManagedOperation
public String[] refresh() {
Set<String> keys = contextRefresher.refresh();
return keys.toArray(new String[keys.size()]);
}
@Override
public Collection<String> invoke() {
return Arrays.asList(refresh());
}
}
從上面的源碼,我們可以看到,重點(diǎn)在ContextRefresher這個(gè)類上,由于這個(gè)類太長(zhǎng)了,下面把這個(gè)類的部分源碼貼出來(lái):
private RefreshScope scope;
public ContextRefresher(ConfigurableApplicationContext context, RefreshScope scope) {
this.context = context;
this.scope = scope;
}
public synchronized Set<String> refresh() {
Map<String, Object> before = extract(
this.context.getEnvironment().getPropertySources());// 1、before,加載提取配置文件
addConfigFilesToEnvironment();// 2、將配置文件加載到環(huán)境中
Set<String> keys = changes(before,
extract(this.context.getEnvironment().getPropertySources())).keySet();// 3、替換原來(lái)環(huán)境變量中的值
this.context.publishEvent(new EnvironmentChangeEvent(keys));// 4、發(fā)布變更事件,
this.scope.refreshAll();
return keys;
}
從上面的代碼不難看出,重點(diǎn)經(jīng)歷了4個(gè)步驟,上面代碼中已標(biāo)注。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
通過FeignClient調(diào)用微服務(wù)提供的分頁(yè)對(duì)象IPage報(bào)錯(cuò)的解決
這篇文章主要介紹了通過FeignClient調(diào)用微服務(wù)提供的分頁(yè)對(duì)象IPage報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL揭秘
這篇文章主要給大家介紹了關(guān)于Mybatis學(xué)習(xí)筆記之動(dòng)態(tài)SQL的相關(guān)資料,小編覺得挺不錯(cuò)的,對(duì)大家學(xué)習(xí)或者使用Mybatis會(huì)有一定的幫助,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-11-11
jpa?EntityManager?復(fù)雜查詢實(shí)例
這篇文章主要介紹了jpa?EntityManager?復(fù)雜查詢實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
從JVM的內(nèi)存管理角度分析Java的GC垃圾回收機(jī)制
這篇文章主要介紹了從JVM的內(nèi)存管理角度分析Java的GC垃圾回收機(jī)制,帶有GC是Java語(yǔ)言的重要特性之一,需要的朋友可以參考下2015-11-11
Java 隊(duì)列實(shí)現(xiàn)原理及簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要介紹了Java 隊(duì)列實(shí)現(xiàn)原理及簡(jiǎn)單實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10
Java實(shí)現(xiàn)酒店客房管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)酒店客房管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Spring Security在標(biāo)準(zhǔn)登錄表單中添加一個(gè)額外的字段
這篇文章主要介紹了Spring Security在標(biāo)準(zhǔn)登錄表單中添加一個(gè)額外的字段,我們將重點(diǎn)關(guān)注兩種不同的方法,以展示框架的多功能性以及我們可以使用它的靈活方式。 需要的朋友可以參考下2019-05-05
基于Java實(shí)現(xiàn)收發(fā)電子郵件功能
Email就是電子郵件,我們平常使用的QQ郵箱,網(wǎng)易郵箱,F(xiàn)oxmail都是用來(lái)收發(fā)郵件的,利用Java程序也可以完成收發(fā)電子郵件的功能,本文就來(lái)為大家詳細(xì)講講實(shí)現(xiàn)步驟2022-07-07

