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

generic#invoke (go to java) #122

Merged
merged 34 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
22498ac
Merge pull request #1 from apache/master
pantianying Jul 4, 2019
47c4d20
Merge pull request #2 from apache/develop
pantianying Jul 4, 2019
9751266
add go to java generic
pantianying Jul 4, 2019
6cde06a
fix test
pantianying Jul 4, 2019
25b1512
update
pantianying Jul 4, 2019
e216bcb
优化
pantianying Jul 5, 2019
2cf9399
改成和java一样的使用方式
pantianying Jul 5, 2019
4df2f94
fix
pantianying Jul 5, 2019
e545dd0
add license
pantianying Jul 5, 2019
48073dd
change file name
pantianying Jul 5, 2019
5e0ebf7
fix、and jsonrpc has no genericComsumer
pantianying Jul 5, 2019
79775de
Merge pull request #3 from apache/develop
pantianying Jul 10, 2019
0b111a5
Merge pull request #5 from apache/develop
pantianying Jul 15, 2019
4dbeb2a
fix bug with Temporary disposal
pantianying Jul 15, 2019
7f00a83
Merge branch 'master' of https://github.com/pantianying/dubbo-go
pantianying Jul 15, 2019
09b9130
update
pantianying Jul 16, 2019
81b8c35
change annotation
pantianying Jul 17, 2019
96f5554
add generic filter
pantianying Jul 30, 2019
e9d7d86
Merge remote-tracking branch 'apache/develop'
pantianying Jul 30, 2019
74e16bb
add ut
pantianying Jul 30, 2019
428fad4
ut be compatible with go 1.11
pantianying Jul 30, 2019
31c898f
add Generic tag
pantianying Jul 31, 2019
d77c8e3
Merge branch 'develop' into master
pantianying Jul 31, 2019
6e5df6c
fix
pantianying Jul 31, 2019
a1e9a4d
fix fmt
pantianying Jul 31, 2019
068983b
add generic config.Load support
pantianying Aug 2, 2019
c3d2c9c
fix bug
pantianying Aug 2, 2019
754252b
add hessain.Object to genericfilter
pantianying Aug 2, 2019
d7be686
add support
pantianying Aug 2, 2019
4271bcc
fix
pantianying Aug 2, 2019
1faca0c
generic filter add slice support
pantianying Aug 3, 2019
cd1d707
change code style
pantianying Aug 3, 2019
57cbc46
fix code
pantianying Aug 5, 2019
0c6e36c
fix bug
pantianying Aug 5, 2019
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
2 changes: 2 additions & 0 deletions common/constant/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
PREFIX_DEFAULT_KEY = "default."
DEFAULT_SERVICE_FILTERS = "echo"
DEFAULT_REFERENCE_FILTERS = ""
GENERIC_REFERENCE_FILTERS = "generic"
GENERIC = "$invoke"
ECHO = "$echo"
)

Expand Down
30 changes: 30 additions & 0 deletions config/generic_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 {
Invoke func(req []interface{}) (interface{}, error) `dubbo:"$invoke"`
referenceStr string
}

func NewGenericService(referenceStr string) *GenericService {
return &GenericService{referenceStr: referenceStr}
}

func (u *GenericService) Reference() string {
return u.referenceStr
}
17 changes: 17 additions & 0 deletions config/generic_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +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
pantianying marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 13 additions & 1 deletion config/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
pantianying marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *ReferenceConfig) Prefix() string {
Expand Down Expand Up @@ -110,7 +111,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 {
Expand Down Expand Up @@ -171,6 +171,9 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values {

//filter
urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, constant.DEFAULT_REFERENCE_FILTERS))
if refconfig.Generic {
urlMap.Set(constant.REFERENCE_FILTER_KEY, mergeValue(consumerConfig.Filter, refconfig.Filter, constant.GENERIC_REFERENCE_FILTERS))
pantianying marked this conversation as resolved.
Show resolved Hide resolved
}
pantianying marked this conversation as resolved.
Show resolved Hide resolved

for _, v := range refconfig.Methods {
urlMap.Set("methods."+v.Name+"."+constant.LOADBALANCE_KEY, v.Loadbalance)
Expand All @@ -180,3 +183,12 @@ func (refconfig *ReferenceConfig) getUrlMap() url.Values {
return urlMap

}
func (refconfig *ReferenceConfig) Load(id string) {
pantianying marked this conversation as resolved.
Show resolved Hide resolved
//gr.Filter = "genericConsumer" //todo: add genericConsumer filter
pantianying marked this conversation as resolved.
Show resolved Hide resolved
genericService := NewGenericService(refconfig.id)
SetConsumerService(genericService)
refconfig.id = id
refconfig.Refer()
refconfig.Implement(genericService)
return
}
29 changes: 25 additions & 4 deletions examples/general/dubbo/go-client/app/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ import (
"syscall"
"time"
)

import (
"github.com/apache/dubbo-go-hessian2"
hessian "github.com/apache/dubbo-go-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"
Expand Down Expand Up @@ -65,7 +64,8 @@ func main() {
test1()
println("\n\ntest2")
test2()

println("\n\ntest3")
test3()
initSignal()
}

Expand Down Expand Up @@ -288,3 +288,24 @@ func test2() {
}
println("error: %v", err)
}
func test3() {
var appName = "UserProviderGer"
var referenceConfig = config.ReferenceConfig{
InterfaceName: "com.ikurento.user.UserProvider",
Cluster: "failover",
Registry: "hangzhouzk",
Protocol: dubbo.DUBBO,
Generic: true,
}
referenceConfig.Load(appName) //appName is the unique identification of RPCService

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"}})
if err != nil {
panic(err)
}
println("res: %v\n", resp)
println("succ!")

}
87 changes: 87 additions & 0 deletions filter/impl/generic_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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/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"
pantianying marked this conversation as resolved.
Show resolved Hide resolved
"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 {
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()
}
}
}
}
}
return result
}
func headerAtoa(a string) (b string) {
b = strings.ToLower(a[:1]) + a[1:]
return
}
55 changes: 55 additions & 0 deletions filter/impl/generic_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 (
"reflect"
"testing"
pantianying marked this conversation as resolved.
Show resolved Hide resolved
)

func Test_struct2MapAll(t *testing.T) {
var testData struct {
AaAa string `m:"aaAa"`
BaBa string
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)
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())
}