Go 使用 Consul 的简单操作

本文最后更新于:2021年10月20日 中午

  • Consul Docker 安装命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    docker run -d -p 8500:8500 -p 8300:8300 -p 8301:8301 -p 8302:8302 -p 8600:8600/udp consul consul agent -dev -client=0.0.0.0

    // 开机关机 自动重启服务
    docker container update --restart=always 容器ID

    默认的web端口号是:8500
    默认的 dns 端口号是:8600,
    dig 命令查询
    dig @127.0.0.1 -p 8600 consul.service.consul SRV


  • Consul 服务注册

    1
    2
    3
    http://127.0.0.1:8500/v1/agent/service/register  PUT请求
    headers 里面需要写 Content-Type application/json
    body 选 raw json

    参数如下

    1
    2
    3
    4
    5
    6
    7
    {
    "Name": "shop_web",
    "ID": "shop_web",
    "Tags": ["shop_api", "user_api", "shop", "user"],
    "Address": "127.0.0.1",
    "Port": 8088
    }

    注册的时候添加健康检查

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    improt "github.com/hashicorp/consul/api"

    func Register(address string, port int, name string, tags []string, id string) error {
    cfg := api.DefaultConfig()
    cfg.Address = "127.0.0.1:8500"

    client, err := api.NewClient(cfg)
    if err != nil {
    panic(err)
    }

    // 生成对应的检查对象
    check := &api.AgentServiceCheck{
    HTTP: "http://192.168.10.178:8089/health",
    Timeout: "5s",
    Interval: "5s",
    DeregisterCriticalServiceAfter: "10s",
    }

    // 生成注册对象
    registration := new(api.AgentServiceRegistration)
    registration.Name = name
    registration.ID = id
    registration.Port = port
    registration.Address = address
    registration.Tags = tags
    registration.Check = check

    err = client.Agent().ServiceRegister(registration)
    if err != nil {
    panic(err)
    }
    return nil
    }

    Register("192.168.10.178", 8089, "user_web", []string{"shop", "user"}, "user_web")

    查询所有已经注册过的服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    func AllServices() {
    cfg := api.DefaultConfig()
    cfg.Address = "127.0.0.1:8500"
    client, err := api.NewClient(cfg)
    if err != nil {
    panic(err)
    }
    data, err := client.Agent().Services()
    if err != nil {
    panic(err)
    }
    for i, _ := range data {
    fmt.Println(i)
    }
    }

    获取指定的服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    func FilterServices() {
    cfg := api.DefaultConfig()
    cfg.Address = "127.0.0.1:8500"
    client, err := api.NewClient(cfg)
    if err != nil {
    panic(err)
    }
    data, err := client.Agent().ServicesWithFilter(`Service == "user_web"`)
    if err != nil {
    panic(err)
    }
    for i, _ := range data {
    fmt.Println(i)
    }
    }
  • Consul 服务注销

    1
    http://127.0.0.1:8500/v1/agent/service/deregister/这里是服务的ID  PUT请求
  • Grpc 下添加 Consul 服务

    添加指定的包

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    import "google.golang.org/grpc/health/grpc_health_v1"

    // 服务器启动监听后添加
    grpc_health_v1.RegisterHealthServer(server, health.NewServer())
    // 服务注册
    cfg := api.DefaultConfig()
    cfg.Address = fmt.Sprintf("%s:%d", global.ServerConfig.ConsulInfo.Host, global.ServerConfig.ConsulInfo.Port)
    client, err := api.NewClient(cfg)
    if err != nil {
    panic(err)
    }

    // 生成对应的检查对象
    check := &api.AgentServiceCheck{
    GRPC: fmt.Sprintf("192.168.10.178:8088"),
    Timeout: "5s",
    Interval: "5s",
    DeregisterCriticalServiceAfter: "15s",
    }

    // 生成注册对象
    registration := new(api.AgentServiceRegistration)
    registration.Name = global.ServerConfig.Name
    registration.ID = global.ServerConfig.Name
    registration.Port = *Port
    registration.Address = "192.168.10.178"
    registration.Tags = []string{"shop", "shop_srv"}
    registration.Check = check

    err = client.Agent().ServiceRegister(registration)
    if err != nil {
    panic(err)
    }

  • web 端需要完成服务注册和服务发现