用 Lile 创建 gRPC-go 服务

来源:互联网 发布:阿里云域名解析到主机 编辑:程序博客网 时间:2024/06/05 21:49

用 Lile 创建 gRPC-go 服务

(金庆的专栏 2017.11)

Lile 是一个工具,用于 Go 语言快速创建 gRPC 服务。
https://github.com/lileio/lile

会自动添加 Prometheus, Zipkin 和 Google PubSub 支持。

go get -u github.com/lileio/lile/...

将安装所有依赖包,并生成 bin/lile.exe, bin/protoc-gen-lile-server.exe.
另外还需要安装 protoc.exe.

按照示例创建 users 服务:

E:\gopath\src\github.comλ lile new jinq0123/usersCreating project in E:\gopath\src\github.com\jinq0123\usersIs this OK? [y]es/[n]oy.├── server│   ├── server.go│   └── server_test.go├── subscribers│   └── subscribers.go├── users│   ├── cmd│   │   ├── root.go│   │   ├── serve.go│   │   ├── subscribe.go│   │   └── up.go│   └── main.go├── users.proto├── Makefile├── Dockerfile├── .travis.yml└── .gitignore

查看 Makefile, 复制其中 protoc 脚本,将 $$GOPATH 改为 %GOPATH%,运行:

E:\gopath\src\github.com\jinq0123\usersλ protoc -I . users.proto --lile-server_out=. --go_out=plugins=grpc:%GOPATH%/src2017/11/28 16:59:24 [Creating] server\read.go2017/11/28 16:59:24 [Creating test] server\read_test.go

protoc-gen-lile-server.exe 将生成 server\read.go, 对应 user.proto 中的方法 Users::Read().
grpc的插件将生成 users.pb.go,与仅仅用 grpc 生成的代码相同。

D:/Go/bin/go.exe install -v [E:/gopath/src/github.com/jinq0123/users/users]github.com/jinq0123/users/users成功: 进程退出代码 0.

可直接编译生成 user.exe.

无参数运行则显示命令行帮助:

E:\gopath\src\github.com\jinq0123\usersλ usersA gRPC based serviceUsage:  users [command]Available Commands:  help        Help about any command  serve       Run the RPC server  subscribe   Subscribe to and process queue messages  up          up runs both RPC and pubub subscribersFlags:  -h, --help   help for usersUse "users [command] --help" for more information about a command.

用子命令serve启动服务:

E:\gopath\src\github.com\jinq0123\usersλ users serveINFO[0000] Serving gRPC on :8000INFO[0000] Using Zipkin Global tracerINFO[0000] Prometheus metrics at :9000/metrics

http://localhost:9000/metrics 会显示

# HELP go_gc_duration_seconds A summary of the GC invocation durations.# TYPE go_gc_duration_seconds summarygo_gc_duration_seconds{quantile="0"} 0go_gc_duration_seconds{quantile="0.25"} 0...

用 grpc-lua 来测试下:

E:\Git\grpc-lua\examples\helloworld (master)λ lua-cpp.exeLua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio> package.path = "../../src/lua/?.lua;" .. package.path> grpc = require("grpc_lua.grpc_lua")> grpc.import_proto_file("users.proto")> stub = grpc.service_stub("localhost:8000", "users.Users")D1128 17:28:13.711000000  4612 dns_resolver.c:301] Using native dns resolver> request = {id = "abcd"}> response, err, cod = stub:sync_request("Read", request)> cod2> insp = require("inspect")> insp(resonse)nil> insp(err)"not yet implemented"

缺省实现返回 “not yet implemented” 错误。更改实现代如下:

func (s UsersServer) Read(ctx context.Context, r *users.Request) (*users.Response, error) {    // return nil, errors.New("not yet implemented")    return &users.Response{Id: "Hello, " + r.Id}, nil}

再次请求:

> response, err, cod = stub:sync_request("Read", request)> errEndpoint read failed...> response, err, cod = stub:sync_request("Read", request)> errnil> insp(response){  id = "Hello, abcd"}
原创粉丝点击