Apollo入门学习
简介
Apollo是一个配置中心,类比的配置中心有Spring Cloud Config 以及 Alibaba的Nacos等
安装
1:因为Apollo是基于Spring Cloud开发的,所以它的安装包是3个jar包,到官网 https://github.com/apolloconfig/apollo/releases 下载三个jar包:
apollo-adminservice-2.0.1-github.zip 管理后台的下层服务
apollo-configservice-2.0.1-github.zip 配置的下层服务
apollo-portal-2.0.1-github.zip 管理后台的应用层
2:另外:Apollo数据是基于Mysql的,我们还需要在官网源码中找到sql文件:
然后在Mysql中执行上述2个文件,会生成2个库,具体是干嘛的后续说明。
3:启动Jar包
启动apollo-configservice,在apollo目录下执行如下命令
java -Xms256m -Xmx256m -Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password=pbteach0430 -jar apollo-configservice-1.3.0.jar
启动apollo-adminservice
java -Xms256m -Xmx256m -Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password=pbteach0430 -jar apollo-adminservice-1.3.0.jar
启动apollo-portal
java -Xms256m -Xmx256m -Ddev_meta=http://localhost:8080/ -Dserver.port=8070 -Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password=pbteach0430 -jar apollo-portal-1.3.0.jar
注意上面的数据库的地址等要和你自己的对应
这样我们的服务端就算安装完成了
核心概念
- application(应用) 对应管理后台配置的appId
- env(环境) 用于区分环境,比如开发 测试 生产
- cluster(集群) 一个应用程序下不同实例的分组 比如可以区分为 北京区域 福州区域
- namespace(命名空间) 一个应用下不同配置的分组 可以简单的把namespace类比成文件,比如数据库配置文件
集成SpringBoot
1:引入apollo-client pom文件
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
2:修改 application.yml 配置文件
app:
id: account-service
3:apollo.bootstrap
apollo.bootstrap.enabled = true
apollo.bootstrap.namespaces = application,xxxxx
4:apollo-env.properties
dev.meta=http://localhost:8080
pro.meta=http://localhost:8081
5:集群环境建议采用 启动命令的方式配置
6:启动类
@SpringBootApplication(scanBasePackages = "com.xxx.xxx")
@EnableApolloConfig
public class AccountApplication {
public static void main(String[] args) {
SpringApplication.run(AccountApplication.class, args);
}
}
然后通过 @Value的方式就可以读取出来了
生产的一般规划
一般生产如果区分多个环境,那么 configserver 和 adminserver 是需要部署多套的,且数据库也要区分开,portal 则不需要,可以在启动的时候指定mata地址即可。
网友评论