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

ftr: Grpc Protocol Support #311

Merged
merged 24 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 21 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
25 changes: 15 additions & 10 deletions common/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ func (p *Proxy) Implement(v common.RPCService) {
makeDubboCallProxy := func(methodName string, outs []reflect.Type) func(in []reflect.Value) []reflect.Value {
return func(in []reflect.Value) []reflect.Value {
var (
err error
inv *invocation_impl.RPCInvocation
inArr []interface{}
reply reflect.Value
err error
inv *invocation_impl.RPCInvocation
inIArr []interface{}
inVArr []reflect.Value
reply reflect.Value
)
if methodName == "Echo" {
methodName = "$echo"
Expand Down Expand Up @@ -104,21 +105,25 @@ func (p *Proxy) Implement(v common.RPCService) {
}

if end-start <= 0 {
inArr = []interface{}{}
inIArr = []interface{}{}
inVArr = []reflect.Value{}
} else if v, ok := in[start].Interface().([]interface{}); ok && end-start == 1 {
inArr = v
inIArr = v
inVArr = []reflect.Value{in[start]}
} else {
inArr = make([]interface{}, end-start)
inIArr = make([]interface{}, end-start)
inVArr = make([]reflect.Value, end-start)
index := 0
for i := start; i < end; i++ {
inArr[index] = in[i].Interface()
inIArr[index] = in[i].Interface()
inVArr[index] = in[i]
index++
}
}

inv = invocation_impl.NewRPCInvocationWithOptions(invocation_impl.WithMethodName(methodName),
invocation_impl.WithArguments(inArr), invocation_impl.WithReply(reply.Interface()),
invocation_impl.WithCallBack(p.callBack))
invocation_impl.WithArguments(inIArr), invocation_impl.WithReply(reply.Interface()),
invocation_impl.WithCallBack(p.callBack), invocation_impl.WithParameterValues(inVArr))

for k, value := range p.attachments {
inv.SetAttachments(k, value)
Expand Down
2 changes: 1 addition & 1 deletion config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Load() {
continue
}
ref.id = key
ref.Refer()
ref.Refer(rpcService)
ref.Implement(rpcService)
}
//wait for invoker is available, if wait over default 3s, then panic
Expand Down
8 changes: 4 additions & 4 deletions config/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (refconfig *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) erro
return nil
}

func (refconfig *ReferenceConfig) Refer() {
func (refconfig *ReferenceConfig) Refer(impl interface{}) {
url := common.NewURLWithOptions(common.WithPath(refconfig.id), common.WithProtocol(refconfig.Protocol), common.WithParams(refconfig.getUrlMap()))

//1. user specified URL, could be peer-to-peer address, or register center's address.
Expand Down Expand Up @@ -123,12 +123,12 @@ func (refconfig *ReferenceConfig) Refer() {
}
}
if len(refconfig.urls) == 1 {
refconfig.invoker = extension.GetProtocol(refconfig.urls[0].Protocol).Refer(*refconfig.urls[0])
refconfig.invoker = extension.GetProtocol(refconfig.urls[0].Protocol).Refer(*refconfig.urls[0], impl)
} else {
invokers := []protocol.Invoker{}
var regUrl *common.URL
for _, u := range refconfig.urls {
invokers = append(invokers, extension.GetProtocol(u.Protocol).Refer(*u))
invokers = append(invokers, extension.GetProtocol(u.Protocol).Refer(*u, impl))
if u.Protocol == constant.REGISTRY_PROTOCOL {
regUrl = u
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func (refconfig *ReferenceConfig) GenericLoad(id string) {
genericService := NewGenericService(refconfig.id)
SetConsumerService(genericService)
refconfig.id = id
refconfig.Refer()
refconfig.Refer(genericService)
refconfig.Implement(genericService)
return
}
20 changes: 10 additions & 10 deletions config/reference_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func Test_ReferMultireg(t *testing.T) {
extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)

for _, reference := range consumerConfig.References {
reference.Refer()
reference.Refer(nil)
assert.NotNil(t, reference.invoker)
assert.NotNil(t, reference.pxy)
}
Expand All @@ -197,7 +197,7 @@ func Test_Refer(t *testing.T) {
extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)

for _, reference := range consumerConfig.References {
reference.Refer()
reference.Refer(nil)
assert.Equal(t, "soa.mock", reference.Params["serviceid"])
assert.NotNil(t, reference.invoker)
assert.NotNil(t, reference.pxy)
Expand All @@ -211,7 +211,7 @@ func Test_ReferAsync(t *testing.T) {
extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)

for _, reference := range consumerConfig.References {
reference.Refer()
reference.Refer(nil)
assert.Equal(t, "soa.mock", reference.Params["serviceid"])
assert.NotNil(t, reference.invoker)
assert.NotNil(t, reference.pxy)
Expand All @@ -227,7 +227,7 @@ func Test_ReferP2P(t *testing.T) {
m.Url = "dubbo://127.0.0.1:20000"

for _, reference := range consumerConfig.References {
reference.Refer()
reference.Refer(nil)
assert.NotNil(t, reference.invoker)
assert.NotNil(t, reference.pxy)
}
Expand All @@ -241,7 +241,7 @@ func Test_ReferMultiP2P(t *testing.T) {
m.Url = "dubbo://127.0.0.1:20000;dubbo://127.0.0.2:20000"

for _, reference := range consumerConfig.References {
reference.Refer()
reference.Refer(nil)
assert.NotNil(t, reference.invoker)
assert.NotNil(t, reference.pxy)
}
Expand All @@ -256,7 +256,7 @@ func Test_ReferMultiP2PWithReg(t *testing.T) {
m.Url = "dubbo://127.0.0.1:20000;registry://127.0.0.2:20000"

for _, reference := range consumerConfig.References {
reference.Refer()
reference.Refer(nil)
assert.NotNil(t, reference.invoker)
assert.NotNil(t, reference.pxy)
}
Expand All @@ -268,7 +268,7 @@ func Test_Implement(t *testing.T) {
extension.SetProtocol("registry", GetProtocol)
extension.SetCluster("registryAware", cluster_impl.NewRegistryAwareCluster)
for _, reference := range consumerConfig.References {
reference.Refer()
reference.Refer(nil)
reference.Implement(&MockService{})
assert.NotNil(t, reference.GetRPCService())

Expand All @@ -284,7 +284,7 @@ func Test_Forking(t *testing.T) {
m.Url = "dubbo://127.0.0.1:20000;registry://127.0.0.2:20000"

for _, reference := range consumerConfig.References {
reference.Refer()
reference.Refer(nil)
forks := int(reference.invoker.GetUrl().GetParamInt(constant.FORKS_KEY, constant.DEFAULT_FORKS))
assert.Equal(t, 5, forks)
assert.NotNil(t, reference.pxy)
Expand All @@ -301,7 +301,7 @@ func Test_Sticky(t *testing.T) {
m.Url = "dubbo://127.0.0.1:20000;registry://127.0.0.2:20000"

reference := consumerConfig.References["MockService"]
reference.Refer()
reference.Refer(nil)
referenceSticky := reference.invoker.GetUrl().GetParam(constant.STICKY_KEY, "false")
assert.Equal(t, "false", referenceSticky)

Expand All @@ -324,7 +324,7 @@ func newRegistryProtocol() protocol.Protocol {

type mockRegistryProtocol struct{}

func (*mockRegistryProtocol) Refer(url common.URL) protocol.Invoker {
func (*mockRegistryProtocol) Refer(url common.URL, impl interface{}) protocol.Invoker {
return protocol.NewBaseInvoker(url)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/go-errors/errors v1.0.1 // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/mock v1.3.1
github.com/golang/protobuf v1.3.2
github.com/google/btree v1.0.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions protocol/dubbo/dubbo_invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func TestDubboInvoker_Invoke(t *testing.T) {
pendingResponses: new(sync.Map),
conf: *clientConf,
opts: Options{
ConnectTimeout: 3e9,
RequestTimeout: 6e9,
ConnectTimeout: 3 * time.Second,
RequestTimeout: 6 * time.Second,
},
}
c.pool = newGettyRPCClientConnPool(c, clientConf.PoolSize, time.Duration(int(time.Second)*clientConf.PoolTTL))
Expand Down
2 changes: 1 addition & 1 deletion protocol/dubbo/dubbo_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (dp *DubboProtocol) Export(invoker protocol.Invoker) protocol.Exporter {
return exporter
}

func (dp *DubboProtocol) Refer(url common.URL) protocol.Invoker {
func (dp *DubboProtocol) Refer(url common.URL, impl interface{}) protocol.Invoker {
//default requestTimeout
var requestTimeout = config.GetConsumerConfig().RequestTimeout

Expand Down
2 changes: 1 addition & 1 deletion protocol/dubbo/dubbo_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestDubboProtocol_Refer(t *testing.T) {
"side=provider&timeout=3000&timestamp=1556509797245")
assert.NoError(t, err)
clientConf = &ClientConfig{}
invoker := proto.Refer(url)
invoker := proto.Refer(url, nil)

// make sure url
eq := invoker.GetUrl().URLEqual(url)
Expand Down
57 changes: 57 additions & 0 deletions protocol/grpc/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
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 grpc

import (
"reflect"
)

import (
"google.golang.org/grpc"
)

import (
"github.com/apache/dubbo-go/common"
)

type Client struct {
*grpc.ClientConn
invoker reflect.Value
}

func NewClient(impl interface{}, url common.URL) *Client {
conn, err := grpc.Dial(url.Location, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
panic(err)
}

invoker := getInvoker(impl, conn)

return &Client{
ClientConn: conn,
invoker: reflect.ValueOf(invoker),
}
}

func getInvoker(impl interface{}, conn *grpc.ClientConn) interface{} {
in := []reflect.Value{}
in = append(in, reflect.ValueOf(conn))
method := reflect.ValueOf(impl).MethodByName("GetDubboStub")
res := method.Call(in)
return res[0].Interface()
}
55 changes: 55 additions & 0 deletions protocol/grpc/client_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 grpc

import (
"context"
"reflect"
"testing"
)

import (
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
)

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/protocol/grpc/internal"
)

func TestGetInvoker(t *testing.T) {
var conn *grpc.ClientConn
var impl *internal.GrpcGreeterImpl
invoker := getInvoker(impl, conn)

i := reflect.TypeOf(invoker)
expected := reflect.TypeOf(internal.NewGreeterClient(nil))
assert.Equal(t, i, expected)
}

func TestNewClient(t *testing.T) {
go internal.InitGrpcServer()
defer internal.ShutdownGrpcServer()

var impl *internal.GrpcGreeterImpl
url, err := common.NewURL(context.Background(), "grpc://127.0.0.1:30000/GrpcGreeterImpl?accesslog=&anyhost=true&app.version=0.0.1&application=BDTService&async=false&bean.name=GrpcGreeterImpl&category=providers&cluster=failover&dubbo=dubbo-provider-golang-2.6.0&environment=dev&execute.limit=&execute.limit.rejected.handler=&generic=false&group=&interface=io.grpc.examples.helloworld.GreeterGrpc%24IGreeter&ip=192.168.1.106&loadbalance=random&methods.SayHello.loadbalance=random&methods.SayHello.retries=1&methods.SayHello.tps.limit.interval=&methods.SayHello.tps.limit.rate=&methods.SayHello.tps.limit.strategy=&methods.SayHello.weight=0&module=dubbogo+say-hello+client&name=BDTService&organization=ikurento.com&owner=ZX&pid=49427&reference.filter=cshutdown&registry.role=3&remote.timestamp=1576923717&retries=&service.filter=echo%2Ctoken%2Caccesslog%2Ctps%2Cexecute%2Cpshutdown&side=provider&timestamp=1576923740&tps.limit.interval=&tps.limit.rate=&tps.limit.rejected.handler=&tps.limit.strategy=&tps.limiter=&version=&warmup=100!")
assert.Nil(t, err)
cli := NewClient(impl, url)
assert.NotNil(t, cli)
}
Loading