最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解spring cloud config實(shí)現(xiàn)datasource的熱部署

 更新時間:2018年01月16日 11:04:06   作者:牛奮lch  
這篇文章主要介紹了詳解spring cloud config實(shí)現(xiàn)datasource的熱部署,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

關(guān)于spring cloud config的基本使用,前面的博客中已經(jī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ù)源的自動配置的前綴 
  public DataSource dataSource(){ 
    return DataSourceBuilder.create().build(); 
  } 
} 

通過上面的幾個步驟,就可以實(shí)現(xiàn)在gitlab上修改配置文件,刷新后,服務(wù)器不用重啟,新的數(shù)據(jù)源就會生效。

2、自定義數(shù)據(jù)源的熱部署

當(dāng)我們使用spring boot集成druid,我們需要手動來配置數(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; 
 
/** 
 * 
 * 描述:如果不使用代碼手動初始化DataSource的話,監(jiān)控界面的SQL監(jiān)控會沒有數(shù)據(jù)("是spring boot的bug???") 
 * @author chhliu 
 * 創(chuàng)建時間: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ù)源的動態(tài)刷新。接下來,我們就來看看,spring cloud config是怎么來實(shí)現(xiàn)數(shù)據(jù)源的熱部署的。

從前面的博客中,我們不難發(fā)現(xiàn),要想實(shí)現(xiàn)動態(tài)刷新,關(guān)鍵點(diǎn)就在post refresh的請求上,那我們就從刷新配置文件開始。
當(dāng)我們post刷新請求的時候,這個請求會被actuator模塊攔截,這點(diǎn)從啟動的日志文件中就可以看出

復(fù)制代碼 代碼如下:

Mapped "{[/refresh || /refresh.json],methods=[POST]}" onto public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke() 

接下來,我們就來看actuator定義的EndPoint,然后我們就找到了RefreshEndpoint這個類,該類的源碼如下:

@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這個類上,由于這個類太長了,下面把這個類的部分源碼貼出來:

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、替換原來環(huán)境變量中的值 
    this.context.publishEvent(new EnvironmentChangeEvent(keys));// 4、發(fā)布變更事件, 
    this.scope.refreshAll(); 
    return keys; 
  } 

從上面的代碼不難看出,重點(diǎn)經(jīng)歷了4個步驟,上面代碼中已標(biāo)注。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java之生產(chǎn)故障定位Arthas問題

    java之生產(chǎn)故障定位Arthas問題

    這篇文章主要介紹了java之生產(chǎn)故障定位Arthas問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 一篇文章帶你入門Java變量

    一篇文章帶你入門Java變量

    這篇文章主要介紹了Java變量,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • MyBatis中動態(tài)sql的實(shí)現(xiàn)方法示例

    MyBatis中動態(tài)sql的實(shí)現(xiàn)方法示例

    這篇文章主要給大家介紹了關(guān)于MyBatis中動態(tài)sql的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法

    Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法

    腳本文件,通常放入/bin目錄下,編寫啟動腳本需要保證能夠識別到對應(yīng)的jar文件,其次需要保證能夠識別到/config中的配置文件信息,這篇文章主要介紹了Windows編寫jar啟動腳本和關(guān)閉腳本的操作方法,需要的朋友可以參考下
    2022-12-12
  • 一文搞懂Java中對象池的實(shí)現(xiàn)

    一文搞懂Java中對象池的實(shí)現(xiàn)

    池化并不是什么新鮮的技術(shù),它更像一種軟件設(shè)計(jì)模式,主要功能是緩存一組已經(jīng)初始化的對象,以供隨時可以使用。本文將為大家詳細(xì)講講Java中對象池的實(shí)現(xiàn),需要的可以參考一下
    2022-07-07
  • 基于Java實(shí)現(xiàn)簡單的身材計(jì)算程序

    基于Java實(shí)現(xiàn)簡單的身材計(jì)算程序

    這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)簡單的身材計(jì)算程序,可以計(jì)算身體的體脂率以及BMI數(shù)值等,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-12-12
  • 使用Spring自定義實(shí)現(xiàn)IOC和依賴注入(注解方式)

    使用Spring自定義實(shí)現(xiàn)IOC和依賴注入(注解方式)

    這篇文章主要介紹了使用Spring自定義實(shí)現(xiàn)IOC和依賴注入(注解方式),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringCloud?Nacos?+?Ribbon?調(diào)用服務(wù)的實(shí)現(xiàn)方式(兩種)

    SpringCloud?Nacos?+?Ribbon?調(diào)用服務(wù)的實(shí)現(xiàn)方式(兩種)

    這篇文章主要介紹了SpringCloud?Nacos?+?Ribbon?調(diào)用服務(wù)的兩種方法,分別是通過代碼的方式調(diào)用服務(wù)和通過注解方式調(diào)用服務(wù),每種方式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Java異常處理 如何跟蹤異常的傳播路徑

    Java異常處理 如何跟蹤異常的傳播路徑

    這篇文章主要介紹了Java異常處理 如何跟蹤異常的傳播路徑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • IDEA中l(wèi)og4j 無法輸出到本地 properties配置無效問題

    IDEA中l(wèi)og4j 無法輸出到本地 properties配置無效問題

    這篇文章主要介紹了IDEA中l(wèi)og4j 無法輸出到本地 properties配置無效問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10

最新評論

遂平县| 永新县| 崇文区| 陵川县| 洮南市| 宝清县| 城市| 威宁| 鄂托克前旗| 苏尼特右旗| 左云县| 关岭| 鄢陵县| 安义县| 晋江市| 会昌县| 尉犁县| 赣州市| 桦南县| 陆丰市| 清河县| 上犹县| 长寿区| 建德市| 盐池县| 永春县| 广饶县| 阿克苏市| 富源县| 石楼县| 泗阳县| 牡丹江市| 富源县| 罗平县| 钟山县| 乐至县| 新龙县| 道孚县| 利川市| 通许县| 遂昌县|