由于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并分别配置数据连接地址

现在的数据库连接信息如下所示:

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/可以看到以下信息就没问题了
  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环境中分别发布不同的配置进行测试

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

每天学点SpringCloud(八):使用Apollo做配置中心的更多相关文章

  1. 使用Apollo做配置中心

    https://blog.51cto.com/12980017/2158490?source=dra 由于Apollo支持的图形化界面相对于我们更加的友好,所以此次我们使用Apollo来做配置中心 本 ...

  2. netcore项目中使用 SpringCloudConfig 和apollo做配置中心

    版权所有,转载请注明出处 https://www.cnblogs.com/netqq/p/14251403.html 一.使用apollo作为配置中心 首先apollo 项目简介和安装请自行百度,本文 ...

  3. SpringCloud学习之Config分布式配置中心(八)

    统一配置中心概述 如果微服务架构中没有使用统一配置中心时,所存在的问题: 配置文件分散在各个项目里,不方便维护 配置内容安全与权限,实际开发中,开发人员是不知道线上环境的配置的 更新配置后,项目需要重 ...

  4. SpringCloud基于消息总线的配置中心

    @https://www.cnblogs.com/ityouknow/p/6931958.html Spring Cloud Bus Spring cloud bus通过轻量消息代理连接各个分布的节点 ...

  5. springcloud 入门 8 (config配置中心)

    Spring Cloud Config: 配置中心为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,它就是Spring Cloud Config. 在分布式系统中,由于服务数量巨多, ...

  6. 携程阿波罗(Apollo)配置中心

    携程阿波罗(Apollo) https://www.cnblogs.com/xiaxiaolu/p/10025597.html 一.瞎扯点什么 1.1 阿波罗 ​ 阿波罗是希腊神话中的光明之神.文艺之 ...

  7. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  8. 微服务SpringCloud之Spring Cloud Config配置中心服务化

    在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文 ...

  9. .Net Core 商城微服务项目系列(十四):分布式部署携程Apollo构建配置中心

    一.开场白 在系统设计里我们有很多配置希望独立于系统之外,而又能够被系统实时读取.但是在传统的系统设计里,配置信息通常是耦合在系统内的,比如.net里通常会放在App.config或者web.conf ...

随机推荐

  1. stm32定时器时钟以及中间对齐模式

    在永磁同步电机的控制中,需要对电机的三相定子施加一定的电压,才能控制电机转动.现在用的较多的是SVPWM(SVPWM的具体原理会在后面另写一篇博客说明),要想产生SVPWM波形,需要控制的三相电压呈如 ...

  2. 在Eclipse中导入web项目时的问题总结

    一.导入项目 在Project Explorer右击,import-->Existing Projects into Workspace,选择要导入的文件,Finish. 二.解决报错 (1)T ...

  3. MongoDB的Replica Set以及Auth的配置

    http://blog.0x01.site/2017/01/13/MongoDB%E7%9A%84Replica-Set%E4%BB%A5%E5%8F%8AAuth%E7%9A%84%E9%85%8D ...

  4. .net webapi 接收 xml 格式数据的三种情况

    webapi 接收 xml 的三种方法 前段时间接到一个任务写一个小接口,要接收java端返回过来的短信xml数据. 刚拿到项目,我的第一想法是对方会以什么形式发送xml格式的数据给我呢,设想三种情况 ...

  5. python基础之Day24

    1.补充内置函数 2.反射 什么是? 通过字符串操作类或者对象的属性 hasattri(a,"b")  判断能否访问到a.b setattri(a,"b",c) ...

  6. MySQL ERROR 1064(42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

    通常出现该错误的原因是使用了 MySQL 的保留字 解决方法是对使用的保留字使用反引号  (Tab键上面)

  7. IT人保护视力的10个有效方法

    保护视力锻炼十法   有关资料表明:近年来,我国中小学生的近视率呈逐年增长的趋势,1993年底已高达60左右.众所周知,近视除了与遗传有一定联系外,还与光线不足.近距离阅读过多等眼睛过度疲劳密切相关. ...

  8. 主要的Ajax框架都有什么?

    *  Dojo(dojotoolkit.org):* Prototype和Scriptaculous (www.prototypejs.org和script.aculo.us):* Direct We ...

  9. Win10下 usart驱动PL2303无法安装的问题

    随着系统的 普及,很多小伙伴也放弃了原有的win7系统,加入了win10的行列.但是相对win7的稳定来说,win10还存在很多的不足 . 新买了一个usart的模块,但是在自家的电脑上使用的时候 一 ...

  10. Linux 下 Samba 服务器搭建

    初学,分享 环境和条件--- 虚拟机:VMware虚拟机 系统:Linux ubuntu 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:0 ...