1. 程式人生 > >每天學點SpringCloud(八):使用Apollo做配置中心

每天學點SpringCloud(八):使用Apollo做配置中心

val override config 數據庫文件 ntc rgb fan lin 相對

由於Apollo支持的圖形化界面相對於我們更加的友好,所以此次我們使用Apollo來做配置中心

本篇文章實現了使用Apollo配置了dev和fat兩個環境下的屬性配置。
Apollo官方文檔https://github.com/ctripcorp/apollo/wiki

1.下載依賴

  1. 從https://github.com/ctripcorp/apollo/releases頁面下載最新版本的apollo-configservice-x.x.x-github.zip、apollo-adminservice-x.x.x-github.zip和apollo-portal-x.x.x-github.zip依賴包(需要×××。不能×××的同學建議使用第二種方式)。

  2. 從https://github.com/ctripcorp/apollo下載源碼後在本地構建。構建步驟為:

  1. 下載項目所需依賴

  2. 使用scripts文件夾下的build.bat或build.sh構建

  3. 分別拷貝出apollo-adminservice、apollo-configservice和apollo-portal三個文件夾下target/apollo-xxx-x.x.x-github.zip文件

2. 創建數據庫

  1. 從https://github.com/ctripcorp/apollo/tree/master/scripts/sql下載apolloconfigdb.sql和apolloportaldb.sql數據庫文件。

  2. 使用apolloportaldb.sql文件創建apolloportaldb數據庫,此數據庫是我們管理各種環境等的通用數據庫。

  3. 使用apolloconfigdb.sql文件分別創建apolloconfigdb_dev和apolloconfigdb_fat數據庫作為我們兩個環境的數據存儲。

3.配置數據庫連接信息

  1. 解壓第一步下載的三個壓縮文件

  2. apollo-portal-1.0.0-github

  1. 在apollo-portal-1.0.0-github/config下application-github.properties文件中配置 apolloportaldb數據庫的連接信息。

  2. 打開apollo-env.properties文件修改dev.mate和fat.mate屬性值為不同環境對 應的Eureka地址。例如在這裏我fat環境使用的本地,dev使用的是服務器地址

  3. 復制一份apollo-adminservice-1.0.0-github文件,分別重命名為apollo-adminservice-dev和apollo-adminservice-fat。

  4. 在apollo-adminservice-dev和apollo-adminservice-fat 的config文件夾下的application-github.properties文件中分別配置 apolloconfigdb_dev和apolloconfigdb_fat數據庫的連接信息。

  5. 按照3.4步驟復制apollo-configservice-1.0.0-github並分別配置數據連接地址

現在的數據庫連接信息如下所示:
技術分享圖片

4.啟動服務

  1. 使用apollo時portal只需要啟動一個來進行管理,在這裏我們暫時把它放在本地啟動。為了啟動方面,使用一個小的腳本

1
2
3
4
#!/bin/bash
sh apollo-portal-1.0.0-github/scripts/startup.sh
sh apollo-configservice-fat/scripts/startup.sh
sh apollo-adminservice-fat/scripts/startup.sh
  1. 將apollo-configservice-dev和apollo-adminservice-dev上傳到服務器,使用如下命令啟動

1
2
sh ./apollo-configservice-dev/scripts/startup.sh 
sh ./apollo-adminservice-dev/scripts/startup.sh
  1. 現在我們訪問http://localhost:8080/以及http://10.10.10.10:8080/可以看到以下信息就沒問題了
    技術分享圖片技術分享圖片

  2. 修改數據庫apolloconfigdb_dev和apolloconfigdb_fat中的ServerConfig表中的key為eureka.service.url的數據,將value分別置為http://10.10.10.10:8080/eureka/和http://localhost:8080/eureka/

5.測試

  1. 創建一個maven工程,引入apollo的相關依賴

1
2
3
4
5
6
<apollo.version>1.0.0</apollo.version>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${apollo.version}</version>
</dependency>
  1. 在application.yml中指定應用的id,以及apollo配置中心的地址

1
2
3
4
App:
Id: demo
apollo:
Meta: http://10.10.10.10:8080 #指定dev環境
  1. 創建ConfigRefresher類

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
@Service
public class ConfigRefresher implements ApplicationContextAware {
private ApplicationContext applicationContext;

@ApolloConfig
private Config config;

@PostConstruct
private void initialize() {
refresher(config.getPropertyNames());
}

@ApolloConfigChangeListener
private void onChange(ConfigChangeEvent changeEvent) {
refresher(changeEvent.changedKeys());
}

private void refresher(Set<String> changedKeys) {

for (String changedKey : changedKeys) {
System.out.println("this key is changed:"+changedKey);
}
this.applicationContext.publishEvent(new EnvironmentChangeEvent(changedKeys));

}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
  1. 創建啟動類並啟動

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableApolloConfig
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
  1. 修改配置文件中的 apollo.Meta為localhost:8080再次啟動

  2. 打開瀏覽器訪問 http://localhost:8070 Apollo默認的用戶名為 apollo,密碼為admin。登陸後點擊創建項目,項目的應用id和名稱填寫我們配置文件中的app.id。

  3. 進入項目可在dev和fat環境中分別發布不同的配置進行測試


每天學點SpringCloud(八):使用Apollo做配置中心