shuvigoss / notes

0 stars 0 forks source link

Consul #1

Open shuvigoss opened 5 years ago

shuvigoss commented 5 years ago
{
  "datacenter": "dc1",
  "data_dir": "/tmp/consul/node1",
  "log_level": "INFO",
  "server": true,
  "node_name": "node1",
  "ui": true,
  "bind_addr": "127.0.0.1",
  "client_addr": "127.0.0.1",
  "advertise_addr": "127.0.0.1",
  "bootstrap_expect": 3,
  "ports": {
    "http": 8500,
    "dns": 8600,
    "server": 8300,
    "serf_lan": 8301,
    "serf_wan": 8302
  }
}
{
  "datacenter": "dc1",
  "data_dir": "/tmp/consul/node2",
  "log_level": "INFO",
  "server": true,
  "node_name": "node2",
  "bind_addr": "127.0.0.1",
  "client_addr": "127.0.0.1",
  "advertise_addr": "127.0.0.1",
  "ports": {
    "http": 8510,
    "dns": 8610,
    "server": 8310,
    "serf_lan": 8311,
    "serf_wan": 8312
  }
}
{
  "datacenter": "dc1",
  "data_dir": "/tmp/consul/node3",
  "log_level": "INFO",
  "server": true,
  "node_name": "node3",
  "bind_addr": "127.0.0.1",
  "client_addr": "127.0.0.1",
  "advertise_addr": "127.0.0.1",
  "ports": {
    "http": 8520,
    "dns": 8620,
    "server": 8320,
    "serf_lan": 8321,
    "serf_wan": 8322
  }
}
shuvigoss commented 5 years ago
consul agent -config-file config1.json
consul agent -config-file config2.json -retry-join=127.0.0.1:8301
consul agent -config-file config3.json -retry-join=127.0.0.1:8301
shuvigoss commented 5 years ago

https://www.imooc.com/article/277290

shuvigoss commented 5 years ago
package main

import (
    "fmt"
    consulapi "github.com/hashicorp/consul/api"
    "github.com/hashicorp/consul/api/watch"
    "log"
    "time"
)

func main() {
    fmt.Println("test begin .")
    config := consulapi.DefaultConfig()
    //config.Address = "localhost"
    fmt.Println("defautl config : ", config)
    client, err := consulapi.NewClient(config)
    if err != nil {
        log.Fatal("consul client error : ", err)
    }
    //创建一个新服务。
    registration := new(consulapi.AgentServiceRegistration)
    registration.ID = "telegraf1"
    registration.Name = "telegraf-agent"
    registration.Port = 8081
    registration.Address = "127.0.0.1"
    registration.Meta = map[string]string{
        "version": "1.7",
    }

    watch.Parse()

    ////增加check。
    //check := new(consulapi.AgentServiceCheck)
    //check.HTTP = fmt.Sprintf("http://%s:%d%s", registration.Address, registration.Port, "/check")
    ////设置超时 5s。
    //check.Timeout = "5s"
    ////设置间隔 5s。
    //check.Interval = "5s"
    ////注册check服务。
    //registration.Check = check
    //log.Println("get check.HTTP:", check)

    err = client.Agent().ServiceRegister(registration)

    if err != nil {
        log.Fatal("register server error : ", err)
    }
    q := consulapi.QueryOptions{}

    agentService, _, _ := client.Agent().Service("consul", &q)
    fmt.Println(agentService)

    time.Sleep(1 * time.Hour)
}