diff --git a/protocol/grpc/client_test.go b/protocol/grpc/client_test.go index 21800a9f6df356b66f4f3e5f2cd9e42e874ff5e5..b63ceefaef76dfd399c6bd502eb9e5ba01a232c6 100644 --- a/protocol/grpc/client_test.go +++ b/protocol/grpc/client_test.go @@ -18,28 +18,19 @@ limitations under the License. package grpc import ( - //"context" "reflect" "testing" +) +import ( "github.com/bmizerany/assert" "google.golang.org/grpc" +) +import ( "github.com/apache/dubbo-go/protocol/grpc/internal" ) -//type GrpcGreeterImpl struct { -// SayHello func(ctx context.Context, in *internal.HelloRequest, out *internal.HelloReply) error -//} -// -//func (u *GrpcGreeterImpl) Reference() string { -// return "GrpcGreeterImpl" -//} -// -//func (u *GrpcGreeterImpl) GetDubboStub(cc *grpc.ClientConn) internal.GreeterClient { -// return internal.NewGreeterClient(cc) -//} - func TestGetInvoker(t *testing.T) { var conn *grpc.ClientConn var impl *internal.GrpcGreeterImpl diff --git a/protocol/grpc/common_test.go b/protocol/grpc/common_test.go index 1669513471498235be48b016f67dd2a440124640..2110f538d64cba33c481c533e066c49c7cd727e2 100644 --- a/protocol/grpc/common_test.go +++ b/protocol/grpc/common_test.go @@ -33,44 +33,44 @@ import ( "github.com/apache/dubbo-go/protocol/invocation" ) - +// userd grpc-dubbo biz service func addService() { config.SetProviderService(NewGreeterProvider()) } -type GreeterProvider struct { - *GreeterProviderBase +type greeterProvider struct { + *greeterProviderBase } -func NewGreeterProvider() *GreeterProvider { - return &GreeterProvider{ - GreeterProviderBase: &GreeterProviderBase{}, +func NewGreeterProvider() *greeterProvider { + return &greeterProvider{ + greeterProviderBase: &greeterProviderBase{}, } } -func (g *GreeterProvider) SayHello(ctx context.Context, req *internal.HelloRequest) (reply *internal.HelloReply, err error) { +func (g *greeterProvider) SayHello(ctx context.Context, req *internal.HelloRequest) (reply *internal.HelloReply, err error) { fmt.Printf("req: %v", req) return &internal.HelloReply{Message: "this is message from reply"}, nil } -func (g *GreeterProvider) Reference() string { +func (g *greeterProvider) Reference() string { return "GrpcGreeterImpl" } // code generated by greeter.go -type GreeterProviderBase struct { +type greeterProviderBase struct { proxyImpl protocol.Invoker } -func (g *GreeterProviderBase) SetProxyImpl(impl protocol.Invoker) { +func (g *greeterProviderBase) SetProxyImpl(impl protocol.Invoker) { g.proxyImpl = impl } -func (g *GreeterProviderBase) GetProxyImpl() protocol.Invoker { +func (g *greeterProviderBase) GetProxyImpl() protocol.Invoker { return g.proxyImpl } -func (g *GreeterProviderBase) ServiceDesc() *native_grpc.ServiceDesc { +func (g *greeterProviderBase) ServiceDesc() *native_grpc.ServiceDesc { return &native_grpc.ServiceDesc{ ServiceName: "helloworld.Greeter", HandlerType: (*internal.GreeterServer)(nil), diff --git a/protocol/grpc/grpc_protocol_test.go b/protocol/grpc/grpc_protocol_test.go index a293e1073d9b0f460b4a66ab97ee704090992001..41c895d24fe371d0226078cf1c164ab4b47759f3 100644 --- a/protocol/grpc/grpc_protocol_test.go +++ b/protocol/grpc/grpc_protocol_test.go @@ -19,15 +19,12 @@ package grpc import ( "context" - "log" - "net" "testing" "time" ) import ( "github.com/stretchr/testify/assert" - native_grpc "google.golang.org/grpc" ) import ( @@ -65,33 +62,8 @@ func TestGrpcProtocol_Export(t *testing.T) { assert.False(t, ok) } -// server is used to implement helloworld.GreeterServer. -type server struct { - internal.UnimplementedGreeterServer -} - -// SayHello implements helloworld.GreeterServer -func (s *server) SayHello(ctx context.Context, in *internal.HelloRequest) (*internal.HelloReply, error) { - log.Printf("Received: %v", in.GetName()) - return &internal.HelloReply{Message: "Hello " + in.GetName()}, nil -} - -func initGrpcServer() { - port := ":20000" - - lis, err := net.Listen("tcp", port) - if err != nil { - log.Fatalf("failed to listen: %v", err) - } - s := native_grpc.NewServer() - internal.RegisterGreeterServer(s, &server{}) - if err := s.Serve(lis); err != nil { - log.Fatalf("failed to serve: %v", err) - } -} - func TestGrpcProtocol_Refer(t *testing.T) { - go initGrpcServer() + go internal.InitGrpcServer() time.Sleep(time.Second) proto := GetProtocol() diff --git a/protocol/grpc/internal/client.go b/protocol/grpc/internal/client.go index 647fe50f4412fe795bb3a3da411c23b18a4b63a4..4703f430840f8e68ff4142de1eae1565784dd107 100644 --- a/protocol/grpc/internal/client.go +++ b/protocol/grpc/internal/client.go @@ -25,6 +25,7 @@ import ( "google.golang.org/grpc" ) +// used for dubbo-grpc biz client type GrpcGreeterImpl struct { SayHello func(ctx context.Context, in *HelloRequest, out *HelloReply) error } diff --git a/protocol/grpc/internal/server.go b/protocol/grpc/internal/server.go new file mode 100644 index 0000000000000000000000000000000000000000..5c08b13267313d4fba1825731d532521fd6f9274 --- /dev/null +++ b/protocol/grpc/internal/server.go @@ -0,0 +1,53 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package internal + +import ( + "context" + "log" + "net" +) + +import ( + "google.golang.org/grpc" +) + +// server is used to implement helloworld.GreeterServer. +type server struct { + UnimplementedGreeterServer +} + +// SayHello implements helloworld.GreeterServer +func (s *server) SayHello(ctx context.Context, in *HelloRequest) (*HelloReply, error) { + log.Printf("Received: %v", in.GetName()) + return &HelloReply{Message: "Hello " + in.GetName()}, nil +} + +func InitGrpcServer() { + port := ":20000" + + lis, err := net.Listen("tcp", port) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + s := grpc.NewServer() + RegisterGreeterServer(s, &server{}) + if err := s.Serve(lis); err != nil { + log.Fatalf("failed to serve: %v", err) + } +}