1.为什么要使用分布式配置中心?

统一管理微服务配置文件,可以实现动态刷新配置文件。

SpringCloudConfig与阿波罗的区别:

前者是将配置存放到git和数据库(1.3.4以后版本)中,后者是将配置存放到数据库中。

2.搭建分布式配置中心阿波罗

1.下载aplolo配置中心

https://github.com/nobodyiam/apollo-build-scripts

2.上传到服务器中,并解压

unzip apollo-build-scripts-master.zip
# 若无unzip命令,需安装
yum -y install zip unzip

3.创建数据库

执行sql目录中的两个sql文件

sql/apolloportaldb.sql
sql/apolloconfigdb.sql

4.配置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=密码(如果没有密码,留空即可)

5.修改服务器端口地址信息

#将地址改为服务器IP地址 
config_server_url=http://10.168.16.125:8080
admin_server_url=http://10.168.16.125:8090
portal_url=http://10.168.16.125:8070

5.启动执行脚本(在此之前,确保8080、8090、8070端口未被占用)

#进入解压文件的根目录
./demo.sh start
#若出现权限不足,需执行
chmod 777 ./demo.sh

3.进入Apollo配置中心

http://ip:8070

默认账号密码 Apollo admin

4.服务客户端集成配置文件

1.将本地配置文件信息导入阿波罗配置中心

yml转换properties文件:http://www.toyaml.com/index.html

2.在项目中引入依赖

<!--阿波罗配置中心依赖-->
       <dependency>
           <groupId>com.ctrip.framework.apollo</groupId>
           <artifactId>apollo-client</artifactId>
           <version>1.0.0</version>
       </dependency>
       <dependency>
           <groupId>com.ctrip.framework.apollo</groupId>
           <artifactId>apollo-core</artifactId>
           <version>1.0.0</version>
       </dependency>

3.创建application.properties文件

app.id=app_test   ##阿波罗配置中心中与项目相对于AppId
apollo.meta=http://ip:8080 ##阿波罗ip地址

4.启动类中加入@EnableApolloConfig注解

5.运行测试

5.注意

不是所有的配置文件都会在阿波罗平台修改后,就会立马生效,因为没有采用监听刷新配置文件。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

Nginx相关资料 上一篇
SpringCloud Gateway 下一篇