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

feat: add JavaClassName option #48

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 9 additions & 39 deletions pkg/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,17 @@ 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.
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
Expand Down Expand Up @@ -104,7 +99,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(),
Expand Down Expand Up @@ -322,8 +317,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
Expand Down Expand Up @@ -447,28 +442,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))
}
52 changes: 52 additions & 0 deletions pkg/options.go
Original file line number Diff line number Diff line change
@@ -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
}}
}
11 changes: 8 additions & 3 deletions tests/benchmark/kitex/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion tests/benchmark/kitex/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion tests/crosstest/dubbo2kitex/base_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}),
Expand Down
8 changes: 6 additions & 2 deletions tests/crosstest/kitex2dubbo/base_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions tests/crosstest/kitex2kitex/base_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}),
Expand Down
4 changes: 3 additions & 1 deletion tests/kitex/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion tests/kitex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down