簡介
說起分布式肯定要想到分布式配置中心、分布式日志、分布式鏈路追蹤等
在分布式部署中業務往往有很多配置比如: 應用程序在啟動和運行時需要讀取一些配置信息,配置基本上伴隨著應用程序的整個生命周期,比如:數據庫連接參數、啟動參數等,都需要去維護和配置,但不可能一臺臺服務器登錄上去配置
今天我要跟大家分享一下分布式配置中心apollo:
apollo(阿波羅)是攜程框架部門研發的分布式配置中心,能夠集中化管理應用不同環境、不同集群的配置,配置修改后能夠實時推送到應用端,并且具備規范的權限、流程治理等特性,適用于微服務配置管理場景。
搭建
官方文檔中有兩種搭建方式一種是下載源代碼進行搭建,一種是使用docker或者k8s進行搭建,今天我們使用docker來進行搭建,畢竟docker對于開發者來說更友好一些。
如果已有mysql服務,推薦已有mysql服務或者云服務rds來當數據庫使用,畢竟數據無價。
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
|
version: "3" services: apollo-configservice: #config service提供配置的讀取、推送等功能,服務對象是apollo客戶端 image: apolloconfig/apollo-configservice:1.8.1 restart: always #container_name: apollo-configservice volumes: - ./logs/apollo-configservice:/opt/logs ports: - "8080:8080" environment: - tz= 'asia/shanghai' - server_port=8080 - eureka_instance_ip_address=xxx.xxx.xxx.xxx - eureka_instance_home_page_url=http://xxx.xxx.xxx.xxx:8080 - spring_datasource_url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/apolloconfigdb?characterencoding=utf8&servertimezone=asia/shanghai - spring_datasource_username=root - spring_datasource_password=mysqkpassword! apollo-adminservice: #admin service提供配置的修改、發布等功能,服務對象是apollo portal(管理界面) image: apolloconfig/apollo-adminservice:1.8.1 restart: always #container_name: apollo-adminservice volumes: - ./logs/apollo-adminservice:/opt/logs ports: - "8090:8090" depends_on: - apollo-configservice environment: - tz= 'asia/shanghai' - server_port=8090 - eureka_instance_ip_address=xxx.xxx.xxx.xxx - spring_datasource_url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/apolloconfigdb?characterencoding=utf8&servertimezone=asia/shanghai - spring_datasource_username=root - spring_datasource_password=mysqkpassword! apollo-portal: #管理界面 image: apolloconfig/apollo-portal:1.8.1 restart: always container_name: apollo-portal volumes: - ./logs/apollo-portal:/opt/logs ports: - "8070:8070" depends_on: - apollo-adminservice environment: - tz= 'asia/shanghai' - server_port=8070 - eureka_instance_ip_address=xxx.xxx.xxx.xxx - apollo_portal_envs=dev - dev_meta=http://xxx.xxx.xxx.xxx:8080 - spring_datasource_url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/apolloportaldb?characterencoding=utf8&servertimezone=asia/shanghai - spring_datasource_username=root - spring_datasource_password=mysqkpassword! |
從以上docker-compose.yaml中可以看出共包含3個服務,分別為:
- config service提供配置的讀取、推送等功能,服務對象是apollo客戶端
- admin service提供配置的修改、發布等功能,服務對象是apollo portal(管理界面)
- portal(管理界面)
如果想了解它們之間的運行方式推薦查看官方文檔
日志掛載到外部./logs目錄下
大家可以看到上方并沒有給出mysql的部署,如果需要使用容器部署mysql可以參照下方docker-compose.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
version: '3' services: mysql: # myslq 數據庫 image: 'mysql/mysql-server' container_name: 'mysql' restart: always command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --lower-case-table-names=1 environment: #環境變量 mysql_root_host: "%" mysql_root_password: password mysql_user: brook mysql_password: password ports: - "3306:3306" |
上述mysql的docker-compose.yaml 僅供測試使用
初始化數據庫
初始化 apolloconfigdb.sql 和 apolloportaldb.sql
數據庫初始化后,記得修改apolloconfigdb庫中serverconfig表的 eureka.service.url 否則 apollo-adminservice無法注冊到eureka
修改后切換到apollo docker-compose.yaml目錄 然后使用
docker-compose up -d #啟動文件中的三個服務并且后臺運行
查看啟動情況
docker-compose ps
默認用戶名:apollo
默認密碼:admin
創建一個測試項目
測試
創建一個.netcore項目 添加apollo.net client
添加apollo
配置apollo
配置如上
添加測試內容
代碼中獲取apollo
啟動程序 請求/weatherforecast/apollotest
發現并未獲取到apollo中設置的配置
檢查apollo發現配置的值并沒有發布
所以大家配置或者修改了apollo一定記得發布,我們發布后再次刷新瀏覽器
發現數據已經是新的數據了,我們再次修改一下apollo的value
刷新
致此 apollo已經搭建完畢并且可以正常使用了
代碼
示例中的代碼在
https://github.com/yuefengkai/brook.apollo
歡迎大家start
注意如果程序啟動后無法拉取配置,可以打開apollo的日志,在控制臺中可以看到詳細的配置 放到program.cs main函數第一行即可!
logmanager.useconsolelogging(com.ctrip.framework.apollo.logging.loglevel.trace);
參考
1.https://github.com/apolloconfig/apollo.net
2.https://github.com/apolloconfig/apollo
3.https://github.com/apolloconfig/apollo/tree/master/scripts/docker-quick-start
到此這篇關于docker compose 一鍵部署分布式配置中心apollo的文章就介紹到這了,更多相關docker compose部署分布式配置中心apollo內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/yuefengkai/archive/2021/09/13/15262523.html