用 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"}
阅读全文
0 0
- 用 Lile 创建 gRPC-go 服务
- 使用Go开发gRPC
- go版本gRPC入门
- go的grpc实现
- gRPC-go 连接管理
- gRPC的简单Go例子
- 使用Go快速创建静态服务
- gRPC服务发现&负载均衡
- gRPC服务发现&负载均衡
- grpc服务认证实现方式
- gRPC的Go语言使用例子
- go语言使用grpc的步骤
- grpc简易教程 go server+php client
- grpc(5):使用grpc+consul 开发服务调用
- grpc-gateway:grpc转换为http协议对外提供服务
- grpc-gateway:grpc转换为http协议对外提供服务
- grpc-gateway:grpc转换为http协议对外提供服务
- grpc-gateway:grpc转换为http协议对外提供服务
- Kafka消息序列化和反序列化(上)
- <font face="微软雅黑" color=DodgerBlue>*IncomesESL Analy*</font>
- 记录使用 Eclipse 的过程中出现以下问题
- XUtils结合使用的登录和注册
- 数据的存储
- 用 Lile 创建 gRPC-go 服务
- Types of Properties--Accessor Properties
- Jdk8新特性
- Selenium环境配置
- Java的垃圾回收机制,GC,和变量的创建和删除的关系(即变量的作用域,for循环中的某个变量,出了for循环再引用就会报错了,因为该变量已经被从内存中删掉了)
- PCA详细讲解
- String不可变不可被继承源码分析
- Kafka消息序列化和反序列化(下)
- JQ对用户名异步校验(JQ的AJAX)