consul
Hireek 12/1/2022
# Consul
服务发现
# i/o模型
阻塞I/O,同步
非阻塞I/O,先返回,再轮询
I/O多路复用select,单线程处理多socket
AIO,处理完回调。
# 常用命令
- consul agent
- -h
- -dev
- -bind 指定
- http-port=8500 自带web访问的默认端口
- -client=127.0.0.1 默认本机可以访问consul
- -config-dir=foo 所有主动注册的服务的描述信息
- -data-dir=path 储存所有注册过来的srv机器的详细信息
- -dev 开发者模式
- -node=hostname 服务发现的名字
- -rejoin consul启动的时候,加入到的集群
- -server 以服务方式开启consul,允许其他的consul连接到开启的consul,形成集群。不加就是,以客户端方式开启
- -ui 可以使用web页面
- members
- info
- Leave 关闭
# 安装
brew tap hashicorp/tap
brew install hashicorp/tap/consul
1
2
2
# 运行
consul agent -server -bootstrap-expect 1 -data-dir /Users/xx/consul/tmp/consul -node=n1 -bind=127.0.0.1 -ui -rejoin -config-dir=/Users/xx/consul/consul.d/ -client 0.0.0.0
1
注册服务/目录,重新启动
{ "service":
{
"name":"wwowo",
"tags":["itcast","itheima"],
"port":8800
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# go代码
# 服务注册
package main
import (
"fmt"
"github.com/hashicorp/consul/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
"net"
"pb"
)
type world struct {
pb.UnimplementedHelloServer
}
func (this *world) SayHello(ctx context.Context, per *pb.Person) (*pb.Person, error) {
per.Name = "你你你"
per.Age = "18"
return per, nil
}
func main() {
// 把grpc服务,注册到consul上.
// 1. 初始化 consul 配置
consulConfig := api.DefaultConfig()
// 2. 创建 consul 对象
consulClient, err := api.NewClient(consulConfig)
if err != nil {
fmt.Println("api.NewClient err:", err)
return
}
// 3. 告诉consul, 即将注册的服务的配置信息
reg := api.AgentServiceRegistration {
ID:"bj38",
Tags:[]string{"grcp", "consul"},
Name:"grpc And Consul",
Address:"127.0.0.1",
Port:8800,
Check:&api.AgentServiceCheck{
CheckID:"consul grpc test",
TCP:"127.0.0.1:8800",
Timeout:"1s",
Interval:"5s",
},
}
// 4. 注册 grpc 服务到 consul 上
consulClient.Agent().ServiceRegister(®)
// 初始化grpc
server := grpc.NewServer()
pb.RegisterHelloServer(server, new(world))
listen, err := net.Listen("tcp", "127.0.0.1:8800")
if err != nil {
fmt.Println("net.Listen",err)
}
defer listen.Close()
server.Serve(listen)
}
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
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
服务调用
package main
import (
"fmt"
"github.com/hashicorp/consul/api"
"golang.org/x/net/context"
"google.golang.org/grpc"
"pb"
"strconv"
)
func main() {
config := api.DefaultConfig()
newClient, _ := api.NewClient(config)
service, _, _ := newClient.Health().Service("grpc And Consul", "grcp", true, nil)
agentService := service[0].Service
address := agentService.Address + ":" + strconv.Itoa(agentService.Port)
dial, _ := grpc.Dial(address, grpc.WithInsecure())
defer dial.Close()
client := pb.NewHelloClient(dial)
person, _ := client.SayHello(context.TODO(), new(pb.Person))
fmt.Println(person.Name, person.Age)
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 服务注销
package main
import "github.com/hashicorp/consul/api"
func main() {
config := api.DefaultConfig()
client, _ := api.NewClient(config)
client.Agent().ServiceDeregister("bj38")
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9