前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Consul 基础7

Consul 基础7

作者头像
franket
发布2021-12-01 17:10:23
发布2021-12-01 17:10:23
20800
代码可运行
举报
文章被收录于专栏:技术杂记技术杂记
运行总次数:0
代码可运行

注册服务

定义服务

定义服务可以使用两种方法:

  • 使用配置文件进行服务定义
  • 调用 HTTP API 进行定义

Consul 会加载配置目录中的所有配置文件,配置文件是以 .json 结尾的,并且以字典顺序加载

创建配置文件

代码语言:javascript
代码运行次数:0
运行
复制
[root@h104 consul]# mkdir /etc/consul.d
[root@h104 consul]# echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' > /etc/consul.d/web.json
[root@h104 consul]# cat /etc/consul.d/web.json 
{"service": {"name": "web", "tags": ["rails"], "port": 80}}
[root@h104 consul]#

指定配置文件启动服务

代码语言:javascript
代码运行次数:0
运行
复制
[root@h104 consul]# consul agent -dev -bind=192.168.100.104 -config-dir /etc/consul.d/
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
         Node name: 'h104'
        Datacenter: 'dc1'
            Server: true (bootstrap: false)
       Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
      Cluster Addr: 192.168.100.104 (LAN: 8301, WAN: 8302)
    Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
             Atlas: <disabled>

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

    2016/03/18 17:07:20 [INFO] serf: EventMemberJoin: h104 192.168.100.104
    2016/03/18 17:07:20 [INFO] serf: EventMemberJoin: h104.dc1 192.168.100.104
    2016/03/18 17:07:20 [INFO] raft: Node at 192.168.100.104:8300 [Follower] entering Follower state
    2016/03/18 17:07:20 [INFO] consul: adding LAN server h104 (Addr: 192.168.100.104:8300) (DC: dc1)
    2016/03/18 17:07:20 [INFO] consul: adding WAN server h104.dc1 (Addr: 192.168.100.104:8300) (DC: dc1)
    2016/03/18 17:07:20 [ERR] agent: failed to sync remote state: No cluster leader
    2016/03/18 17:07:22 [WARN] raft: Heartbeat timeout reached, starting election
    2016/03/18 17:07:22 [INFO] raft: Node at 192.168.100.104:8300 [Candidate] entering Candidate state
    2016/03/18 17:07:22 [DEBUG] raft: Votes needed: 1
    2016/03/18 17:07:22 [DEBUG] raft: Vote granted from 192.168.100.104:8300. Tally: 1
    2016/03/18 17:07:22 [INFO] raft: Election won. Tally: 1
    2016/03/18 17:07:22 [INFO] raft: Node at 192.168.100.104:8300 [Leader] entering Leader state
    2016/03/18 17:07:22 [INFO] raft: Disabling EnableSingleNode (bootstrap)
    2016/03/18 17:07:22 [DEBUG] raft: Node 192.168.100.104:8300 updated peer set (2): [192.168.100.104:8300]
    2016/03/18 17:07:22 [INFO] consul: cluster leadership acquired
    2016/03/18 17:07:22 [DEBUG] consul: reset tombstone GC to index 2
    2016/03/18 17:07:22 [INFO] consul: member 'h104' joined, marking health alive
    2016/03/18 17:07:22 [INFO] consul: New leader elected: h104
    2016/03/18 17:07:25 [INFO] agent: Synced service 'consul'
    2016/03/18 17:07:25 [INFO] agent: Synced service 'web'
    2016/03/18 17:08:44 [DEBUG] dns: request for {h104.node.consul. 1 1} (396.33µs) from client 127.0.0.1:48961 (udp)
    2016/03/18 17:09:15 [DEBUG] agent: Service 'consul' in sync
    2016/03/18 17:09:15 [DEBUG] agent: Service 'web' in sync
...
...
...

输出中表示 Synced service ‘web’,说明定义的服务已经成功被注册进来了,如果要以配置文件的形式注册更多的服务,可以在配置目录中添加其它的服务定义

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 注册服务
    • 定义服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档