/
register.go
52 lines (44 loc) · 1.21 KB
/
register.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
package main
import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/sd"
"github.com/go-kit/kit/sd/consul"
"github.com/hashicorp/consul/api"
"github.com/pborman/uuid"
"os"
"strconv"
)
func Register(consulHost, consulPort, svcHost, svcPort string, logger log.Logger) (registar sd.Registrar) {
// 创建Consul客户端连接
var client consul.Client
{
consulCfg := api.DefaultConfig()
consulCfg.Address = consulHost + ":" + consulPort
consulClient, err := api.NewClient(consulCfg)
if err != nil {
logger.Log("create consul client error:", err)
os.Exit(1)
}
client = consul.NewClient(consulClient)
}
// 设置Consul对服务健康检查的参数
check := api.AgentServiceCheck{
HTTP: "http://" + svcHost + ":" + svcPort + "/health",
Interval: "10s",
Timeout: "1s",
Notes: "Consul check service health status.",
}
port, _ := strconv.Atoi(svcPort)
//设置微服务想Consul的注册信息
reg := api.AgentServiceRegistration{
ID: "arithmetic" + uuid.New(),
Name: "arithmetic",
Address: svcHost,
Port: port,
Tags: []string{"arithmetic", "raysonxin"},
Check: &check,
}
// 执行注册
registar = consul.NewRegistrar(client, ®, logger)
return
}