Apollo簡介
Apollo(阿波羅)是攜程框架部門研發的分佈式配置中心,能夠集中化管理應用不同環境、不同集羣的配置,配置修改後能夠實時推送到應用端,並且具備規範的權限、流程治理等特性,適用於微服務配置管理場景。
Apollo整體架構原理
一、Apollo(阿波羅)環境搭建
在此用的是centos6.7系統,IP:192.168.106.242
安裝需要mysql數據庫,建議5.7以上,jdk1.8
1、下載aploll配置中心 https://github.com/nobodyiam/apollo-build-scripts
下載完成後,上傳到linux機器上
解壓:
[root@common apollo]# unzip apollo-build-scripts-master.zip
2、新建mysql數據庫
Apollo服務端共需要兩個數據庫:ApolloPortalDB和ApolloConfigDB,新建這兩個庫,並從解壓後的apollo-build-scripts-master目錄中找到sql目錄,對sql文件導入到對應的庫中。
3、配置demo.sh
解壓後的apollo-build-scripts-master目錄中有一個demo.sh,編輯此文件,修改如下內容
前兩個紅框內是數據庫連接修改,寫成自己的數據地址、用戶名和密碼即可
最後一個紅框內是apollo的一些配置, 寫成部署apollo機器的ip:端口
修改完成後,啓動:
[root@common apollo-build-scripts-master]# ./demo.sh start
訪問:http://192.168.106.242:8080/
訪問:http://192.168.106.242:8070/
apollo的默認帳號:apollo 密碼:admin ,登錄後界面如下,點擊“創建項目”
輸入相應的內容並提交,加新增一個配置
點擊發布按鈕,變量發佈成功
二、Springboot 整合Apollo
在spingboot 框架在此不說,我用的是2.0.2版本
pom.xml加入以入依賴
<!-- apollo-client -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
<version>1.0.0</version>
</dependency>
<!-- springcloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
在application.yml 中加入配置
server:
port : 8081
spring:
application:
name: demo-01
eureka:
client:
service-url:
defaultZone: http://192.168.106.242:8080/eureka #部署apollo服務的ip
在resources 目錄下新建如下配置文件:
app.properties內容:
app.id=apollo_example
app.id就是在apollo界面中,創建項目時的id
apollo-env.properties內容,其中ip爲部署apollo服務的機器ip
local.meta=http://192.168.106.242:8080
dev.meta=http://192.168.106.242:8080
fat.meta=${fat.meta}
uat.meta=${uat.meta}
lpt.meta=${lpt.meta}
pro.meta=${pro.meta}
同時在C:\opt\settings(目錄自己建)下新建server.properties,寫入:env=DEV,我們在開發環境下測試
新建一個controller
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/apollo")
public class ApolloController {
//test爲默認值
@Value("${appolo_config:test}")
private String configStr;
@RequestMapping("/getConfig")
public String getConfig(){
return configStr;
}
}
在主啓動類上加上@EnableApolloConfig註解。
啓動springboot項目測試
“張三“ 就是我們在apollo中設置的內容
注:在阿里雲或百度雲服務器上部署apollo時,Eureka會用服務器的內網ip,導致我們的項目在連接apollo時,無法連接,打開你的ip:8080 eureka可以發現此問題的根源----Eureka是內網地址,我本地或公網無法訪問到, 怎麼辦???
不着急,apollo做爲一個成熟的框架,肯定不會這麼low,問題在於自己對apollo不是很精通,翻閱資料發現,加一點配置就可以了