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.修改部门(无特殊说明则修改完一分钟内实时生效)

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

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

9.修改启动端口(重启后生效)
9.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的端口

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

步骤一:在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.在pom.xml中添加maven依赖

        <dependency>
            <groupId>com.ctrip.framework.apollo</groupId>
            <artifactId>apollo-client-config-data</artifactId>
            <version>2.0.1</version>
        </dependency>

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

# app id
app.id=001
# meta service服务地址,多个地址用英文逗号隔开
apollo.meta=http://localhost:11004,http://localhost:11005
apollo.bootstrap.enabled=true
# 多个namespace有相同的key时,从左到右优先级从高到低
apollo.bootstrap.namespace=application,local

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker Compose是一个用于定义和运行多容器Docker应用程序的工具。而Apollo是携程开源的一款分布式配置中心,可以用于管理和配置应用程序的各种配置信息。下面是使用Docker Compose部署Apollo的步骤: 1. 创建一个名为`docker-compose.yml`的文件,并在其中定义Apollo服务的配置。以下是一个示例配置: ```yaml version: '3' services: apollo-configservice: image: apolloconfig/apollo-configservice ports: - 8080:8080 environment: - SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 - SPRING_DATASOURCE_USERNAME=your_username - SPRING_DATASOURCE_PASSWORD=your_password - DEV_META=http://localhost:8080 apollo-adminservice: image: apolloconfig/apollo-adminservice ports: - 8090:8090 environment: - SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 - SPRING_DATASOURCE_USERNAME=your_username - SPRING_DATASOURCE_PASSWORD=your_password - DEV_META=http://localhost:8080 apollo-portal: image: apolloconfig/apollo-portal ports: - 8070:8070 environment: - SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8 - SPRING_DATASOURCE_USERNAME=your_username - SPRING_DATASOURCE_PASSWORD=your_password - DEV_META=http://localhost:8080 ``` 2. 根据实际情况修改上述配置中的数据库连接信息和端口映射。 3. 在终端中进入包含`docker-compose.yml`文件的目录,并运行以下命令启动Apollo服务: ```bash docker-compose up -d ``` 4. 等待一段时间,直到所有容器都成功启动。可以使用以下命令检查容器的状态: ```bash docker-compose ps ``` 5. 通过浏览器访问Apollo的管理界面,地址为`http://localhost:8070`。在管理界面中可以进行配置管理和发布等操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值