-
安装gRPC
1.protobuf
https://github.com/google/protobuf/releases // protobuf版本
go get github.com/golang/protobuf/proto // golang protobuf 库
go get github.com/golang/protobuf/protoc-gen-go // protoc --go_out 工具
2.gRPC-go
https://github.com/grpc/grpc-go
// 下载完成后,放入 src/google.golang.org/grpc 文件夹下
-
proto
syntax = "proto3";
package ... ;
import ... ;
option go_package = "..." ;
service FunctionPb {
//方法 1 CUD
rpc FuncProtoA ( Parameter ) returns ( pubpb.ResultPb ) {
}
//方法 2 R
rpc FuncProtoB ( Parameter ) returns ( ReturnValue ) {
}
}
message Parameter {
type variable_a = 1 ;
type variable_b = 2 ;
}
message ReturnValue {
type variable_a = 1 ;
type variable_b = 2 ;
}
-
pb.go
protoc -I ./ path --go_out=plugins=grpc:.
-
models/...(使用proto文件中方法定义)
//1.CUD
func FuncModelsA ( in *Parameter ) ( *pubpb.ResultPb, error ){
o := orm.NewOrm()
ret := &pubpb.ResultPb{Code: "FAIL", Data: "...失败"}
sql := ` `
_, err := o.Raw(sql, ?).Exec()
if err != nil {
ret.Msg = err.Error()
return ret, err
}
ret.Code = "SUCC"
ret.Data = "...成功"
return ret, nil
}
//2.R
func FuncModelsB ( in *Parameter ) ( *ReturnValue, error ) {
o := orm.NewOrm()
o.Using("read")
list := *ReturnValue{}
sql := ` `
_, err := o.Raw(sql, ?).QueryRows(&list)
if err != nil {
return nil, err
}
return list, nil
}
server
-
servers/...
type FunctionSrv struct {
}
//1.CUD
func (s *FunctionSrv) InterfaceServer (ctx context.Context, in *Parameter) ( *pubpb.ResultPb, error ) {
out, err := FuncModelsA(in)
return out, err
}
//2.R
func (s *FunctionSrv) InterfaceServer (ctx context.Context, in *Parameter) ( *RespStruct, error ) {
list, err := FuncModelsB(in)
pageOut := &pubpb.PageOut{Page: pager.Page, Size: pager.Size, Count: pager.Count}
return &RespParameter{List:list, Page: pageOut}, err
}
client
-
routers/...
func init(){
beego.GlobalControllerRouter["..."] = append(beego.GlobalControllerRouter["..."],
beego.ControllerComments{
Method: "InterfaceClient",
Router: `/func`,
AllowHTTPMethods: []string{"get/post"},
MethodParams: param.Make(),
Params: nil})
}
-
rpc/...
//RPC
type FunctionRpc struct{
}
//Client
func (c *FunctionRpc) Client() (Functionpb.FunctionPbClient, error) {
conn, err := GrpcConn()
if err != nil {
return nil, err
}
cli := Functionpb.NewFunctionPbClient(conn)
return cli, nil
}
func (c *FunctionRpc) InterfaceClient(in *Functionpb.Parameter) (){
cli, err := c.Client()
out, err := cli.InterfaceClient(context.Background(), in)
return out, err
}
-
rpc/init.go
var (
FunctionCli *FunctionRpc
)
func init() {
NewGrpcPool()
FunctionCli = &FuncRpc{}
}
-
controllers/...
type FunctionCtrl struct {
controllers.BaseController
}
type FunctionWebCtrl struct {
controllers.BaseController
}
// FunctionWeb页面
// @router / [get]
func (c *FunctionPbWebCtrl) FunctionIndex() {
c.TplName = "..."
}
// @Description 描述
// @Param req query string true "描述"
// @Param resp query string true "描述"
// @Success 200
// @Failure 400 请求失败
// @Failure 404 找不到页面
// @router /func [get/post]
func (c *FunctionPbWebCtrl) InterfaceClient() {
defer c.ServeJSON()
a := c.GetInt64("...")
b := c.GetString("...")
in := .pb.go.Parameter {
字段1 : a,
字段2 : b,
字段3 : c. ...
字段4 : c. ...
}
out, err := grpc.FunctionCli.InterfaceClient(&in)
c.Data["json"] = controllers.ResultStruct{
}
}