micro初探
# micro 微服务框架初探
# 准备工作
需要protobuf
和protoc
以及protoc-gen-go
工具
# 下载这2个插件
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/protoc-gen-micro
1
2
3
4
5
2
3
4
5
greeter.proto
案例
syntax = "proto3";
service Greeter {
rpc Hello(Request) returns (Response) {}
}
message Request {
string name = 1;
}
message Response {
string msg = 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
Generate the code
protoc --proto_path=$GOPATH/src:. --micro_out=. --go_out=. greeter.proto
1
./
greeter.proto # original protobuf file
greeter.pb.go # auto-generated by protoc-gen-go
greeter.micro.go # auto-generated by protoc-gen-micro
1
2
3
4
2
3
4
# 创建一个 mod 项目
定义好module
module microdemo
go 1.17
require (
github.com/golang/protobuf v1.5.2
github.com/micro/go-micro v1.18.0
github.com/micro/go-micro/v2 v2.9.1
google.golang.org/protobuf v1.27.1
)
// 下面的是后续下载依赖生成的 一开始就一个module名称和go的版本
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/coreos/etcd v3.3.18+incompatible // indirect
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.0.0 // indirect
github.com/go-git/go-git/v5 v5.1.0 // indirect
github.com/go-log/log v0.1.0 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/imdario/mergo v0.3.9 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
github.com/micro/cli/v2 v2.1.2 // indirect
github.com/micro/mdns v0.3.0 // indirect
github.com/miekg/dns v1.1.27 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/hashstructure v1.0.0 // indirect
github.com/nats-io/jwt v0.3.2 // indirect
github.com/nats-io/nats.go v1.9.2 // indirect
github.com/nats-io/nkeys v0.1.4 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/xanzy/ssh-agent v0.2.1 // indirect
go.uber.org/atomic v1.5.0 // indirect
go.uber.org/multierr v1.3.0 // indirect
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee // indirect
go.uber.org/zap v1.13.0 // indirect
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20191216173652-a0e659d51361 // indirect
google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1 // indirect
google.golang.org/grpc v1.26.0 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.2.4 // indirect
honnef.co/go/tools v0.0.1-2019.2.3 // indirect
)
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
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
# 创建 proto 文件
创建proto
目录以及子目录cap
在cap
目录下创建proto
文件
demo.proto
syntax = "proto3"; // 定义版本
package go.micro.service.demo; // 定义包名
option go_package="./proto/cap";
// 定义服务
service Cap {
rpc SayHello(SayRequest) returns (SayResponse) {}
}
message SayRequest {
// 类型 名称 = 标识符 1~15 最好不要超过15
string message = 1;
}
message SayResponse {
string answer = 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
生成文件
protoc -I ./ --go_out=./ --micro_out=./ ./proto/cap/demo*.proto
1
生成以下文件:
demo.pb.go
demo.pb.micro.go
1
2
2
# 创建服务端和客户端
我们来下载go-micro/v2
的模块
go get github.com/micro/go-micro/v2
1
服务端:server.go
package main
import (
"context"
"fmt"
"github.com/micro/go-micro" // 这个可能是最新的不是v2
cap2 "microdemo/proto/cap"
)
type CapServer struct{}
func (c *CapServer) SayHello(ctx context.Context, req *cap2.SayRequest, res *cap2.SayResponse) error {
res.Answer = "消息内容是: \"" + req.Message + "\""
return nil
}
func main() {
service := micro.NewService(micro.Name("cap.demo.server"))
service.Init()
cap2.RegisterCapHandler(service.Server(), new(CapServer))
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
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
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
客户端:client.go
package main
import (
demo "cap-demo/proto/cap"
"context"
"fmt"
"github.com/micro/go-micro/v2"
)
func main() {
// 创建新的服务
// 实例化
service := micro.NewService(micro.Name("cap.demo.client"))
// 初始化
service.Init()
capDemo := demo.NewCapService("cap.demo.server", service.Client())
res, err := capDemo.SayHello(context.TODO(), &demo.SayRequest{Message: "无解的游戏"})
if err != nil {
fmt.Println(err)
}
fmt.Println(res.Answer)
}
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
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
# 服务端启动
➜ go run server.go
2021-12-09 00:28:42 file=v2@v2.9.1/service.go:200 level=info Starting [service] cap.demo.server
2021-12-09 00:28:42 file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:55677
2021-12-09 00:28:42 file=grpc/grpc.go:697 level=info Registry [mdns] Registering node: cap.demo.server-bfbca399-5edc-40a8-8064-161233aaabef
1
2
3
4
5
6
2
3
4
5
6
# 客户端
➜ go run client.go
消息内容是: "无解的游戏"
1
2
3
4
2
3
4
# 小结
- Micro 组成:运行时、框架、客户端
- 运行时组成:api、broker、network、new 等
- Go-micro 组件,框架,通信模型,如何编写一个服务
编辑 (opens new window)
上次更新: 2021/12/09, 23:18:36