This is an automated email from the ASF dual-hosted git repository. zhaoyunxing pushed a commit to branch 3.0 in repository https://gitbox.apache.org/repos/asf/dubbo-go-samples.git
commit 5582c91e8d661e8e40080a3ce9e29acf087088f4 Author: ChangedenChan <[email protected]> AuthorDate: Sat Aug 21 18:25:01 2021 +0800 Add dubbo3-protobuf-nacos sample (#194) Co-authored-by: Changeden <[email protected]> --- generic/protobuf-nacos/client/cmd/client.go | 126 +++++++ generic/protobuf-nacos/client/conf/client.yml | 23 ++ generic/protobuf-nacos/client/conf/log.yml | 27 ++ generic/protobuf-nacos/client/pkg/helloworld.pb.go | 419 +++++++++++++++++++++ generic/protobuf-nacos/client/pkg/helloworld.proto | 38 ++ generic/protobuf-nacos/client/pkg/protobuf.mk | 26 ++ generic/protobuf-nacos/server/cmd/server.go | 81 ++++ generic/protobuf-nacos/server/conf/client.yml | 22 ++ generic/protobuf-nacos/server/conf/log.yml | 27 ++ generic/protobuf-nacos/server/conf/server.yml | 28 ++ generic/protobuf-nacos/server/pkg/greeter.go | 81 ++++ 11 files changed, 898 insertions(+) diff --git a/generic/protobuf-nacos/client/cmd/client.go b/generic/protobuf-nacos/client/cmd/client.go new file mode 100644 index 0000000..f0704dd --- /dev/null +++ b/generic/protobuf-nacos/client/cmd/client.go @@ -0,0 +1,126 @@ +/* + * 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 main + +import ( + "context" + "fmt" + "os" + "os/signal" + "syscall" + "time" +) + +import ( + _ "dubbo.apache.org/dubbo-go/v3/cluster/cluster_impl" + _ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance" + "dubbo.apache.org/dubbo-go/v3/common/logger" + _ "dubbo.apache.org/dubbo-go/v3/common/proxy/proxy_factory" + "dubbo.apache.org/dubbo-go/v3/config" + _ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl" + _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3" + _ "dubbo.apache.org/dubbo-go/v3/protocol/grpc" + _ "dubbo.apache.org/dubbo-go/v3/registry/nacos" + _ "dubbo.apache.org/dubbo-go/v3/registry/protocol" + _ "dubbo.apache.org/dubbo-go/v3/registry/zookeeper" +) + +import ( + "github.com/apache/dubbo-go-samples/generic/protobuf-nacos/client/pkg" +) + +var ( + greeterProvider = new(pkg.GreeterClientImpl) + survivalTimeout = int(3 * time.Second) +) + +func init() { + config.SetConsumerService(greeterProvider) +} + +// need to setup environment variable "CONF_CONSUMER_FILE_PATH" to "conf/client.yml" before run +func main() { + config.Load() + time.Sleep(time.Second * 3) + + testSayHello() + + initSignal() +} + +func testSayHello() { + ctx := context.Background() + ctx = context.WithValue(ctx, "tri-req-id", "triple-request-id-demo") + + req := pkg.HelloRequest{ + Name: "laurence", + } + + r, err := greeterProvider.SayHelloStream(ctx) + if err != nil { + panic(err) + } + + for i := 0; i < 2; i++ { + if err := r.Send(&req); err != nil { + logger.Errorf("Send SayHelloStream num %d request error = %v\n", i+1, err) + return + } + } + + rspUser := &pkg.User{} + if err := r.RecvMsg(rspUser); err != nil { + logger.Errorf("Receive 1 SayHelloStream response user error = %v\n", err) + return + } + logger.Infof("Receive 1 user = %+v\n", rspUser) + if err := r.Send(&req); err != nil { + logger.Errorf("Send SayHelloStream num %d request error = %v\n", 3, err) + return + } + rspUser2 := &pkg.User{} + if err := r.RecvMsg(rspUser2); err != nil { + logger.Errorf("Receive 2 SayHelloStream response user error = %v\n", err) + return + } + logger.Infof("Receive 2 user = %+v\n", rspUser2) +} + +func initSignal() { + signals := make(chan os.Signal, 1) + // It is not possible to block SIGKILL or syscall.SIGSTOP + signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + for { + sig := <-signals + logger.Infof("get signal %s", sig.String()) + switch sig { + case syscall.SIGHUP: + // reload() + default: + time.Sleep(time.Second * 5) + time.AfterFunc(time.Duration(survivalTimeout), func() { + logger.Warnf("app exit now by force...") + os.Exit(1) + }) + + // The program exits normally or timeout forcibly exits. + fmt.Println("provider app exit now...") + return + } + } +} diff --git a/generic/protobuf-nacos/client/conf/client.yml b/generic/protobuf-nacos/client/conf/client.yml new file mode 100644 index 0000000..74e0f18 --- /dev/null +++ b/generic/protobuf-nacos/client/conf/client.yml @@ -0,0 +1,23 @@ +# dubbo-go3 client yaml configure file +# application config (not necessary) +application: + organization: "dubbo.io" + name: "greeterImpl" + module: "dubbo-go3.0 client" + version: "0.0.1" + environment: "dev" + +# registry config +registries: + "demoZk": + protocol: "nacos" + timeout: "3s" + address: "console.nacos.io:80" + +# reference config +references: + "greeterImpl": + registry: "demoZk" + protocol: "tri" + interface: "protobuf.Greeter" + url: tri://localhost:20001 diff --git a/generic/protobuf-nacos/client/conf/log.yml b/generic/protobuf-nacos/client/conf/log.yml new file mode 100644 index 0000000..8c3f700 --- /dev/null +++ b/generic/protobuf-nacos/client/conf/log.yml @@ -0,0 +1,27 @@ +level: "info" +development: true +disableCaller: false +disableStacktrace: false +sampling: +encoding: "console" + +# encoder +encoderConfig: + messageKey: "message" + levelKey: "level" + timeKey: "time" + nameKey: "logger" + callerKey: "caller" + stacktraceKey: "stacktrace" + lineEnding: "" + levelEncoder: "capital" + timeEncoder: "iso8601" + durationEncoder: "seconds" + callerEncoder: "short" + nameEncoder: "" + +outputPaths: + - "stderr" +errorOutputPaths: + - "stderr" +initialFields: diff --git a/generic/protobuf-nacos/client/pkg/helloworld.pb.go b/generic/protobuf-nacos/client/pkg/helloworld.pb.go new file mode 100644 index 0000000..bdd4fc5 --- /dev/null +++ b/generic/protobuf-nacos/client/pkg/helloworld.pb.go @@ -0,0 +1,419 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: helloworld.proto + +package pkg + +import ( + context "context" + fmt "fmt" + math "math" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/protocol" + dgrpc "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3" + "dubbo.apache.org/dubbo-go/v3/protocol/invocation" + + tripleConstant "github.com/dubbogo/triple/pkg/common/constant" + dubbo3 "github.com/dubbogo/triple/pkg/triple" + + proto "github.com/golang/protobuf/proto" + + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// The request message containing the user's name. +type HelloRequest struct { + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *HelloRequest) Reset() { *m = HelloRequest{} } +func (m *HelloRequest) String() string { return proto.CompactTextString(m) } +func (*HelloRequest) ProtoMessage() {} +func (*HelloRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_17b8c58d586b62f2, []int{0} +} + +func (m *HelloRequest) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_HelloRequest.Unmarshal(m, b) +} +func (m *HelloRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_HelloRequest.Marshal(b, m, deterministic) +} +func (m *HelloRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_HelloRequest.Merge(m, src) +} +func (m *HelloRequest) XXX_Size() int { + return xxx_messageInfo_HelloRequest.Size(m) +} +func (m *HelloRequest) XXX_DiscardUnknown() { + xxx_messageInfo_HelloRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_HelloRequest proto.InternalMessageInfo + +func (m *HelloRequest) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +// The response message containing the greetings +type User struct { + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"` + Age int32 `protobuf:"varint,3,opt,name=age,proto3" json:"age,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *User) Reset() { *m = User{} } +func (m *User) String() string { return proto.CompactTextString(m) } +func (*User) ProtoMessage() {} +func (*User) Descriptor() ([]byte, []int) { + return fileDescriptor_17b8c58d586b62f2, []int{1} +} + +func (m *User) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_User.Unmarshal(m, b) +} +func (m *User) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_User.Marshal(b, m, deterministic) +} +func (m *User) XXX_Merge(src proto.Message) { + xxx_messageInfo_User.Merge(m, src) +} +func (m *User) XXX_Size() int { + return xxx_messageInfo_User.Size(m) +} +func (m *User) XXX_DiscardUnknown() { + xxx_messageInfo_User.DiscardUnknown(m) +} + +var xxx_messageInfo_User proto.InternalMessageInfo + +func (m *User) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +func (m *User) GetId() string { + if m != nil { + return m.Id + } + return "" +} + +func (m *User) GetAge() int32 { + if m != nil { + return m.Age + } + return 0 +} + +func init() { + proto.RegisterType((*HelloRequest)(nil), "protobuf.HelloRequest") + proto.RegisterType((*User)(nil), "protobuf.User") +} + +func init() { proto.RegisterFile("helloworld.proto", fileDescriptor_17b8c58d586b62f2) } + +var fileDescriptor_17b8c58d586b62f2 = []byte{ + // 178 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0xc8, 0x48, 0xcd, 0xc9, + 0xc9, 0x2f, 0xcf, 0x2f, 0xca, 0x49, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x00, 0x53, + 0x49, 0xa5, 0x69, 0x4a, 0x4a, 0x5c, 0x3c, 0x1e, 0x20, 0xd9, 0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2, + 0x12, 0x21, 0x21, 0x2e, 0x96, 0xbc, 0xc4, 0xdc, 0x54, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xce, 0x20, + 0x30, 0x5b, 0xc9, 0x86, 0x8b, 0x25, 0xb4, 0x38, 0xb5, 0x08, 0x9b, 0x9c, 0x10, 0x1f, 0x17, 0x53, + 0x66, 0x8a, 0x04, 0x13, 0x58, 0x84, 0x29, 0x33, 0x45, 0x48, 0x80, 0x8b, 0x39, 0x31, 0x3d, 0x55, + 0x82, 0x59, 0x81, 0x51, 0x83, 0x35, 0x08, 0xc4, 0x34, 0xaa, 0xe7, 0x62, 0x77, 0x2f, 0x4a, 0x4d, + 0x2d, 0x49, 0x2d, 0x12, 0x32, 0xe1, 0xe2, 0x08, 0x4e, 0xac, 0x04, 0xdb, 0x27, 0x24, 0xa6, 0x07, + 0x73, 0x83, 0x1e, 0xb2, 0x03, 0xa4, 0xf8, 0x10, 0xe2, 0x20, 0x4b, 0x95, 0x18, 0x84, 0xec, 0xb8, + 0xf8, 0x60, 0xba, 0x82, 0x4b, 0x8a, 0x52, 0x13, 0x73, 0x89, 0xd7, 0xab, 0xc1, 0x68, 0xc0, 0x98, + 0xc4, 0x06, 0x16, 0x34, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x98, 0x59, 0x9f, 0x6b, 0x07, 0x01, + 0x00, 0x00, +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConnInterface + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion6 + +// GreeterClient is the client API for Greeter service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type GreeterClient interface { + // Sends a greeting + SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*User, error) + SayHelloStream(ctx context.Context, opts ...grpc.CallOption) (Greeter_SayHelloStreamClient, error) +} + +type greeterClient struct { + cc grpc.ClientConnInterface +} + +func NewGreeterClient(cc grpc.ClientConnInterface) GreeterClient { + return &greeterClient{cc} +} + +func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*User, error) { + out := new(User) + err := c.cc.Invoke(ctx, "/protobuf.Greeter/SayHello", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *greeterClient) SayHelloStream(ctx context.Context, opts ...grpc.CallOption) (Greeter_SayHelloStreamClient, error) { + stream, err := c.cc.NewStream(ctx, &_Greeter_serviceDesc.Streams[0], "/protobuf.Greeter/SayHelloStream", opts...) + if err != nil { + return nil, err + } + x := &greeterSayHelloStreamClient{stream} + return x, nil +} + +type Greeter_SayHelloStreamClient interface { + Send(*HelloRequest) error + Recv() (*User, error) + grpc.ClientStream +} + +type greeterSayHelloStreamClient struct { + grpc.ClientStream +} + +func (x *greeterSayHelloStreamClient) Send(m *HelloRequest) error { + return x.ClientStream.SendMsg(m) +} + +func (x *greeterSayHelloStreamClient) Recv() (*User, error) { + m := new(User) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + +// GreeterServer is the server API for Greeter service. +type GreeterServer interface { + // Sends a greeting + SayHello(context.Context, *HelloRequest) (*User, error) + SayHelloStream(Greeter_SayHelloStreamServer) error +} + +// UnimplementedGreeterServer can be embedded to have forward compatible implementations. +type UnimplementedGreeterServer struct { +} + +func (*UnimplementedGreeterServer) SayHello(ctx context.Context, req *HelloRequest) (*User, error) { + return nil, status.Errorf(codes.Unimplemented, "method SayHello not implemented") +} +func (*UnimplementedGreeterServer) SayHelloStream(srv Greeter_SayHelloStreamServer) error { + return status.Errorf(codes.Unimplemented, "method SayHelloStream not implemented") +} + +func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) { + s.RegisterService(&_Greeter_serviceDesc, srv) +} + +func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(HelloRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GreeterServer).SayHello(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/protobuf.Greeter/SayHello", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GreeterServer).SayHello(ctx, req.(*HelloRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Greeter_SayHelloStream_Handler(srv interface{}, stream grpc.ServerStream) error { + return srv.(GreeterServer).SayHelloStream(&greeterSayHelloStreamServer{stream}) +} + +type Greeter_SayHelloStreamServer interface { + Send(*User) error + Recv() (*HelloRequest, error) + grpc.ServerStream +} + +type greeterSayHelloStreamServer struct { + grpc.ServerStream +} + +func (x *greeterSayHelloStreamServer) Send(m *User) error { + return x.ServerStream.SendMsg(m) +} + +func (x *greeterSayHelloStreamServer) Recv() (*HelloRequest, error) { + m := new(HelloRequest) + if err := x.ServerStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + +var _Greeter_serviceDesc = grpc.ServiceDesc{ + ServiceName: "protobuf.Greeter", + HandlerType: (*GreeterServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "SayHello", + Handler: _Greeter_SayHello_Handler, + }, + }, + Streams: []grpc.StreamDesc{ + { + StreamName: "SayHelloStream", + Handler: _Greeter_SayHelloStream_Handler, + ServerStreams: true, + ClientStreams: true, + }, + }, + Metadata: "helloworld.proto", +} + +type greeterDubbo3Client struct { + cc *dubbo3.TripleConn +} + +func NewGreeterDubbo3Client(cc *dubbo3.TripleConn) GreeterClient { + return &greeterDubbo3Client{cc} +} +func (c *greeterDubbo3Client) SayHello(ctx context.Context, in *HelloRequest, opt ...grpc.CallOption) (*User, error) { + out := new(User) + interfaceKey := ctx.Value(tripleConstant.InterfaceKey).(string) + err := c.cc.Invoke(ctx, "/"+interfaceKey+"/SayHello", in, out) + if err != nil { + return nil, err + } + return out, nil +} +func (c *greeterDubbo3Client) SayHelloStream(ctx context.Context, opt ...grpc.CallOption) (Greeter_SayHelloStreamClient, error) { + interfaceKey := ctx.Value(tripleConstant.InterfaceKey).(string) + stream, err := c.cc.NewStream(ctx, "/"+interfaceKey+"/SayHelloStream", opt...) + if err != nil { + return nil, err + } + x := &greeterSayHelloStreamClient{stream} + return x, nil +} + +// GreeterClientImpl is the client API for Greeter service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type GreeterClientImpl struct { + // Sends a greeting + SayHello func(ctx context.Context, in *HelloRequest, out *User) error + SayHelloStream func(ctx context.Context) (Greeter_SayHelloStreamClient, error) +} + +func (c *GreeterClientImpl) Reference() string { + return "greeterImpl" +} + +func (c *GreeterClientImpl) GetDubboStub(cc *dubbo3.TripleConn) GreeterClient { + return NewGreeterDubbo3Client(cc) +} + +type GreeterProviderBase struct { + proxyImpl protocol.Invoker +} + +func (s *GreeterProviderBase) SetProxyImpl(impl protocol.Invoker) { + s.proxyImpl = impl +} + +func (s *GreeterProviderBase) GetProxyImpl() protocol.Invoker { + return s.proxyImpl +} + +func (c *GreeterProviderBase) Reference() string { + return "greeterImpl" +} + +func _DUBBO_Greeter_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(HelloRequest) + if err := dec(in); err != nil { + return nil, err + } + base := srv.(dgrpc.Dubbo3GrpcService) + args := []interface{}{} + args = append(args, in) + invo := invocation.NewRPCInvocation("SayHello", args, nil) + if interceptor == nil { + result := base.GetProxyImpl().Invoke(ctx, invo) + return result.Result(), result.Error() + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/protobuf.Greeter/SayHello", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + result := base.GetProxyImpl().Invoke(context.Background(), invo) + return result.Result(), result.Error() + } + return interceptor(ctx, in, info, handler) +} + +func _DUBBO_Greeter_SayHelloStream_Handler(srv interface{}, stream grpc.ServerStream) error { + _, ok := srv.(dgrpc.Dubbo3GrpcService) + invo := invocation.NewRPCInvocation("SayHelloStream", nil, nil) + if !ok { + fmt.Println(invo) + } + return srv.(GreeterServer).SayHelloStream(&greeterSayHelloStreamServer{stream}) +} + +func (s *GreeterProviderBase) ServiceDesc() *grpc.ServiceDesc { + return &grpc.ServiceDesc{ + ServiceName: "protobuf.Greeter", + HandlerType: (*GreeterServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "SayHello", + Handler: _DUBBO_Greeter_SayHello_Handler, + }, + }, + Streams: []grpc.StreamDesc{ + { + StreamName: "SayHelloStream", + Handler: _DUBBO_Greeter_SayHelloStream_Handler, + ServerStreams: true, + ClientStreams: true, + }, + }, + Metadata: "helloworld.proto", + } +} diff --git a/generic/protobuf-nacos/client/pkg/helloworld.proto b/generic/protobuf-nacos/client/pkg/helloworld.proto new file mode 100644 index 0000000..dac8ef0 --- /dev/null +++ b/generic/protobuf-nacos/client/pkg/helloworld.proto @@ -0,0 +1,38 @@ +/* + * 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. + */ + +syntax = "proto3"; +package pkg; + +// The greeting service definition. +service Greeter { + // Sends a greeting + rpc SayHello (HelloRequest) returns (User) {} + rpc SayHelloStream (stream HelloRequest) returns (stream User) {} +} + +// The request message containing the user's name. +message HelloRequest { + string name = 1; +} + +// The response message containing the greetings +message User { + string name = 1; + string id = 2; + int32 age = 3; +} diff --git a/generic/protobuf-nacos/client/pkg/protobuf.mk b/generic/protobuf-nacos/client/pkg/protobuf.mk new file mode 100644 index 0000000..beffc5e --- /dev/null +++ b/generic/protobuf-nacos/client/pkg/protobuf.mk @@ -0,0 +1,26 @@ +.PHONY: compile +PROTOC_GEN_GO := $(GOPATH)/bin/protoc-gen-go +PROTOC := $(shell which protoc) +ifeq ($(PROTOC),) + PROTOC = must-rebuild +endif + +UNAME := $(shell uname) + +$(PROTOC): +ifeq ($(UNAME), Darwin) + brew install protobuf +endif +ifeq ($(UNAME), Linux) + sudo apt-get install protobuf-compiler +endif + +$(PROTOC_GEN_GO): + go get -u github.com/apache/dubbo-go/protocol/dubbo3/[email protected] + +helloworld.pb.go: helloworld.proto | $(PROTOC_GEN_GO) $(PROTOC) + protoc -I . helloworld.proto --dubbo3_out=plugins=grpc+dubbo:. + +.PHONY: compile +compile: helloworld.pb.go + diff --git a/generic/protobuf-nacos/server/cmd/server.go b/generic/protobuf-nacos/server/cmd/server.go new file mode 100644 index 0000000..446117f --- /dev/null +++ b/generic/protobuf-nacos/server/cmd/server.go @@ -0,0 +1,81 @@ +/* + * 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 main + +import ( + "fmt" + "os" + "os/signal" + "syscall" + "time" +) + +import ( + _ "dubbo.apache.org/dubbo-go/v3/cluster/cluster_impl" + _ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance" + "dubbo.apache.org/dubbo-go/v3/common/logger" + _ "dubbo.apache.org/dubbo-go/v3/common/proxy/proxy_factory" + "dubbo.apache.org/dubbo-go/v3/config" + _ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl" + _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3" + _ "dubbo.apache.org/dubbo-go/v3/registry/nacos" + _ "dubbo.apache.org/dubbo-go/v3/registry/protocol" + _ "dubbo.apache.org/dubbo-go/v3/registry/zookeeper" + + _ "github.com/dubbogo/triple/pkg/triple" +) + +import ( + //"github.com/apache/dubbo-go-samples/general/dubbo3/pb/dubbogo-grpc/server/dubbogo-server/pkg" + "github.com/apache/dubbo-go-samples/generic/protobuf-nacos/server/pkg" +) + +var ( + survivalTimeout = int(3 * time.Second) +) + +// need to setup environment variable "CONF_PROVIDER_FILE_PATH" to "conf/server.yml" before run +func main() { + config.SetProviderService(pkg.NewGreeterProvider()) + config.Load() + initSignal() +} + +func initSignal() { + signals := make(chan os.Signal, 1) + // It is not possible to block SIGKILL or syscall.SIGSTOP + signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT) + for { + sig := <-signals + logger.Infof("get signal %s", sig.String()) + switch sig { + case syscall.SIGHUP: + // reload() + default: + time.Sleep(time.Second * 5) + time.AfterFunc(time.Duration(survivalTimeout), func() { + logger.Warnf("app exit now by force...") + os.Exit(1) + }) + + // The program exits normally or timeout forcibly exits. + fmt.Println("provider app exit now...") + return + } + } +} diff --git a/generic/protobuf-nacos/server/conf/client.yml b/generic/protobuf-nacos/server/conf/client.yml new file mode 100644 index 0000000..eb65e8c --- /dev/null +++ b/generic/protobuf-nacos/server/conf/client.yml @@ -0,0 +1,22 @@ +# dubbo-go3 client yaml configure file +# application config (not necessary) +application: + organization: "dubbo.io" + name: "greeterImpl" + module: "dubbo-go3.0 client" + version: "0.0.1" + environment: "dev" + +# registry config +registries: + "demoZk": + protocol: "zookeeper" + timeout: "3s" + address: "127.0.0.1:2181" + +# reference config +references: + "greeterImpl": + registry: "demoZk" + protocol: "tri" + interface: "protobuf.Greeter" \ No newline at end of file diff --git a/generic/protobuf-nacos/server/conf/log.yml b/generic/protobuf-nacos/server/conf/log.yml new file mode 100644 index 0000000..8c3f700 --- /dev/null +++ b/generic/protobuf-nacos/server/conf/log.yml @@ -0,0 +1,27 @@ +level: "info" +development: true +disableCaller: false +disableStacktrace: false +sampling: +encoding: "console" + +# encoder +encoderConfig: + messageKey: "message" + levelKey: "level" + timeKey: "time" + nameKey: "logger" + callerKey: "caller" + stacktraceKey: "stacktrace" + lineEnding: "" + levelEncoder: "capital" + timeEncoder: "iso8601" + durationEncoder: "seconds" + callerEncoder: "short" + nameEncoder: "" + +outputPaths: + - "stderr" +errorOutputPaths: + - "stderr" +initialFields: diff --git a/generic/protobuf-nacos/server/conf/server.yml b/generic/protobuf-nacos/server/conf/server.yml new file mode 100644 index 0000000..6018b37 --- /dev/null +++ b/generic/protobuf-nacos/server/conf/server.yml @@ -0,0 +1,28 @@ +# dubbo-go3 server yaml configure file +# application config (not necessary) +application: + organization: "dubbo.io" + name: "GreeterProvider" + module: "dubbo-go3.0 server" + version: "0.0.1" + environment: "dev" + +# registry config +registries: + "demoZK": + protocol: "nacos" + timeout: "3s" + address: "console.nacos.io:80" + +# service config +services: + "greeterImpl": + registry: "demoZK" + protocol: "tri" + interface: "protobuf.Greeter" # must be compatible with grpc or dubbo-java + +# protocol config +protocols: + "tri": + name: "tri" + port: 20001 diff --git a/generic/protobuf-nacos/server/pkg/greeter.go b/generic/protobuf-nacos/server/pkg/greeter.go new file mode 100644 index 0000000..39777a0 --- /dev/null +++ b/generic/protobuf-nacos/server/pkg/greeter.go @@ -0,0 +1,81 @@ +/* + * 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 pkg + +import ( + "context" + "fmt" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common/logger" + + tripleConstant "github.com/dubbogo/triple/pkg/common/constant" +) + +import ( + dubbo3 "github.com/apache/dubbo-go-samples/general/dubbo3/pb/dubbogo-grpc/protobuf/dubbo3" +) + +type GreeterProvider struct { + *dubbo3.GreeterProviderBase +} + +func NewGreeterProvider() *GreeterProvider { + return &GreeterProvider{ + GreeterProviderBase: &dubbo3.GreeterProviderBase{}, + } +} + +func (s *GreeterProvider) SayHelloStream(svr dubbo3.Greeter_SayHelloStreamServer) error { + c, err := svr.Recv() + if err != nil { + return err + } + logger.Infof("Dubbo-go3 GreeterProvider recv 1 user, name = %s\n", c.Name) + c2, err := svr.Recv() + if err != nil { + return err + } + logger.Infof("Dubbo-go3 GreeterProvider recv 2 user, name = %s\n", c2.Name) + + svr.Send(&dubbo3.User{ + Name: "hello " + c.Name, + Age: 18, + Id: "123456789", + }) + c3, err := svr.Recv() + if err != nil { + return err + } + logger.Infof("Dubbo-go3 GreeterProvider recv 3 user, name = %s\n", c3.Name) + + svr.Send(&dubbo3.User{ + Name: "hello " + c2.Name, + Age: 19, + Id: "123456789", + }) + return nil +} + +func (s *GreeterProvider) SayHello(ctx context.Context, in *dubbo3.HelloRequest) (*dubbo3.User, error) { + logger.Infof("Dubbo3 GreeterProvider get user name = %s\n" + in.Name) + fmt.Println("get triple header tri-req-id = ", ctx.Value(tripleConstant.TripleCtxKey(tripleConstant.TripleRequestID))) + fmt.Println("get triple header tri-service-version = ", ctx.Value(tripleConstant.TripleCtxKey(tripleConstant.TripleServiceVersion))) + return &dubbo3.User{Name: "Hello " + in.Name, Id: "12345", Age: 21}, nil +}
