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 出现一下信息 证明安装成功。
- Usage: consul [--version] [--help] <command> [<args>]
- Available commands are:
- acl Interact with Consul's ACLs
- agent Runs a Consul agent
- catalog Interact with the catalog
- config Interact with Consul's Centralized Configurations
- connect Interact with Consul Connect
- debug Records a debugging archive for operators
- event Fire a new event
- exec Executes a command on Consul nodes
- force-leave Forces a member of the cluster to enter the "left" state
- info Provides debugging information for operators.
- intention Interact with Connect service intentions
- join Tell Consul agent to join cluster
- keygen Generates a new encryption key
- keyring Manages gossip layer encryption keys
- kv Interact with the key-value store
- leave Gracefully leaves the Consul cluster and shuts down
- lock Execute a command holding a lock
- login Login to Consul using an auth method
- logout Destroy a Consul token created with login
- maint Controls node or service maintenance mode
- members Lists the members of a Consul cluster
- monitor Stream logs from a Consul agent
- operator Provides cluster-level tools for Consul operators
- reload Triggers the agent to reload configuration files
- rtt Estimates network round trip time between nodes
- services Interact with services
- snapshot Saves, restores and inspects snapshots of Consul server state
- tls Builtin helpers for creating CAs and certificates
- validate Validate config files/directories
- version Prints the Consul version
- watch Watch for changes in Consul
我们看看Consul的页面是怎么样的。
这个时候Consul界面上 还没有我们自己的服务 :
接下来我们就需要在Consul上去注册我们自己的服务:
- public static void RegisterConsul(this IConfiguration configuration)
- {
- //创建Consul客户端实例
- ConsulClient consulClient = new ConsulClient(cfg =>
- {
- //Consul监听地址
- cfg.Address = new Uri("http://localhost:8501");
- });
- //命令行获取IP地址
- var ip = configuration["ip"];
- var port = configuration["port"];
- Random rd = new Random();
- //注册服务
- consulClient.Agent.ServiceRegister(new AgentServiceRegistration
- {
- //服务唯一标识
- ID = "API_" + rd.Next(10000,99999).ToString() ,
- //服务分组
- Name = "API",
- //服务对应的ip地址
- Address = ip,
- //服务对应的端口
- Port = int.Parse(port),
- //心跳检查
- Check = new AgentServiceCheck
- {
- //心跳检查路径
- HTTP = $"http://{ip}:{port}/Heartbeat/index",
- //检查间隔
- Interval = TimeSpan.FromSeconds(12),
- //超时时间
- Timeout = TimeSpan.FromSeconds(5),
- //失败多久取消注册
- DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(60)
- },
- Tags = new string[] { weight.ToString() }
- }).Wait();
-
- }
这个时候我们使用命令行 进行注册Consul服务:
- dotnet demo1.dll --urls="http://*:5005" --ip="127.0.0.1" --port=5005
这会再去看看Consul:
这个时候API的服务就已经注册好了 我们在来详细看看API里面有几个服务器:
这会我们看见了API只有一个服务器在运行 但是我们在项目中 经常会遇见项目崩溃的情况 导致无法进行访问。
那么这个时候我们就有多个服务器 就不会存在无法访问。
我们多添加几个服务器。
这个时候我们API接口 就不会因为崩溃导致不能访问。
因为我们那么多的服务器 不可能全部都崩溃吧 。
然后我们来看看这个Cousul请求我们服务器检查的记录。
这个Consul每隔一段时间就会检测服务器是否能正常运行 是否能让用户使用。不能使用的服务器不让用户进行去调用。
当API这个上面 有某一个服务器 挂点以后界面上会有怎么样的显示呢?
这个画红色的就是服务器挂掉啦 。但是有时候会可能是服务承受的压力会导致服务器短暂的不能使用 ,当这个Consul 一直不断请求超过1分钟未响应 这个时候Consul微服务上的节点就会取消接入该服务器点。
取消接入以后 界面就没有当服务器的信息啦。我们的管理员可以通过微服务去查询问题所在 并及时的重新启动该服务器。
微服务的集群就搭建成功啦。完美!下期讲解:Consul 微服务的访问。