go-micro-入门示例

go-micro-入门示例


demo1

参考: Go Micro 入门指南 - https://erikjiang.github.io/2018/07/05/GoMicroGuide/

服务发现依赖项工具 Consul

首先 micro 这个框架需要且依赖于服务发现工具(service discovery),框架默认的服务发现工具是 Consul ,同时框架的插拔机制也可确保能够切换到其他的服务发现工具上,如Etcd、NATS等,详见 micro plugins

关于consul的安装与运行:

启用模块化

参考: https://micro.mu/docs/framework.html#installation

Making use of go modules will enable this.

1
2
3
4
5
6
# enable go modules
export GO111MODULE=on
# initialise go modules in your app
go mod init
# now go get
go get ./...

安装依赖包

1
2
3
4
5
6
go get -v -u google.golang.org/grpc // gRPC
go get -v -u github.com/golang/protobuf/protoc-gen-go // 安装protoc go插件
go get -v -u github.com/micro/go-micro // Micro 框架
go get -v -u github.com/micro/go-plugins // 插件包
go get -v -u github.com/micro/protoc-gen-micro // protoc micro插件, 用于通过.proto文件生成.micro.go代码文件
go get -v -u github.com/micro/micro // micro工具包
  • 其中有几个会生成可执行程序. micro.exe, protoc-gen-go.exe, protoc-gen-micro.exe (没错, 我是在 windows 中玩的)

  • 安装其他依赖, 不然在 run 下面的 service.go 时会报找不到这些包的错误. ( 貌似安装了 go-plugins 插件包 会自动安装以下依赖 )

    1
    2
    3
    4
    5
    6
    7
    go get -v -u github.com/hashicorp/go-cleanhttp
    go get -v -u github.com/hashicorp/go-rootcerts
    go get -v -u github.com/hashicorp/memberlist
    go get -v -u github.com/hashicorp/serf/coordinate
    go get -v -u github.com/lucas-clemente/quic-go
    go get -v -u github.com/mitchellh/hashstructure
    go get -v -u github.com/mitchellh/mapstructure

创建 proto 文件. user.proto

1
2
3
4
5
6
7
8
9
10
11
12
13
syntax = "proto3";

service User {
rpc Hello(Request) returns (Response) {}
}

message Request {
string name = 1;
}

message Response {
string msg = 1;
}

然后生成 user.micro.gouser.pb.go 文件

1
2
F:\a_link_workspace\go\GoWinEnv_Test01\src\GoMicro (master -> origin)
λ protoc -I ./protos/ --go_out=protoc-gen-go:./proto_gen --micro_out=./proto_gen ./protos/user.proto

创建服务端代码. service.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
package main

import (
"context"
"fmt"
proto "GoMicro/proto_gen"
micro "github.com/micro/go-micro"
)

type User struct{}

func (u *User) Hello(ctx context.Context, req *proto.Request, res *proto.Response) error {
res.Msg = "Hello " + req.Name
return nil
}

func main() {
service := micro.NewService(
micro.Name("user"),
)

service.Init()

proto.RegisterUserHandler(service.Server(), new(User))

if err := service.Run(); err != nil {
fmt.Println(err)
}
}

创建客户端代码. client.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import (
"context"
"fmt"
proto "GoMicro/proto_gen"
micro "github.com/micro/go-micro"
)

func main() {
service := micro.NewService(micro.Name("user.client"))
service.Init()

user := proto.NewUserService("user", service.Client())

res, err := user.Hello(context.TODO(), &proto.Request{Name: "World ^_^"})
if err != nil {
fmt.Println(err)
}
fmt.Println(res.Msg)
}

打开 consul

  1. 命令: consul agent -dev

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ consul agent -dev
    ==> Starting Consul agent...
    Version: 'v1.6.1'
    Node ID: '700d980f-5e00-4595-c759-a54286dab099'
    Node name: 'DESKTOP-B3461GK'
    Datacenter: 'dc1' (Segment: '<all>')
    Server: true (Bootstrap: false)
    Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
    Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
    Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
  2. 可以通过网页访问到. http://127.0.0.1:8500/ui

运行 服务端/客户端

  1. 启动服务端. go run service.go

    1
    2
    3
    4
    5
    F:\a_link_workspace\go\GoWinEnv_Test01\src\GoMicro\test001\srv (master -> origin)
    λ go run service.go
    2019/09/28 02:04:58 Transport [http] Listening on [::]:53802
    2019/09/28 02:04:58 Broker [http] Connected to [::]:53803
    2019/09/28 02:04:58 Registry [mdns] Registering node: user-d9a8a8db-28c6-424f-ab2a-843f80049cea
  2. 启动客户端. go run client.go. 会输出服务端返回的值

    1
    2
    3
    F:\a_link_workspace\go\GoWinEnv_Test01\src\GoMicro\test001\cli (master -> origin)
    λ go run client.go
    Hello World ^_^

列出服务

使用前面生成的工具 micro.exe, 可以查看到 service.go 起的服务 user ( micro.Name("user") )

1
2
3
F:\a_link_workspace\go\GoWinEnv_Test01 (master -> origin)
λ micro list services // 指令
user

web 界面

1
2
3
4
5
6
F:\a_link_workspace\go\GoWinEnv_Test01 (master -> origin)
λ micro web // 指令
2019/09/28 15:23:02 HTTP API Listening on [::]:8082 // 默认端口
2019/09/28 15:23:02 Transport [http] Listening on [::]:60676
2019/09/28 15:23:02 Broker [http] Connected to [::]:60677
2019/09/28 15:23:02 Registry [mdns] Registering node: go.micro.web-4832fb75-d027-4b66-a420-09abae8e6ecb

然后打开 http://localhost:8082/registry 就能访问已有的服务

点击 user 查看服务的具体信息