Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve grpcx #2522

Merged
merged 11 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/genpb/genpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type (
CGenPb struct{}
CGenPbInput struct {
g.Meta `name:"pb" brief:"parse proto files and generate protobuf go files"`
g.Meta `name:"pb" config:"gfcli.gen.pb" brief:"parse proto files and generate protobuf go files"`
Path string `name:"path" short:"p" dc:"protobuf file folder path" d:"manifest/protobuf"`
OutputApi string `name:"outputApi" short:"oa" dc:"output folder path storing generated go files of api" d:"api"`
OutputCtrl string `name:"outputCtrl" short:"oc" dc:"output folder path storing generated go files of controller" d:"internal/controller"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/packed/template-mono.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/gf/internal/packed/template-single.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contrib/rpc/grpcx/grpcx_registry_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ func autoLoadAndRegisterFileRegistry() {
)

g.Log().Debug(ctx, `set default registry using file registry as no custom registry set`)
resolver.SetRegistry(fileRegistry)
resolver.Registry(fileRegistry)
}
4 changes: 2 additions & 2 deletions contrib/rpc/grpcx/internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func init() {
resolver.Register(NewBuilder(gsvc.GetRegistry()))
}

// SetRegistry sets the default Registry implements as your own implemented interface.
func SetRegistry(registry gsvc.Registry) {
// Registry sets the default Registry implements as your own implemented interface.
func Registry(registry gsvc.Registry) {
if registry == nil {
panic(gerror.New(`invalid Registry value "nil" given`))
}
Expand Down
6 changes: 6 additions & 0 deletions contrib/rpc/grpcx/internal/resolver/resolver_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
// Manager for Builder creating.
type Manager struct{}

// New creates and returns a Builder.
func (m Manager) New(discovery gsvc.Discovery) resolver.Builder {
return NewBuilder(discovery)
}

// Registry sets the default Registry implements as your own implemented interface.
func (m Manager) Registry(registry gsvc.Registry) {
Registry(registry)
}
8 changes: 4 additions & 4 deletions example/registry/file/client/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"time"

"github.com/gogf/gf/contrib/registry/file/v2"
Expand All @@ -15,12 +14,13 @@ func main() {
gsvc.SetRegistry(file.New(gfile.Temp("gsvc")))

client := g.Client()
for i := 0; i < 100; i++ {
res, err := client.Get(gctx.New(), `http://hello.svc/`)
for i := 0; i < 10; i++ {
ctx := gctx.New()
res, err := client.Get(ctx, `http://hello.svc/`)
if err != nil {
panic(err)
}
fmt.Println(res.ReadAllString())
g.Log().Debug(ctx, res.ReadAllString())
res.Close()
time.Sleep(time.Second)
}
Expand Down
14 changes: 6 additions & 8 deletions example/rpc/grpcx/basic/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ import (
"time"

"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"github.com/gogf/gf/example/rpc/grpcx/basic/protocol"
"github.com/gogf/gf/example/rpc/grpcx/basic/protobuf"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)

func main() {
var (
ctx = gctx.GetInitCtx()
client = protocol.NewEchoClient(grpcx.Client.MustNewGrpcClientConn("demo"))
)
for i := 0; i < 100; i++ {
res, err := client.Say(ctx, &protocol.SayReq{Content: "Hello"})
var client = protobuf.NewGreeterClient(grpcx.Client.MustNewGrpcClientConn("demo"))
for i := 0; i < 10; i++ {
ctx := gctx.New()
res, err := client.SayHello(ctx, &protobuf.HelloRequest{Name: "gfer"})
if err != nil {
g.Log().Error(ctx, err)
return
}
g.Log().Print(ctx, "Response:", res.Content)
g.Log().Debug(ctx, "Response:", res.Message)
time.Sleep(time.Second)
}
}
27 changes: 27 additions & 0 deletions example/rpc/grpcx/basic/controller/helloworld.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https:/gogf/gf.

package controller

import (
"context"

"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"github.com/gogf/gf/example/rpc/grpcx/basic/protobuf"
)

type Controller struct {
protobuf.UnimplementedGreeterServer
}

func Register(s *grpcx.GrpcServer) {
protobuf.RegisterGreeterServer(s.Server, &Controller{})
}

// SayHello implements helloworld.GreeterServer
func (s *Controller) SayHello(ctx context.Context, in *protobuf.HelloRequest) (*protobuf.HelloReply, error) {
return &protobuf.HelloReply{Message: "Hello " + in.GetName()}, nil
}
21 changes: 0 additions & 21 deletions example/rpc/grpcx/basic/protobuf/echo.proto

This file was deleted.

217 changes: 217 additions & 0 deletions example/rpc/grpcx/basic/protobuf/helloworld.pb.go

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

24 changes: 24 additions & 0 deletions example/rpc/grpcx/basic/protobuf/helloworld.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// protoc --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. *.proto

syntax = "proto3";

package protobuf;

option go_package = "github.com/gogf/gf/grpc/example/helloworld/protobuf";


// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}
Loading