文章目录
- 一、介绍
- 1. 痛点
- 2. 为什么选择Apollo
- 二、快速开始
- 2.1. 安装mysql
- 2.2. 下载脚本
- 2.3. 初始化脚本
- 三、服务端配置
- 3.1. 下载安装包
- 3.2. 创建目录
- 3.3. 解压
- 3.4. 配置
- 3.5. 启动
- 3.6. 防火墙策略
- 3.6. 效果验证
- 四、测试
- 4.1. 引入依赖
- 4.2. 配置
- 4.3. 测试类
- 4.4. 启动项目
- 4.5. 效果图
一、介绍
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
– 摘自Apollo官网
1. 痛点
修改配置后,需要重启项目
大量重复的配置,不便于管理
2. 为什么选择Apollo
Apollo VS Spring Cloud Config
Spring Cloud Config 依赖于GitHub,所有配置都是写在文件中,然后通过提交GitHub上来生效的,不同的环境用不用的文件区分
Apollo VS 阿里云应用配置管理ACM
依赖于阿里云,不便于后续迁移
阿里云已经放弃了该项目,不再维护
二、快速开始
这里安装mysql需要提前安装docker环境
docker yum 安装
2.1. 安装mysql
docker mysql:8.0.27
2.2. 下载脚本
链接:https://github.com/apolloconfig/apollo/tree/master/scripts/sql
2.3. 初始化脚本
直接运行脚本,不需要提前创建数据库
初始化后,就会有2个数据库
三、服务端配置
3.1. 下载安装包
下载链接:https://github.com/ctripcorp/apollo/releases
wget https://github.com/apolloconfig/apollo/releases/download/v1.9.2/apollo-adminservice-1.9.2-github.zip
wget https://github.com/apolloconfig/apollo/releases/download/v1.9.2/apollo-configservice-1.9.2-github.zip
wget
3.2. 创建目录
mkdir apollo-adminservice-1.9.2
mkdir apollo-configservice-1.9.2
mkdir
3.3. 解压
unzip apollo-adminservice-1.9.2-github.zip
unzip apollo-configservice-1.9.2-github.zip
unzip
3.4. 配置
- apollo-adminservice配置
cd apollo-adminservice-1.9.2/config/
vim
这里需要修改数据的连接地址、用户名、密码
- apollo-configservice配置
cd
这里需要修改数据的连接地址、用户名、密码
- apollo-portal配置
cd apollo/apollo-portal-1.9.2/config
vim
这里需要修改数据的连接地址、用户名、密码
vim
将其他环境都注释掉,打开第一个和第二个即可
3.5. 启动
- apollo-configservice启动
cd
- apollo-adminservice启动
cd
- apollo-portal启动
cd
如无意外,至此,adminservice、configservice、portal都起来了
3.6. 防火墙策略
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=8070/tcp --permanent
firewall-cmd --reload
systemctl status firewalld
3.6. 效果验证
http://192.168.159.139:8080/
http://192.168.159.139:8070/signin
初始账号密码是:apollo/admin
四、测试
4.1. 引入依赖
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.5.0</version>
</dependency>
4.2. 配置
application.properties
server.port=9000
# 配置Appid
app.id=hello
# 配置 Apollo Meta Server
apollo.meta=http://192.168.159.139:8080/
# 配置本地缓存路径
apollo.cacheDir=C:/Users/gblfy/Desktop/apollo/data
# 配置Environment
env=DEV
apollo.bootstrap.enabled=true
4.3. 测试类
package com.gblfy.apolloclient.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Value("${my.name}")
private String name;
@GetMapping("/index")
public String getName() {
return name;
}
}
4.4. 启动项目
4.5. 效果图
http://localhost:9000/hello/index