由于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. 十三、实现Comparable接口和new Comparator<T>(){ }排序的实现过程

    参考:https://www.cnblogs.com/igoodful/p/9517784.html Collections有两种比较规则方式,第一种是使用自身的比较规则: 该类必须实现Compara ...

  2. Failed to create agent because it is not close enough to the NavMesh

    主要原因是:两个相同对象navmesh点太近造成. 解决方案:通过NavMesh.SamplePosition 获得可以行走点 ;i<;i++) { , mRadius); , 3.14f); ...

  3. Oracle 数据库中对记录进行分页处理

    学习到 oracle 的视图的时候,了解到对 Oracle 中数据的记录进行分页处理和 Mysql 提供的 limit 来进行分页处理大有不同,limit 是 mysql 中特有的关键字. 那么在 o ...

  4. AsyncContext的startAsync()方法开启异步

    Servlet 3.0的异步处理支持特性,使Servlet 线程不再需要一直阻塞,直到业务处理完毕才能再输出响应,最后才结束该 Servlet 线程.在接收到请求之后,Servlet 线程可以将耗时的 ...

  5. 修改Tomcat控制台标题

    1.在tomcat的bin目录下新建文件setenv.bat 2.重启tomcat即可

  6. NodeManager介绍

    原文链接: http://blog.csdn.net/zhangzhebjut/article/details/37730013 参考文档: https://blog.csdn.net/u013384 ...

  7. Spring Boot 异常处理

    Spring Boot 异常处理 本节介绍一下 Spring Boot 启动时是如何处理异常的?核心类是 SpringBootExceptionReporter 和 SpringBootExcepti ...

  8. laravel 填充器Seed

    我们在进行多人开发时,发现运行php artisan db:seed --class=填充器名称 发现有报错 Exception trace: 1 ReflectionClass::__constru ...

  9. Linux学习笔记:nginx基础

    nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP pro ...

  10. node.js 使用 superagent 与 cheerio 完成简单爬虫

    目标 建立一个 lesson3 项目,在其中编写代码. 当在浏览器中访问 http://localhost:3000/ 时,输出 CNode(https://cnodejs.org/ ) 社区首页的所 ...