Open shuvigoss opened 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
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)
}