-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhongyu mao
committed
Feb 19, 2024
1 parent
4b6bccc
commit 62ff558
Showing
7 changed files
with
422 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
find proto -type f -name "*.pb.go" -delete | ||
|
||
# https://developers.google.com/protocol-buffers/docs/reference/go-generated#package | ||
# 默认就是import 模式 | ||
protoc --go_out=. --go_opt=paths=import \ | ||
--go-grpc_out=. --go-grpc_opt=paths=import \ | ||
proto/helloworld.proto | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
User: cr-mao | ||
Date: 2024/2/19 12:52 | ||
Email: crmao@qq.com | ||
Desc: grpc_server_client.go | ||
*/ | ||
package example | ||
|
||
import ( | ||
"context" | ||
"github.com/cr-mao/lori/example/proto" | ||
"github.com/cr-mao/lori/log" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cr-mao/lori/transport/grpc" | ||
) | ||
|
||
func TestGrpcClient(t *testing.T) { | ||
baseCtx := context.Background() | ||
conn, err := grpc.DialInsecure(baseCtx, | ||
grpc.WithClientEndpoint("127.0.0.1:8081"), | ||
grpc.WithClientTimeout(time.Second*5)) | ||
if err != nil { | ||
log.Fatalf("err:%v", err) | ||
} | ||
client := proto.NewGreeterClient(conn) | ||
resp, err := client.SayHello(context.Background(), &proto.HelloRequest{ | ||
Name: "cr-mao", | ||
}) | ||
if err != nil { | ||
log.Errorf("SayHello err:%v", err) | ||
return | ||
} | ||
log.Info(resp.Message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
User: cr-mao | ||
Date: 2024/2/19 12:48 | ||
Email: crmao@qq.com | ||
Desc: test_grpc_server.go | ||
*/ | ||
package example | ||
|
||
import ( | ||
"context" | ||
"github.com/cr-mao/lori/example/proto" | ||
"github.com/cr-mao/lori/log" | ||
"testing" | ||
|
||
"github.com/cr-mao/lori/transport/grpc" | ||
) | ||
|
||
type HelloWorldServer struct { | ||
proto.UnsafeGreeterServer | ||
} | ||
|
||
func (s *HelloWorldServer) SayHello(ctx context.Context, r *proto.HelloRequest) (*proto.HelloResponse, error) { | ||
return &proto.HelloResponse{ | ||
Message: "hello " + r.Name, | ||
}, nil | ||
} | ||
|
||
func registerServer(server *grpc.Server) { | ||
proto.RegisterGreeterServer(server, &HelloWorldServer{}) | ||
} | ||
|
||
func TestGrpcServer(t *testing.T) { | ||
baseCtx := context.Background() | ||
grpcServer := grpc.NewServer(grpc.WithAddress("0.0.0.0:8081")) | ||
registerServer(grpcServer) | ||
err := grpcServer.Start(baseCtx) | ||
if err != nil { | ||
log.Errorf("err %v", err) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
syntax = "proto3"; | ||
|
||
package lori.example.proto; | ||
|
||
option go_package = "./proto"; | ||
|
||
service Greeter { | ||
rpc SayHello (HelloRequest) returns (HelloResponse); | ||
} | ||
|
||
message HelloRequest { | ||
string name = 1; | ||
} | ||
|
||
message HelloResponse{ | ||
string message = 1; | ||
} |
Oops, something went wrong.