開源配置中心 - Apollo
Apollo(阿波羅)是攜程框架部門研發(fā)的配置管理平臺,能夠集中化管理應用不同環(huán)境、不同集群的配置,配置修改后能夠?qū)崟r推送到應用端,并且具備規(guī)范的權(quán)限、流程治理等特性。服務端基于Spring Boot和Spring Cloud開發(fā),打包后可以直接運行,不需要額外安裝Tomcat等應用容器。
檢出代碼
可以fork下然后本地使用idea打開
數(shù)據(jù)庫腳本
執(zhí)行以下腳本創(chuàng)建ApolloConifgDB、ApolloPortalDB
- apollo.scripts.sql.apolloconfigdb.sql
- apollo.scripts.sql.apolloportaldb.sql
啟動configservice adminservice
Main class配置
1
|
com.ctrip.framework.apollo.assembly.ApolloApplication |
VM opions
1
2
3
4
5
6
|
-Dapollo_profile=github -Dspring.datasource.url=jdbc:mysql: //localhost:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password= Program arguments --configservice --adminservice |
啟動完后,打開 http://localhost:8080可以看到apollo-configservice和apollo-adminservice都已經(jīng)啟動完成并注冊到Eureka
啟動Apollo-Portal
Main class配置
1
2
3
4
5
6
7
|
com.ctrip.framework.apollo.portal.PortalApplication -Dapollo_profile=github,auth -Ddev_meta=http: //localhost:8080/ -Dserver.port= 8070 -Dspring.datasource.url=jdbc:mysql: //localhost:3306/ApolloPortalDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password= |
如果啟用了auth profile的話,默認的用戶名是apollo,密碼是admin
應用在SIT、UAT、生產(chǎn)環(huán)境機器上
1.新增目錄/opt/data/目錄,且有可讀寫權(quán)限;
2.新增文件:/opt/settings/server.properties 且加入配置:
1
2
3
4
|
env=DEV sit: env=FAT uat: env=UAT 生產(chǎn):env=PRO |
客戶端例子
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
|
@Component 設置組件名稱 @RefreshScope 指定配置改變可以刷新 @ConfigurationProperties (prefix = "redis.cache" ) @Component ( "sampleRedisConfig" ) @RefreshScope public class SampleRedisConfig { private static final Logger logger = LoggerFactory.getLogger(SampleRedisConfig. class ); private int expireSeconds; private String clusterNodes; private int commandTimeout; private Map<String, String> someMap = Maps.newLinkedHashMap(); private List<String> someList = Lists.newLinkedList(); @PostConstruct private void initialize() { logger.info( "SampleRedisConfig initialized - expireSeconds: {}, clusterNodes: {}, commandTimeout: {}, someMap: {}, someList: {}" , expireSeconds, clusterNodes, commandTimeout, someMap, someList); } public void setExpireSeconds( int expireSeconds) { this .expireSeconds = expireSeconds; } public void setClusterNodes(String clusterNodes) { this .clusterNodes = clusterNodes; } public void setCommandTimeout( int commandTimeout) { this .commandTimeout = commandTimeout; } public Map<String, String> getSomeMap() { return someMap; } public List<String> getSomeList() { return someList; } @Override public String toString() { return String.format( "[SampleRedisConfig] expireSeconds: %d, clusterNodes: %s, commandTimeout: %d, someMap: %s, someList: %s" , expireSeconds, clusterNodes, commandTimeout, someMap, someList); } } |
設置監(jiān)聽
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Component public class SpringBootApolloRefreshConfig { private static final Logger logger = LoggerFactory.getLogger(SpringBootApolloRefreshConfig. class ); @Autowired private ApolloRefreshConfig apolloRefreshConfig; @Autowired private SampleRedisConfig sampleRedisConfig; @Autowired private RefreshScope refreshScope; @ApolloConfigChangeListener public void onChange(ConfigChangeEvent changeEvent) { logger.info( "before refresh {}" , sampleRedisConfig.toString()); refreshScope.refresh( "sampleRedisConfig" ); logger.info( "after refresh {}" , sampleRedisConfig.toString()); } } |
總結(jié)
以上所述是小編給大家介紹的spring cloud 阿波羅 apollo 本地開發(fā)環(huán)境搭建過程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/qq_27384769/article/details/79123193