• apolloconfig分布式部署


    apolloconfig搭建

    前言

    apollo阿波罗)是一款可靠的分布式配置管理中心,诞生于携程框架研发部,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。

    apollo的官方文档很详细,但是有点过于详细和全面了,对于想快速搭建apollo环境试用的人来说,干扰信息太多。为了给后来者参考,也作为自己部署apollo的笔记,故写下这篇文章。

    本文记录从源码编译开始,在单机上以分布式部署的方式,部署单环境单实例apollo服务,以及多环境多实例的apollo服务的过程,并附上简单的使用示例。

    编译及运行环境

    • 系统版本:Windows 10

    • apollo版本:2.0.1

    • MySQL版本:5.7.20

    • Java版本:1.8.0_101

    • Git Bash:2.12.0.windows.1


    单机部署单环境单实例

    • 本次部署除MySQL配置外,其它均使用默认配置,需要使用以下三个端口:8070、8080、8090
    1.下载代码

    github地址:https://github.com/apolloconfig/apollo/tree/v2.0.1

    2.数据准备

    执行apollo-2.0.1/scripts/sql目录下的两个sql文件:apolloconfigdb.sqlapolloportaldb.sql

    3.修改apollo-2.0.1/scripts/build.bat文件(如果是Linux系统,请修改apollo-2.0.1/scripts/build.sh
    3.1.修改数据库连接信息
    rem apollo config db info
    set apollo_config_db_url="jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8"
    set apollo_config_db_username="root"
    set apollo_config_db_password="root"
    
    rem apollo portal db info
    set apollo_portal_db_url="jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8"
    set apollo_portal_db_username="root"
    set apollo_portal_db_password="root"
    
    3.2.修改meta service信息

    (本次部署使用默认配置, 默认环境为dev,此处可以跳过)

    rem meta server url, different environments should have different meta server addresses
    set dev_meta="http://localhost:8080"
    set fat_meta="http://someIp:8080"
    set uat_meta="http://anotherIp:8080"
    set pro_meta="http://yetAnotherIp:8080"
    
    4.执行apollo-2.0.1/scripts/build.bat,执行完毕后获取以下文件,并拷贝到安装目录
    apollo-2.0.1/apollo-adminservice/target/apollo-adminservice-2.0.1-github.zip
    apollo-2.0.1/apollo-configservice/target/apollo-configservice-2.0.1-github.zip
    apollo-2.0.1/apollo-portal/target/apollo-portal-2.0.1-github.zip
    
    5.解压并启动

    进入安装目录,解压:

    unzip apollo-adminservice-2.0.1-github.zip -d apollo-adminservice-2.0.1-github
    unzip apollo-configservice-2.0.1-github.zip -d apollo-configservice-2.0.1-github
    unzip apollo-portal-2.0.1-github.zip -d apollo-portal-2.0.1-github
    

    configservice -> adminservice -> portal的顺序依次启动服务,可以使用以下脚本启动:

    sh apollo-configservice-2.0.1-github/scripts/startup.sh
    sh apollo-adminservice-2.0.1-github/scripts/startup.sh
    sh apollo-portal-2.0.1-github/scripts/startup.sh
    
    6.访问apollo

    地址:http://localhost:8070

    用户名:apollo

    密码:admin

    7.停止apollo
    sh apollo-portal-2.0.1-github/scripts/shutdown.sh
    sh apollo-adminservice-2.0.1-github/scripts/shutdown.sh
    sh apollo-configservice-2.0.1-github/scripts/shutdown.sh
    
    8.修改启动端口(重启后生效)
    8.1.修改configservice端口

    configservice和meta service在同一个服务中,共用一个端口,所以修改configservice的端口后,也要将meta service信息进行更新

    • 修改apollo-configservice-2.0.1-github/scripts/startup.sh中的SERVER_PORT

    • 修改meta service信息,包括以下两个内容:

      • 修改apollo-portal-2.0.1-github/config/apollo-env.properties中的meta service地址的端口为configservice的端口

      • 修改ApolloConfigDB.ServerConfig表中Key='eureka.service.url'的数据对应的Value值,修改端口为configservice的端口

    8.2.修改adminservice端口
    • 修改apollo-adminservice-2.0.1-github/scripts/startup.sh中的SERVER_PORT即可
    8.3.修改portal端口
    • 修改apollo-portal-2.0.1-github/scripts/startup.sh中的SERVER_PORT即可
    9.添加环境(重启后生效)

    步骤一:在apollo-portal-2.0.1-github/config/apollo-env.properties中添加meta service地址,环境名以.meta结尾

    步骤二:修改支持的环境列表(以下方式任选一种即可)

    • 方式一:在ApolloPortalDB.ServerConfig表中Key='apollo.portal.envs'的数据对应的Value值中添加环境名,多个环境间以英文逗号分隔

    • 方式二:在apollo页面,依次点击 管理员工具 -> 系统参数 -> 在Key中填入apollo.portal.envs,点击查询 -> 在Value中添加环境名,多个环境间以英文逗号分隔,点击保存


    单机部署多环境多实例

    • 多个环境的情况下,portal只需要部署一套即可,但configservice和adminservice需要每个环境部署一套,每个环境也需要一个对应的ApolloConfigDB数据库

    • 本次部署test和production两个环境,其中:

      • portal部署两个实例,所有环境共用这两个portal,访问其中任一portal即可

      • configservice和adminservice在test环境部署单实例,production环境部署两个实例

    • 各服务实例的端口占用如下:

    portal1:11000
    portal2:11001
    configservice(test):11002
    adminservice(test):11003
    configservice1(production):11004
    configservice2(production):11005
    adminservice1(production):11006
    adminservice2(production):11007
    
    1.下载代码

    github地址:https://github.com/apolloconfig/apollo/tree/v2.0.1

    2.数据准备
    2.1.准备ApolloPortalDB数据

    直接执行apollo-2.0.1/scripts/sql/apolloportaldb.sql

    2.2.准备ApolloConfigDB数据

    复制apollo-2.0.1/scripts/sql/apolloportaldb.sqlapollo-2.0.1/scripts/sql/apolloportaldb-test.sqlapollo-2.0.1/scripts/sql/apolloportaldb-production.sql

    apollo-2.0.1/scripts/sql/apolloportaldb-test.sql中的数据库改为ApolloConfigDBTest,包括创建和使用数据库的两条语句,然后执行该sql文件

    apollo-2.0.1/scripts/sql/apolloportaldb-production.sql中的数据库改为ApolloConfigDBProduction,包括创建和使用数据库的两条语句,然后执行该sql文件

    3.删除默认配置
    3.1.删除打包脚本apollo-2.0.1/scripts/build.bat中关于数据库的内容,被删除部分示例:
    rem apollo config db info
    set apollo_config_db_url="jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8"
    set apollo_config_db_username="root"
    set apollo_config_db_password=""
    
    rem apollo portal db info
    set apollo_portal_db_url="jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8"
    set apollo_portal_db_username="root"
    set apollo_portal_db_password=""
    
    3.2.删除meta service配置

    通过打包脚本传入的环境变量,其优先级高于我们后面要修改的配置文件,所以先删掉这部分内容(apollo加载meta service配置的源码位于com.ctrip.framework.apollo.portal.environment.DefaultPortalMetaServerProvider#initializeDomains

    被删除部分示例:

    rem meta server url, different environments should have different meta server addresses
    set dev_meta="http://localhost:8080"
    set fat_meta="http://someIp:8080"
    set uat_meta="http://anotherIp:8080"
    set pro_meta="http://yetAnotherIp:8080"
    
    set META_SERVERS_OPTS=-Ddev_meta=%dev_meta% -Dfat_meta=%fat_meta% -Duat_meta=%uat_meta% -Dpro_meta=%pro_meta%
    

    并删除后面maven打包时使用到META_SERVERS_OPTS变量的地方

    4.执行apollo-2.0.1/scripts/build.bat,执行完毕后获取以下文件,并拷贝到安装目录
    apollo-2.0.1/apollo-adminservice/target/apollo-adminservice-2.0.1-github.zip
    apollo-2.0.1/apollo-configservice/target/apollo-configservice-2.0.1-github.zip
    apollo-2.0.1/apollo-portal/target/apollo-portal-2.0.1-github.zip
    
    5.解压文件到对应目录,可参考如下脚本
    # 创建文件夹
    mkdir -p portal/portal1
    mkdir -p portal/portal2
    mkdir -p test/configservice
    mkdir -p test/adminservice
    mkdir -p production/configservice1
    mkdir -p production/configservice2
    mkdir -p production/adminservice1
    mkdir -p production/adminservice2
    
    # 解压
    unzip apollo-portal-2.0.1-github.zip -d portal/portal1/
    unzip apollo-portal-2.0.1-github.zip -d portal/portal2/
    unzip apollo-configservice-2.0.1-github.zip -d test/configservice/
    unzip apollo-configservice-2.0.1-github.zip -d production/configservice1/
    unzip apollo-configservice-2.0.1-github.zip -d production/configservice2/
    unzip apollo-adminservice-2.0.1-github.zip -d test/adminservice/
    unzip apollo-adminservice-2.0.1-github.zip -d production/adminservice1/
    unzip apollo-adminservice-2.0.1-github.zip -d production/adminservice2/
    
    6.修改端口、数据库连接信息、meta service服务地址等
    6.1.修改test环境中configservice和adminservice配置

    修改端口

    • 修改test/configservice/scripts/startup.shSERVER_PORT为11002

    • 修改test/adminservice/scripts/startup.shSERVER_PORT为11003

    添加数据库连接信息

    以上两个文件,在JAVA_OPTS变量被定义后,被使用前,添加如下代码(启动时,JAVA_OPTS参数被放在-jar之前,所以此处需要用-D传参,而不能用--。下同)

    # 数据库连接信息
    apollo_config_db_url='jdbc:mysql://localhost:3306/ApolloConfigDBTest?characterEncoding=utf8'
    apollo_config_db_username='root'
    apollo_config_db_password='root'
    
    JAVA_OPTS="$JAVA_OPTS -Dspring.datasource.url=$apollo_config_db_url -Dspring.datasource.username=$apollo_config_db_username -Dspring.datasource.password=$apollo_config_db_password"
    
    6.2.修改production环境中configservice和adminservice配置

    修改端口

    • 修改production/configservice1/scripts/startup.shSERVER_PORT为11004

    • 修改production/configservice2/scripts/startup.shSERVER_PORT为11005

    • 修改production/adminservice1/scripts/startup.shSERVER_PORT为11006

    • 修改production/adminservice2/scripts/startup.shSERVER_PORT为11007

    添加数据库连接信息

    以上四个文件,在JAVA_OPTS变量被定义后,被使用前,添加如下代码

    # 数据库连接信息
    apollo_config_db_url='jdbc:mysql://localhost:3306/ApolloConfigDBProduction?characterEncoding=utf8'
    apollo_config_db_username='root'
    apollo_config_db_password='root'
    
    JAVA_OPTS="$JAVA_OPTS -Dspring.datasource.url=$apollo_config_db_url -Dspring.datasource.username=$apollo_config_db_username -Dspring.datasource.password=$apollo_config_db_password"
    
    6.3.修改portal配置

    修改端口

    • 修改portal/portal1/scripts/startup.shSERVER_PORT为11000

    • 修改portal/portal2/scripts/startup.shSERVER_PORT为11001

    添加数据库连接信息

    以上两个文件,在JAVA_OPTS变量被定义后,被使用前,添加如下代码

    # 数据库连接信息
    apollo_portal_db_url='jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8'
    apollo_portal_db_username='root'
    apollo_portal_db_password='root'
    
    JAVA_OPTS="$JAVA_OPTS -Dspring.datasource.url=$apollo_portal_db_url -Dspring.datasource.username=$apollo_portal_db_username -Dspring.datasource.password=$apollo_portal_db_password"
    

    添加环境信息

    portal/portal1/config/apollo-env.propertiesportal/portal2/config/apollo-env.properties的内容清空,然后添加如下内容

    test.meta=http://localhost:11002
    production.meta=http://localhost:11004,http://localhost:11005
    

    修改数据库中的配置

    • 修改ApolloConfigDBTest.ServerConfigKey=eureka.service.url的数据,将对应的Value改为http://localhost:11002/eureka/

    • 修改ApolloConfigDBProduction.ServerConfigKey=eureka.service.url的数据,将对应的Value改为http://localhost:11004/eureka/,http://localhost:11005/eureka/

    • 修改ApolloPortalDB.ServerConfigKey=apollo.portal.envs的数据,将对应的Value改为test,production

    7.启动apollo

    依次启动各个环境的configservice、adminservice,最后启动portal,可参考如下脚本

    sh test/configservice/scripts/startup.sh
    sh test/adminservice/scripts/startup.sh
    sh production/configservice1/scripts/startup.sh
    sh production/configservice2/scripts/startup.sh
    sh production/adminservice1/scripts/startup.sh
    sh production/adminservice2/scripts/startup.sh
    sh portal/portal1/scripts/startup.sh
    sh portal/portal2/scripts/startup.sh
    
    8.访问apollo

    由于启动了两个portal服务,所以以下两个地址都可以访问

    9.停止apollo

    可参考如下脚本

    sh portal/portal1/scripts/shutdown.sh
    sh portal/portal2/scripts/shutdown.sh
    sh production/adminservice1/scripts/shutdown.sh
    sh production/adminservice2/scripts/shutdown.sh
    sh production/configservice1/scripts/shutdown.sh
    sh production/configservice2/scripts/shutdown.sh
    sh test/adminservice/scripts/shutdown.sh
    sh test/configservice/scripts/shutdown.sh
    

    apollo的使用

    1.apollo配置
    1.1.修改/添加部门(无特殊说明则修改完一分钟内实时生效)

    方式一:在apollo页面,依次点击 管理员工具 -> 系统参数 -> 在Key中填入organizations,点击查询 -> 修改Value的Json字符串,点击保存

    方式二:直接修改数据库数据,修改ApolloConfigDB.ServerConfig表中Key='organizations'的数据对应的Value值

    1.2.修改/添加/删除用户

    修改/添加:在apollo页面,依次点击 管理员工具 -> 用户管理,输入用户信息(若用户已存在,则修改用户)

    删除:直接修改数据库数据,删除ApolloPortalDB.Users表中对应的行

    1.3.修改用户权限

    在apollo页面,依次点击 管理员工具 -> 系统权限管理,修改用户权限

    1.4.创建应用

    填写参数说明:

    1.5.创建Namespace

    Namespace相关知识

    Q:什么是Namespace?

    A:Namespace是配置项的集合,类似于一个配置文件的概念。

    Q:Namespace支持哪些格式?

    A:支持properties、xml、json、yml、yaml、txt。推荐使用和项目一致的格式。

    Q:公共(public)Namespace和私有(private)Namespace的区别是什么?

    A:public权限的Namespace能被任何应用获取;private权限的Namespace,只能被所属的应用获取到;private权限的Namespace可以关联(继承)自public权限的Namespace,关联(继承)类似Java的类继承,可以在子Namespace中覆盖父Namespace的配置。

    Q:Namespace的命名格式是否有要求?

    A:没有要求。应用创建后,会创建一个默认名为application的Namespace,绝大部分情况下,该Namespace已经满足日常配置使用场景了。

    创建Namespace

    1. 点击左下角“添加Namespace”;
    2. 选择权限类型,填写Namespace名称,选择Namespace类型;
    3. 点击提交即可。
    1.6.新增/修改配置

    在应用的Namespace下新增或修改单条配置后,提交,此时配置还未生效,可批量新增/修改配置项,最后点击Namespace对应的发布按钮,即可生效新增/修改的配置。

    2.应用配置

    2.1.在pom.xml中添加maven依赖

            <dependency>
                <groupId>com.ctrip.framework.apollogroupId>
                <artifactId>apollo-client-config-dataartifactId>
                <version>2.0.1version>
            dependency>
    

    2.2.在application.properties中添加如下配置

    # apollo中配置的AppId
    app.id=001
    # meta service服务地址(和configservice地址相同),多个地址用英文逗号隔开
    apollo.meta=http://localhost:11004,http://localhost:11005
    # 该开关必须打开,否则apolloconfig不生效(默认关闭-false)
    apollo.bootstrap.enabled=true
    # apollo中配置的Namespace,多个Namespace有相同的key时,从左到右优先级从高到低
    apollo.bootstrap.namespace=application,localhost
    

    3.启动应用后即可接入apolloconfig

  • 相关阅读:
    同事问我为什么上级喊他把js实现改成css
    万字详解 Google Play 上架应用标准包格式 AAB
    使用Compose实现基于MVI架构、retrofit2、支持 glance 小部件的TODO应用
    前端:综合例题详细解说(含源代码),导航栏,商品展示页面。
    基于opencv进行双目相机的标定
    Stability AI推出Stable Audio;ChatGPT:推荐系统的颠覆者
    第十九章 源代码文件 REST API 参考(一)
    docker安装flink
    MVME5500 MVME55006E-0163 人工智能和工业4.0解决方案
    【华为OD机试真题 python】勾股数元组 【2022 Q4 | 100分】
  • 原文地址:https://blog.csdn.net/java_t_t/article/details/127116670