筹备工作

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 infoapollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8apollo_config_db_username=用户名apollo_config_db_password=明码(如果没有明码,留空即可)# apollo portal db infoapollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8apollo_portal_db_username=用户名apollo_portal_db_password=明码(如果没有明码,留空即可)

留神:

  1. 数据库用户须要有读写权限。
  2. 不要批改 demo.sh 的其余局部。

启动 Apollo 配置核心

步骤:

  1. 确保端口 8070、8080、8090 未被占用。
  2. 执行启动脚本 -->demo.sh start

留神:

  1. 可通过脚本 -->demo.sh stop 进行 Apollo。
  2. 可查看 service 和 portal 目录下的 log 文件排查问题。

应用配置核心

  1. 拜访 http://localhost:8070。
  2. 输出用户名 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 配置核心我的项目的 AppIdapollo:  #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@RestControllerpublic class ApolloController {        @Value("${test}")    private String test;         @GetMapping("/test")    public String test() {        System.out.println("test: " + (test));        return "test: " + test;    }}

启动类

@SpringBootApplication@EnableApolloConfigpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

测试配置获取

拜访 http://localhost:8082/test 能够发现 test 值为 Apollo 配置核心配置的值。