一、Apollo简介
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用的不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
Apollo包括服务端和客户端两部分:
服务端基于Spring Boot和Spring Cloud开发,打包后可以直接运行,不需要额外安装Tomcat等应用容器。
Java客户端不依赖任何框架,能够运行于所有Java运行时环境,同时对Spring/Spring Boot环境也有较好的支持。
执行流程:
操作流程如下:
1、在Apollo配置中心修改配置
2、应用程序通过Apollo客户端从配置中心拉取配置信息
用户通过Apollo配置中心修改或发布配置后,会有两种机制来保证应用程序来获取最新配置:一种是Apollo配置中心会向客户端推送最新的配置;另外一种是Apollo客户端会定时从Apollo配置中心拉取最新的配置,通过以上两种机制共同来保证应用程序能及时获取到配置。
二、部署方案
在企业中常用的部署方案为:Apollo-adminservice和Apollo-configservice两个服务分别在线上环境(pro),仿真环境(uat)和开发环境(dev)各部署一套,Apollo-portal做为管理端只部署一套,统一管理上述三套环境。
Apollo服务端共需要两个数据库:ApolloPortalDB和ApolloConfigDB,ApolloPortalDB只需要在生产环境部署一个即可,而ApolloConfigDB需要在每个环境部署一套。
三、部署环境
依据部署方案,我这里在服务器192.168.132.64分别部署两套环境:pro(生产)、dev(开发)。
pro部署configservice、adminservice、portal服务,并且使用服务器192.168.132.63上的apolloconfigdb_pro、apolloportal两个数据库。
dev部署configservice、adminservice服务,并且使用服务器192.168.132.63上的apolloconfigdb数据库。
Apollo默认会启动3个服务,分别使用8070, 8080, 8090端口,请确保这3个端口当前没有被使用。如果有端口被使用了,则需要修改端口。我这里因为8080端口被其他应用使用了,所以修改为8081端口。
Apollo服务端: | |||
部署环境 | 服务器ip | 端口 | 服务名称 |
pro | 192.168.132.64 | 8082 | configservice |
8092 | adminservice | ||
8070 | portal | ||
dev | 192.168.132.64 | 8081 | configservice |
8090 | adminservice | ||
数据库: | |||
部署环境 | 服务器ip | 端口 | 数据库 |
pro | 192.168.132.63 | 3307 | apolloportaldb |
apolloconfigdb_pro | |||
dev | 192.168.132.63 | 3307 | apolloconfigdb |
注意:每个环境都要部署configservice、adminservice服务,并且要对应一个apolloconfigdb的数据库。
portal服务只在pro环境部署一次,只需一个apolloportaldb的数据库即可。
四、创建数据库
1、在服务器192.168.132.63上安装MySq数据库,从github:apollo-build-scripts/sql at master · apolloconfig/apollo-build-scripts · GitHub 上下载sql脚本。
2、创建数据库apolloconfigdb,导入scripts/sql/apolloconfigdb.sql。
创建数据库apolloconfigdb_pro,拷贝一份scripts/sql/apolloconfigdb.sql脚本,重命名为scripts/sql/apolloconfigdb_pro.sql,修改如下图部分:
导入scripts/sql/apolloconfigdb_pro.sql。
创建apolloportaldb,导入scripts/sql/apolloportaldb.sql。
apolloportaldb只需要在pro生产环境部署一个即可,而apolloconfigdb需要在每个环境部署一套,如dev和pro分别部署2套apolloconfigdb。
五、安装Apollo
1、Apollo提供docker、Quick-Start、分布式三种方式部署,其中docker和Quick-Start建议使用开发和测试,生产环境建议使用分布式部署。
安装包下载地址:Releases · apolloconfig/apollo · GitHub 。
下载安装包 :
apollo-configservice-1.9.1-github.zip
apollo-adminservice-1.9.1-github.zip
apollo-portal-1.9.1-github.zip
2、在linux系统上创建目录:
mkdir -p /DATA/soft/apollo
将上面下载的3个压缩包上传到apollo目录下并解压,unzip命令进行解压,-d参数为解压之后存放目录:
pro:
unzip apollo-configservice-1.9.1-github.zip -d apollo-configservice-pro
unzip apollo-adminservice-1.9.1-github.zip -d apollo-adminservice-pro
unzip apollo-portal-1.9.1-github.zip -d apollo-adminservice-pro
dev:
unzip apollo-configservice-1.9.1-github.zip -d apollo-configservice-dev
unzip apollo-adminservice-1.9.1-github.zip -d apollo-adminservice-dev
3、修改pro环境的configservice、adminservice、portal数据库配置文件
configservice、adminservice连接的数据库为:apolloconfigdb_pro,portal连接的数据库为apolloportaldb。
configservice:
cd /DATA/soft/apollo/apollo-configservice-pro/config
vim application-github.properties
adminservice:
cd /DATA/soft/apollo/apollo-adminservice-pro/config
vim application-github.properties
portal:
cd /DATA/soft/apollo/apollo-portal-pro/config
vim application-github.properties
4、修改dev环境的configservice、adminservice数据库配置文件
configservice、adminservice连接的数据为:apolloconfigdb
configservice:
cd /DATA/soft/apollo/apollo-configservice-dev/config
vim application-github.properties
adminservice:
cd /DATA/soft/apollo/apollo-adminservice-dev/config
vim application-github.properties
5、修改pro、dev环境的端口、日志存放目录
因为在同一台服务上部署两个环境,为避免端口冲突需要修改端口。由于8080端口已被占用,pro环境configservice端口改为8082,adminservice改为8092,dev环境configservice端口改为8081。日志存放目录修改参数LOG_DIR。
pro:
dev:
6、ApolloPortalDB.ServerConfig表修改。默认值是dev,增加环境变量支持:
修改apolloconfigdb_pro.ServerConfig表eureka.service.url:
修改apolloconfigdb.ServerConfig表eureka.service.url:
7、修改protal环境配置
Apollo Portal需要在不同的环境访问不同的meta service(apollo-configservice)地址,所以我们需要在配置中提供这些信息。默认情况下,meta service和config service是部署在同一个JVM进程,所以meta service的地址就是config service的地址。根据实际情况进行配置,如果有的环境不需要就直接删除就可以。
8、分别启动应用。
按照apollo-configservice-pro,apollo-adminservice-pro,apollo-configservice-dev,apollo-adminservice-dev,apollo-portal-pro的顺序去对应的路径下执行./startup.sh启动应用。
启动成功之后,可以看到如下信息输出:
9、防火墙开放端口
firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --zone=public --add-port=8090/tcp --permanent
firewall-cmd --zone=public --add-port=8070/tcp --permanent
firewall-cmd --zone=public --add-port=8082/tcp --permanent
firewall-cmd --zone=public --add-port=8092/tcp --permanent
firewall-cmd --reload
10、访问http://192.168.132.64:8070,输入用户名和密码之后就可以访问了,默认用户名apollo,密码:admin
参考:Apollohttps://www.apolloconfig.com/#/zh/deployment/quick-start 分布式配置中心Apollo教程-微服务配置中心Apollo教程-攀博课堂自学Java网站攀博课堂微服务配置中心Apollo教程内容概要:什么是配置与配置中心、Apollo入门、Apollo工作原理、核心概念、项目管理、配置管理、集群管理、Spring Boot应用集成、生产环境部署、灰度发布、项目实战http://www.pbteach.com/java/java_05_03/20210527/582534840956485632.html
六、应用配置
七、集成springboot
1、引入依赖
<!--apollo-->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.0.0</version>
</dependency>
2、启动类注解
3、application.properties配置
4、创建apollo-env.properties
5、启动类参数配置
6、启动项目