准备工作
Java
Java 版本要求 1.8+,可通过如下命令检查:
java -version
样例输出:
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
MySQL
MySQL 版本要求 5.6.5+,MySQL 原生客户端连接数据库后可通过如下命令检查:
select version();
样例输出:
+------------+
| version() |
+------------+
| 5.7.39-log |
+------------+
下载 Quick Start 安装包
Github 下载地址 https://github.com/apolloconf...
百度网盘下载地址 https://pan.baidu.com/s/1Ieel... 提取码:9wwe
安装步骤
创建数据库
在 MySQL 中创建 ApolloPortalDB 和 ApolloConfigDB 两个数据库:
drop database if exists `ApolloPortalDB`;
create database `ApolloPortalDB` default character set utf8 collate utf8_general_ci;
drop database if exists `ApolloConfigDB`;
create database `ApolloConfigDB` default character set utf8 collate utf8_general_ci;
导入数据
MySQL 原生客户端连接数据库后向两个数据库中导入数据:
use ApolloPortalDB;
source /apollo-quick-start/sql/apolloportaldb.sql;
use ApolloConfigDB;
source /apollo-quick-start/sql/apolloconfigdb.sql;
修改配置
配置数据库连接信息,修改 /apollo-quick-start/demo.sh:
#apollo config db info
apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=用户名
apollo_config_db_password=密码(如果没有密码,留空即可)
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=用户名
apollo_portal_db_password=密码(如果没有密码,留空即可)
注意:
- 数据库用户需要有读写权限。
- 不要修改 demo.sh 的其他部分。
启动 Apollo 配置中心
步骤:
- 确保端口 8070、8080、8090 未被占用。
- 执行启动脚本
-->demo.sh start
。
注意:
- 可通过脚本
-->demo.sh stop
停止 Apollo。 - 可查看 service 和 portal 目录下的 log 文件排查问题。
使用配置中心
- 访问 http://localhost:8070。
- 输入用户名 apollo,密码 admin 登录。
SpringBoot 整合 Apollo
导入 maven 依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
</parent>
<dependencies>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
修改 application.yml 配置文件
server:
port: 8082 #避免与 8070、8080、8090 冲突
app:
id: SampleApp #Apollo 配置中心项目的 AppId
apollo:
#meta: Config Services 和 Admin Services 注册在 Eureka 中,可通过 ->管理员信息->系统信息查看
meta: http://localhost:8080
bootstrap:
enabled: true #应用启动阶段将 yml 中 Apollo 配置信息注入 Spring 容器
namespaces: application #默认值,可自行创建
eagerLoad:
enabled: true #饥饿加载,在初始化日志系统前就加载 Apollo 配置
logging:
level:
com: info #系统默认日志以 info 模式输出
在 Apollo 配置中心添加配置
ApolloController 类
@Slf4j
@RestController
public class ApolloController {
@Value("${test}")
private String test;
@GetMapping("/test")
public String test() {
System.out.println("test: " + (test));
return "test: " + test;
}
}
启动类
@SpringBootApplication
@EnableApolloConfig
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试配置获取
访问 http://localhost:8082/test 可以发现 test 值为 Apollo 配置中心配置的值。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。