一、開場白
在系統設計里我們有很多配置希望獨立於系統之外,而又能夠被系統實時讀取。但是在傳統的系統設計里,配置信息通常是耦合在系統內的,比如.net里通常會放在App.config或者web.config里,.net core則是appsettings.json里,這些都不夠靈活,如果在制度嚴格,不允許隨便登錄服務器的中大型公司里,每次的配置更改就意味着系統的發布,毫無疑問,肯定有帶哥要吐槽了,什么垃圾架構!為了解決這一問題,Apollo應運而生,專門用來構建微服務架構里的配置中心,在實際生產項目里為了達到高可用,我們會將其以分布式的方式部署。
粘貼下官方的下載和文檔:
官網:https://github.com/ctripcorp/apollo
Wiki:https://github.com/ctripcorp/apollo/wiki(一切的集成方式和使用方法都在這里)
Issues:https://github.com/ctripcorp/apollo/issues(如果期間有任何問題,請通過這里查找大部分解決方法)
二、Maven編譯源碼
首先要說明一下,官方提供的Quick Start以及預先打好的安裝包如果不進行特殊配置都只能單機搭建使用,通過所以如果你想把Apollo搭建在公有雲或者調用放和部署不在同一環境,最好自己編譯項目。
我這里使用阿里雲搭建Apollo,系統是CentOs 7。
1.首先我們需要在以下地址下載源碼:
https://github.com/ctripcorp/apollo/releases
2. 源碼下載完成后我們需要修改兩個地方,Apoolo是微服務架構,使用Eureka實現服務的注冊和發現,分布式部署的時候,apollo-configservice
和apollo-adminservice
需要把自己的IP和端口注冊到MetaServer(apollo-configservice本身)Server(apollo-configservice本身)。所以如果實際部署的機器有多塊網卡(如docker),或者存在某些網卡的IP是Apollo客戶端和Portal無法訪問的(如網絡安全限制),那么我們就需要在apollo-configservice
和apollo-adminservice
中做相關限制以避免Eureka將這些網卡的IP注冊到Meta Server。
我這里的解決方式是直接指定IP。通過修改apollo-adminservice或apollo-configservice 的bootstrap.yml文件,指定apollo-configservice和apollo-adminservice的IP端口。
解壓源碼文件,通過 apollo-1.3.0\apollo-adminservice\src\main\resources 找到 bootstrap.yml 文件,添加以下配置:
eureka: instance: ip-address: xx.xx.xx.xx hostname: ${hostname:47.99.92.76} preferIpAddress: true status-page-url-path: /info health-check-url-path: /health client: serviceUrl: # This setting will be overridden by eureka.service.url setting from ApolloConfigDB.ServerConfig or System Property # see com.ctrip.framework.apollo.biz.eureka.ApolloEurekaClientConfig defaultZone: http://${eureka.instance.hostname}:8080/eureka/ healthcheck: enabled: true eurekaServiceUrlPollIntervalSeconds: 60 management: health: status: order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP
ip-address是我阿里雲的公有IP,其它部署環境同理。
通過 apollo-1.3.0\apollo-configservice\src\main\resources 找到 bootstrap.yml 文件,配置同上。
同時我們還需要配置下MySQL數據庫的鏈接信息,通過 apollo-1.3.0\scripts 找到 build.sh 文件,編輯鏈接信息:
# apollo config db info apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 apollo_config_db_username=xxx apollo_config_db_password=xxx # apollo portal db info apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8 apollo_portal_db_username=xxx apollo_portal_db_password=xxx
因為我的MySQL就是部署在同一個阿里雲上,所以就使用localhost,大家酌情修改。
Apollo依賴於MySQL,我們需要先把其對應的數據庫跑出來,通過以下路徑加載兩個sql文件:
Configdb:apollo-1.3.0\scripts\db\migration\configdb
Portaldb:apollo-1.3.0\scripts\db\migration\portaldb
配置就到這里,接下來我們要安裝Maven環境來編譯源碼:
1.安裝maven
Windows環境通過本文安裝:https://blog.csdn.net/Snowprincev/article/details/81670037
2、解壓安裝
tar -zxvf apache-maven-3.3.9-bin.tar.gz
mv apache-maven-3.3.9 /usr/local/maven-3.3.9
3、配置環境變量
vi /etc/profile
#在適當的位置添加
export M2_HOME=/usr/local/maven3 (這里需要制定你的安裝目錄 自定義的哈)
export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
4、使配置生效
保存退出后運行下面的命令使配置生效,或者重啟服務器生效。
source /etc/profile
5、驗證版本
mvn -v
6.配置阿里雲倉庫,國內速度快
在maven的settings.xml 文件里配置mirrors的子節點,添加如下mirror:
<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
7.阿里雲cd到源碼文件夾路徑 apollo-1.3.0\scripts ,執行以下命令編譯源碼:
./build.sh
該腳本會依次打包apollo-configservice, apollo-adminservice, apollo-portal。
8.編譯完成后
獲取位於apollo-configservice/target/
目錄下的apollo-configservice-x.x.x-github.zip。解壓后執行scripts/startup.sh即可。如需停止服務,執行scripts/shutdown.sh.
獲取位於apollo-adminservice/target/
目錄下的apollo-adminservice-x.x.x-github.zip。解壓后執行scripts/startup.sh即可。如需停止服務,執行scripts/shutdown.sh.
獲取位於apollo-portal/target/
目錄下的apollo-portal-x.x.x-github.zip。解壓后執行scripts/startup.sh即可。如需停止服務,執行scripts/shutdown.sh.
OK,上面3個啟動后,我們就可以通過Ip:8070 訪問Apollo的UI界面的,通過默認的 賬戶密碼登錄:apollo/admin
通過IP:8080訪問Eureka的UI界面查看被注冊的configure和admin兩個進程。
二、新建.Net Core API項目試用
1.新建.net core api項目,通過nuget引入項目包:Com.Ctrip.Framework.Apollo.Configuration。
2.在Program.cs中添加如下代碼:
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, builder) => { builder .AddApollo(builder.Build().GetSection("apollo")) .AddDefault(); }) .UseStartup<Startup>(); }
3.appsettings.json中進行如下配置,我們這里試用默認自帶的SimpleApp測試:
{ "apollo": { "AppId": "SampleApp", "MetaServer": "http://47.99.92.76:8080", "Env": "Dev" } }
這里配置的意思是客戶端需要通過Eureka查詢Configure服務的地址,從而獲取配置信息。
private IConfiguration _configuration; public ValuesController(IConfiguration configuration) { _configuration = configuration; } [HttpGet] public ActionResult<IEnumerable<string>> Get() { string title = _configuration["timeout"]; return new string[] { "value1", "value2", title }; }
運行項目查看:
OK,運行成功,更多信息大家可以去官方gitHub查看哦,目前博主所在的公司已經使用Apollo很久了,而且.net 端一個分支的維護者還是博主公司架構組的一位同事,就是下面這個:
看下使用Apollo后的項目運行圖,服務配置數據可以正常請求:
今天就到這了,12點睡覺了!!