写点什么

微服务 - 搭建 Consul 集群服务,Consul 配置中心

作者:Java你猿哥
  • 2023-04-23
    湖南
  • 本文字数:1848 字

    阅读完需:约 6 分钟

传统配置文件的弊端

  • 静态化配置,例如 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 versionConsul v1.12.1
复制代码


wget https://releases.hashicorp.com/consul/1.12.1/consul_1.12.1_darwin_arm64.zipunzip consul_1.12.1_darwin_arm64.zipmv 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"    ]}
复制代码



用户头像

Java你猿哥

关注

一只在编程路上渐行渐远的程序猿 2023-03-09 加入

关注我,了解更多Java、架构、Spring等知识

评论

发布
暂无评论
微服务 - 搭建Consul集群服务,Consul配置中心_Java_Java你猿哥_InfoQ写作社区