微服务Consul系列之服务部署、搭建、使用

280次阅读  |  发布于3年以前

使用Consul解决了哪些问题:

以上只是列举的笔者曾经遇到的几点问题,当然问题还不止于这些,下面介绍的Consul可以有效解决这些问题,当然还有一些其它的优点,让我们一起期待下文的Consul的讲解。

Consul的四大核心特性:

Consul架构

图片来自官网 Consul Architecture

上图很好的展示了Consul对于多数据中心的支持,另外在两个数据中心之间只有Service层可以相互通信。

Consul是一个分布式高可用的系统,一个发现和配置服务的工具。客户端可以利用它提供的API注册和发现服务,及监控检测功能实现服务的高可用,深入的架构描述具体细节,可以参考官网Consul Architecture,下面开始进入实战操作部分。

安装部署

本处主要以linux来讲解,其他操作平台见官网Download Consul

下载 wget https://releases.hashicorp.com/consul/1.4.0/consul_1.4.0_linux_amd64.zip

解压 unzip consul_1.4.0_linux_amd64.zip 得到目录consul 复制 consul到你的系统的任何一个地方,如果你想使用命令行直接访问,确保复制的目录在你的PATH里 cp consul /usr/local/bin/

验证consul是否安装成功,出现以下窗口则安装成功

[root@iZbp1isjfk2rw8fpnxx8wgZ ~]# 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
    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
    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
    validate       Validate config files/directories
    version        Prints the Consul version
    watch          Watch for changes in Consul

Consul Agent

执行consul agent -dev,启动开发模式,这个模式会快速启动一个单节点的Consul。注意,这个模式不能数据持久化,因此,不能用于生产环境

启动命令简介:

Start Agent

[root@iZbp1isjfk2rw8fpnxx8wgZ ~]# consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
           Version: 'v1.4.0'
           Node ID: '9e05f4d6-56c1-e57c-c726-15d9ab1c0dd5'
         Node name: 'iZbp1isjfk2rw8fpnxx8wgZ'
        Datacenter: 'dc1' (Segment: '<all>')
            Server: true (Bootstrap: false)
       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

看下 consul agent 输出的几个重要信息:

查看集群成员

打开一个新终端执行consul members,可以看到集群的成员。

Stop Agent

Agent两种停止方式:gracefullyforcefully gracefully方式停止,则是发送中断信号到Agent进程两种方法:Ctrl+Ckill -INT consul_pid

服务注册

Consul服务搭建好之后,通过提供服务定义HTTP API注册一个服务,通用的模式是通过提供服务定义的方式,下文还会介绍怎么应用Consul进行健康检查

提供服务定义方式服务注册

创建目录/etc/consul.d(.d 后缀意思是这个路径包含了一组配置文件),Consul会载入该目录下的所有文件。

例如我现在有个测试服务test01端口为3010

sudo mkdir /etc/consul.d/test01.json

{
    "service":{
        "name":"test01",
        "tags":[
            "",
            ""
        ],
        "address":"127.0.0.1",
        "port":3010,
        "enable_tag_override": false,
        "check":{
            "deregisterCriticalServiceAfter":"90m",
            "http":"http://127.0.0.1:3010/health",
            "interval":"10s"
        }
    }
}

服务定义配置文件含义:

重启Agent设置配置目录

consul agent -dev -config-dir /etc/consul.d

看以下运行结果:

启动之后控制台输出了Synced service "test01",意思是Agent从配置文件中载入了服务定义,且成功注册到服务目录,另外右边的服务test01也收到了健康检查接口调用

采用HTTP API服务注册

调用/v1/agent/service/register接口进行注册,请求Method为PUT方式

请求Body值为服务定义中的service值,看一下示例:

curl -X PUT \
  http://127.0.0.1:8301/v1/agent/service/register \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 6b672c02-350f-3d1c-7793-1a0d9e800fc9' \
  -d '{
    "id": "test01",
    "name":"test01",
    "tags":[
        "",
        ""
    ],
    "address":"127.0.0.1",
    "port":3010,
    "check":{
        "deregisterCriticalServiceAfter":"90m",
        "http":"http://127.0.0.1:3010/health",
        "interval":"10s"
    }
}'

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8