Skip to content

Commit

Permalink
grpc example
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyu mao committed Feb 19, 2024
1 parent 4b6bccc commit 62ff558
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 3 deletions.
8 changes: 8 additions & 0 deletions example/codegen.sh
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

36 changes: 36 additions & 0 deletions example/grpc_client_test.go
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)
}
40 changes: 40 additions & 0 deletions example/grpc_server_test.go
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)
}
}
213 changes: 213 additions & 0 deletions example/proto/helloworld.pb.go

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

17 changes: 17 additions & 0 deletions example/proto/helloworld.proto
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;
}
Loading

0 comments on commit 62ff558

Please sign in to comment.