>
Consul 微服务 集群搭建
2021-02-07 15:22
微服务
  • 2144
  • 527
  • 64
  • 51

Consul简述:

consul是google开源的一个使用go语言开发的服务发现、配置管理中心服务。内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。服务部署简单,只有一个可运行的二进制的包。每个节点都需要运行agent,他有两种运行模式server和client。每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。

Consul集群搭建:

1.首先需要去Consul官网下载:https://www.consul.io/docs/intro

2.在命令行中 输入consul 出现一下信息 证明安装成功。

  1. Usageconsul [--version] [--help] <command[<args>]
  2. Available commands are:
  3.     acl            Interact with Consul's ACLs
  4.     agent          Runs a Consul agent
  5.     catalog        Interact with the catalog
  6.     config         Interact with Consul's Centralized Configurations
  7.     connect        Interact with Consul Connect
  8.     debug          Records a debugging archive for operators
  9.     event          Fire a new event
  10.     exec           Executes a command on Consul nodes
  11.     force-leave    Forces a member of the cluster to enter the "left" state
  12.     info           Provides debugging information for operators.
  13.     intention      Interact with Connect service intentions
  14.     join           Tell Consul agent to join cluster
  15.     keygen         Generates a new encryption key
  16.     keyring        Manages gossip layer encryption keys
  17.     kv             Interact with the key-value store
  18.     leave          Gracefully leaves the Consul cluster and shuts down
  19.     lock           Execute a command holding a lock
  20.     login          Login to Consul using an auth method
  21.     logout         Destroy a Consul token created with login
  22.     maint          Controls node or service maintenance mode
  23.     members        Lists the members of a Consul cluster
  24.     monitor        Stream logs from a Consul agent
  25.     operator       Provides cluster-level tools for Consul operators
  26.     reload         Triggers the agent to reload configuration files
  27.     rtt            Estimates network round trip time between nodes
  28.     services       Interact with services
  29.     snapshot       Saves, restores and inspects snapshots of Consul server state
  30.     tls            Builtin helpers for creating CAs and certificates
  31.     validate       Validate config files/directories
  32.     version        Prints the Consul version
  33.     watch          Watch for changes in Consul

我们看看Consul的页面是怎么样的。

image.png

这个时候Consul界面上 还没有我们自己的服务 :

接下来我们就需要在Consul上去注册我们自己的服务:

  1.  public static void RegisterConsul(this IConfiguration configuration)
  2.         {
  3.             //创建Consul客户端实例
  4.             ConsulClient consulClient = new ConsulClient(cfg =>
  5.             {
  6.                 //Consul监听地址
  7.                 cfg.Address = new Uri("http://localhost:8501");
  8.             });
  9.             //命令行获取IP地址
  10.             var ip = configuration["ip"];
  11.             var port = configuration["port"];
  12.             Random rd = new Random(); 
  13.             //注册服务
  14.             consulClient.Agent.ServiceRegister(new AgentServiceRegistration
  15.             {
  16.                 //服务唯一标识
  17.                 ID = "API_" + rd.Next(10000,99999).ToString() ,
  18.                 //服务分组
  19.                 Name = "API",
  20.                 //服务对应的ip地址
  21.                 Address = ip,
  22.                 //服务对应的端口
  23.                 Port = int.Parse(port),
  24.                 //心跳检查
  25.                 Check = new AgentServiceCheck
  26.                 {
  27.                     //心跳检查路径
  28.                     HTTP = $"http://{ip}:{port}/Heartbeat/index",
  29.                     //检查间隔
  30.                     Interval = TimeSpan.FromSeconds(12),
  31.                     //超时时间
  32.                     Timeout = TimeSpan.FromSeconds(5),
  33.                     //失败多久取消注册
  34.                     DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(60)
  35.                 },
  36.                 Tags = new string[] { weight.ToString() }
  37.             }).Wait();
  38.         }

这个时候我们使用命令行 进行注册Consul服务:

  1. dotnet demo1.dll --urls="http://*:5005" --ip="127.0.0.1" --port=5005

这会再去看看Consul:

Consul服务.png

这个时候API的服务就已经注册好了 我们在来详细看看API里面有几个服务器:


image.png

这会我们看见了API只有一个服务器在运行 但是我们在项目中 经常会遇见项目崩溃的情况 导致无法进行访问。

那么这个时候我们就有多个服务器 就不会存在无法访问。

我们多添加几个服务器。

image.png

这个时候我们API接口 就不会因为崩溃导致不能访问。

因为我们那么多的服务器 不可能全部都崩溃吧 。

然后我们来看看这个Cousul请求我们服务器检查的记录。

image.png

这个Consul每隔一段时间就会检测服务器是否能正常运行 是否能让用户使用。不能使用的服务器不让用户进行去调用。

当API这个上面 有某一个服务器 挂点以后界面上会有怎么样的显示呢?

image.png

这个画红色的就是服务器挂掉啦 。但是有时候会可能是服务承受的压力会导致服务器短暂的不能使用 ,当这个Consul 一直不断请求超过1分钟未响应 这个时候Consul微服务上的节点就会取消接入该服务器点。

image.png

取消接入以后 界面就没有当服务器的信息啦。我们的管理员可以通过微服务去查询问题所在 并及时的重新启动该服务器。

微服务的集群就搭建成功啦。完美!下期讲解:Consul 微服务的访问。

全部留言 ()
返回
顶部