传统配置文件的弊端
- 静态化配置,例如env文件
- 配置文件无法区分环境
- 配置文件过于分散
- 历史版本无法查看
配置中心如何解决的呢?配置中心的思路是把项目中的配置参数全部放在一个集中的地方来管理,并提供一套标准的接口,当各个服务需要获取配置的时候就来拉取信息,当配置中心有更新的时候,也能通知其他服务,实时同步最新消息。
微服务配置中心
- 配置信息的管理
- 配置信息的查看、读取、更新等,完善的Api管理界面
- 高可用、权限管理等功能
主流的配置中心
- Apollo是由携程开源的分布式配置中心
- Spring Cloud Config
- Consul
Consul 配置操作
1.添加配置信息
2.获取配置信息
GET http://192.168.88.144:8500/v1/kv/mic/pro/pro
[
{
"LockIndex":0,
"Key":"mic/pro/pro",
"Flags":0,
"Value":"ewoJImhvc3QiOiIxMjcuMC4wLjEiLAogICJwcm90IjogMzMwNiwKICAidXNlciI6InRlc3QiLAogICJwd2QiOiIxMjcuMC4wLjEiCn0=",
"CreateIndex":473,
"ModifyIndex":473
}
]
单点服务器Consul集群
补充一下单点服务器Consul集群的步骤,我的虚拟机ip 192.168.88.144
,配置3个节点, Consul v1.12.1
server01@server01-virtual-machine:~$ consul version
Consul v1.12.1
wget https://releases.hashicorp.com/consul/1.12.1/consul_1.12.1_darwin_arm64.zip
unzip consul_1.12.1_darwin_arm64.zip
mv consul /usr/local/bin/consul
目录:
├── client1
├── client2
├── condifg
├── data
├── server1
│ ├── basic.json
│ ├── data
│ ├── log
│ └── nohup.out
├── server2
│ ├── basic.json
│ ├── data
│ ├── log
│ └── nohup.out
└── server3
├── basic.json
├── data
├── log
└── nohup.out
server1 basic.json详细参数,执行命令consul agent -config-dir=/home/server01/soft/consul/server1/basic.json
{
"bind_addr":"127.0.0.1",
"client_addr":"0.0.0.0",
"ports":{
"http":8500,
"dns":8600,
"serf_lan":8011,
"serf_wan":8002,
"server":8700
},
"datacenter":"dc1",
"data_dir":"/home/server01/soft/consul/server1/data",
"log_level":"INFO",
"log_file":"/home/server01/soft/consul/server1/log/consul.log",
"node_name":"consul-server-1",
"disable_host_node_id":true,
"server":true,
"ui":true,
"bootstrap_expect":3,
"rejoin_after_leave":true,
"retry_join":[
"127.0.0.1:8011",
"127.0.0.1:8101",
"127.0.0.1:8201"
]
}
server2 basic.json详细参数,执行命令consul agent -config-dir=/home/server01/soft/consul/server2/basic.json
{
"bind_addr":"127.0.0.1",
"client_addr":"0.0.0.0",
"ports":{
"http":8501,
"dns":8601,
"serf_lan":8111,
"serf_wan":8102,
"server":8701
},
"datacenter":"dc1",
"data_dir":"/home/server01/soft/consul/server2/data",
"log_level":"INFO",
"log_file":"/home/server01/soft/consul/server2/log/consul.log",
"node_name":"consul-server-2",
"disable_host_node_id":true,
"server":true,
"ui":true,
"bootstrap_expect":3,
"rejoin_after_leave":true,
"retry_join":[
"127.0.0.1:8011",
"127.0.0.1:8111",
"127.0.0.1:8211"
]
}
server3 basic.json详细参数,执行命令consul agent -config-dir=/home/server01/soft/consul/server3/basic.json
{
"bind_addr":"127.0.0.1",
"client_addr":"0.0.0.0",
"ports":{
"http":8502,
"dns":8602,
"serf_lan":8211,
"serf_wan":8202,
"server":8702
},
"datacenter":"dc1",
"data_dir":"/home/server01/soft/consul/server3/data",
"log_level":"INFO",
"log_file":"/home/server01/soft/consul/server3/log/consul.log",
"node_name":"consul-server-3",
"disable_host_node_id":true,
"server":true,
"ui":true,
"bootstrap_expect":3,
"rejoin_after_leave":true,
"retry_join":[
"127.0.0.1:8011",
"127.0.0.1:8111",
"127.0.0.1:8211"
]
}