使用Apollo做配置中心

640?wx_fmt=gif
学习更多Java干货,戳上面的
蓝色字题
关注我!

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

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

1.下载依赖

  1. 从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依赖包(需要翻墙。不能翻墙的同学建议使用第二种方式)。

  2. 从https://github.com/ctripcorp/apollo下载源码后在本地构建。构建步骤为:

  1. 下载项目所需依赖

  2. 使用scripts文件夹下的build.bat或build.sh构建

  3. 分别拷贝出apollo-adminservice、apollo-configservice和apollo-portal三个文件夹下target/apollo-xxx-x.x.x-github.zip文件

2. 创建数据库

  1. 从https://github.com/ctripcorp/apollo/tree/master/scripts/sql下载apolloconfigdb.sql和apolloportaldb.sql数据库文件。

  2. 使用apolloportaldb.sql文件创建apolloportaldb数据库,此数据库是我们管理各种环境等的通用数据库。

  3. 使用apolloconfigdb.sql文件分别创建apolloconfigdb_dev和apolloconfigdb_fat数据库作为我们两个环境的数据存储。

3.配置数据库连接信息

  1. 解压第一步下载的三个压缩文件

  2. apollo-portal-1.0.0-github

  1. 在apollo-portal-1.0.0-github/config下application-github.properties文件中配置 apolloportaldb数据库的连接信息。

  2. 打开apollo-env.properties文件修改dev.mate和fat.mate属性值为不同环境对 应的Eureka地址。例如在这里我fat环境使用的本地,dev使用的是服务器地址

  3. 复制一份apollo-adminservice-1.0.0-github文件,分别重命名为apollo-adminservice-dev和apollo-adminservice-fat。

  4. 在apollo-adminservice-dev和apollo-adminservice-fat 的config文件夹下的application-github.properties文件中分别配置 apolloconfigdb_dev和apolloconfigdb_fat数据库的连接信息。

  5. 按照3.4步骤复制apollo-configservice-1.0.0-github并分别配置数据连接地址

现在的数据库连接信息如下所示:
640?wx_fmt=png

4.启动服务

  1. 使用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
  1. 将apollo-configservice-dev和apollo-adminservice-dev上传到服务器,使用如下命令启动

1
2
sh ./apollo-configservice-dev/scripts/startup.sh 
sh ./apollo-adminservice-dev/scripts/startup.sh
  1. 现在我们访问http://localhost:8080/以及http://10.10.10.10:8080/可以看到以下信息就没问题了
    640?wx_fmt=png640?wx_fmt=png

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

5.测试

  1. 创建一个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>
  1. 在application.yml中指定应用的id,以及apollo配置中心的地址

1
2
3
4
App:
 Id: demo
apollo:
 Meta: http://10.10.10.10:8080 #指定dev环境
  1. 创建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. 创建启动类并启动

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableApolloConfig
public class Application {

   public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
   }
}
  1. 修改配置文件中的 apollo.Meta为localhost:8080再次启动

  2. 打开浏览器访问 http://localhost:8070 Apollo默认的用户名为 apollo,密码为admin。登陆后点击创建项目,项目的应用id和名称填写我们配置文件中的app.id。

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


640?wx_fmt=gif


640?wx_fmt=jpeg

 点赞收藏转发好看再走呗!

640?wx_fmt=gif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值