zoukankan      html  css  js  c++  java
  • 每天学点SpringCloud(八):使用Apollo做配置中心

    由于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,转载请保留。

  • 相关阅读:
    《MySQL是怎样运行的:从根儿上理解MySQL》笔记4
    《MySQL是怎样运行的:从根儿上理解MySQL》笔记3
    推荐一个对比jar包依赖的工具
    《MySQL是怎样运行的:从根儿上理解MySQL》笔记2
    《MySQL是怎样运行的:从根儿上理解MySQL》笔记1
    查询异步更新状态的一种思路
    springAop:Aop(Xml)配置,Aop注解配置,spring_Aop综合案例,Aop底层原理分析
    java知识点总结
    Maven基础&&Spring框架阶段常用工具类整理
    Idea快捷键整理
  • 原文地址:https://www.cnblogs.com/zhixiang-org-cn/p/9463716.html
Copyright © 2011-2022 走看看