由于Apollo支持的图形化界面相对于我们更加的友好,所以此次我们使用Apollo来做配置中心

本篇文章实现了使用Apollo配置了dev和fat两个环境下的属性配置。
Apollo官方文档https://github.com/ctripcorp/apollo/wiki
<https://github.com/ctripcorp/apollo/wiki>

1.下载依赖

* 从https://github.com/ctripcorp/apollo/releases
<https://github.com/ctripcorp/apollo/releases>
页面下载最新版本的apollo-configservice-x.x.x-github.zip、apollo-adminservice-x.x.x-github.zip和apollo-portal-x.x.x-github.zip依赖包(需要翻墙。不能翻墙的同学建议使用第二种方式)。
* 从https://github.com/ctripcorp/apollo <https://github.com/ctripcorp/apollo>
下载源码后在本地构建。构建步骤为:
* 下载项目所需依赖
* 使用scripts文件夹下的build.bat或build.sh构建
*
分别拷贝出apollo-adminservice、apollo-configservice和apollo-portal三个文件夹下target/apollo-xxx-x.x.x-github.zip文件
2. 创建数据库

* 从https://github.com/ctripcorp/apollo/tree/master/scripts/sql
<https://github.com/ctripcorp/apollo/tree/master/scripts/sql>
下载apolloconfigdb.sql和apolloportaldb.sql数据库文件。
* 使用apolloportaldb.sql文件创建apolloportaldb数据库,此数据库是我们管理各种环境等的通用数据库。
*
使用apolloconfigdb.sql文件分别创建apolloconfigdb_dev和apolloconfigdb_fat数据库作为我们两个环境的数据存储。
3.配置数据库连接信息

* 解压第一步下载的三个压缩文件
* apollo-portal-1.0.0-github
* 在apollo-portal-1.0.0-github/config下application-github.properties文件中配置
apolloportaldb数据库的连接信息。
* 打开apollo-env.properties文件修改dev.mate和fat.mate属性值为不同环境对
应的Eureka地址。例如在这里我fat环境使用的本地,dev使用的是服务器地址
*
复制一份apollo-adminservice-1.0.0-github文件,分别重命名为apollo-adminservice-dev和apollo-adminservice-fat。
* 在apollo-adminservice-dev和apollo-adminservice-fat
的config文件夹下的application-github.properties文件中分别配置
apolloconfigdb_dev和apolloconfigdb_fat数据库的连接信息。
* 按照3.4步骤复制apollo-configservice-1.0.0-github并分别配置数据连接地址
现在的数据库连接信息如下所示:


4.启动服务

* 使用apollo时portal只需要启动一个来进行管理,在这里我们暂时把它放在本地启动。为了启动方面,使用一个小的脚本
1 2 3 4 #!/bin/bash sh apollo-portal-1.0.0-github/scripts/startup.sh sh
apollo-configservice-fat/scripts/startup.sh sh
apollo-adminservice-fat/scripts/startup.sh
* 将apollo-configservice-dev和apollo-adminservice-dev上传到服务器,使用如下命令启动
1 2 sh ./apollo-configservice-dev/scripts/startup.sh sh
./apollo-adminservice-dev/scripts/startup.sh
* 现在我们访问http://localhost:8080/以及http://10.10.10.10:8080/可以看到以下信息就没问题了

*
修改数据库apolloconfigdb_dev和apolloconfigdb_fat中的ServerConfig表中的key为eureka.service.url的数据,将value分别置为http://10.10.10.10:8080/eureka/和http://localhost:8080/eureka/
5.测试

* 创建一个maven工程,引入apollo的相关依赖
1 2 3 4 5 6 <apollo.version>1.0.0</apollo.version> <dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId> <version>${apollo.version}</version>
</dependency>
* 在application.yml中指定应用的id,以及apollo配置中心的地址
1 2 3 4 App: Id: demo apollo: Meta: http://10.10.10.10:8080 #指定dev环境
* 创建ConfigRefresher类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 @Service public class ConfigRefresher implements ApplicationContextAware
{ private ApplicationContext applicationContext; @ApolloConfig private Config
config; @PostConstruct private void initialize() {
refresher(config.getPropertyNames()); } @ApolloConfigChangeListener private
void onChange(ConfigChangeEvent changeEvent) {
refresher(changeEvent.changedKeys()); } private void refresher(Set<String>
changedKeys) { for (String changedKey : changedKeys) { System.out.println("this
key is changed:"+changedKey); } this.applicationContext.publishEvent(new
EnvironmentChangeEvent(changedKeys)); } @Override public void
setApplicationContext(ApplicationContext applicationContext) throws
BeansException { this.applicationContext = applicationContext; } }
* 创建启动类并启动
1 2 3 4 5 6 7 8 @SpringBootApplication @EnableApolloConfig public class
Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args); } }
* 修改配置文件中的 apollo.Meta为localhost:8080再次启动
* 打开浏览器访问 http://localhost:8070 <http://localhost:8070/> Apollo默认的用户名为
apollo,密码为admin。登陆后点击创建项目,项目的应用id和名称填写我们配置文件中的app.id
<http://xn--idapp-6n1hq8bkc523ao3g5ob210av8tnfl0tm124d82ena31rh91az71aye1geq8a.id/>

* 进入项目可在dev和fat环境中分别发布不同的配置进行测试
由于Apollo支持的图形化界面相对于我们更加的友好,所以此次我们使用Apollo来做配置中心

