Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
Apollo包括服务端和客户端两部分
服务端基于Spring Boot和Spring Cloud开发,打包后可以直接运行,不需要额外安装Tomcat等应用容器。
Java客户端不依赖任何框架,能够运行于所有Java运行时环境,同时对Spring/Spring Boot环境也有较好的支持。
架构图待补充
本次安装采用Windows和Apollo1.7.1版本
下载SQL文件,Apollo需要使用到两个数据库
https://github.com/ctripcorp/apollo/tree/master/scripts/sql
下载后解压到:apollo-sql
目录
下载Apollo的三个端
https://github.com/ctripcorp/apollo/releases/tag/v1.7.1
apollo-adminservice-1.7.1-github.zip
下载后解压到:apollo-adminservice
目录
apollo-configservice-1.7.1-github.zip
下载后解压到:apollo-configservice
目录
apollo-portal-1.7.1-github.zip
下载后解压到:apollo-portal
目录
执行apollo-sql
路径下面的两个sql文件到MySQL数据库中
修改配置
编辑apollo-configservice/config/application-github.properties
将数据库信息修改为上一步导入的数据库
这个数据库应该对应的是apolloconfigdb.sql
启动服务
apollo-configservice/scripts
目录GitBash
./startup.sh
测试
大概这样子就是启动成功
访问localhost:8080
,如果是Eureka页面则启动成功
因为ConfigService服务底层帮我们封装了Eureka,所以才会有Eureka面板。
修改配置
和ConfigService服务一样
启动服务
和ConfigService服务一样
测试
访问http://localhost:8080/
查看AdminService服务是否注册到Eureka
也可也访问http://localhost:8090/
查看是否打印出:apollo-adminservice
修改配置
application-github.properties
配置文件和上面一样
Protal服务还有一个apollo-env.properties
配置文件
它主要是配置不同环境的Eureka,用来连接AdminService
将其他的注释起来,只保留一个dev.meat,这个的value是eureka的地址,ConfigService中集成了Eureka;
这个环境配置是根据ApolloPortalDB.ServerConfig
表中的apollo.portal.envs
决定的。
SELECT * FROM ApolloPortalDB.ServerConfig WHERE Key = 'apollo.portal.envs';
#local.meta=http://localhost:8080
dev.meta=http://localhost:8080
#fat.meta=http://fill-in-fat-meta-server:8080
#uat.meta=http://fill-in-uat-meta-server:8080
#lpt.meta=${lpt_meta}
#pro.meta=http://fill-in-pro-meta-server:8080
测试
访问localhost:8070
得到这个界面后表示启动成功(注意,Portal服务不会注册到Eureka中)
默认的账号密码是:apollo / admin
本次集成采用到了服务发现Consul 1.7.1
和Apollo 1.7.1
创建spring-cloud-apollo
工程
加入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- consul注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- 健康检查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Apollo -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
创建启动类
这里创建了一个SpringBoot的启动类,并将这个工程注册到Consul中
并且还有一个Controller,用来测试,这个@Value里的Key,待会我们会在Apollo中配置
package io.mvvm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@SpringBootApplication
@EnableDiscoveryClient
public class ApolloApplication {
public static void main(String[] args) {
SpringApplication.run(ApolloApplication.class, args);
}
@RestController
class DemoController{
@Value("${apollo.version}")
private String apolloVersion;
@GetMapping("/getV")
public String getVersion(){
return apolloVersion;
}
}
}
创建配置文件
创建application.properties
这里发现很奇特的一个问题。application.yml和application.properties在Apollo中居然不一样。
用yml报错。properties就正常。
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
server.port=2333
spring.application.name=spring-cloud-apollo
apollo.bootstrap.enabled=true
apollo.meta=http://localhost:8080
apollo.cacheDir=C:\\Users\\dell\\Desktop\\springcloudalibaba
apollo.accesskey.secret=9562c487f36d47eb948b6a07e9f8ed6b
app.id=202010100102
env=DEV
KEY | 描述 |
---|---|
spring.cloud.consul.host | Consul服务器的地址 |
spring.cloud.consul.port | Consul的端口 |
app.id | AppId唯一标识,在Apollo的Portal服务创建项目时自定义 |
apollo.meta | 这个其实就是Eureka的地址,但是Apollo在ConfigService中已经帮我们实现了集成,所以一般就是填入ConfigService服务的IP+8080 |
apollo.cacheDir | 自定义配置文件缓存到本地的路径,也就是说,Apollo会将配置文件从ConfigService中拿到配置文件后缓存到本地,如果遇到网络波动的时候也可也保证配置文件的正常读取 |
apollo.accesskey.secret | 访问密钥,如果在Apollo中设置了密钥,那么需要在这里填入你设置的密钥才能访问 |
apollo.bootstrap.enabled | 当有些配置如果需要在服务启动阶段就注入的话,那么就需要添加这个为true |
env | 自定义环境(DEV、FAT、UAT、PRO) |
接下来我们需要在Apollo中添加一个apollo.version
的数据
1、创建项目
AppId可以自定义但是一定要和${app.id}一致
应用名称一般直接写模块名称也可以
创建成功之后会进入到项目中
然后我们来添加一个配置信息
首先点击添加配置,然后编写K/V不多说,然后环境DEV,如果是其他环境,可以勾选。
然后提交
提交之后,还不能被服务发现,因为在Apollo中需要手动发布才行
上面我们说到了密钥的配置,这里是在左下角有一个管理密钥按钮
添加之后我们需要启用这个密钥
然后将密钥复制到配置文件中
启动spring-cloud-apollo
服务
访问:http://localhost:2333/getV
得到1.0则配置成功,接下来测试动态修改数据
回到Portal服务
在操作中编辑Key为apollo.version
将1.0修改为2.0后保存
注意:当配置文件修改后,需要手动发布才能被服务监听到刷新配置
点击发布之后,查看控制台
2020-10-10 11:05:26.146 INFO 9144 --- [Apollo-Config-2] c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: 2.0, key: apollo.version, beanName: io.mvvm.ApolloApplication$DemoController, field: io.mvvm.ApolloApplication$DemoController.apolloVersion
再次访问http://localhost:2333/getV
查看是否打印出2.0