一个go-micro的demo

weiwenwang · 2019-06-21 18:24:58 · 2547 次点击 · 预计阅读时间 6 分钟 · 大约8小时之前 开始浏览    
这是一个创建于 2019-06-21 18:24:58 的文章,其中的信息可能已经有所发展或是发生改变。

go-micro-微服务框架

启动etcdv3集群

这里我用两台机器三个节点([192.168.3.45:2379, 192.168.3.45:2479], [192.168.3.118:2379])

node1

etcd --name cd0 \
--initial-advertise-peer-urls http://192.168.3.45:2380 \
--listen-peer-urls http://192.168.3.45:2380 \
--listen-client-urls http://192.168.3.45:2379 \
--advertise-client-urls http://192.168.3.45:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster cd0=http://192.168.3.45:2380,cd1=http://192.168.3.45:2480,cd2=http://192.168.3.118:2380 \
--initial-cluster-state new

node2

etcd --name cd1 \
--initial-advertise-peer-urls http://192.168.3.45:2480 \
--listen-peer-urls http://192.168.3.45:2480 \
--listen-client-urls http://192.168.3.45:2479 \
--advertise-client-urls http://192.168.3.45:2479 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster cd0=http://192.168.3.45:2380,cd1=http://192.168.3.45:2480,cd2=http://192.168.3.118:2380 \
--initial-cluster-state new

node3

etcd --name cd2 \
--initial-advertise-peer-urls http://192.168.3.118:2380 \
--listen-peer-urls http://192.168.3.118:2380 \
--listen-client-urls http://192.168.3.118:2379 \
--advertise-client-urls http://192.168.3.118:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster cd0=http://192.168.3.45:2380,cd1=http://192.168.3.45:2480,cd2=http://192.168.3.118:2380 \
--initial-cluster-state new

测试etcd集群是否正常

thomasdeMacBook-Pro:~ www1$ etcdctl --endpoints="http://192.168.3.45:2379" endpoint health
http://192.168.3.45:2379 is healthy: successfully committed proposal: took = 24.957199ms

thomasdeMacBook-Pro:~ www1$ etcdctl --endpoints="http://192.168.3.45:2479" endpoint health
http://192.168.3.45:2479 is healthy: successfully committed proposal: took = 19.160559ms

thomasdeMacBook-Pro:~ www1$ etcdctl --endpoints="http://192.168.3.118:2379" endpoint health
http://192.168.3.118:2379 is healthy: successfully committed proposal: took = 27.850165ms

micro安装etcd插件

在$GOPATH/src/github.com/micro/micro 下新建plugins.go

package main

import (
    _ "github.com/micro/go-plugins/registry/etcdv3"
)
$ go build -i -o micro ./main.go ./plugins.go
$ mv micro $GOBIN/
$ micro --registry=etcdv3 list services

启动服务

clone代码

git clone git@github.com:weiwenwang/go-mcro-demo.git cd go-micro-demo

  • --selector=cache的作用是客户端(api,srv)在调服务器的时候,不用每次都去etcd拿数据,减轻注册中心的压力
  • api1, api2, srv1, srv2是为了模拟多节点, 测试负债均衡

    启动Micro Api

    micro --selector=cache --registry=etcdv3 --registry_address=http://192.168.3.45:2479 api --handler=api
    
    HTTP API Listening on [::]:8080 监听到本地8080端口

启动greeter Api并注册到etcd

cd go-micro-demo/api1(启动第一个)

go run main.go --selector=cache --registry=etcdv3 --registry_address=http://192.168.3.45:2479

cd go-micro-demo/api2(启动第二个)

go run main.go --selector=cache --registry=etcdv3 --registry_address=http://192.168.3.45:2479

启动greeter srv并注册到etcd

cd go-micro-demo/srv/srv1(启动第一个)

go run main.go --selector=cache --registry=etcdv3 --registry_address=http://192.168.3.118:2379
```bash
cd go-micro-demo/srv/srv2(启动第二个)
```bash
go run main.go --selector=cache --registry=etcdv3 --registry_address=http://192.168.3.118:2379

逻辑图

这个图是我借用别处的, 图上的ip和我这不一致, 服务之间的调用完全一样

greeter.png

查看etcdv3所有的key

etcd-list.png

浏览器访问Micro Api

http://localhost:8080/greeter/say/hello?name=John

{
    "api": "api two",
    "message": "Hello John srv one. rand:R"
}

{
    "api": "api one",
    "message": "Hello John srv one. rand:x"
}

{
    "api": "api two",
    "message": "Hello John srv two. rand:Y"
}

{
    "api": "api one",
    "message": "Hello John srv two. rand:a"
}

以上四种结果随机返回

验证注册中心的ttl

thomasdeMacBook-Pro:~ www1$ etcdctl --endpoints="http://192.168.3.45:2379" lease list
found 2 leases
05016b78d58c0234
05016b78d58c0230

thomasdeMacBook-Pro:~ www1$ etcdctl --endpoints="http://192.168.3.45:2379" lease timetolive 05016b78d58c0230 --keys
lease 05016b78d58c0230 granted with TTL(30s), remaining(27s), attached keys([/micro-registry/go.micro.srv.greeter/go.micro.srv.greeter-6382949a-92c6-46c9-b5c9-4cfbb8ce2420])

thomasdeMacBook-Pro:~ www1$ etcdctl --endpoints="http://192.168.3.45:2379" lease timetolive 05016b78d58c0230 --keys
lease 05016b78d58c0230 granted with TTL(30s), remaining(21s), attached keys([/micro-registry/go.micro.srv.greeter/go.micro.srv.greeter-6382949a-92c6-46c9-b5c9-4cfbb8ce2420])

thomasdeMacBook-Pro:~ www1$ etcdctl --endpoints="http://192.168.3.45:2379" lease timetolive 05016b78d58c0230 --keys
lease 05016b78d58c0230 granted with TTL(30s), remaining(29s), attached keys([/micro-registry/go.micro.srv.greeter/go.micro.srv.greeter-6382949a-92c6-46c9-b5c9-4cfbb8ce2420])

这里有两个lease ID, 选一个05016b78d58c0230, 可以看出是存的是srv, ttl是30,remaining小于20的时候就会重新变成30, 符合我们的代码设置

micro.RegisterTTL(time.Second*30),      // 这是设置注册到etcd那个key的过期时间
micro.RegisterInterval(time.Second*10), // 这是服务去etcd报告自己还活着的周期

有疑问加站长微信联系(非本文作者))

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2547 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传