Skip to content

Commit

Permalink
U-更新readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yasenagat committed Sep 4, 2018
1 parent b5c3fc6 commit ee51c00
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
18 changes: 18 additions & 0 deletions grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## GRPC使用

#### GRPC标准使用
>std中
#### gokit中使用grpc
>kit中,输入,输出参数都使用了pb的生成的struct,没有自定义,如有需要,可以自定义,然后通过decode和encode,与业务逻辑接口进行对接。
##### transport
>透明的,直接传递,不做任何解码编码。
##### pb的业务接口
>grpc中要实现pb中定义的接口,这个项目中,有2个地方实现了pb的接口。
1. 业务逻辑直接使用pb的接口。(可以自定义接口,与pb不耦合,但是要做encode和decode)。
2. grpc中注册的接口,通过gokit中的handler,endpoint中执行具体的业务逻辑,然后把返回的数据,转化成pb的格式。

#### 具体的业务逻辑
>biz中,业务逻辑接口直接使用了pb定义出来的接口,减少了自定义接口。
4 changes: 2 additions & 2 deletions grpc/biz/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func (UserServer) Login(ctx context.Context, req *pb.LoginReq) (*pb.LoginRes, er
fmt.Println(req.Pwd)
if (req.Username == "abc" && req.Pwd == "123") {
res.Code = 0
res.Err = "登录成功"
res.Msg = "登录成功"
} else {
res.Code = -1
res.Err = "登录失败"
res.Msg = "登录失败"
}
return &res, nil
}
Expand Down
4 changes: 2 additions & 2 deletions grpc/kit/cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {

req := pb.LoginReq{}
req.Username = "abc"
req.Pwd = "abc"
req.Pwd = "abc123"

res, e := us.Login(context.Background(), &req)

Expand All @@ -32,7 +32,7 @@ func main() {
}

fmt.Println(res.Code)
fmt.Println(res.Err)
fmt.Println(res.Msg)
}

type UserClient struct {
Expand Down
20 changes: 10 additions & 10 deletions grpc/pb/usersvc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion grpc/pb/usersvc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ message LoginReq {

message LoginRes {
int64 code = 1;
string err = 2;
string msg = 2;
}

2 changes: 1 addition & 1 deletion grpc/std/cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func main() {
}

fmt.Println(res.Code)
fmt.Println(res.Err)
fmt.Println(res.Msg)
}

0 comments on commit ee51c00

Please sign in to comment.