spring cloud 阿波罗 apollo 本地开发环境搭建过程
开源配置中心-Apollo
Apollo(阿波罗)是携程框架部门研发的配置管理平台,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。服务端基于SpringBoot和SpringCloud开发,打包后可以直接运行,不需要额外安装Tomcat等应用容器。
检出代码
apollogithub
可以fork下然后本地使用idea打开
数据库脚本
执行以下脚本创建ApolloConifgDB、ApolloPortalDB
- apollo.scripts.sql.apolloconfigdb.sql
- apollo.scripts.sql.apolloportaldb.sql
启动configserviceadminservice
Mainclass配置
com.ctrip.framework.apollo.assembly.ApolloApplication
VMopions
-Dapollo_profile=github -Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password= Programarguments --configservice--adminservice
启动完后,打开http://localhost:8080可以看到apollo-configservice和apollo-adminservice都已经启动完成并注册到Eureka
启动Apollo-Portal
Mainclass配置
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=
如果启用了authprofile的话,默认的用户名是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 publicclassSampleRedisConfig{ privatestaticfinalLoggerlogger=LoggerFactory.getLogger(SampleRedisConfig.class); privateintexpireSeconds; privateStringclusterNodes; privateintcommandTimeout; privateMapsomeMap=Maps.newLinkedHashMap(); privateList someList=Lists.newLinkedList(); @PostConstruct privatevoidinitialize(){ logger.info( "SampleRedisConfiginitialized-expireSeconds:{},clusterNodes:{},commandTimeout:{},someMap:{},someList:{}", expireSeconds,clusterNodes,commandTimeout,someMap,someList); } publicvoidsetExpireSeconds(intexpireSeconds){ this.expireSeconds=expireSeconds; } publicvoidsetClusterNodes(StringclusterNodes){ this.clusterNodes=clusterNodes; } publicvoidsetCommandTimeout(intcommandTimeout){ this.commandTimeout=commandTimeout; } publicMap getSomeMap(){ returnsomeMap; } publicList getSomeList(){ returnsomeList; } @Override publicStringtoString(){ returnString.format( "[SampleRedisConfig]expireSeconds:%d,clusterNodes:%s,commandTimeout:%d,someMap:%s,someList:%s", expireSeconds,clusterNodes,commandTimeout,someMap,someList); } }
设置监听
@Component publicclassSpringBootApolloRefreshConfig{ privatestaticfinalLoggerlogger=LoggerFactory.getLogger(SpringBootApolloRefreshConfig.class); @Autowired privateApolloRefreshConfigapolloRefreshConfig; @Autowired privateSampleRedisConfigsampleRedisConfig; @Autowired privateRefreshScoperefreshScope; @ApolloConfigChangeListener publicvoidonChange(ConfigChangeEventchangeEvent){ logger.info("beforerefresh{}",sampleRedisConfig.toString()); refreshScope.refresh("sampleRedisConfig"); logger.info("afterrefresh{}",sampleRedisConfig.toString()); } }
总结
以上所述是小编给大家介绍的springcloud阿波罗apollo本地开发环境搭建过程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!