本篇文章实现了使用Apollo配置了dev和fat两个环境下的属性配置。
Apollo官方文档https://github.com/ctripcorp/apollo/wiki
<https://github.com/ctripcorp/apollo/wiki>

1.下载依赖

* 从https://github.com/ctripcorp/apollo/releases
<https://github.com/ctripcorp/apollo/releases>
页面下载最新版本的apollo-configservice-x.x.x-github.zip、apollo-adminservice-x.x.x-github.zip和apollo-portal-x.x.x-github.zip依赖包(需要翻墙。不能翻墙的同学建议使用第二种方式)。
* 从https://github.com/ctripcorp/apollo <https://github.com/ctripcorp/apollo>
下载源码后在本地构建。构建步骤为:
* 下载项目所需依赖
* 使用scripts文件夹下的build.bat或build.sh构建
*
分别拷贝出apollo-adminservice、apollo-configservice和apollo-portal三个文件夹下target/apollo-xxx-x.x.x-github.zip文件
*
2. 创建数据库

* 从https://github.com/ctripcorp/apollo/tree/master/scripts/sql
<https://github.com/ctripcorp/apollo/tree/master/scripts/sql>
下载apolloconfigdb.sql和apolloportaldb.sql数据库文件。
* 使用apolloportaldb.sql文件创建apolloportaldb数据库,此数据库是我们管理各种环境等的通用数据库。
*
使用apolloconfigdb.sql文件分别创建apolloconfigdb_dev和apolloconfigdb_fat数据库作为我们两个环境的数据存储。
*
3.配置数据库连接信息

* 解压第一步下载的三个压缩文件
* apollo-portal-1.0.0-github
* 在apollo-portal-1.0.0-github/config下application-github.properties文件中配置
apolloportaldb数据库的连接信息。
* 打开apollo-env.properties文件修改dev.mate和fat.mate属性值为不同环境对
应的Eureka地址。例如在这里我fat环境使用的本地,dev使用的是服务器地址
*
复制一份apollo-adminservice-1.0.0-github文件,分别重命名为apollo-adminservice-dev和apollo-adminservice-fat。
* 在apollo-adminservice-dev和apollo-adminservice-fat
的config文件夹下的application-github.properties文件中分别配置
apolloconfigdb_dev和apolloconfigdb_fat数据库的连接信息。
* 按照3.4步骤复制apollo-configservice-1.0.0-github并分别配置数据连接地址
*
现在的数据库连接信息如下所示:


4.启动服务

* 使用apollo时portal只需要启动一个来进行管理,在这里我们暂时把它放在本地启动。为了启动方面,使用一个小的脚本
*
1 2 3 4 #!/bin/bash sh apollo-portal-1.0.0-github/scripts/startup.sh sh
apollo-configservice-fat/scripts/startup.sh sh
apollo-adminservice-fat/scripts/startup.sh
* 将apollo-configservice-dev和apollo-adminservice-dev上传到服务器,使用如下命令启动
*
1 2 sh ./apollo-configservice-dev/scripts/startup.sh sh
./apollo-adminservice-dev/scripts/startup.sh
* 现在我们访问http://localhost:8080/以及http://10.10.10.10:8080/可以看到以下信息就没问题了

*
修改数据库apolloconfigdb_dev和apolloconfigdb_fat中的ServerConfig表中的key为eureka.service.url的数据,将value分别置为http://10.10.10.10:8080/eureka/和http://localhost:8080/eureka/
*
5.测试

* 创建一个maven工程,引入apollo的相关依赖
*
1 2 3 4 5 6 <apollo.version>1.0.0</apollo.version> <dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId> <version>${apollo.version}</version>
</dependency>
* 在application.yml中指定应用的id,以及apollo配置中心的地址
*
1 2 3 4 App: Id: demo apollo: Meta: http://10.10.10.10:8080 #指定dev环境
* 创建ConfigRefresher类
*
0 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 @Service
public class ConfigRefresher implements ApplicationContextAware { private
ApplicationContext applicationContext; @ApolloConfig private Config config;
@PostConstruct private void initialize() {
refresher(config.getPropertyNames()); } @ApolloConfigChangeListener private
void onChange(ConfigChangeEvent changeEvent) {
refresher(changeEvent.changedKeys()); } private void refresher(Set<String>
changedKeys) { for (String changedKey : changedKeys) { System.out.println("this
key is changed:"+changedKey); } this.applicationContext.publishEvent(new
EnvironmentChangeEvent(changedKeys)); } @Override public void
setApplicationContext(ApplicationContext applicationContext) throws
BeansException { this.applicationContext = applicationContext; } }
* 创建启动类并启动
*
1 2 3 4 5 6 7 8 @SpringBootApplication @EnableApolloConfig public class
Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args); } }
* 修改配置文件中的 apollo.Meta为localhost:8080再次启动
* 打开浏览器访问 http://localhost:8070 <http://localhost:8070/> Apollo默认的用户名为
apollo,密码为admin。登陆后点击创建项目,项目的应用id和名称填写我们配置文件中的app.id
<http://xn--idapp-6n1hq8bkc523ao3g5ob210av8tnfl0tm124d82ena31rh91az71aye1geq8a.id/>

* 进入项目可在dev和fat环境中分别发布不同的配置进行测试


本文出自zhixiang.org.cn <http://zhixiang.org.cn>,转载请保留

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信