1 Apollo快速入门
1.1 执行流程
操作流程如下:
1、在Apollo配置中心修改配置
2、应用程序通过Apollo客户端从配置中心拉取配置信息
用户通过Apollo配置中心修改或发布配置后,会有两种机制来保证应用程序来获取最新配置:一种是Apollo配置中心会向客户端推送最新的配置;另外一种是Apollo客户端会定时从Apollo配置中心拉取最新的配置,通过以上两种机制共同来保证应用程序能及时获取到配置。
1.2 安装Apollo
1.2.1 运行时环境
Java
- Apollo服务端:1.8+
- Apollo客户端:1.7+
由于需要同时运行服务端和客户端,所以建议安装Java 1.8+。
MySQL
- 版本要求:5.6.5+
Apollo的表结构对timestamp
使用了多个default声明,所以需要5.6.5以上版本。
1.2.2 下载配置
-
访问Apollo的官方主页获取安装包(本次使用1.3版本):
https://github.com/ctripcorp/apollo/tags
-
打开1.3发布链接,下载必须的安装包:https://github.com/ctripcorp/apollo/releases/tag/v1.3.0
-
解压安装包后将apollo-configservice-1.3.0.jar, apollo-adminservice-1.3.0.jar, apollo-portal-1.3.0.jar放置于apollo目录下
1.2.3 创建数据库
Apollo服务端共需要两个数据库:ApolloPortalDB
和ApolloConfigDB
,ApolloPortalDB只需要在生产环境部署一个即可,而ApolloConfigDB需要在每个环境部署一套。
-
创建ApolloPortalDB,sql脚本下载地址:https://github.com/ctripcorp/apollo/blob/v1.3.0/scripts/db/migration/configdb/V1.0.0__initialization.sql
以MySQL原生客户端为例:
source apollo/ApolloPortalDB__initialization.sql
-
验证ApolloPortalDB
导入成功后,可以通过执行以下sql语句来验证:
select `Id`, `Key`, `Value`, `Comment` from `ApolloPortalDB`.`ServerConfig` limit 1;
注:ApolloPortalDB只需要在生产环境部署一个即可
-
创建ApolloConfigDB,sql脚本下载地址:https://github.com/ctripcorp/apollo/blob/v1.3.0/scripts/db/migration/configdb/V1.0.0__initialization.sql
以MySQL原生客户端为例:
source apollo/ApolloConfigDB__initialization.sql
-
验证ApolloConfigDB
导入成功后,可以通过执行以下sql语句来验证:
select `Id`, `Key`, `Value`, `Comment` from `ApolloConfigDB`.`ServerConfig` limit 1;
1.2.4 启动Apollo
-
确保端口未被占用
Apollo默认会启动3个服务,分别使用8070, 8080, 8090端口,请确保这3个端口当前没有被使用
-
启动apollo-configservice,在apollo目录下执行如下命令
可通过-Dserver.port=8080修改默认端口
java -Xms256m -Xmx256m -Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password=pbteach0430 -jar apollo-configservice-1.3.0.jar
-
启动apollo-adminservice
可通过-Dserver.port=8090修改默认端口
java -Xms256m -Xmx256m -Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password=pbteach0430 -jar apollo-adminservice-1.3.0.jar
-
启动apollo-portal
可通过-Dserver.port=8070修改默认端口
java -Xms256m -Xmx256m -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=pbteach0430 -jar apollo-portal-1.3.0.jar
-
也可以使用提供的runApollo.bat快速启动三个服务(修改数据库连接地址,数据库以及密码)
echo set url="localhost:3306" set username="root" set password="123" start "configService" java -Xms256m -Xmx256m -Dapollo_profile=github -Dspring.datasource.url=jdbc:mysql://%url%/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=%username% -Dspring.datasource.password=%password% -Dlogging.file=.\logs\apollo-configservice.log -jar .\apollo-configservice-1.3.0.jar start "adminService" java -Xms256m -Xmx256m -Dapollo_profile=github -Dspring.datasource.url=jdbc:mysql://%url%/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=%username% -Dspring.datasource.password=%password% -Dlogging.file=.\logs\apollo-adminservice.log -jar .\apollo-adminservice-1.3.0.jar start "ApolloPortal" java -Xms256m -Xmx256m -Dapollo_profile=github,auth -Ddev_meta=http://localhost:8080/ -Dserver.port=8070 -Dspring.datasource.url=jdbc:mysql://%url%/ApolloPortalDB?characterEncoding=utf8 -Dspring.datasource.username=%username% -Dspring.datasource.password=%password% -Dlogging.file=.\logs\apollo-portal.log -jar .\apollo-portal-1.3.0.jar
-
运行runApollo.bat即可启动Apollo
-
待启动成功后,访问管理页面 apollo/admin
1.3 代码实现
1.3.1 发布配置
-
打开apollo :新建项目apollo-quickstart
-
新建配置项sms.enable
确认提交配置项
-
发布配置项
1.3.2 应用读取配置
1、新建Maven工程
打开idea,新建apollo-quickstart项目
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9IV6hRaS-1572873470114)(image-20190919141052497.png)]
打开pom.xml文件添加apollo依赖,配置JDK为1.8
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pbteach</groupId>
<artifactId>apollo-quickstart</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、编写测试类GetConfigTest
新建com.pbteach.apollo.quickstart包,添加测试类GetConfigTest
添加如下代码读取sms.enable的值
package com.pbteach.apollo.quickstart;
public class GetConfigTest {
// VM options:
// -Dapp.id=apollo-quickstart -Denv=DEV -Ddev_meta=http://localhost:8080
public static void main(String[] args) {
Config config = ConfigService.getAppConfig();
String someKey = "sms.enable";
String value = config.getProperty(someKey, null);
System.out.println("sms.enable: " + value);
}
}
3、测试
配置VM options,设置系统属性:
-Dapp.id=apollo-quickstart -Denv=DEV -Ddev_meta=http://localhost:8080
运行GetConfigTest,打开控制台,观察输出结果
1.3.4 修改配置
-
修改sms.enable的值为false
-
再次运行GetConfigTest,可以看到输出结果已为false
1.3.5 热发布
-
修改代码为每3秒获取一次
public class GetConfigTest { public static void main(String[] args) throws InterruptedException { Config config = ConfigService.getAppConfig(); String someKey = "sms.enable"; while (true) { String value = config.getProperty(someKey, null); System.out.printf("now: %s, sms.enable: %s%n", LocalDateTime.now().toString(), value); Thread.sleep(3000L); } } }
-
运行GetConfigTest观察输出结果
-
在Apollo管理界面修改配置项
-
发布配置
-
在控制台查看详细情况:可以看到程序获取的sms.enable的值已由false变成了修改后的true