spring cloud 阿波羅 apollo 本地開(kāi)發(fā)環(huán)境搭建過(guò)程
開(kāi)源配置中心 - Apollo
Apollo(阿波羅)是攜程框架部門(mén)研發(fā)的配置管理平臺(tái),能夠集中化管理應(yīng)用不同環(huán)境、不同集群的配置,配置修改后能夠?qū)崟r(shí)推送到應(yīng)用端,并且具備規(guī)范的權(quán)限、流程治理等特性。服務(wù)端基于Spring Boot和Spring Cloud開(kāi)發(fā),打包后可以直接運(yùn)行,不需要額外安裝Tomcat等應(yīng)用容器。
檢出代碼
可以fork下然后本地使用idea打開(kāi)
數(shù)據(jù)庫(kù)腳本
執(zhí)行以下腳本創(chuàng)建ApolloConifgDB、ApolloPortalDB
- apollo.scripts.sql.apolloconfigdb.sql
- apollo.scripts.sql.apolloportaldb.sql
啟動(dòng)configservice adminservice
Main class配置
com.ctrip.framework.apollo.assembly.ApolloApplication
VM opions
-Dapollo_profile=github -Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password= Program arguments --configservice --adminservice
啟動(dòng)完后,打開(kāi) http://localhost:8080可以看到apollo-configservice和apollo-adminservice都已經(jīng)啟動(dòng)完成并注冊(cè)到Eureka
啟動(dòng)Apollo-Portal
Main class配置
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的話,默認(rèn)的用戶名是apollo,密碼是admin
應(yīng)用在SIT、UAT、生產(chǎn)環(huán)境機(jī)器上
1.新增目錄/opt/data/目錄,且有可讀寫(xiě)權(quán)限;
2.新增文件:/opt/settings/server.properties 且加入配置:
env=DEV sit: env=FAT uat: env=UAT 生產(chǎn):env=PRO
客戶端例子
@Component 設(shè)置組件名稱
@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);
}
}
設(shè)置監(jiān)聽(tīng)
@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 本地開(kāi)發(fā)環(huán)境搭建過(guò)程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
IDEA?2022?中的Lombok?使用基礎(chǔ)教程
? Lombok是使用java編寫(xiě)的一款開(kāi)源類庫(kù)。其主作用是使用注解來(lái)代替一些具有格式固定,沒(méi)有過(guò)多技術(shù)含量的編碼工作,這篇文章主要介紹了IDEA?2022?中的Lombok?使用基礎(chǔ)教程,需要的朋友可以參考下2022-12-12
Spring如何基于Proxy及cglib實(shí)現(xiàn)動(dòng)態(tài)代理
這篇文章主要介紹了Spring如何基于Proxy及cglib實(shí)現(xiàn)動(dòng)態(tài)代理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Java實(shí)現(xiàn)ATM機(jī)操作系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)ATM機(jī)操作系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Java中隨機(jī)數(shù)的產(chǎn)生方式與原理詳解
這篇文章主要介紹了Java中隨機(jī)數(shù)的產(chǎn)生方式與原理詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11
JavaWeb pageContext對(duì)象原理解析
這篇文章主要介紹了JavaWeb pageContext對(duì)象原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
java中@JsonFormat和@JSONField的使用方法詳解
這篇文章主要介紹了java中@JsonFormat和@JSONField使用的相關(guān)資料,@JsonFormat和@JSONField都是用于處理日期格式化的注解,但分別屬于不同的庫(kù)和框架,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12

