grpc(4):使用 golang 调用consul api 接口,注册user-tomcat服务

释放双眼,带上耳机,听听看~!

1,关于consul


dubbo的注册中心是zookeeper,redis。
motan的注册中心是zookeeper,consul。
kubernetes的注册中心是 etcd。
使用consul的好处是服务发现啥的都支持了。
可以使用域名进行负载均衡。
也是一个不错的 Server-Side Discovery Pattern 。

2,启动consul服务,调用接口


首先要在服务器安装一个consul服务:
http://blog.csdn.net/freewebsys/article/details/56296013
然后下载go的客户端。
go get github.com/hashicorp/consul
然后就可以使用 consul api服务了。


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
1package src
2
3import (
4    "fmt"
5    consulapi "github.com/hashicorp/consul/api"
6    "log"
7    "testing"
8)
9
10const Id = "1234567890"
11
12func TestRegister(t *testing.T) {
13
14    fmt.Println("test begin .")
15    config := consulapi.DefaultConfig()
16    //config.Address = "localhost"
17    fmt.Println("defautl config : ", config)
18    client, err := consulapi.NewClient(config)
19    if err != nil {
20        log.Fatal("consul client error : ", err)
21    }
22    //创建一个新服务。
23    registration := new(consulapi.AgentServiceRegistration)
24    registration.ID = Id
25    registration.Name = "user-tomcat"
26    registration.Port = 8080
27    registration.Tags = []string{"user-tomcat"}
28    registration.Address = "127.0.0.1"
29
30    //增加check。
31    check := new(consulapi.AgentServiceCheck)
32    check.HTTP = fmt.Sprintf("http://%s:%d%s", registration.Address, registration.Port, "/check")
33    //设置超时 5s。
34    check.Timeout = "5s"
35    //设置间隔 5s。
36    check.Interval = "5s"
37    //注册check服务。
38    registration.Check = check
39    log.Println("get check.HTTP:",check)
40
41    err = client.Agent().ServiceRegister(registration)
42
43
44
45    if err != nil {
46        log.Fatal("register server error : ", err)
47    }
48
49}
50
51func TestDregister(t *testing.T){
52
53
54    fmt.Println("test begin .")
55    config := consulapi.DefaultConfig()
56    //config.Address = "localhost"
57    fmt.Println("defautl config : ", config)
58    client, err := consulapi.NewClient(config)
59    if err != nil {
60        log.Fatal("consul client error : ", err)
61    }
62
63    err = client.Agent().ServiceDeregister(Id)
64    if err != nil {
65        log.Fatal("register server error : ", err)
66    }
67
68
69}
70

代码很简单,创建了一个consul的服务,说明tomcat的服务端口,ip。并且声明了一个check方法用来检查服务是否可用。

可以通过ui界面观察服务注册情况:
服务可用。
check失败服务不可用。

3,使用dig命令检查服务



1
2
1yum install bind-utils
2

在服务器上面直接查看user-tomcat 服务情况:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1# dig @10.0.2.15 -p 8600 user-tomcat.service.consul SRV
2
3; <<>> DiG 9.9.4-RedHat-9.9.4-38.el7_3.2 <<>> @10.0.2.15 -p 8600 user-tomcat.service.consul SRV
4; (1 server found)
5;; global options: +cmd
6;; Got answer:
7;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17543
8;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
9;; WARNING: recursion requested but not available
10
11;; QUESTION SECTION:
12;user-tomcat.service.consul.    IN      SRV
13
14;; ANSWER SECTION:
15user-tomcat.service.consul. 0   IN      SRV     1 1 8080 consul-dev.node.dc1.consul.
16
17;; ADDITIONAL SECTION:
18consul-dev.node.dc1.consul. 0   IN      A       127.0.0.1
19
20;; Query time: 0 msec
21;; SERVER: 10.0.2.15#8600(10.0.2.15)
22;; WHEN: Sun Mar 05 03:06:06 EST 2017
23;; MSG SIZE  rcvd: 100
24

consul-dev.node.dc1.consul. 0 IN A 127.0.0.1
可以查询到一个域名节点。

4,总结


本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/60466381
未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

总体上感觉 consul 还是非常简单实用的。
在做 Server-side Discovery 的时候是非常的方便的。
可以降低client的代码逻辑。

给TA打赏
共{{data.count}}人
人已打赏
安全经验

Google Adsense广告CPC(单元点击价格)为什么会减少??

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索