From 975126625bc93a99f42f634858df6eb648dfe068 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Thu, 4 Jul 2019 19:03:30 +0800 Subject: [PATCH 01/26] add go to java generic --- config/generic_reference_config.go | 63 ++++++++++++++++++++++++++ config/reference_config.go | 5 +- examples/dubbo/go-client/app/client.go | 49 +++++++++++++------- 3 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 config/generic_reference_config.go diff --git a/config/generic_reference_config.go b/config/generic_reference_config.go new file mode 100644 index 0000000000..d47cfe355e --- /dev/null +++ b/config/generic_reference_config.go @@ -0,0 +1,63 @@ +package config + +import ( + "context" + "github.com/apache/dubbo-go/common/logger" +) + +type GenericService struct { + Invoke func(req []interface{}) (interface{}, error) `dubbo:"$invoke"` + referenceStr string +} + +func NewGenericService(reference string) *GenericService { + return &GenericService{referenceStr: reference} +} + +func (u *GenericService) Reference() string { + return u.referenceStr +} + +type GenericConsumerConfig struct { + Protocol string + Registry string + Version string + Group string + InterfaceName string + Cluster string + Retries int64 + ref *ReferenceConfig + genericService *GenericService +} + +func (gConfig *GenericConsumerConfig) LoadGenericReferenceConfig(key string) { + gConfig.genericService = NewGenericService(key) + SetConsumerService(gConfig.genericService) + gConfig.NewGenericReferenceConfig(key) + + rpcService := GetConsumerService(key) + if rpcService == nil { + logger.Warnf("%s is not exsist!", key) + return + } + + gConfig.ref.id = key + gConfig.ref.Refer() + gConfig.ref.Implement(rpcService) + +} +func (gConfig *GenericConsumerConfig) GetService() *GenericService { + return gConfig.genericService +} +func (gConfig *GenericConsumerConfig) NewGenericReferenceConfig(id string) { + gr := NewReferenceConfig(id, context.TODO()) + //gr.Filter = "genericConsumer" //todo: add genericConsumer filter + gr.Registry = gConfig.Registry + gr.Protocol = gConfig.Protocol + gr.Version = gConfig.Version + gr.Group = gConfig.Group + gr.InterfaceName = gConfig.InterfaceName + gr.Cluster = gConfig.Cluster + gr.Methods = append(gr.Methods, &MethodConfig{Name: "$invoke", Retries: gConfig.Retries}) + gConfig.ref = gr +} diff --git a/config/reference_config.go b/config/reference_config.go index f76c973ed5..0e08180295 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -61,8 +61,8 @@ func (c *ReferenceConfig) Prefix() string { return constant.ReferenceConfigPrefix + c.InterfaceName + "." } -func NewReferenceConfig(ctx context.Context) *ReferenceConfig { - return &ReferenceConfig{context: ctx} +func NewReferenceConfig(id string, ctx context.Context) *ReferenceConfig { + return &ReferenceConfig{id: id, context: ctx} } func (refconfig *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { @@ -109,7 +109,6 @@ func (refconfig *ReferenceConfig) Refer() { regUrl.SubURL = url } } - if len(refconfig.urls) == 1 { refconfig.invoker = extension.GetProtocol(refconfig.urls[0].Protocol).Refer(*refconfig.urls[0]) } else { diff --git a/examples/dubbo/go-client/app/client.go b/examples/dubbo/go-client/app/client.go index 0b77e60cec..250f2160f8 100644 --- a/examples/dubbo/go-client/app/client.go +++ b/examples/dubbo/go-client/app/client.go @@ -20,16 +20,14 @@ package main import ( "context" "fmt" + "github.com/apache/dubbo-go/protocol/dubbo" + hessian "github.com/dubbogo/hessian2" "os" "os/signal" "syscall" "time" ) -import ( - "github.com/dubbogo/hessian2" -) - import ( "github.com/apache/dubbo-go/common/logger" _ "github.com/apache/dubbo-go/common/proxy/proxy_factory" @@ -53,19 +51,20 @@ var ( // export APP_LOG_CONF_FILE="xxx" func main() { - hessian.RegisterJavaEnum(Gender(MAN)) - hessian.RegisterJavaEnum(Gender(WOMAN)) - hessian.RegisterPOJO(&User{}) - - config.Load() - - println("\n\ntest") - test() - println("\n\ntest1") - test1() - println("\n\ntest2") - test2() - + //hessian.RegisterJavaEnum(Gender(MAN)) + //hessian.RegisterJavaEnum(Gender(WOMAN)) + //hessian.RegisterPOJO(&User{}) + // + //config.Load() + + //println("\n\ntest") + //test() + //println("\n\ntest1") + //test1() + //println("\n\ntest2") + //test2() + //println("\n\ngeneric") + test3() initSignal() } @@ -288,3 +287,19 @@ func test2() { } println("error: %v", err) } +func test3() { + println("\n\n\nstart to generic invoke") + var genericConfig config.GenericConsumerConfig + genericConfig.InterfaceName = "com.ikurento.user.UserProvider" + genericConfig.Cluster = "failover" + genericConfig.Registry = "hangzhouzk" + genericConfig.Protocol = dubbo.DUBBO + genericConfig.LoadGenericReferenceConfig("getUser-generic") + time.Sleep(3 * time.Second) + resp, err := genericConfig.GetService().Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}}) + if err != nil { + panic(err) + } + fmt.Println("get resp:", resp) + +} From 6cde06ac3417bcac478a9e7a85bc119a09b8d3e2 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Thu, 4 Jul 2019 19:05:03 +0800 Subject: [PATCH 02/26] fix test --- examples/dubbo/go-client/app/client.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/dubbo/go-client/app/client.go b/examples/dubbo/go-client/app/client.go index 250f2160f8..01481381f6 100644 --- a/examples/dubbo/go-client/app/client.go +++ b/examples/dubbo/go-client/app/client.go @@ -51,19 +51,19 @@ var ( // export APP_LOG_CONF_FILE="xxx" func main() { - //hessian.RegisterJavaEnum(Gender(MAN)) - //hessian.RegisterJavaEnum(Gender(WOMAN)) - //hessian.RegisterPOJO(&User{}) - // - //config.Load() - - //println("\n\ntest") - //test() - //println("\n\ntest1") - //test1() - //println("\n\ntest2") - //test2() - //println("\n\ngeneric") + hessian.RegisterJavaEnum(Gender(MAN)) + hessian.RegisterJavaEnum(Gender(WOMAN)) + hessian.RegisterPOJO(&User{}) + + config.Load() + + println("\n\ntest") + test() + println("\n\ntest1") + test1() + println("\n\ntest2") + test2() + println("\n\ngeneric") test3() initSignal() } From 25b1512d7c55c2b64588d77a273045ae523c1d14 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Thu, 4 Jul 2019 19:08:02 +0800 Subject: [PATCH 03/26] update --- examples/dubbo/go-client/app/client.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/dubbo/go-client/app/client.go b/examples/dubbo/go-client/app/client.go index 01481381f6..63e7c91ddc 100644 --- a/examples/dubbo/go-client/app/client.go +++ b/examples/dubbo/go-client/app/client.go @@ -63,7 +63,7 @@ func main() { test1() println("\n\ntest2") test2() - println("\n\ngeneric") + println("\n\ntest3") test3() initSignal() } @@ -295,11 +295,13 @@ func test3() { genericConfig.Registry = "hangzhouzk" genericConfig.Protocol = dubbo.DUBBO genericConfig.LoadGenericReferenceConfig("getUser-generic") + time.Sleep(3 * time.Second) resp, err := genericConfig.GetService().Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}}) if err != nil { panic(err) } - fmt.Println("get resp:", resp) + println("res: %v\n", resp) + println("succ!") } From e216bcb562af0d33946e7ff6bce2b3545f468559 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 5 Jul 2019 14:37:30 +0800 Subject: [PATCH 04/26] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/generic_reference_config.go | 47 +++++++++++++------------ config/generic_reference_config_test.go | 1 + config/reference_config.go | 4 +-- examples/dubbo/go-client/app/client.go | 26 ++++++++------ 4 files changed, 42 insertions(+), 36 deletions(-) create mode 100644 config/generic_reference_config_test.go diff --git a/config/generic_reference_config.go b/config/generic_reference_config.go index d47cfe355e..87ec0002ca 100644 --- a/config/generic_reference_config.go +++ b/config/generic_reference_config.go @@ -2,16 +2,18 @@ package config import ( "context" - "github.com/apache/dubbo-go/common/logger" + "errors" ) +const GenericReferKey = "GenericReferKey" + type GenericService struct { Invoke func(req []interface{}) (interface{}, error) `dubbo:"$invoke"` referenceStr string } -func NewGenericService(reference string) *GenericService { - return &GenericService{referenceStr: reference} +func NewGenericService(referenceStr string) *GenericService { + return &GenericService{referenceStr: GenericReferKey} } func (u *GenericService) Reference() string { @@ -19,6 +21,7 @@ func (u *GenericService) Reference() string { } type GenericConsumerConfig struct { + ID string Protocol string Registry string Version string @@ -30,27 +33,9 @@ type GenericConsumerConfig struct { genericService *GenericService } -func (gConfig *GenericConsumerConfig) LoadGenericReferenceConfig(key string) { - gConfig.genericService = NewGenericService(key) - SetConsumerService(gConfig.genericService) - gConfig.NewGenericReferenceConfig(key) - - rpcService := GetConsumerService(key) - if rpcService == nil { - logger.Warnf("%s is not exsist!", key) - return - } +func (gConfig *GenericConsumerConfig) Load() (err error) { - gConfig.ref.id = key - gConfig.ref.Refer() - gConfig.ref.Implement(rpcService) - -} -func (gConfig *GenericConsumerConfig) GetService() *GenericService { - return gConfig.genericService -} -func (gConfig *GenericConsumerConfig) NewGenericReferenceConfig(id string) { - gr := NewReferenceConfig(id, context.TODO()) + gr := NewReferenceConfig(context.TODO()) //gr.Filter = "genericConsumer" //todo: add genericConsumer filter gr.Registry = gConfig.Registry gr.Protocol = gConfig.Protocol @@ -60,4 +45,20 @@ func (gConfig *GenericConsumerConfig) NewGenericReferenceConfig(id string) { gr.Cluster = gConfig.Cluster gr.Methods = append(gr.Methods, &MethodConfig{Name: "$invoke", Retries: gConfig.Retries}) gConfig.ref = gr + gConfig.genericService = NewGenericService(gConfig.ID) + SetConsumerService(gConfig.genericService) + rpcService := GetConsumerService(GenericReferKey) + if rpcService == nil { + err = errors.New("get rpcService err,GenericReferKey not Set ") + return + } + + gConfig.ref.id = gConfig.ID + gConfig.ref.Refer() + gConfig.ref.Implement(rpcService) + return + +} +func (gConfig *GenericConsumerConfig) GetGenericService() *GenericService { + return gConfig.genericService } diff --git a/config/generic_reference_config_test.go b/config/generic_reference_config_test.go new file mode 100644 index 0000000000..d912156bec --- /dev/null +++ b/config/generic_reference_config_test.go @@ -0,0 +1 @@ +package config diff --git a/config/reference_config.go b/config/reference_config.go index 0e08180295..5300e46116 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -61,8 +61,8 @@ func (c *ReferenceConfig) Prefix() string { return constant.ReferenceConfigPrefix + c.InterfaceName + "." } -func NewReferenceConfig(id string, ctx context.Context) *ReferenceConfig { - return &ReferenceConfig{id: id, context: ctx} +func NewReferenceConfig(ctx context.Context) *ReferenceConfig { + return &ReferenceConfig{context: ctx} } func (refconfig *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { diff --git a/examples/dubbo/go-client/app/client.go b/examples/dubbo/go-client/app/client.go index 63e7c91ddc..d0694615a3 100644 --- a/examples/dubbo/go-client/app/client.go +++ b/examples/dubbo/go-client/app/client.go @@ -20,19 +20,21 @@ package main import ( "context" "fmt" - "github.com/apache/dubbo-go/protocol/dubbo" - hessian "github.com/dubbogo/hessian2" + "os" "os/signal" "syscall" "time" ) +import ( + hessian "github.com/dubbogo/hessian2" +) import ( "github.com/apache/dubbo-go/common/logger" _ "github.com/apache/dubbo-go/common/proxy/proxy_factory" "github.com/apache/dubbo-go/config" - _ "github.com/apache/dubbo-go/protocol/dubbo" + "github.com/apache/dubbo-go/protocol/dubbo" _ "github.com/apache/dubbo-go/registry/protocol" _ "github.com/apache/dubbo-go/filter/impl" @@ -288,16 +290,18 @@ func test2() { println("error: %v", err) } func test3() { - println("\n\n\nstart to generic invoke") - var genericConfig config.GenericConsumerConfig - genericConfig.InterfaceName = "com.ikurento.user.UserProvider" - genericConfig.Cluster = "failover" - genericConfig.Registry = "hangzhouzk" - genericConfig.Protocol = dubbo.DUBBO - genericConfig.LoadGenericReferenceConfig("getUser-generic") + var genericConfig = config.GenericConsumerConfig{ + ID: "UserProviderGer", //GetService的唯一标识不可缺少 + InterfaceName: "com.ikurento.user.UserProvider", + Cluster: "failover", + Registry: "hangzhouzk", + Protocol: dubbo.DUBBO, + } + genericConfig.Load() time.Sleep(3 * time.Second) - resp, err := genericConfig.GetService().Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}}) + println("\n\n\nstart to generic invoke") + resp, err := genericConfig.GetGenericService().Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}}) if err != nil { panic(err) } From 2cf93999879b6eb792fa12749c915fc3afaf8759 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 5 Jul 2019 15:37:51 +0800 Subject: [PATCH 05/26] =?UTF-8?q?=E6=94=B9=E6=88=90=E5=92=8Cjava=E4=B8=80?= =?UTF-8?q?=E6=A0=B7=E7=9A=84=E4=BD=BF=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/generic_reference_config.go | 54 ++++---------------------- examples/dubbo/go-client/app/client.go | 8 ++-- 2 files changed, 11 insertions(+), 51 deletions(-) diff --git a/config/generic_reference_config.go b/config/generic_reference_config.go index 87ec0002ca..aa73ed2bab 100644 --- a/config/generic_reference_config.go +++ b/config/generic_reference_config.go @@ -1,64 +1,24 @@ package config -import ( - "context" - "errors" -) - -const GenericReferKey = "GenericReferKey" - type GenericService struct { Invoke func(req []interface{}) (interface{}, error) `dubbo:"$invoke"` referenceStr string } func NewGenericService(referenceStr string) *GenericService { - return &GenericService{referenceStr: GenericReferKey} + return &GenericService{referenceStr: referenceStr} } func (u *GenericService) Reference() string { return u.referenceStr } -type GenericConsumerConfig struct { - ID string - Protocol string - Registry string - Version string - Group string - InterfaceName string - Cluster string - Retries int64 - ref *ReferenceConfig - genericService *GenericService -} - -func (gConfig *GenericConsumerConfig) Load() (err error) { - - gr := NewReferenceConfig(context.TODO()) +func (refconfig *ReferenceConfig) Load(id string) { //gr.Filter = "genericConsumer" //todo: add genericConsumer filter - gr.Registry = gConfig.Registry - gr.Protocol = gConfig.Protocol - gr.Version = gConfig.Version - gr.Group = gConfig.Group - gr.InterfaceName = gConfig.InterfaceName - gr.Cluster = gConfig.Cluster - gr.Methods = append(gr.Methods, &MethodConfig{Name: "$invoke", Retries: gConfig.Retries}) - gConfig.ref = gr - gConfig.genericService = NewGenericService(gConfig.ID) - SetConsumerService(gConfig.genericService) - rpcService := GetConsumerService(GenericReferKey) - if rpcService == nil { - err = errors.New("get rpcService err,GenericReferKey not Set ") - return - } - - gConfig.ref.id = gConfig.ID - gConfig.ref.Refer() - gConfig.ref.Implement(rpcService) + genericService := NewGenericService(refconfig.id) + SetConsumerService(genericService) + refconfig.id = id + refconfig.Refer() + refconfig.Implement(genericService) return - -} -func (gConfig *GenericConsumerConfig) GetGenericService() *GenericService { - return gConfig.genericService } diff --git a/examples/dubbo/go-client/app/client.go b/examples/dubbo/go-client/app/client.go index d0694615a3..186ef2612a 100644 --- a/examples/dubbo/go-client/app/client.go +++ b/examples/dubbo/go-client/app/client.go @@ -290,18 +290,18 @@ func test2() { println("error: %v", err) } func test3() { - var genericConfig = config.GenericConsumerConfig{ - ID: "UserProviderGer", //GetService的唯一标识不可缺少 + var appName = "UserProviderGer" + var referenceConfig = config.ReferenceConfig{ InterfaceName: "com.ikurento.user.UserProvider", Cluster: "failover", Registry: "hangzhouzk", Protocol: dubbo.DUBBO, } - genericConfig.Load() + referenceConfig.Load(appName) //appName是GetService的唯一标识不可缺少 time.Sleep(3 * time.Second) println("\n\n\nstart to generic invoke") - resp, err := genericConfig.GetGenericService().Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}}) + resp, err := referenceConfig.GetRPCService().(*config.GenericService).Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}}) if err != nil { panic(err) } From 4df2f94106dab4e0dab861987273473e33a41f14 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 5 Jul 2019 15:53:20 +0800 Subject: [PATCH 06/26] fix --- config/generic_reference_config.go | 10 ---------- config/reference_config.go | 9 +++++++++ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/config/generic_reference_config.go b/config/generic_reference_config.go index aa73ed2bab..9a57c6d480 100644 --- a/config/generic_reference_config.go +++ b/config/generic_reference_config.go @@ -12,13 +12,3 @@ func NewGenericService(referenceStr string) *GenericService { func (u *GenericService) Reference() string { return u.referenceStr } - -func (refconfig *ReferenceConfig) Load(id string) { - //gr.Filter = "genericConsumer" //todo: add genericConsumer filter - genericService := NewGenericService(refconfig.id) - SetConsumerService(genericService) - refconfig.id = id - refconfig.Refer() - refconfig.Implement(genericService) - return -} diff --git a/config/reference_config.go b/config/reference_config.go index 5300e46116..dae66bdb84 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -178,3 +178,12 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values { return urlMap } +func (refconfig *ReferenceConfig) Load(id string) { + //gr.Filter = "genericConsumer" //todo: add genericConsumer filter + genericService := NewGenericService(refconfig.id) + SetConsumerService(genericService) + refconfig.id = id + refconfig.Refer() + refconfig.Implement(genericService) + return +} From e545dd01a7fae152157f24d9c1d37e5e164940f1 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 5 Jul 2019 15:55:03 +0800 Subject: [PATCH 07/26] add license --- config/generic_reference_config.go | 16 ++++++++++++++++ config/generic_reference_config_test.go | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/config/generic_reference_config.go b/config/generic_reference_config.go index 9a57c6d480..8a4e88df97 100644 --- a/config/generic_reference_config.go +++ b/config/generic_reference_config.go @@ -1,3 +1,19 @@ +/* + * 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 config type GenericService struct { diff --git a/config/generic_reference_config_test.go b/config/generic_reference_config_test.go index d912156bec..027edf85b4 100644 --- a/config/generic_reference_config_test.go +++ b/config/generic_reference_config_test.go @@ -1 +1,17 @@ +/* + * 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 config From 48073dde13f652b7715ac759ebe007b95d7b9b24 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 5 Jul 2019 16:07:41 +0800 Subject: [PATCH 08/26] change file name --- config/{generic_reference_config.go => generic_service.go} | 0 .../{generic_reference_config_test.go => generic_service_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename config/{generic_reference_config.go => generic_service.go} (100%) rename config/{generic_reference_config_test.go => generic_service_test.go} (100%) diff --git a/config/generic_reference_config.go b/config/generic_service.go similarity index 100% rename from config/generic_reference_config.go rename to config/generic_service.go diff --git a/config/generic_reference_config_test.go b/config/generic_service_test.go similarity index 100% rename from config/generic_reference_config_test.go rename to config/generic_service_test.go From 5e0ebf7eb0d7380d09856ab0e13ecec982cc9463 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 5 Jul 2019 18:50:26 +0800 Subject: [PATCH 09/26] =?UTF-8?q?fix=E3=80=81and=20jsonrpc=20has=20no=20ge?= =?UTF-8?q?nericComsumer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/dubbo/go-client/app/client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/dubbo/go-client/app/client.go b/examples/dubbo/go-client/app/client.go index 186ef2612a..aadcea6ab7 100644 --- a/examples/dubbo/go-client/app/client.go +++ b/examples/dubbo/go-client/app/client.go @@ -20,7 +20,6 @@ package main import ( "context" "fmt" - "os" "os/signal" "syscall" From 4dbeb2a86681e23d3f44e642fd75e10d0249ddae Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Mon, 15 Jul 2019 20:31:27 +0800 Subject: [PATCH 10/26] fix bug with Temporary disposal --- remoting/zookeeper/client.go | 37 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/remoting/zookeeper/client.go b/remoting/zookeeper/client.go index ffd98391af..2519806159 100644 --- a/remoting/zookeeper/client.go +++ b/remoting/zookeeper/client.go @@ -301,26 +301,27 @@ func (z *ZookeeperClient) UnregisterEvent(zkPath string, event *chan struct{}) { } z.Lock() - for { - a, ok := z.eventRegistry[zkPath] - if !ok { - break - } - for i, e := range a { - if e == event { - arr := a - a = append(arr[:i], arr[i+1:]...) - logger.Debugf("zkClient{%s} unregister event{path:%s, event:%p}", z.name, zkPath, event) - } - } - logger.Debugf("after zkClient{%s} unregister event{path:%s, event:%p}, array length %d", - z.name, zkPath, event, len(a)) - if len(a) == 0 { - delete(z.eventRegistry, zkPath) - } else { - z.eventRegistry[zkPath] = a + //for { + a, ok := z.eventRegistry[zkPath] + if !ok { + return + //break + } + for i, e := range a { + if e == event { + arr := a + a = append(arr[:i], arr[i+1:]...) + logger.Debugf("zkClient{%s} unregister event{path:%s, event:%p}", z.name, zkPath, event) } } + logger.Debugf("after zkClient{%s} unregister event{path:%s, event:%p}, array length %d", + z.name, zkPath, event, len(a)) + if len(a) == 0 { + delete(z.eventRegistry, zkPath) + } else { + z.eventRegistry[zkPath] = a + } + //} z.Unlock() } From 81b8c35bfa32c18a304206e61d07d8e8c66a243c Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Wed, 17 Jul 2019 20:14:33 +0800 Subject: [PATCH 11/26] change annotation --- examples/dubbo/go-client/app/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dubbo/go-client/app/client.go b/examples/dubbo/go-client/app/client.go index aadcea6ab7..b79874b2bd 100644 --- a/examples/dubbo/go-client/app/client.go +++ b/examples/dubbo/go-client/app/client.go @@ -296,7 +296,7 @@ func test3() { Registry: "hangzhouzk", Protocol: dubbo.DUBBO, } - referenceConfig.Load(appName) //appName是GetService的唯一标识不可缺少 + referenceConfig.Load(appName) //appName is the unique identification of RPCService time.Sleep(3 * time.Second) println("\n\n\nstart to generic invoke") From 96f5554a4c8b45ca08c25b834b74534a5e5959a6 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Tue, 30 Jul 2019 18:03:24 +0800 Subject: [PATCH 12/26] add generic filter --- common/constant/default.go | 2 + config/reference_config.go | 2 +- filter/impl/generic_filter.go | 75 +++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 filter/impl/generic_filter.go diff --git a/common/constant/default.go b/common/constant/default.go index 05461ca6e7..7ab92249ad 100644 --- a/common/constant/default.go +++ b/common/constant/default.go @@ -38,6 +38,8 @@ const ( PREFIX_DEFAULT_KEY = "default." DEFAULT_SERVICE_FILTERS = "echo" DEFAULT_REFERENCE_FILTERS = "" + GENERIC_REFERENCE_FILTERS = "generic" + GENERIC = "$invoke" ECHO = "$echo" ) diff --git a/config/reference_config.go b/config/reference_config.go index 0ab24e184a..c1565004c0 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -170,7 +170,7 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values { //filter urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, constant.DEFAULT_REFERENCE_FILTERS)) - + urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, constant.GENERIC_REFERENCE_FILTERS)) for _, v := range refconfig.Methods { urlMap.Set("methods."+v.Name+"."+constant.LOADBALANCE_KEY, v.Loadbalance) urlMap.Set("methods."+v.Name+"."+constant.RETRIES_KEY, strconv.FormatInt(v.Retries, 10)) diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go new file mode 100644 index 0000000000..14da6eeabe --- /dev/null +++ b/filter/impl/generic_filter.go @@ -0,0 +1,75 @@ +package impl + +import ( + "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/filter" + "github.com/apache/dubbo-go/protocol" + invocation2 "github.com/apache/dubbo-go/protocol/invocation" + "reflect" + "strings" +) + +const ( + GENERIC = "generic" +) + +func init() { + extension.SetFilter(GENERIC, GetGenericFilter) +} + +// when do a generic invoke, struct need to be map + +type GenericFilter struct{} + +func (ef *GenericFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { + if invocation.MethodName() == constant.GENERIC { + var newArguments = invocation.Arguments() + for i := range newArguments { + newArguments[i] = Struct2MapAll(newArguments[i]) + } + newInvocation := invocation2.NewRPCInvocation(invocation.MethodName(), newArguments, invocation.Attachments()) + return invoker.Invoke(newInvocation) + } + return invoker.Invoke(invocation) + +} + +func (ef *GenericFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { + return result +} + +func GetGenericFilter() filter.Filter { + return &GenericFilter{} +} +func Struct2MapAll(obj interface{}) map[string]interface{} { + t := reflect.TypeOf(obj) + v := reflect.ValueOf(obj) + result := make(map[string]interface{}) + if reflect.TypeOf(obj).Kind() == reflect.Struct { + for i := 0; i < t.NumField(); i++ { + if v.Field(i).Kind() == reflect.Struct { + if v.Field(i).CanInterface() { + result[headerAtoa(t.Field(i).Name)] = Struct2MapAll(v.Field(i).Interface()) + } else { + println("not in to map,field:" + t.Field(i).Name) + } + } else { + if v.Field(i).CanInterface() { + if tagName := t.Field(i).Tag.Get("m"); tagName == "" { + result[headerAtoa(t.Field(i).Name)] = v.Field(i).Interface() + } else { + result[tagName] = v.Field(i).Interface() + } + } else { + println("not in to map,field:" + t.Field(i).Name) + } + } + } + } + return result +} +func headerAtoa(a string) (b string) { + b = strings.ToLower(a[:1]) + a[1:] + return +} From 74e16bbe275eb1d8517489a2e2842ab4c1871008 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Tue, 30 Jul 2019 20:12:57 +0800 Subject: [PATCH 13/26] add ut --- filter/impl/generic_filter.go | 28 +++++++++++----- filter/impl/generic_filter_test.go | 52 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 filter/impl/generic_filter_test.go diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go index 14da6eeabe..b9ed9b62e9 100644 --- a/filter/impl/generic_filter.go +++ b/filter/impl/generic_filter.go @@ -1,3 +1,20 @@ +/* + * 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 impl import ( @@ -26,13 +43,12 @@ func (ef *GenericFilter) Invoke(invoker protocol.Invoker, invocation protocol.In if invocation.MethodName() == constant.GENERIC { var newArguments = invocation.Arguments() for i := range newArguments { - newArguments[i] = Struct2MapAll(newArguments[i]) + newArguments[i] = struct2MapAll(newArguments[i]) } newInvocation := invocation2.NewRPCInvocation(invocation.MethodName(), newArguments, invocation.Attachments()) return invoker.Invoke(newInvocation) } return invoker.Invoke(invocation) - } func (ef *GenericFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { @@ -42,7 +58,7 @@ func (ef *GenericFilter) OnResponse(result protocol.Result, invoker protocol.Inv func GetGenericFilter() filter.Filter { return &GenericFilter{} } -func Struct2MapAll(obj interface{}) map[string]interface{} { +func struct2MapAll(obj interface{}) map[string]interface{} { t := reflect.TypeOf(obj) v := reflect.ValueOf(obj) result := make(map[string]interface{}) @@ -50,9 +66,7 @@ func Struct2MapAll(obj interface{}) map[string]interface{} { for i := 0; i < t.NumField(); i++ { if v.Field(i).Kind() == reflect.Struct { if v.Field(i).CanInterface() { - result[headerAtoa(t.Field(i).Name)] = Struct2MapAll(v.Field(i).Interface()) - } else { - println("not in to map,field:" + t.Field(i).Name) + result[headerAtoa(t.Field(i).Name)] = struct2MapAll(v.Field(i).Interface()) } } else { if v.Field(i).CanInterface() { @@ -61,8 +75,6 @@ func Struct2MapAll(obj interface{}) map[string]interface{} { } else { result[tagName] = v.Field(i).Interface() } - } else { - println("not in to map,field:" + t.Field(i).Name) } } } diff --git a/filter/impl/generic_filter_test.go b/filter/impl/generic_filter_test.go new file mode 100644 index 0000000000..3e236a1c08 --- /dev/null +++ b/filter/impl/generic_filter_test.go @@ -0,0 +1,52 @@ +/* + * 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 impl + +import ( + "github.com/stretchr/testify/assert" +) +import ( + "fmt" + "testing" +) + +func Test_struct2MapAll(t *testing.T) { + var testData struct { + AaAa string `m:"aaAa"` + BaBa string `m:"baBa"` + CaCa struct { + AaAa string + BaBa string `m:"baBa"` + XxYy struct { + xxXx string `m:"xxXx"` + Xx string `m:"xx"` + } `m:"xxYy"` + } `m:"caCa"` + } + testData.AaAa = "1" + testData.BaBa = "1" + testData.CaCa.BaBa = "2" + testData.CaCa.AaAa = "2" + testData.CaCa.XxYy.xxXx = "3" + testData.CaCa.XxYy.Xx = "3" + m := struct2MapAll(testData) + fmt.Printf("%v", m) + expect := `map[aaAa:1 baBa:1 caCa:map[aaAa:2 baBa:2 xxYy:map[xx:3]]]` + get := fmt.Sprintf("%v", m) + assert.Equal(t, expect, get) +} From 428fad4fe1209146f97682fe79cd445c61358580 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Tue, 30 Jul 2019 20:35:43 +0800 Subject: [PATCH 14/26] ut be compatible with go 1.11 --- filter/impl/generic_filter_test.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/filter/impl/generic_filter_test.go b/filter/impl/generic_filter_test.go index 3e236a1c08..b039fbd89e 100644 --- a/filter/impl/generic_filter_test.go +++ b/filter/impl/generic_filter_test.go @@ -19,16 +19,16 @@ package impl import ( "github.com/stretchr/testify/assert" + "reflect" ) import ( - "fmt" "testing" ) func Test_struct2MapAll(t *testing.T) { var testData struct { AaAa string `m:"aaAa"` - BaBa string `m:"baBa"` + BaBa string CaCa struct { AaAa string BaBa string `m:"baBa"` @@ -45,8 +45,11 @@ func Test_struct2MapAll(t *testing.T) { testData.CaCa.XxYy.xxXx = "3" testData.CaCa.XxYy.Xx = "3" m := struct2MapAll(testData) - fmt.Printf("%v", m) - expect := `map[aaAa:1 baBa:1 caCa:map[aaAa:2 baBa:2 xxYy:map[xx:3]]]` - get := fmt.Sprintf("%v", m) - assert.Equal(t, expect, get) + assert.Equal(t, "1", m["aaAa"].(string)) + assert.Equal(t, "1", m["baBa"].(string)) + assert.Equal(t, "2", m["caCa"].(map[string]interface{})["aaAa"].(string)) + assert.Equal(t, "3", m["caCa"].(map[string]interface{})["xxYy"].(map[string]interface{})["xx"].(string)) + + assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"]).Kind()) + assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"].(map[string]interface{})["xxYy"]).Kind()) } From 31c898f5950f39ae7f677cd82867fb541c38140b Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Wed, 31 Jul 2019 10:41:01 +0800 Subject: [PATCH 15/26] add Generic tag --- config/reference_config.go | 6 +++++- examples/general/dubbo/go-client/app/client.go | 1 + filter/impl/generic_filter_test.go | 2 +- registry/directory/directory.go | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/config/reference_config.go b/config/reference_config.go index c1565004c0..99d442ad46 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -55,6 +55,7 @@ type ReferenceConfig struct { Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` invoker protocol.Invoker urls []*common.URL + Generic bool } func (c *ReferenceConfig) Prefix() string { @@ -170,7 +171,10 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values { //filter urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, constant.DEFAULT_REFERENCE_FILTERS)) - urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, constant.GENERIC_REFERENCE_FILTERS)) + if refconfig.Generic { + urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, constant.GENERIC_REFERENCE_FILTERS)) + } + for _, v := range refconfig.Methods { urlMap.Set("methods."+v.Name+"."+constant.LOADBALANCE_KEY, v.Loadbalance) urlMap.Set("methods."+v.Name+"."+constant.RETRIES_KEY, strconv.FormatInt(v.Retries, 10)) diff --git a/examples/general/dubbo/go-client/app/client.go b/examples/general/dubbo/go-client/app/client.go index b79874b2bd..62113e2d31 100644 --- a/examples/general/dubbo/go-client/app/client.go +++ b/examples/general/dubbo/go-client/app/client.go @@ -295,6 +295,7 @@ func test3() { Cluster: "failover", Registry: "hangzhouzk", Protocol: dubbo.DUBBO, + Generic: true, } referenceConfig.Load(appName) //appName is the unique identification of RPCService diff --git a/filter/impl/generic_filter_test.go b/filter/impl/generic_filter_test.go index b039fbd89e..bdeb953297 100644 --- a/filter/impl/generic_filter_test.go +++ b/filter/impl/generic_filter_test.go @@ -19,9 +19,9 @@ package impl import ( "github.com/stretchr/testify/assert" - "reflect" ) import ( + "reflect" "testing" ) diff --git a/registry/directory/directory.go b/registry/directory/directory.go index 11687f82ee..6a5a194391 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -116,7 +116,7 @@ func (dir *registryDirectory) Subscribe(url common.URL) { } //subscribe service from registry , and update the cacheServices -func (dir *registryDirectory) update(res *registry.ServiceEvent) { +func (dir *registryDirectory) /**/ update(res *registry.ServiceEvent) { if res == nil { return } From 6e5df6c293811344c0e84f5a2186234152ebbfbe Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Wed, 31 Jul 2019 10:51:47 +0800 Subject: [PATCH 16/26] fix --- registry/directory/directory.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registry/directory/directory.go b/registry/directory/directory.go index 6a5a194391..11687f82ee 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -116,7 +116,7 @@ func (dir *registryDirectory) Subscribe(url common.URL) { } //subscribe service from registry , and update the cacheServices -func (dir *registryDirectory) /**/ update(res *registry.ServiceEvent) { +func (dir *registryDirectory) update(res *registry.ServiceEvent) { if res == nil { return } From a1e9a4d65b22bb5a023c1401c6f3c57e7ba8141d Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Wed, 31 Jul 2019 14:47:15 +0800 Subject: [PATCH 17/26] fix fmt --- common/constant/key.go | 1 + config/generic_service_test.go | 17 ----------------- config/reference_config.go | 6 +++--- examples/general/dubbo/go-client/app/client.go | 2 +- filter/impl/generic_filter.go | 6 ++++-- filter/impl/generic_filter_test.go | 6 +++--- 6 files changed, 12 insertions(+), 26 deletions(-) delete mode 100644 config/generic_service_test.go diff --git a/common/constant/key.go b/common/constant/key.go index bca658b262..ba86c531c9 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -30,6 +30,7 @@ const ( METHODS_KEY = "methods" TIMEOUT_KEY = "timeout" BEAN_NAME_KEY = "bean.name" + GENERIC_KEY = "generic" ) const ( diff --git a/config/generic_service_test.go b/config/generic_service_test.go deleted file mode 100644 index 027edf85b4..0000000000 --- a/config/generic_service_test.go +++ /dev/null @@ -1,17 +0,0 @@ -/* - * 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 config diff --git a/config/reference_config.go b/config/reference_config.go index 99d442ad46..4f2bdbbc29 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -55,7 +55,7 @@ type ReferenceConfig struct { Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` invoker protocol.Invoker urls []*common.URL - Generic bool + Generic bool `yaml:"generic" json:"generic,omitempty" property:"generic"` } func (c *ReferenceConfig) Prefix() string { @@ -157,6 +157,7 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values { urlMap.Set(constant.RETRIES_KEY, strconv.FormatInt(refconfig.Retries, 10)) urlMap.Set(constant.GROUP_KEY, refconfig.Group) urlMap.Set(constant.VERSION_KEY, refconfig.Version) + urlMap.Set(constant.GENERIC_KEY, strconv.FormatBool(refconfig.Generic)) //getty invoke async or sync urlMap.Set(constant.ASYNC_KEY, strconv.FormatBool(refconfig.async)) @@ -183,8 +184,7 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values { return urlMap } -func (refconfig *ReferenceConfig) Load(id string) { - //gr.Filter = "genericConsumer" //todo: add genericConsumer filter +func (refconfig *ReferenceConfig) GenericLoad(id string) { genericService := NewGenericService(refconfig.id) SetConsumerService(genericService) refconfig.id = id diff --git a/examples/general/dubbo/go-client/app/client.go b/examples/general/dubbo/go-client/app/client.go index 8fc7d992a4..9b50ca0a36 100644 --- a/examples/general/dubbo/go-client/app/client.go +++ b/examples/general/dubbo/go-client/app/client.go @@ -297,7 +297,7 @@ func test3() { Protocol: dubbo.DUBBO, Generic: true, } - referenceConfig.Load(appName) //appName is the unique identification of RPCService + referenceConfig.GenericLoad(appName) //appName is the unique identification of RPCService time.Sleep(3 * time.Second) println("\n\n\nstart to generic invoke") diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go index b9ed9b62e9..e23c34ebc2 100644 --- a/filter/impl/generic_filter.go +++ b/filter/impl/generic_filter.go @@ -17,14 +17,16 @@ package impl +import ( + "reflect" + "strings" +) import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/filter" "github.com/apache/dubbo-go/protocol" invocation2 "github.com/apache/dubbo-go/protocol/invocation" - "reflect" - "strings" ) const ( diff --git a/filter/impl/generic_filter_test.go b/filter/impl/generic_filter_test.go index bdeb953297..405bf789b7 100644 --- a/filter/impl/generic_filter_test.go +++ b/filter/impl/generic_filter_test.go @@ -17,13 +17,13 @@ package impl -import ( - "github.com/stretchr/testify/assert" -) import ( "reflect" "testing" ) +import ( + "github.com/stretchr/testify/assert" +) func Test_struct2MapAll(t *testing.T) { var testData struct { From 068983bf33bdfa44b44e3690c8e25519ab87151a Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 2 Aug 2019 15:57:29 +0800 Subject: [PATCH 18/26] add generic config.Load support --- config/config_loader.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/config_loader.go b/config/config_loader.go index 0dd0fb7f96..720f65f5de 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -66,6 +66,10 @@ func Load() { logger.Errorf("[consumer config center refresh] %#v", err) } for key, ref := range consumerConfig.References { + if ref.Generic { + genericService := NewGenericService(key) + SetConsumerService(genericService) + } rpcService := GetConsumerService(key) if rpcService == nil { From c3d2c9cab067d499236a5337b071b212ad8bdac2 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 2 Aug 2019 16:37:28 +0800 Subject: [PATCH 19/26] fix bug --- .../general/dubbo/go-client/app/client.go | 2 +- filter/impl/generic_filter.go | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/examples/general/dubbo/go-client/app/client.go b/examples/general/dubbo/go-client/app/client.go index 9b50ca0a36..956ab7466c 100644 --- a/examples/general/dubbo/go-client/app/client.go +++ b/examples/general/dubbo/go-client/app/client.go @@ -301,7 +301,7 @@ func test3() { time.Sleep(3 * time.Second) println("\n\n\nstart to generic invoke") - resp, err := referenceConfig.GetRPCService().(*config.GenericService).Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []hessian.Object{"A003"}}) + resp, err := referenceConfig.GetRPCService().(*config.GenericService).Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, "A003"}) if err != nil { panic(err) } diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go index e23c34ebc2..d369ca0a52 100644 --- a/filter/impl/generic_filter.go +++ b/filter/impl/generic_filter.go @@ -18,6 +18,7 @@ package impl import ( + hessian "github.com/apache/dubbo-go-hessian2" "reflect" "strings" ) @@ -42,10 +43,14 @@ func init() { type GenericFilter struct{} func (ef *GenericFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { - if invocation.MethodName() == constant.GENERIC { - var newArguments = invocation.Arguments() - for i := range newArguments { - newArguments[i] = struct2MapAll(newArguments[i]) + if invocation.MethodName() == constant.GENERIC && len(invocation.Arguments()) == 3 { + var ( + oldArguments = invocation.Arguments() + ) + newArguments := []interface{}{ + oldArguments[0], + oldArguments[1], + hessian.Object(struct2MapAll(oldArguments[2])), } newInvocation := invocation2.NewRPCInvocation(invocation.MethodName(), newArguments, invocation.Attachments()) return invoker.Invoke(newInvocation) @@ -61,9 +66,13 @@ func GetGenericFilter() filter.Filter { return &GenericFilter{} } func struct2MapAll(obj interface{}) map[string]interface{} { + result := make(map[string]interface{}) + if obj == nil { + return result + } t := reflect.TypeOf(obj) v := reflect.ValueOf(obj) - result := make(map[string]interface{}) + if reflect.TypeOf(obj).Kind() == reflect.Struct { for i := 0; i < t.NumField(); i++ { if v.Field(i).Kind() == reflect.Struct { From 754252be511c8b8d3c5663d15471a2519886e7ec Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 2 Aug 2019 17:05:38 +0800 Subject: [PATCH 20/26] add hessain.Object to genericfilter --- filter/impl/generic_filter.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go index d369ca0a52..dcb369e545 100644 --- a/filter/impl/generic_filter.go +++ b/filter/impl/generic_filter.go @@ -44,13 +44,19 @@ type GenericFilter struct{} func (ef *GenericFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { if invocation.MethodName() == constant.GENERIC && len(invocation.Arguments()) == 3 { - var ( - oldArguments = invocation.Arguments() - ) + oldArguments := invocation.Arguments() + var newParams []hessian.Object + if oldParams, ok := oldArguments[2].([]interface{}); ok { + for i := range oldParams { + newParams = append(newParams, hessian.Object(struct2MapAll(oldParams[i]))) + } + } else { + return invoker.Invoke(invocation) + } newArguments := []interface{}{ oldArguments[0], oldArguments[1], - hessian.Object(struct2MapAll(oldArguments[2])), + newParams, } newInvocation := invocation2.NewRPCInvocation(invocation.MethodName(), newArguments, invocation.Attachments()) return invoker.Invoke(newInvocation) From d7be6867eebb408c9911662f942023bcda9acd89 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 2 Aug 2019 19:51:28 +0800 Subject: [PATCH 21/26] add support --- filter/impl/generic_filter.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go index dcb369e545..7fae41af39 100644 --- a/filter/impl/generic_filter.go +++ b/filter/impl/generic_filter.go @@ -18,10 +18,12 @@ package impl import ( - hessian "github.com/apache/dubbo-go-hessian2" "reflect" "strings" ) +import ( + hessian "github.com/apache/dubbo-go-hessian2" +) import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" @@ -59,6 +61,7 @@ func (ef *GenericFilter) Invoke(invoker protocol.Invoker, invocation protocol.In newParams, } newInvocation := invocation2.NewRPCInvocation(invocation.MethodName(), newArguments, invocation.Attachments()) + newInvocation.SetReply(invocation.Reply()) return invoker.Invoke(newInvocation) } return invoker.Invoke(invocation) @@ -71,15 +74,15 @@ func (ef *GenericFilter) OnResponse(result protocol.Result, invoker protocol.Inv func GetGenericFilter() filter.Filter { return &GenericFilter{} } -func struct2MapAll(obj interface{}) map[string]interface{} { +func struct2MapAll(obj interface{}) interface{} { result := make(map[string]interface{}) if obj == nil { - return result + return obj } t := reflect.TypeOf(obj) v := reflect.ValueOf(obj) - if reflect.TypeOf(obj).Kind() == reflect.Struct { + if t.Kind() == reflect.Struct { for i := 0; i < t.NumField(); i++ { if v.Field(i).Kind() == reflect.Struct { if v.Field(i).CanInterface() { @@ -95,8 +98,10 @@ func struct2MapAll(obj interface{}) map[string]interface{} { } } } + return result + } else { + return obj } - return result } func headerAtoa(a string) (b string) { b = strings.ToLower(a[:1]) + a[1:] From 4271bcca1e307f9460ae0f8232c59b2d8100406a Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Fri, 2 Aug 2019 19:56:24 +0800 Subject: [PATCH 22/26] fix --- filter/impl/generic_filter_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filter/impl/generic_filter_test.go b/filter/impl/generic_filter_test.go index 405bf789b7..c9443449e7 100644 --- a/filter/impl/generic_filter_test.go +++ b/filter/impl/generic_filter_test.go @@ -44,7 +44,7 @@ func Test_struct2MapAll(t *testing.T) { testData.CaCa.AaAa = "2" testData.CaCa.XxYy.xxXx = "3" testData.CaCa.XxYy.Xx = "3" - m := struct2MapAll(testData) + m := struct2MapAll(testData).(map[string]interface{}) assert.Equal(t, "1", m["aaAa"].(string)) assert.Equal(t, "1", m["baBa"].(string)) assert.Equal(t, "2", m["caCa"].(map[string]interface{})["aaAa"].(string)) From 1faca0c8898830d5ce83ab9823488189656270da Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Sat, 3 Aug 2019 22:16:09 +0800 Subject: [PATCH 23/26] generic filter add slice support --- filter/impl/generic_filter.go | 13 ++++++++++-- filter/impl/generic_filter_test.go | 34 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go index 7fae41af39..1ec4b99b82 100644 --- a/filter/impl/generic_filter.go +++ b/filter/impl/generic_filter.go @@ -75,19 +75,20 @@ func GetGenericFilter() filter.Filter { return &GenericFilter{} } func struct2MapAll(obj interface{}) interface{} { - result := make(map[string]interface{}) if obj == nil { return obj } t := reflect.TypeOf(obj) v := reflect.ValueOf(obj) - if t.Kind() == reflect.Struct { + result := make(map[string]interface{}) for i := 0; i < t.NumField(); i++ { if v.Field(i).Kind() == reflect.Struct { if v.Field(i).CanInterface() { result[headerAtoa(t.Field(i).Name)] = struct2MapAll(v.Field(i).Interface()) } + } else if v.Field(i).Kind() == reflect.Slice { + result[headerAtoa(t.Field(i).Name)] = struct2MapAll(v.Field(i).Interface()) } else { if v.Field(i).CanInterface() { if tagName := t.Field(i).Tag.Get("m"); tagName == "" { @@ -99,6 +100,14 @@ func struct2MapAll(obj interface{}) interface{} { } } return result + } else if t.Kind() == reflect.Slice { + value := reflect.ValueOf(obj) + var newTemps []interface{} + for i := 0; i < value.Len(); i++ { + newTemp := struct2MapAll(value.Index(i).Interface()) + newTemps = append(newTemps, newTemp) + } + return newTemps } else { return obj } diff --git a/filter/impl/generic_filter_test.go b/filter/impl/generic_filter_test.go index c9443449e7..a71a9db957 100644 --- a/filter/impl/generic_filter_test.go +++ b/filter/impl/generic_filter_test.go @@ -53,3 +53,37 @@ func Test_struct2MapAll(t *testing.T) { assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"]).Kind()) assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"].(map[string]interface{})["xxYy"]).Kind()) } + +type testStruct struct { + AaAa string + BaBa string `m:"baBa"` + XxYy struct { + xxXx string `m:"xxXx"` + Xx string `m:"xx"` + } `m:"xxYy"` +} + +func Test_struct2MapAll_Slice(t *testing.T) { + var testData struct { + AaAa string `m:"aaAa"` + BaBa string + CaCa []testStruct `m:"caCa"` + } + testData.AaAa = "1" + testData.BaBa = "1" + var tmp testStruct + tmp.BaBa = "2" + tmp.AaAa = "2" + tmp.XxYy.xxXx = "3" + tmp.XxYy.Xx = "3" + testData.CaCa = append(testData.CaCa, tmp) + m := struct2MapAll(testData).(map[string]interface{}) + + assert.Equal(t, "1", m["aaAa"].(string)) + assert.Equal(t, "1", m["baBa"].(string)) + assert.Equal(t, "2", m["caCa"].([]interface{})[0].(map[string]interface{})["aaAa"].(string)) + assert.Equal(t, "3", m["caCa"].([]interface{})[0].(map[string]interface{})["xxYy"].(map[string]interface{})["xx"].(string)) + + assert.Equal(t, reflect.Slice, reflect.TypeOf(m["caCa"]).Kind()) + assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"].([]interface{})[0].(map[string]interface{})["xxYy"]).Kind()) +} From cd1d707895ce4ab964f40959f71ec7014b9b0727 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Sat, 3 Aug 2019 22:43:58 +0800 Subject: [PATCH 24/26] change code style --- filter/impl/generic_filter.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go index 1ec4b99b82..f65e51dddd 100644 --- a/filter/impl/generic_filter.go +++ b/filter/impl/generic_filter.go @@ -85,17 +85,15 @@ func struct2MapAll(obj interface{}) interface{} { for i := 0; i < t.NumField(); i++ { if v.Field(i).Kind() == reflect.Struct { if v.Field(i).CanInterface() { - result[headerAtoa(t.Field(i).Name)] = struct2MapAll(v.Field(i).Interface()) + setInMap(result, t.Field(i), struct2MapAll(v.Field(i).Interface())) } } else if v.Field(i).Kind() == reflect.Slice { - result[headerAtoa(t.Field(i).Name)] = struct2MapAll(v.Field(i).Interface()) + if v.Field(i).CanInterface() { + setInMap(result, t.Field(i), struct2MapAll(v.Field(i).Interface())) + } } else { if v.Field(i).CanInterface() { - if tagName := t.Field(i).Tag.Get("m"); tagName == "" { - result[headerAtoa(t.Field(i).Name)] = v.Field(i).Interface() - } else { - result[tagName] = v.Field(i).Interface() - } + setInMap(result, t.Field(i), v.Field(i).Interface()) } } } @@ -112,6 +110,15 @@ func struct2MapAll(obj interface{}) interface{} { return obj } } +func setInMap(m map[string]interface{}, structField reflect.StructField, value interface{}) (result map[string]interface{}) { + result = m + if tagName := structField.Tag.Get("m"); tagName == "" { + result[headerAtoa(structField.Name)] = value + } else { + result[tagName] = value + } + return +} func headerAtoa(a string) (b string) { b = strings.ToLower(a[:1]) + a[1:] return From 57cbc46a0beb3bd46b171da8a157adc664903add Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Mon, 5 Aug 2019 20:54:29 +0800 Subject: [PATCH 25/26] fix code --- config/reference_config.go | 5 +++-- examples/general/dubbo/go-client/app/client.go | 2 +- filter/impl/generic_filter.go | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/config/reference_config.go b/config/reference_config.go index 4f2bdbbc29..db51dd2914 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -171,10 +171,11 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values { urlMap.Set(constant.ENVIRONMENT_KEY, consumerConfig.ApplicationConfig.Environment) //filter - urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, constant.DEFAULT_REFERENCE_FILTERS)) + var defaultReferenceFilter = constant.DEFAULT_REFERENCE_FILTERS if refconfig.Generic { - urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, constant.GENERIC_REFERENCE_FILTERS)) + defaultReferenceFilter = defaultReferenceFilter + constant.DEFAULT_REFERENCE_FILTERS } + urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, defaultReferenceFilter)) for _, v := range refconfig.Methods { urlMap.Set("methods."+v.Name+"."+constant.LOADBALANCE_KEY, v.Loadbalance) diff --git a/examples/general/dubbo/go-client/app/client.go b/examples/general/dubbo/go-client/app/client.go index 956ab7466c..b7ee0e662a 100644 --- a/examples/general/dubbo/go-client/app/client.go +++ b/examples/general/dubbo/go-client/app/client.go @@ -301,7 +301,7 @@ func test3() { time.Sleep(3 * time.Second) println("\n\n\nstart to generic invoke") - resp, err := referenceConfig.GetRPCService().(*config.GenericService).Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, "A003"}) + resp, err := referenceConfig.GetRPCService().(*config.GenericService).Invoke([]interface{}{"GetUser", []string{"java.lang.String"}, []interface{}{"A003"}}) if err != nil { panic(err) } diff --git a/filter/impl/generic_filter.go b/filter/impl/generic_filter.go index f65e51dddd..12cb4c7fa5 100644 --- a/filter/impl/generic_filter.go +++ b/filter/impl/generic_filter.go @@ -81,7 +81,7 @@ func struct2MapAll(obj interface{}) interface{} { t := reflect.TypeOf(obj) v := reflect.ValueOf(obj) if t.Kind() == reflect.Struct { - result := make(map[string]interface{}) + result := make(map[string]interface{}, t.NumField()) for i := 0; i < t.NumField(); i++ { if v.Field(i).Kind() == reflect.Struct { if v.Field(i).CanInterface() { @@ -100,7 +100,7 @@ func struct2MapAll(obj interface{}) interface{} { return result } else if t.Kind() == reflect.Slice { value := reflect.ValueOf(obj) - var newTemps []interface{} + var newTemps = make([]interface{}, 0, value.Len()) for i := 0; i < value.Len(); i++ { newTemp := struct2MapAll(value.Index(i).Interface()) newTemps = append(newTemps, newTemp) From 0c6e36cfa35a52d1a274e63b545949e2af0afb13 Mon Sep 17 00:00:00 2001 From: pantianying <601666418@qq.com> Date: Mon, 5 Aug 2019 21:01:28 +0800 Subject: [PATCH 26/26] fix bug --- config/reference_config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/reference_config.go b/config/reference_config.go index db51dd2914..bbf3c66b06 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -173,7 +173,7 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values { //filter var defaultReferenceFilter = constant.DEFAULT_REFERENCE_FILTERS if refconfig.Generic { - defaultReferenceFilter = defaultReferenceFilter + constant.DEFAULT_REFERENCE_FILTERS + defaultReferenceFilter = constant.GENERIC_REFERENCE_FILTERS + defaultReferenceFilter } urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, defaultReferenceFilter))