From 51b95be83d2888939ee8bd71fd15e36d9cfab3fa Mon Sep 17 00:00:00 2001 From: yuxuanwang <464621663@qq.com> Date: Mon, 9 Oct 2023 21:29:53 +0800 Subject: [PATCH 1/2] feat: add JavaClassName option --- pkg/codec.go | 40 ++++---------- pkg/options.go | 52 +++++++++++++++++++ tests/benchmark/kitex/client/main.go | 11 ++-- tests/benchmark/kitex/server/main.go | 4 +- tests/crosstest/dubbo2kitex/base_type_test.go | 4 +- tests/crosstest/kitex2dubbo/base_type_test.go | 8 ++- tests/crosstest/kitex2kitex/base_type_test.go | 8 ++- tests/kitex/client/client.go | 4 +- tests/kitex/main.go | 4 +- 9 files changed, 93 insertions(+), 42 deletions(-) create mode 100644 pkg/options.go diff --git a/pkg/codec.go b/pkg/codec.go index 8902b5be..8a53fa96 100644 --- a/pkg/codec.go +++ b/pkg/codec.go @@ -41,11 +41,14 @@ service %s { var _ remote.Codec = (*DubboCodec)(nil) // DubboCodec NewDubboCodec creates the dubbo codec. -type DubboCodec struct{} +type DubboCodec struct { + opt *Options +} // NewDubboCodec creates a new codec instance. -func NewDubboCodec() *DubboCodec { - return &DubboCodec{} +func NewDubboCodec(opts ...Option) *DubboCodec { + o := newOptions(opts) + return &DubboCodec{opt: o} } // Name codec name @@ -104,7 +107,7 @@ func (m *DubboCodec) encodeRequestPayload(ctx context.Context, message remote.Me service := &dubbo_spec.Service{ ProtocolVersion: dubbo_spec.DEFAULT_DUBBO_PROTOCOL_VERSION, - Path: getJavaClassName(message), + Path: m.opt.JavaClassName, // todo: kitex mapping Version: "", Method: message.RPCInfo().Invocation().MethodName(), @@ -322,8 +325,8 @@ func (m *DubboCodec) decodeRequestBody(ctx context.Context, header *dubbo_spec.D return err } - if name := getJavaClassName(message); service.Path != name { - return fmt.Errorf("dubbo requested Path: %s, kitex generated JavaClassName: %s", service.Path, name) + if name := m.opt.JavaClassName; service.Path != name { + return fmt.Errorf("dubbo requested Path: %s, kitex service specified JavaClassName: %s", service.Path, name) } // decode payload @@ -447,28 +450,3 @@ func readBody(header *dubbo_spec.DubboHeader, in remote.ByteBuffer) ([]byte, err length := int(header.DataLength) return in.Next(length) } - -func getJavaClassName(message remote.Message) string { - extra := message.ServiceInfo().Extra - if extra == nil { - promptJavaClassName(message, "extra field is missing in Hessian2 generated ServiceInfo") - } - annotationsRaw, ok := extra["IDLAnnotations"] - if !ok { - promptJavaClassName(message, "IDLAnnotations is missing in Hessian2 generated ServiceInfo.extra") - } - annotations, ok := annotationsRaw.(map[string][]string) - if !ok { - promptJavaClassName(message, "IDLAnnotations is not with type map[string][]string in Hessian2 generated ServiceInfo.extra") - } - names := annotations["JavaClassName"] - if len(names) <= 0 { - promptJavaClassName(message, "JavaClassName is missing in Hessian2 generated ServiceInfo.extra[\"JavaClassName\"]") - } - return names[0] -} - -func promptJavaClassName(message remote.Message, addition string) { - serviceName := message.ServiceInfo().ServiceName - panic(addition + fmt.Sprintf(annotationPrompt, serviceName, serviceName)) -} diff --git a/pkg/options.go b/pkg/options.go new file mode 100644 index 00000000..943d57f2 --- /dev/null +++ b/pkg/options.go @@ -0,0 +1,52 @@ +/* + * Copyright 2023 CloudWeGo Authors + * + * 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 dubbo + +type Options struct { + JavaClassName string +} + +func (o *Options) Apply(opts []Option) { + for _, opt := range opts { + opt.F(o) + } +} + +func newOptions(opts []Option) *Options { + o := &Options{} + + o.Apply(opts) + if o.JavaClassName == "" { + panic("DubboCodec must be initialized with JavaClassName. Please use dubbo.WithJavaClassName().") + } + return o +} + +type Option struct { + F func(o *Options) +} + +// WithJavaClassName configures InterfaceName for server-side service and specifies target InterfaceName for client. +// Each client and service must have its own corresponding DubboCodec. +func WithJavaClassName(name string) Option { + return Option{F: func(o *Options) { + o.JavaClassName = name + }} +} diff --git a/tests/benchmark/kitex/client/main.go b/tests/benchmark/kitex/client/main.go index 1dd400e0..ba6a8c7f 100644 --- a/tests/benchmark/kitex/client/main.go +++ b/tests/benchmark/kitex/client/main.go @@ -20,18 +20,23 @@ func main() { flag.StringVar(&srvAddr, "addr", "127.0.0.1:20001", "") flag.Parse() - codec := dubbo.NewDubboCodec() + cliCodec := dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.UserProvider"), + ) cli, err := userservice.NewClient("test", client.WithHostPorts(srvAddr), - client.WithCodec(codec), + client.WithCodec(cliCodec), ) if err != nil { panic(err) } + srvCodec := dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.UserProviderProxy"), + ) addr, _ := net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(cliPort)) svr := proxyservice.NewServer(&ProxyServiceImpl{cli: cli}, server.WithServiceAddr(addr), - server.WithCodec(codec), + server.WithCodec(srvCodec), ) if err = svr.Run(); err != nil { diff --git a/tests/benchmark/kitex/server/main.go b/tests/benchmark/kitex/server/main.go index 6c0ab1c3..84a8c347 100644 --- a/tests/benchmark/kitex/server/main.go +++ b/tests/benchmark/kitex/server/main.go @@ -18,7 +18,9 @@ func main() { addr, _ := net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(srvPort)) svr := userservice.NewServer(new(UserServiceImpl), server.WithServiceAddr(addr), - server.WithCodec(dubbo.NewDubboCodec()), + server.WithCodec(dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.UserProvider"), + )), ) err := svr.Run() diff --git a/tests/crosstest/dubbo2kitex/base_type_test.go b/tests/crosstest/dubbo2kitex/base_type_test.go index 80172b21..d0e2a733 100644 --- a/tests/crosstest/dubbo2kitex/base_type_test.go +++ b/tests/crosstest/dubbo2kitex/base_type_test.go @@ -46,7 +46,9 @@ func runKitexServer(startCh chan struct{}, exitCh chan error, addr string) { svr := testservice.NewServer( new(testsuite.TestServiceImpl), server.WithServiceAddr(netAddr), - server.WithCodec(dubbo.NewDubboCodec()), + server.WithCodec(dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.tests.api.UserProvider"), + )), server.WithExitSignal(func() <-chan error { return exitCh }), diff --git a/tests/crosstest/kitex2dubbo/base_type_test.go b/tests/crosstest/kitex2dubbo/base_type_test.go index 489a62c6..c8af5ede 100644 --- a/tests/crosstest/kitex2dubbo/base_type_test.go +++ b/tests/crosstest/kitex2dubbo/base_type_test.go @@ -57,14 +57,18 @@ func TestMain(m *testing.M) { var err error cli2Go, err = testservice.NewClient("test", client.WithHostPorts("127.0.0.1:20000"), - client.WithCodec(dubbo.NewDubboCodec()), + client.WithCodec(dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.tests.api.UserProvider"), + )), ) if err != nil { panic(err) } cli2Java, err = testservice.NewClient("test", client.WithHostPorts("127.0.0.1:20001"), - client.WithCodec(dubbo.NewDubboCodec()), + client.WithCodec(dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.tests.api.UserProvider"), + )), ) if err != nil { panic(err) diff --git a/tests/crosstest/kitex2kitex/base_type_test.go b/tests/crosstest/kitex2kitex/base_type_test.go index f95efd22..00d1ee60 100644 --- a/tests/crosstest/kitex2kitex/base_type_test.go +++ b/tests/crosstest/kitex2kitex/base_type_test.go @@ -41,7 +41,9 @@ func initKitexClient(destService, hostPort string) { var err error cli, err = testservice.NewClient(destService, client.WithHostPorts(hostPort), - client.WithCodec(dubbo.NewDubboCodec()), + client.WithCodec(dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.tests.api.UserProvider"), + )), ) if err != nil { panic(fmt.Sprintf("Kitex client initialized failed, err :%s", err)) @@ -56,7 +58,9 @@ func runKitexServer(startCh chan struct{}, exitCh chan error, addr string) { svr := testservice.NewServer( new(testsuite.TestServiceImpl), server.WithServiceAddr(netAddr), - server.WithCodec(dubbo.NewDubboCodec()), + server.WithCodec(dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.tests.api.UserProvider"), + )), server.WithExitSignal(func() <-chan error { return exitCh }), diff --git a/tests/kitex/client/client.go b/tests/kitex/client/client.go index 3a298e63..88734ca3 100644 --- a/tests/kitex/client/client.go +++ b/tests/kitex/client/client.go @@ -13,7 +13,9 @@ import ( func main() { cli, err := testservice.NewClient("test", client.WithHostPorts("127.0.0.1:20000"), - client.WithCodec(dubbo.NewDubboCodec()), + client.WithCodec(dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.tests.api.UserProvider"), + )), ) if err != nil { panic(err) diff --git a/tests/kitex/main.go b/tests/kitex/main.go index 4fe78e0f..c0ce09b6 100644 --- a/tests/kitex/main.go +++ b/tests/kitex/main.go @@ -15,7 +15,9 @@ func main() { addr, _ := net.ResolveTCPAddr("tcp", ":20000") svr := echo.NewServer(new(TestServiceImpl), server.WithServiceAddr(addr), - server.WithCodec(dubbo.NewDubboCodec()), + server.WithCodec(dubbo.NewDubboCodec( + dubbo.WithJavaClassName("org.apache.dubbo.tests.api.UserProvider"), + )), ) err := svr.Run() From 63b8fede899bdfebf14a9250fe9c4f1dc53ea98d Mon Sep 17 00:00:00 2001 From: yuxuanwang <464621663@qq.com> Date: Mon, 9 Oct 2023 21:34:26 +0800 Subject: [PATCH 2/2] fix staticcheck --- pkg/codec.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pkg/codec.go b/pkg/codec.go index 8a53fa96..0231e0bd 100644 --- a/pkg/codec.go +++ b/pkg/codec.go @@ -30,14 +30,6 @@ import ( "github.com/kitex-contrib/codec-dubbo/pkg/iface" ) -var annotationPrompt = ` -Please add JavaClassName annotation as Dubbo Interface Name for %s service. -Assuming Interface Name is org.apache.dubbo.api.UserProvider, api.thrift should be: - -service %s { -}(JavaClassName="org.apache.dubbo.api.UserProvider") -` - var _ remote.Codec = (*DubboCodec)(nil) // DubboCodec NewDubboCodec creates the dubbo codec.