在现实的Coding事情中,我们是否会碰着这样的问题,我们本地开拓环境会用到一套配置参数和配置文件,测试环境会用到另一套配置参数和配置文件,然而项目打包发布时又会用到一套配置参数和配置文件。由于考虑到信息安全的主要性,生产环境用到的配置参数,例如:数据库IP,用户名和密码,以及第三方账号信息等,这些参数我们空想情形下仅限于有限的职员理解,我们该如何保护这些敏感数据呢?再比如我们每次发布生产环境,我们不想由于错改或者漏改了配置参数,而给我们的发布带来发布失落败或者灾害?这个时候我们或许会考虑利用一个统一的配置中央来管理我们项目中所利用的配置参数,并且有相应的权限管理。那么我以为Apollo该当是我们能够想到的办理方案的一种。
Apollo是携程利用的一种集中管理配置的一个很好的办理方案,它供应一个web界面,很友好的让我们管理我们的配置参数,并且供应配置回滚的特性,我们只需用在web界面用鼠标操作就可以将相应的配置参数回退到上一版本。
Apollo在Github的访问量

Apollo在GitHub开源,通过截图可以看出该项目的关注量和下载量。
下面我们通过在本地支配一个单机版的平台研究下Apollo是如何事情的,如果须要将该项目用于生产环境,则须要将该项目以分布式的办法支配成集群,以提高全体系统的高可用。详细参考官方文档:https://github.com/ctripcorp/apollo/wiki/%E5%88%86%E5%B8%83%E5%BC%8F%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97
一,确认Java版本,确保JDK版本在1.8以上
java -versionjava version "1.8.0_221"Java(TM) SE Runtime Environment (build 1.8.0_221-b11)Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
二,Apollo须要Mysql数据存储数据,确保Mysql数据库版本在5.6.5以上
SHOW VARIABLES WHERE Variable_name = 'version';
三,从GitHub下载Apollo安装包
git clone https://github.com/nobodyiam/apollo-build-scripts
四,创建数据库和表
cd apollo-build-scriptsmysql -h localhost -u root -p>输入数据库密码create database config_portal;use config_portal;source sql/apolloportaldb.sqlcreate database config;use config;source sql/apolloconfigdb.sql
五,验证数据库和表是否创建成功
select `Id`, `AppId`, `Name` from config_portal.App;select `NamespaceId`, `Key`, `Value`, `Comment` from config.Item;
查询结果
查询结果
六,在启动脚本中配置数据库连接信息
vi demo.sh# apollo config db infoapollo_config_db_url=jdbc:mysql://192.168.0.128:3306/config?characterEncoding=utf8apollo_config_db_username=rootapollo_config_db_password=root# apollo portal db infoapollo_portal_db_url=jdbc:mysql://192.168.0.128:3306/config_portal?characterEncoding=utf8apollo_portal_db_username=rootapollo_portal_db_password=root
七,实行启动脚本
./demo.sh startWindows new JAVA_HOME is: /c/tools/JDK-8U~1==== starting service ====Service logging file is ./service/apollo-service.logStarted [293]Waiting for config service startup.......Config service started. You may visit http://localhost:8080 for service status now!Waiting for admin service startup..Admin service started==== starting portal ====Portal logging file is ./portal/apollo-portal.logStarted [351]Waiting for portal startup....Portal started. You can visit http://localhost:8070 now!
涌现上述信息则解释这个Apollo启动成功。
八,浏览器地址栏输入:http://localhost:8070访问Apollo Web登录界面,默认账号是apollo/admin
演示结果
到此为止,Apollo单机版的平台已经支配完成。
网络图
下面我们创建一个SpringBoot项目,研究下SpringBoot如何与Apollo整合利用。
一,在STS中新建一个SpringBoot工程,工程构造如下图:
源代码构造
二,将Apollo客户真个依赖加入pom.xml中
maven依赖
三,须要在Apollo配置中央创建一个运用,运用名:my-app,运用ID:123test321,该运用ID必须在Apollo平台中唯一
演示结果
四,将运用ID配置到SpringBoot项目的resources/META-INF/app.properties
# testapp.id=123test321apollo.meta=http://localhost:8080env=DEV
五,配置SpringBoot主配置文件application.yml,开启Apollo
apollo: bootstrap: enabled: true # will inject 'application' and 'TEST1.apollo' namespaces in bootstrap phase namespaces: application
六,我们通过一个大略的Controller来验证是否能够从Apollo拿到最新的配置参数,同时我们通过配置参数来掌握Swagger的开启和关闭。
@Slf4j@RestController@RequestMapping("/apollo")public class ConfigController { @ApolloConfig private Config config; @GetMapping("/index/{key}") @ResponseBody public String index(@PathVariable("key") String key) { log.info("request key:{}", key); return config.getProperty(key, "defaultValue"); }}
@Configuration@EnableSwagger2public class Swagger2Config { @Value("${swagger.enable:false}") private boolean enableSwagger; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(enableSwagger).select() .apis(RequestHandlerSelectors.basePackage("com.demo.apollo.controller")).paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("SprintBoot Swagger2 build API docs") .description("Spring Boot Swagger2 Restful API").contact(new Contact("xiaobaoqiang", "", "")) .version("1.0").build(); }}
七,启动SpringBoot项目
演示结果
通过启动日志,我们可以看到SpringBoot已经创造了我们上面支配的Apollo配置做事,并且读取到SpringBoot启动所须要的配置参数,例如:端口号9002
curl -X GET "http://localhost:9002/apollo/index/aa" -H "accept: /"testcurl -X GET "http://localhost:9002/apollo/index/cc" -H "accept: /"defaultValue
通过以上实践,我们在本地支配了一套最小化的统一配置管理做事,通过Apollo配置中央,我们可以有效的保护敏感的配置参数,并且做到权限管理,担保了我们的数据安全。同时担保了项目在生产环境的安全,不会由于数据账号等关键信息透露,遭受恶意攻击。
参考:
https://github.com/ctripcorp/apollo
https://github.com/ctripcorp/apollo/wiki/Quick-Start
文章如有欠妥之处,欢迎示正!
感激!