金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  423 随笔 :: 0 文章 :: 454 评论 :: 0 Trackbacks
用 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/users
Creating project in E:\gopath\src\github.com\jinq0123\users
Is this OK? [y]es/[n]o
y
.
├── 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%/src
2017/11/28 16:59:24 [Creating] server\read.go
2017/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
λ users
A gRPC based service

Usage:
  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 subscribers

Flags:
  -h, --help   help for users

Use "users [command] --help" for more information about a command.

用子命令serve启动服务:

E:\gopath\src\github.com\jinq0123\users
λ users serve
INFO[0000] Serving gRPC on :8000
INFO[0000] Using Zipkin Global tracer
INFO[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 summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
...

用 grpc-lua 来测试下:

E:\Git\grpc-lua\examples\helloworld (master)
λ lua-cpp.exe
Lua 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)
> cod
2
> 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)
> err
Endpoint read failed
...
> response, err, cod = stub:sync_request("Read", request)
> err
nil
> insp(response)
{
  id = "Hello, abcd"
}
posted on 2017-11-28 18:47 金庆 阅读(755) 评论(0)  编辑 收藏 引用 所属分类: 9. 其它

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理