spring cloud 阿波罗 apollo 本地开发环境搭建过程
程序员文章站
2023-12-02 19:48:58
开源配置中心 - apollo
apollo(阿波罗)是携程框架部门研发的配置管理平台,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具...
开源配置中心 - apollo
apollo(阿波罗)是携程框架部门研发的配置管理平台,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。服务端基于spring boot和spring cloud开发,打包后可以直接运行,不需要额外安装tomcat等应用容器。
检出代码
可以fork下然后本地使用idea打开
数据库脚本
执行以下脚本创建apolloconifgdb、apolloportaldb
- apollo.scripts.sql.apolloconfigdb.sql
- apollo.scripts.sql.apolloportaldb.sql
启动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
启动完后,打开 可以看到apollo-configservice和apollo-adminservice都已经启动完成并注册到eureka
启动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的话,默认的用户名是apollo,密码是admin
应用在sit、uat、生产环境机器上
1.新增目录/opt/data/目录,且有可读写权限;
2.新增文件:/opt/settings/server.properties 且加入配置:
env=dev sit: env=fat uat: env=uat 生产:env=pro
客户端例子
@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); } }
设置监听
@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()); } }
总结
以上所述是小编给大家介绍的spring cloud 阿波罗 apollo 本地开发环境搭建过程,希望对大家有所帮助