forked from pibigstar/go-demo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsul.go
75 lines (67 loc) · 1.38 KB
/
consul.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package consul
import (
"fmt"
"github.com/hashicorp/consul/api"
)
var (
client *api.Client
)
func init() {
config := &api.Config{
Address: "127.0.0.1:8500",
}
var err error
client, err = api.NewClient(config)
if err != nil {
panic(err)
}
}
// 注册一个新服务
func Register(id string) {
registration := &api.AgentServiceRegistration{
ID: id,
Name: "sns-cnt-center",
Address: "127.0.0.1",
Port: 8080,
Tags: []string{"sns"},
}
//增加check
check := &api.AgentServiceCheck{
HTTP: fmt.Sprintf("http://%s:%d%s", registration.Address, registration.Port, "/check"),
Timeout: "5s", //设置超时 5s
Interval: "5s", //设置间隔 5s
}
//注册check服务
registration.Check = check
err := client.Agent().ServiceRegister(registration)
if err != nil {
fmt.Println("register server error : ", err)
}
}
// 读取配置
func ReadConfig(name string) string {
kvs, _, err := client.KV().List(name, nil)
if err != nil {
fmt.Println("get kvs error : ", err)
}
var result string
for _, k := range kvs {
result = string(k.Value)
}
return result
}
// 移除一个服务
func DeleteService(Id string) error {
return client.Agent().ServiceDeregister(Id)
}
// 服务列表
func ListService() error {
service, err := client.Agent().Services()
if err != nil {
return err
}
for k, s := range service {
fmt.Println(k, s.Address)
}
return nil
}