Apolloä»ç»
Apolloï¼é¿æ³¢ç½ï¼æ¯æºç¨æ¡æ¶é¨é¨ç åçå¼æºé 置管çä¸å¿ï¼è½å¤éä¸å管çåºç¨ä¸åç¯å¢ãä¸åé群çé ç½®ï¼é 置修æ¹åè½å¤å®æ¶æ¨éå°åºç¨ç«¯ï¼å¹¶ä¸å ·å¤è§èçæéãæµç¨æ²»ççç¹æ§ã
Apolloæ¯æ4个维度管çKey-Valueæ ¼å¼çé ç½®ï¼
Apolloçä¼å¿
æ£æ¯åºäºé ç½®çç¹æ®æ§ï¼æ以Apolloä»è®¾è®¡ä¹åå°±ç«å¿äºæ为ä¸ä¸ªææ²»çè½åçé ç½®åå¸å¹³å°ï¼ç®åæä¾äºä»¥ä¸çç¹æ§ï¼
Apolloæä¾äºä¸ä¸ªç»ä¸çé¢éä¸å¼ç®¡çä¸åç¯å¢ï¼environmentï¼ãä¸åé群ï¼clusterï¼ãä¸åå½å空é´ï¼namespaceï¼çé ç½®ã
åä¸ä»½ä»£ç é¨ç½²å¨ä¸åçé群ï¼å¯ä»¥æä¸åçé ç½®ï¼æ¯å¦zookeeperçå°åç
éè¿å½å空é´ï¼namespaceï¼å¯ä»¥å¾æ¹ä¾¿å°æ¯æå¤ä¸ªä¸ååºç¨å ±äº«åä¸ä»½é ç½®ï¼åæ¶è¿å 许åºç¨å¯¹å ±äº«çé ç½®è¿è¡è¦ç
é 置修æ¹å®æ¶çæï¼çåå¸ï¼
æä¾å¼æ¾å¹³å°API
Apolloçä¼å¿è¿å¯ä»¥éè¿ä¸Spring Cloud Config对æ¯æ¥ä½ç°åº
Apolloçæ ¸å¿æ¦å¿µä»ç»
Namespace
Namespaceå°±ç¸å½äºspring boot项ç®ä¸çé ç½®æ件ï¼ä¾å¦namespaceä¸æé»è®¤çapplication,æ ¼å¼ä¸ºpropertiesï¼å对åºçapplication.propertiesã
1 Namespaceçæ ¼å¼
é ç½®æ件æå¤ç§æ ¼å¼ï¼ä¾å¦ï¼propertiesãxmlãymlãyamlãjsonçãåæ ·Namespaceä¹å ·æè¿äºæ ¼å¼ã
2 Namespaceçè·åæéåç±»
Namespaceçè·åæéå为两ç§ï¼
private ï¼ç§æçï¼
public ï¼å
Œ
񇋬
è¿éçè·åæéæ¯ç¸å¯¹äºApollo客æ·ç«¯æ¥è¯´çã
2.1 privateæé
privateæéçNamespaceï¼åªè½è¢«æå±çåºç¨è·åå°ãä¸ä¸ªåºç¨å°è¯è·åå ¶å®åºç¨privateçNamespaceï¼Apolloä¼æ¥â404âå¼å¸¸ã
2.2 publicæé
publicæéçNamespaceï¼è½è¢«ä»»ä½åºç¨è·åã
3. Namespaceçç±»å
Namespaceç±»åæä¸ç§ï¼
3.1 ç§æç±»å
ç§æç±»åçNamespaceå ·æprivateæéãä¾å¦ä¸ææå°çâapplicationâ Namespaceå°±æ¯ç§æç±»åã
3.2 å
Œ
±ç±»å
3.2.1 å«ä¹
å
Œ
±ç±»åçNamespaceå
·æpublicæéãå
Œ
±ç±»åçNamespaceç¸å½äºæ¸¸ç¦»äºåºç¨ä¹å¤çé
ç½®ï¼ä¸éè¿Namespaceçå称å»æ è¯å
Œ
±Namespaceï¼æ以å
Œ
±çNamespaceçå称å¿
é¡»å
¨å±å¯ä¸ã
3.2.2 使ç¨åºæ¯
3.3 å ³èç±»å
3.3.1 å«ä¹
å ³èç±»ååå¯ç§°ä¸ºç»§æ¿ç±»åï¼å ³èç±»åå ·æprivateæéãå ³èç±»åçNamespace继æ¿äºå ¬å ±ç±»åçNamespaceï¼ç¨äºè¦çå ¬å ±Namespaceçæäºé ç½®ãä¾å¦å ¬å ±çNamespaceæ两个é 置项
k1 = v1
k2 = v2
ç¶ååºç¨Aæä¸ä¸ªå ³èç±»åçNamespaceå ³èäºæ¤å ¬å ±Namespaceï¼ä¸è¦çäºé 置项k1ï¼æ°å¼ä¸ºv3ãé£ä¹å¨åºç¨Aå®é è¿è¡æ¶ï¼è·åå°çå ¬å ±Namespaceçé 置为ï¼
k1 = v3
k2 = v2
Apolloæå¡ç«¯ï¼æµè¯å®¢æ·ç«¯ç¨ï¼
1 åå¤å·¥ä½
apolloæå¡ç«¯è¦æ±1.8+
apollo客æ·ç«¯è¦æ±1.7+
1.2 MySQL
çæ¬è¦æ±5.6.5+
1.3 ä¸è½½Quick Startå®è£ å
ä»Githubä¸è½½
2. å®è£ æ¥éª¤
2.1 å建æ°æ®åº
Apolloæå¡ç«¯å ±éè¦ä¸¤ä¸ªæ°æ®åºï¼ ApolloPortalDB å ApolloConfigDB ï¼æ°æ®åºã表çå建åæ ·ä¾æ°æ®é½åå«åå¤äºsqlæ件ï¼åªéè¦å¯¼å ¥æ°æ®åºå³å¯ã
éªè¯æ¹æ³ï¼
ApolloPortalDBï¼
select `Id`, `AppId`, `Name` from ApolloPortalDB.App;
ApolloConfigDBï¼
select `NamespaceId`, `Key`, `Value`, `Comment` from ApolloConfigDB.Item;
å¦æé½ææ¥è¯¢åºç»æé£è¡¨ç¤ºæ°æ®åºå建æåã
2.2 é
ç½®æ°æ®åºè¿æ¥ä¿¡æ¯
Apolloæå¡ç«¯éè¦ç¥éå¦ä½è¿æ¥å°ä½ åé¢å建çæ°æ®åºï¼æ以éè¦ç¼è¾demo.shï¼ä¿®æ¹ApolloPortalDBåApolloConfigDBç¸å
³çæ°æ®åºè¿æ¥ä¸²ä¿¡æ¯ã
注æï¼å¡«å
¥çç¨æ·éè¦å
·å¤å¯¹ApolloPortalDBåApolloConfigDBæ°æ®ç读åæéã
# apollo config db info
apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=ç¨æ·å
apollo_config_db_password=å¯ç ï¼å¦æ没æå¯ç ï¼ç空å³å¯ï¼
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=ç¨æ·å
apollo_portal_db_password=å¯ç ï¼å¦æ没æå¯ç ï¼ç空å³å¯ï¼
3 å¯å¨Apolloé
ç½®ä¸å¿
Apolloçquick startèæ¬ä¼å¨æ¬å°å¯å¨ä¸ä¸ªç«¯å£åå«ä¸º8070ã8080ã8090çæå¡ï¼éå¨ç¡®ä¿ç«¯å£æªè¢«å ç¨çæ
åµä¸æ§è¡èæ¬
./demo.sh start
eg:å
³éæå¡çå½ä»¤ï¼
./demo.sh stop
å½çå°å¦ä¸è¾åºåï¼å°±è¯´æå¯å¨æåäºï¼
==== starting service ====
Service logging file is ./service/apollo-service.log
Started [10768]
Waiting for config service startup.......
Config service started. You may visit http://localhost:8080 for service status now!
Waiting for admin service startup....
Admin service started
==== starting portal ====
Portal logging file is ./portal/apollo-portal.log
Started [10846]
Waiting for portal startup......
Portal started. You can visit http://localhost:8070 now!
å¦æå¯å¨éå°äºå¼å¸¸ï¼å¯ä»¥åå«æ¥çserviceåportalç®å½ä¸çlogæ件ææ¥é®é¢ã
注ï¼å¨å¯å¨apollo-configserviceçè¿ç¨ä¸ä¼å¨æ¥å¿ä¸è¾åºeureka注å失败çä¿¡æ¯ï¼å¦com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refusedãéè¦æ³¨æçæ¯ï¼è¿ä¸ªæ¯é¢æçæ
åµï¼å 为apollo-configserviceéè¦åMeta Serverï¼å®èªå·±ï¼æ³¨åæå¡ï¼ä½æ¯å 为å¨å¯å¨è¿ç¨ä¸ï¼èªå·±è¿æ²¡èµ·æ¥ï¼æ以ä¼æ¥è¿ä¸ªéãåé¢ä¼è¿è¡éè¯çå¨ä½ï¼æ以çèªå·±æå¡èµ·æ¥åå°±ä¼æ³¨åæ£å¸¸äºã
æå¡ç«¯ä½¿ç¨
éè¿è®¿é®http://localhost:8070/å¯è®¿é®web页é¢çæå¡ç«¯ã
åå§è´¦å·/å¯ç 为ï¼admin/apollo
å
·ä½ä½¿ç¨æåå¯åèå®ç½
该æå¡ç«¯åªæ¯ç¨æ¥åæ¥ä½éªåæµè¯ä½¿ç¨ã
Apolloç客æ·ç«¯
Apolloæ¯æåºäºSpring BootãåºäºJavaãåºäºXMLççå¤ç§æ¹å¼çé
ç½®ï¼å
·ä½è¯¦æ
å¯åèå®ç½
Apolloä¼å¨æ¬å°åä¸ä»½ç¼åé
ç½®æ件
Mac/Linuxä¸æ件ä½ç½®ä¸º/opt/data/{appId}/config-cache
Windowsä¸æ件ä½ç½®ä¸ºC:\opt\data{appId}\config-cache
æ¬å°é
ç½®æ件ä¼ä»¥{appId}+{cluster}+{namespace}.propertiesçæ件ååæ ¼å¼åå¨ï¼å½è·åä¸å°ç½ç»é
ç½®æ¶ä¼ä»è¿ä¸ªé
ç½®ä¸åå¼
ä¸é¢æå°±ä»ç»æ常ç¨ãç®ä¾¿ãå¿«æ·ãéç¨çæ¹å¼ï¼å³åºäºSpring Bootçéææ¹å¼ã
1 é¦å
å å
¥ä¾èµ
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
2 å¨æå¡åºç¨çé
ç½®ä¸å å
¥ä»¥ä¸é
ç½®ï¼æ¬ä¾åä¸çæ件为application.ymlï¼ï¼
id: test-app
apollo:
meta: http://localhost:8080
#cluster: myCluster
#cacheDir: /opt/testDir
bootstrap:
enable: true
namespaces: application
env: DEV
app.id为å¨æå¡ç«¯web页é¢ä¸å建ç项ç®APPçid
apollo.meta为apolloæå¡ç«¯çå°å
apollo.cluster为è¦é
ç½®çé群å称ï¼å¦æ没æå¯ä¸å¡«
apollo.cacheDir为èªå®ä¹ç¼åè·¯å¾
apollo.bootstrap.enable为æ¯å¦å¨å·¥ç¨å¯å¨çbootstrapé¶æ®µåSpring容å¨æ³¨å
¥è¢«æ管çnamespacesä¸çé
ç½®
apollo.bootstrap.namespaces为è¦ä½¿ç¨çnamespacesï¼å¯å¨web页é¢ä¸çå°
env为è¦è·åçé
ç½®ç¯å¢ï¼ç®åæ¯æ以ä¸å 个å¼ï¼å¤§å°åä¸ææï¼ï¼
DEV(Development environment)
FAT(Feature Acceptance Test environment)
UAT(User Acceptance Test environment)
PRO(Production environment)
å¨å¯å¨Spring Bootçjavaæ件ä¸æ·»å 注解ï¼
@EnableApolloConfig
Apollo客æ·ç«¯ç使ç¨
éè¿@Value注解å³å¯è·åå°å¼
å¯ä»¥ä»¥ç±»çæ¹å¼ä½ä¸ºbeanå è½½å
¥Springï¼ä¾å¦
@EnableApolloConfig
@Configuration
Class A {
@Bean
public getB() {
return new B();
Class B {
private int x;
public void setX(int x) {
this.x = x;
public int getX() {
return this.x;