PS:源碼已上傳Github, 歡迎指教。https://github.com/shileishmily/spring-cloud-x.git
一、介紹
Apollo(阿波羅)[參考附錄1]是攜程框架部研發並開源的一款生產級的配置中心產品,它能夠集中管理應用在不同環境、不同集群的配置,配置修改后能夠實時推送到應用端,並且具備規范的權限、流程治理等特性,適用於微服務配置管理場景。
Apollo目前在國內開發者社區比較熱,在Github上有超過5k顆星,在國內眾多互聯網公司有落地案例,可以說Apollo是目前配置中心產品領域Number1的產品,其成熟度和企業級特性要遠遠強於Spring Cloud體系中的Spring Cloud Config產品。
二、架構和模塊
下面是Apollo的七個模塊,其中四個模塊是和功能相關的核心模塊,另外三個模塊是輔助服務發現的模塊:
四個核心模塊及其主要功能
-
ConfigService
-
提供配置獲取接口
-
提供配置推送接口
-
服務於Apollo客戶端
-
AdminService
-
提供配置管理接口
-
提供配置修改發布接口
-
服務於管理界面Portal
-
Client
-
為應用獲取配置,支持實時更新
-
通過MetaServer獲取ConfigService的服務列表
-
使用客戶端軟負載SLB方式調用ConfigService
-
Portal
-
配置管理界面
-
通過MetaServer獲取AdminService的服務列表
-
使用客戶端軟負載SLB方式調用AdminService
三個輔助服務發現模塊
-
Eureka
-
用於服務發現和注冊
-
Config/AdminService注冊實例並定期報心跳
-
和ConfigService住在一起部署
-
MetaServer
-
Portal通過域名訪問MetaServer獲取AdminService的地址列表
-
Client通過域名訪問MetaServer獲取ConfigService的地址列表
-
相當於一個Eureka Proxy
-
邏輯角色,和ConfigService住在一起部署
-
NginxLB
-
和域名系統配合,協助Portal訪問MetaServer獲取AdminService地址列表
-
和域名系統配合,協助Client訪問MetaServer獲取ConfigService地址列表
-
和域名系統配合,協助用戶訪問Portal進行配置管理
更多關於apollo的介紹請參考:https://github.com/ctripcorp/apollo
三、Apollo配置中心搭建
apollo環境搭建搭建可以參考:https://ctripcorp.github.io/apollo/#/zh/deployment/quick-start,我是通過網盤鏈接下載的安裝包。但是下載安裝后,遇到一個問題,就是apollo自身集成了eureka注冊中心,而且eureka默認的啟動端口是8080。
但是我們之前已經搭建了自己獨立的eureka服務,http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/。
下面主要說一下如何讓apollo使用我們自己的注冊中心。在網上一番搜索后,發現相關帖子太多了,而且大家的版本號都不相同(我搭建的apollo版本號是v1.8.0),走了很多彎路。
一頓操作后,在官方的readme下發現了這么一句話:
#### 1. 配置Config Service不啟動內置Eureka Server ##### 1.1 1.5.0及以上版本 為apollo-configservice配置`apollo.eureka.server.enabled=false`即可,通過bootstrap.yml或-D參數等方式皆可。 ##### 1.2 1.5.0之前的版本 修改[com.ctrip.framework.apollo.configservice.ConfigServiceApplication](https://github.com/ctripcorp/apollo/blob/master/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/ConfigServiceApplication.java),把`@EnableEurekaServer`改為`@EnableEurekaClient` ```java @EnableEurekaClient @EnableAspectJAutoProxy @EnableAutoConfiguration // (exclude = EurekaClientConfigBean.class) @Configuration @EnableTransactionManagement @PropertySource(value = {"classpath:configservice.properties"}) @ComponentScan(basePackageClasses = {ApolloCommonConfig.class, ApolloBizConfig.class, ConfigServiceApplication.class, ApolloMetaServiceConfig.class}) public class ConfigServiceApplication { ... } ``` #### 2. 修改ApolloConfigDB.ServerConfig表中的`eureka.service.url`,指向自己的Eureka地址 比如自己的Eureka服務地址是1.1.1.1:8761和2.2.2.2:8761,那么就將ApolloConfigDB.ServerConfig表中設置eureka.service.url為: ``` http://1.1.1.1:8761/eureka/,http://2.2.2.2:8761/eureka/ ``` 需要注意的是更改Eureka地址只需要改ApolloConfigDB.ServerConfig表中的`eureka.service.url`即可,不需要修改meta server地址。 > 默認情況下,meta service和config service是部署在同一個JVM進程,所以meta service的地址就是config service的地址,修改Eureka地址時不需要修改meta server地址。
說白了就是如果安裝1.8.0版本的apollo,如果用獨立eureka服務,需要修改兩個地方
1、apollo.eureka.server.enabled改成false
2、ApolloConfigDB.ServerConfig表中的`eureka.service.url`改成http://localhost:8671/eureka/
3、打包命令:mvn clean package -pl apollo-assembly -am -DskipTests=true
4、打包之后將apollo-assembly-1.8.0.jar拷貝到D:\soft\apollo-quick-start-1.8.0目錄,然后重命名為apollo-all-in-one.jar
四、Apollo Client獲取配置演示Demo
4.1 新建一個名稱為spring-cloud-config-client的模塊
4.2 build.gradle依賴
dependencies { compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client") compile("com.ctrip.framework.apollo:apollo-client:1.8.0") }
4.3 創建啟動類
/** * @author Leo */ @SpringBootApplication @EnableApolloConfig public class ApolloConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ApolloConfigClientApplication.class, args); } }
4.4 新建測試Controller
/** * @author Leo */ @RestController public class ConfigClientController { @Value("${pay.url}") private String payUrl; @Value("${pay.name}") private String payName; @GetMapping("/getConfig") public String getConfig() { return "從apollo獲取配置:payUrl=" + payUrl + ", payName=" + payName; } }
4.5 配置中心截圖
4.6 依次啟動spring-cloud-x,apollo相關服務,spring-cloud-config-client
在瀏覽器輸入:http://localhost:18888/getConfig,返回:
4.7 修改配置中心pay.url為http://www.qrpay.com,同時觀察spring-cloud-config-client后台日志輸出
2021-03-08 11:26:42.510 INFO 2624 --- [Apollo-Config-2] c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: http://www.qrpay.com, key: pay.url, beanName: configClientController, field: com.x.demo.config.client.controller.ConfigClientController.payUrl
可以看到配置中心修改后(記得點發布),Client實時刷新了配置。
再次調用:http://localhost:18888/getConfig