一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解spring cloud config實現datasource的熱部署

詳解spring cloud config實現datasource的熱部署

2021-03-22 14:48牛奮lch Java教程

這篇文章主要介紹了詳解spring cloud config實現datasource的熱部署,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

關于spring cloud config的基本使用,前面的博客中已經說過了,如果不了解的話,請先看以前的博客

spring cloud config整合gitlab搭建分布式的配置中心

spring cloud config分布式配置中心的高可用

今天,我們的重點是如何實現數據源的熱部署。

1、在客戶端配置數據源

?
1
2
3
4
5
6
7
8
9
10
11
@RefreshScope
@Configuration// 配置數據源
public class DataSourceConfigure {
 
  @Bean
  @RefreshScope// 刷新配置文件
  @ConfigurationProperties(prefix="spring.datasource") // 數據源的自動配置的前綴
  public DataSource dataSource(){
    return DataSourceBuilder.create().build();
  }
}

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

2、自定義數據源的熱部署

當我們使用spring boot集成druid,我們需要手動來配置數據源,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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的話,監控界面的SQL監控會沒有數據("是spring boot的bug???")
 * @author chhliu
 * 創建時間: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實例
  @Primary //在同樣的DataSource中,首先使用被標注的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;
  }
}

通過上面的示例,也可以實現數據源的動態刷新。接下來,我們就來看看,spring cloud config是怎么來實現數據源的熱部署的。

從前面的博客中,我們不難發現,要想實現動態刷新,關鍵點就在post refresh的請求上,那我們就從刷新配置文件開始。
當我們post刷新請求的時候,這個請求會被actuator模塊攔截,這點從啟動的日志文件中就可以看出

 

復制代碼 代碼如下:

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

 

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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());
  
}

從上面的源碼,我們可以看到,重點在ContextRefresher這個類上,由于這個類太長了,下面把這個類的部分源碼貼出來:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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、將配置文件加載到環境中
    Set<String> keys = changes(before,
        extract(this.context.getEnvironment().getPropertySources())).keySet();// 3、替換原來環境變量中的值
    this.context.publishEvent(new EnvironmentChangeEvent(keys));// 4、發布變更事件,
    this.scope.refreshAll();
    return keys;
  }

從上面的代碼不難看出,重點經歷了4個步驟,上面代碼中已標注。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/liuchuanhong1/article/details/75446850

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: jiuse在线| 国产成人啪精品午夜在线观看 | 60岁了天天要小伙子 | 国产在线一区二区杨幂 | 干露露视频 性感写真 | 亚洲黄色免费在线观看 | ipx358cn出差被男上司在线 | 俄罗斯处女摘花 | 婷婷综合七月激情啪啪 | 亚洲精品人成网在线播放影院 | 青春草在线观看精品免费视频 | 成年人在线视频免费观看 | 24adc年龄18岁欢迎大驾光临 | 国产精品反差婊在线观看 | 欧洲男同直粗无套播放视频 | 423hk四虎 | 图片专区小说专区卡通动漫 | 贰佰麻豆剧果冻传媒一二三区 | 精品国产成人a区在线观看 精品高潮呻吟99AV无码视频 | 肉文小说 | 日本www色视频成人免费 | 四虎在线最新永久免费 | 热九九精品 | 亚洲一二三区久久五月天婷婷 | 亚洲欧美日韩中文字幕网址 | 欧美亚洲视频在线观看 | 黑帮少爷爱上我第8集在线观看 | 拔插拔插8x8x海外华人免费视频 | 小小水蜜桃视频高清在线观看免费 | 2020韩国三级理论在线观看 | 草久久网| 激情图片 激情小说 | 99热er | 99re在线视频观看 | 跪在老师脚下吃丝袜脚 | ssni-497新任美脚女教师 | 青草视频在线观看免费网站 | 国产视频91在线 | 男同巨黄gay小说好爽 | 黑人chinese女人 | 精品一区视频 |