Apollo(阿波罗)是携程框架部门研发的配置管理平台,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性。服务端基于Spring Boot和Spring Cloud开发,打包后可以直接运行,不需要额外安装Tomcat等应用容器。
apollo github
执行以下脚本创建ApolloConifgDB、ApolloPortalDB
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
启动完后,打开 http://localhost:8080可以看到apollo-configservice和apollo-adminservice都已经启动完成并注册到Eureka
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=
env=DEV sit: env=FAT uat: env=UAT 生产:env=PRO
@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()); } }