Golang 微服务 - 03 以 consul 方式注册服务

nnealguo · · 2453 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

安装

Install Consul

下载地址 https://www.consul.io/downloads.html,解压后,将 consul_1.2.3_windows_amd64\consul.exe 拷贝到 GOBIN 目录下(比如:C:\Users\your-host-name\go\bin)

Run the Agent

使用 Git Bash Here,输入如下命令,会看到相应日志

$ consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
           Version: 'v1.2.3'
           Node ID: '542fc5d7-cb10-22e7-54bc-3c12481a3e2b'
         Node name: 'DESKTOP-BFE31M3'
        Datacenter: 'dc1' (Segment: '<all>')
            Server: true (Bootstrap: false)
       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

简单起见,这里以 development 模式启动了 Consul agent,该 agent 运行在 Server 模式下,形成单节点的 Consul 集群。

$ consul members
Node             Address         Status  Type    Build  Protocol  DC   Segment
DESKTOP-BFE31M3  127.0.0.1:8301  alive   server  1.2.3  2         dc1  <all>

查看 Consul 集群中节点的信息:节点名、地址、状态、类型...

服务

Run our Services

先后运行两个 greeter/service.go,会看到 Consul 集群中打印如下信息

[INFO] agent: Synced service "greeter-9413c936-b95b-11e8-a2a4-0a0027000009"
[DEBUG] agent: Node info in sync
[DEBUG] http: Request PUT /v1/agent/service/register (1.0039ms) from=127.0.0.1:59065
[DEBUG] agent: Service "greeter-9413c936-b95b-11e8-a2a4-0a0027000009" in sync
[DEBUG] agent: Node info in sync

[DEBUG] agent: Service "greeter-9413c936-b95b-11e8-a2a4-0a0027000009" in sync
[INFO] agent: Synced service "greeter-dfc3152f-b95b-11e8-aea2-0a0027000009"
[DEBUG] agent: Node info in sync
[DEBUG] http: Request PUT /v1/agent/service/register (999.9µs) from=127.0.0.1:59075
[DEBUG] agent: Service "greeter-9413c936-b95b-11e8-a2a4-0a0027000009" in sync
[DEBUG] agent: Service "greeter-dfc3152f-b95b-11e8-aea2-0a0027000009" in sync
[DEBUG] agent: Node info in sync

使用 Git Bash Here,启动了两个 greeter 服务端。再启动一个客户端 greeter/client.go,将看到正确的输出“Hello John”(命令见附录A

Show the Services

查看 Consul 集群中两个服务的详情,将得到 json 格式的内容

$ curl http://localhost:8500/v1/catalog/service/greeter
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3307    0  3307    0     0  15100      0 --:--:-- --:--:-- --:--:-- 15100
[
    {
        "ID": "5330b88d-a1ab-b1a1-e4ce-22b51f8a7125",
        "Node": "DESKTOP-BFE31M3",
        "Address": "127.0.0.1",
        "Datacenter": "dc1",
        "TaggedAddresses": {
            "lan": "127.0.0.1",
            "wan": "127.0.0.1"
        },
        "NodeMeta": {
            "consul-network-segment": ""
        },
        "ServiceKind": "",
        "ServiceID": "greeter-9413c936-b95b-11e8-a2a4-0a0027000009",
        "ServiceName": "greeter",
        "ServiceTags": [
            "t-789caa562a294acc2b2ec82f2a51b252ca28292950aa05040000ffff4f930768",
            "t-789caa564a2acacf4e2d52b252ca28292950aa05040000ffff364d0600",
            "t-789caa562a4e2d2a4b2d52b2522a2a4856aa05040000ffff30dd0597",
            "t-789caa562a4a4dcf2c2e29aa54b2524acecf2b2ecd51aa05040000ffff560f07c8",
            "e-789cdc90b1eac2301087dfe5e6feff832e9217d0d9b57438f06729a469bdbb08227d7769ad51da0cce4e81efbe5cc277a7c02dc8d15e0083fc1fe07d4705092e116ae492314d8e332ec86e7d865ed94728b9325d9b8ea4ab4913ea0f3144ef8722d97f6ff52ba9ac62136c977df93599d5a5382dadf26b9b60db4dc62581f65d50acbbcc7c1926e1f5ffea31f9b3c68fd469617c62e3b18e9a805b727466afa06178040000ffffa215cb62",
            "v-789c32d433d03300040000ffff02ce00ee"
        ],
        "ServiceAddress": "192.168.56.1",
        "ServiceWeights": {
            "Passing": 1,
            "Warning": 1
        },
        "ServiceMeta": {},
        "ServicePort": 59063,
        "ServiceEnableTagOverride": false,
        "ServiceProxyDestination": "",
        "ServiceConnect": {
            "Native": false,
            "Proxy": null
        },
        "CreateIndex": 13,
        "ModifyIndex": 152
    },
    {
        "ID": "5330b88d-a1ab-b1a1-e4ce-22b51f8a7125",
        "Node": "DESKTOP-BFE31M3",
        "Address": "127.0.0.1",
        "Datacenter": "dc1",
        "TaggedAddresses": {
            "lan": "127.0.0.1",
            "wan": "127.0.0.1"
        },
        "NodeMeta": {
            "consul-network-segment": ""
        },
        "ServiceKind": "",
        "ServiceID": "greeter-dfc3152f-b95b-11e8-aea2-0a0027000009",
        "ServiceName": "greeter",
        "ServiceTags": [
            "t-789caa562a4e2d2a4b2d52b2522a2a4856aa05040000ffff30dd0597",
            "t-789caa562a4a4dcf2c2e29aa54b2524acecf2b2ecd51aa05040000ffff560f07c8",
            "t-789caa562a294acc2b2ec82f2a51b252ca28292950aa05040000ffff4f930768",
            "t-789caa564a2acacf4e2d52b252ca28292950aa05040000ffff364d0600",
            "e-789cdc90b1eac2301087dfe5e6feff832e9217d0d9b57438f06729a469bdbb08227d7769ad51da0cce4e81efbe5cc277a7c02dc8d15e0083fc1fe07d4705092e116ae492314d8e332ec86e7d865ed94728b9325d9b8ea4ab4913ea0f3144ef8722d97f6ff52ba9ac62136c977df93599d5a5382dadf26b9b60db4dc62581f65d50acbbcc7c1926e1f5ffea31f9b3c68fd469617c62e3b18e9a805b727466afa06178040000ffffa215cb62",
            "v-789c32d433d03300040000ffff02ce00ee"
        ],
        "ServiceAddress": "192.168.56.1",
        "ServiceWeights": {
            "Passing": 1,
            "Warning": 1
        },
        "ServiceMeta": {},
        "ServicePort": 59073,
        "ServiceEnableTagOverride": false,
        "ServiceProxyDestination": "",
        "ServiceConnect": {
            "Native": false,
            "Proxy": null
        },
        "CreateIndex": 23,
        "ModifyIndex": 153
    }
]

其中包括 ServiceName, ServiceID, ServicePort 等重要信息

附录A

cd $GOPATH/src/greeter
go run service.go
cd $GOPATH/src/greeter
go run client.go

服务端和客户端启动时,不再需要 mdns 了,服务注册和发现由 Consul 来完成。

参考

《Getting Started: Install Consul, Run the Agent, Services》https://www.consul.io/intro/getting-started/install.html


有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:nnealguo

查看原文:Golang 微服务 - 03 以 consul 方式注册服务

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2453 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传