概述
- 完成consul的安装后,必须运行agent
- agent可以运行为 server模式、client模式或者dev模式
- 每个数据中心至少必须拥有一台server
- 建议在一个集群中有3或者5个server
- 部署单一server,在出现失败时,会不可避免的出现数据丢失
consul的角色
- client客户端: 将 HTTP 和 DNS 接口请求转发给局域网内的Server服务端集群
- server服务端: 保存配置信息、实现高可用集群、在局域网内与本地客户端通讯、通过广域网与其他数据中心通讯等,每个数据中心的 server 数量推荐为 3 个或是 5 个
Consul相关命令
- 使用命令 consul -h 可以查看consul支持的所有参数,而且每个参数里面还支持其他参数
$ consul -h 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 license Interact with Consul Enterprise licensing 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 namespace Interact with Consul Enterprise Namespaces operator Provides cluster-level tools for Consul operators partition Interact with Consul Enterprise admin partitions peering Create and manage peering connections between Consul clusters 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 troubleshoot CLI tools for troubleshooting Consul service mesh validate Validate config files/directories version Prints the Consul version watch Watch for changes in Consul
- agent:指令是consul的核心,它运行agent来维护成员的重要信息、运行检查、服务宣布、查询处理等等。
- consul agent -dev 开发者模式启动consul
- 开发阶段我们可以使用下面命令启动consul ,执行 consul agent -dev 启动了一个consul 服务端
- $
consul agent -dev
- 访问 http://localhost:8500 可以打开Web管理界面
Consul和Grpc结合使用
1 ) 开发模式
- 主要用于开发阶段(dev模式也是server模式)
2 ) 启动consul
- 开发阶段通过 consul agent -dev 启动consul
3 ) 下载consul依赖
- $
cnpm i consul --save
- 文档参考:https://github.com/silas/node-consul#readme
4 ) 把grpc服务注册到consul上
const Consul = require('consul');
const consul = new Consul({ host: '127.0.0.1', port: 8500, promisify: true }); // 客户端需要加 promisify
const serviceName = "someServer"
consul.agent.service.register(
{
name: serviceName,
address: '127.0.0.1',
port: 3000,
check: {
tcp: "127.0.0.1:3000",
interval: '10s', // 每轮间隔
timeout: '5s', // 超时时间
}
},
function(err, result) {
if (err) {
console.error(err);
throw err;
}
console.log(result)
console.log(serviceName + ' 注册成功!');
}
)
- 编写多个微服务,启动它们,直接访问:http://localhost:8500/
- 就可以自动重定向到consul 的web管理界面,可以查看服务列表
5 ) 注销某个服务
const Consul = require('consul');
// 1、获取consul实例
const consul = new Consul({ host: '127.0.0.1', port: 8500, promisify: true });
const serviceName = "someServer";
// 2、注销微服务
consul.agent.service.deregister(serviceName)