Skip to content

Commit

Permalink
fixes the issue that grpc client does not registered due to the wrong…
Browse files Browse the repository at this point in the history
… instance name is used
  • Loading branch information
john-deng committed Sep 3, 2018
1 parent 210a355 commit 16fe114
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/grpc/helloworld/greeter-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func init() {
// host: localhost # server host
// port: 7575 # server port
//
grpc.RegisterClient("greeter-client", protobuf.NewGreeterClient)
grpc.Client("greeter-client", protobuf.NewGreeterClient)

// must: register greeterController
web.RestController(new(greeterController))
Expand Down
2 changes: 1 addition & 1 deletion examples/grpc/helloworld/greeter-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *greeterServerImpl) SayHello(ctx context.Context, request *protobuf.Hell
func init() {
// must: register grpc server
// please note that greeterService must implement protobuf.GreeterServer, or it won't be registered.
grpc.RegisterServer(protobuf.RegisterGreeterServer, new(greeterServerImpl))
grpc.Server(protobuf.RegisterGreeterServer, new(greeterServerImpl))
}

func main() {
Expand Down
3 changes: 3 additions & 0 deletions pkg/app/web/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Dependency injection is a concept valid for any programming language. The genera
called Inversion of Control. According to this concept a struct should not configure its dependencies statically but
should be configured from the outside.
Dependency Injection design pattern allows us to remove the hard-coded dependencies and make our application loosely
coupled, extendable and maintainable.
A Go struct has a dependency on another struct, if it uses an instance of this struct. We call this a struct dependency.
For example, a struct which accesses a user controller has a dependency on user service struct.
Expand Down
20 changes: 14 additions & 6 deletions pkg/starter/grpc/autoconfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/hidevopsio/hiboot/pkg/log"
"github.com/hidevopsio/hiboot/pkg/utils/mapstruct"
"github.com/hidevopsio/hiboot/pkg/utils/reflector"
"github.com/hidevopsio/hiboot/pkg/utils/str"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"net"
Expand Down Expand Up @@ -54,6 +53,9 @@ func RegisterServer(cb interface{}, s interface{}) {
grpcServers = append(grpcServers, svr)
}

// Server alias to RegisterServer
var Server = RegisterServer

// RegisterClient register client from application
func RegisterClient(name string, cb interface{}, s ...interface{}) {
var svc interface{}
Expand All @@ -68,6 +70,9 @@ func RegisterClient(name string, cb interface{}, s ...interface{}) {
grpcClients = append(grpcClients, svr)
}

// Client register client from application, it is a alias to RegisterClient
var Client = RegisterClient

func init() {
app.AutoConfiguration(new(grpcConfiguration))
}
Expand Down Expand Up @@ -96,16 +101,19 @@ func (c *grpcConfiguration) BuildGrpcClients() {
break
}
log.Infof("gRPC client connected to: %v", address)
clientInstanceName := str.ToLowerCamel(cli.name)
if cli.cb != nil {
// get return type for register instance name
gRpcCli, err := reflector.CallFunc(cli.cb, conn)
if err == nil {
// register grpc client
c.instantiateFactory.SetInstance(clientInstanceName, gRpcCli)
clientInstanceName, err := reflector.GetName(gRpcCli)
if err == nil {
// register grpc client
c.instantiateFactory.SetInstance(clientInstanceName, gRpcCli)
// register clientConn
c.instantiateFactory.SetInstance(clientInstanceName+"Conn", conn)
}
}
}
// register clientConn
c.instantiateFactory.SetInstance(clientInstanceName+"Conn", conn)

// register client service
if cli.svc != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/idgen/idgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func init() {
//}
}

// Next generate next id as uint64
// Next generates next id as an uint64
func Next() (id uint64, err error) {
var i uint64
i, err = sf.NextID()
Expand All @@ -24,7 +24,7 @@ func Next() (id uint64, err error) {
return
}

// NextString generate next id as string
// NextString generates next id as a string
func NextString() (id string, err error) {
var i uint64
i, err = sf.NextID()
Expand Down

0 comments on commit 16fe114

Please sign in to comment.