2021-01-23 02:33:03 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2021-01-23 02:37:04 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-01-23 02:33:03 +00:00
|
|
|
)
|
|
|
|
|
|
2021-01-23 05:57:51 +00:00
|
|
|
const (
|
|
|
|
|
consulAddr string = "192.168.8.254:8500"
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-23 02:33:03 +00:00
|
|
|
//Register 注册服务到consul
|
2021-01-23 03:16:46 +00:00
|
|
|
func Register(id string, name string, port int, tags ...string) {
|
2021-01-23 02:33:03 +00:00
|
|
|
// 创建连接consul服务配置
|
|
|
|
|
config := consulapi.DefaultConfig()
|
2021-01-23 05:57:51 +00:00
|
|
|
config.Address = consulAddr
|
2021-01-23 02:33:03 +00:00
|
|
|
client, err := consulapi.NewClient(config)
|
|
|
|
|
if err != nil {
|
2021-01-23 02:37:04 +00:00
|
|
|
log.WithFields(log.Fields{
|
2021-01-23 05:57:51 +00:00
|
|
|
"func": "NewClient",
|
2021-01-23 02:37:04 +00:00
|
|
|
}).Errorf("%v", err)
|
|
|
|
|
return
|
2021-01-23 02:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建注册到consul的服务到
|
|
|
|
|
registration := new(consulapi.AgentServiceRegistration)
|
2021-01-23 03:16:46 +00:00
|
|
|
registration.ID = id
|
|
|
|
|
registration.Name = name
|
|
|
|
|
registration.Port = port
|
|
|
|
|
registration.Tags = tags
|
|
|
|
|
registration.Address = "0.0.0.0"
|
2021-01-23 02:33:03 +00:00
|
|
|
|
|
|
|
|
// 增加consul健康检查回调函数
|
|
|
|
|
check := new(consulapi.AgentServiceCheck)
|
2021-01-23 05:57:51 +00:00
|
|
|
check.GRPC = fmt.Sprintf("%s:%d", registration.Address, registration.Port)
|
2021-01-23 02:33:03 +00:00
|
|
|
check.Timeout = "5s"
|
|
|
|
|
check.Interval = "5s"
|
|
|
|
|
check.DeregisterCriticalServiceAfter = "30s" // 故障检查失败30s后 consul自动将注册服务删除
|
|
|
|
|
registration.Check = check
|
|
|
|
|
|
|
|
|
|
// 注册服务到consul
|
2021-01-23 05:57:51 +00:00
|
|
|
if err := client.Agent().ServiceRegister(registration); err != nil {
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
|
"func": "ServiceRegister",
|
|
|
|
|
}).Errorf("%v", err)
|
|
|
|
|
}
|
2021-01-23 02:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//DeRegister 取消consul注册的服务
|
|
|
|
|
func DeRegister() {
|
|
|
|
|
// 创建连接consul服务配置
|
|
|
|
|
config := consulapi.DefaultConfig()
|
|
|
|
|
config.Address = "127.0.0.1:8500"
|
|
|
|
|
client, err := consulapi.NewClient(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("consul client error : ", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.Agent().ServiceDeregister("111")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//FindServer 从consul中发现服务
|
|
|
|
|
func FindServer() {
|
|
|
|
|
// 创建连接consul服务配置
|
|
|
|
|
config := consulapi.DefaultConfig()
|
|
|
|
|
config.Address = "127.0.0.1:8500"
|
|
|
|
|
client, err := consulapi.NewClient(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("consul client error : ", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取所有service
|
|
|
|
|
services, _ := client.Agent().Services()
|
|
|
|
|
for _, value := range services {
|
|
|
|
|
fmt.Println(value.Address)
|
|
|
|
|
fmt.Println(value.Port)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("=================================")
|
|
|
|
|
// 获取指定service
|
|
|
|
|
service, _, err := client.Agent().Service("111", nil)
|
|
|
|
|
if err == nil {
|
|
|
|
|
fmt.Println(service.Address)
|
|
|
|
|
fmt.Println(service.Port)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//CheckHeath 健康检查
|
|
|
|
|
func CheckHeath() {
|
|
|
|
|
// 创建连接consul服务配置
|
|
|
|
|
config := consulapi.DefaultConfig()
|
|
|
|
|
config.Address = "127.0.0.1:8500"
|
|
|
|
|
client, err := consulapi.NewClient(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("consul client error : ", err)
|
|
|
|
|
}
|
|
|
|
|
// 健康检查
|
|
|
|
|
a, b, _ := client.Agent().AgentHealthServiceByID("111")
|
|
|
|
|
fmt.Println(a)
|
|
|
|
|
fmt.Println(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//KVTest test
|
|
|
|
|
func KVTest() {
|
|
|
|
|
// 创建连接consul服务配置
|
|
|
|
|
config := consulapi.DefaultConfig()
|
|
|
|
|
config.Address = "127.0.0.1:8500"
|
|
|
|
|
client, err := consulapi.NewClient(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal("consul client error : ", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// KV, put值
|
|
|
|
|
values := "test"
|
|
|
|
|
key := "go-consul-test/127.0.0.1:8100"
|
|
|
|
|
client.KV().Put(&consulapi.KVPair{Key: key, Flags: 0, Value: []byte(values)}, nil)
|
|
|
|
|
|
|
|
|
|
// KV get值
|
|
|
|
|
data, _, _ := client.KV().Get(key, nil)
|
|
|
|
|
fmt.Println(string(data.Value))
|
|
|
|
|
|
|
|
|
|
// KV list
|
|
|
|
|
datas, _, _ := client.KV().List("go", nil)
|
|
|
|
|
for _, value := range datas {
|
|
|
|
|
fmt.Println(value)
|
|
|
|
|
}
|
|
|
|
|
keys, _, _ := client.KV().Keys("go", "", nil)
|
|
|
|
|
fmt.Println(keys)
|
|
|
|
|
}
|