Skip to content

Commit

Permalink
Merge pull request apache#119 from fangyincheng/develop
Browse files Browse the repository at this point in the history
Add:support multi-implementation of service
  • Loading branch information
AlexStocks authored Jul 3, 2019
2 parents d59c087 + f3e33d7 commit 4fa45b5
Show file tree
Hide file tree
Showing 71 changed files with 1,754 additions and 632 deletions.
3 changes: 1 addition & 2 deletions cluster/cluster_impl/failover_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ func Test_FailoverInvoke2(t *testing.T) {
urlParams.Set(constant.RETRIES_KEY, "2")
urlParams.Set("methods.test."+constant.RETRIES_KEY, "3")

ivc := &invocation.RPCInvocation{}
ivc.SetMethod("test")
ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))
result := normalInvoke(t, 3, urlParams, ivc)
assert.NoError(t, result.Error())
count = 0
Expand Down
3 changes: 1 addition & 2 deletions cluster/loadbalance/least_active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func TestLeastActiveByWeight(t *testing.T) {
invokers = append(invokers, protocol.NewBaseInvoker(url))
}

inv := new(invocation.RPCInvocation)
inv.SetMethod("test")
inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))
protocol.BeginCount(invokers[2].GetUrl(), inv.MethodName())

loop = 10000
Expand Down
6 changes: 2 additions & 4 deletions cluster/loadbalance/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func Test_RandomlbSelectWeight(t *testing.T) {
urlParams.Set("methods.test."+constant.WEIGHT_KEY, "10000000000000")
urll, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.100:20000/com.ikurento.user.UserProvider"), common.WithParams(urlParams))
invokers = append(invokers, protocol.NewBaseInvoker(urll))
ivc := &invocation.RPCInvocation{}
ivc.SetMethod("test")
ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))

var selectedInvoker []protocol.Invoker
var selected float64
Expand Down Expand Up @@ -99,8 +98,7 @@ func Test_RandomlbSelectWarmup(t *testing.T) {
urlParams.Set(constant.REMOTE_TIMESTAMP_KEY, strconv.FormatInt(time.Now().Add(time.Minute*(-9)).Unix(), 10))
urll, _ := common.NewURL(context.TODO(), fmt.Sprintf("dubbo://192.168.1.100:20000/com.ikurento.user.UserProvider"), common.WithParams(urlParams))
invokers = append(invokers, protocol.NewBaseInvoker(urll))
ivc := &invocation.RPCInvocation{}
ivc.SetMethod("test")
ivc := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("test"))

var selectedInvoker []protocol.Invoker
var selected float64
Expand Down
2 changes: 1 addition & 1 deletion common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
SERVICE_KEY = "service"
METHODS_KEY = "methods"
TIMEOUT_KEY = "timeout"
BEAN_NAME_KEY = "bean.name"
)

const (
Expand All @@ -44,7 +45,6 @@ const (
WEIGHT_KEY = "weight"
WARMUP_KEY = "warmup"
RETRIES_KEY = "retries"
BEAN_NAME = "bean.name"
)

const (
Expand Down
4 changes: 3 additions & 1 deletion common/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func (p *Proxy) Implement(v common.RPCService) {
}
}

inv = invocation_impl.NewRPCInvocationForConsumer(methodName, nil, inArr, reply.Interface(), p.callBack, common.URL{}, nil)
inv = invocation_impl.NewRPCInvocationWithOptions(invocation_impl.WithMethodName(methodName),
invocation_impl.WithArguments(inArr), invocation_impl.WithReply(reply.Interface()),
invocation_impl.WithCallBack(p.callBack))

for k, value := range p.attachments {
inv.SetAttachments(k, value)
Expand Down
10 changes: 2 additions & 8 deletions common/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,15 @@ type TestService struct {
Echo func(interface{}, *interface{}) error
}

func (s *TestService) Service() string {
func (s *TestService) Reference() string {
return "com.test.Path"
}
func (s *TestService) Version() string {
return ""
}

type TestServiceInt int

func (s *TestServiceInt) Service() string {
func (s *TestServiceInt) Reference() string {
return "com.test.TestServiceInt"
}
func (s *TestServiceInt) Version() string {
return ""
}

func TestProxy_Implement(t *testing.T) {

Expand Down
15 changes: 7 additions & 8 deletions common/rpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import (

// rpc service interface
type RPCService interface {
Service() string // Path InterfaceName
Version() string
Reference() string // rpc service id or reference id
}

// for lowercase func
Expand Down Expand Up @@ -149,7 +148,7 @@ func (sm *serviceMap) Register(protocol string, rcvr RPCService) (string, error)
return "", perrors.New(s)
}

sname = rcvr.Service()
sname = rcvr.Reference()
if server := sm.GetService(protocol, sname); server != nil {
return "", perrors.New("service already defined: " + sname)
}
Expand All @@ -172,8 +171,8 @@ func (sm *serviceMap) Register(protocol string, rcvr RPCService) (string, error)
return strings.TrimSuffix(methods, ","), nil
}

func (sm *serviceMap) UnRegister(protocol, serviceName string) error {
if protocol == "" || serviceName == "" {
func (sm *serviceMap) UnRegister(protocol, serviceId string) error {
if protocol == "" || serviceId == "" {
return perrors.New("protocol or serviceName is nil")
}
sm.mutex.RLock()
Expand All @@ -182,16 +181,16 @@ func (sm *serviceMap) UnRegister(protocol, serviceName string) error {
sm.mutex.RUnlock()
return perrors.New("no services for " + protocol)
}
_, ok = svcs[serviceName]
_, ok = svcs[serviceId]
if !ok {
sm.mutex.RUnlock()
return perrors.New("no service for " + serviceName)
return perrors.New("no service for " + serviceId)
}
sm.mutex.RUnlock()

sm.mutex.Lock()
defer sm.mutex.Unlock()
delete(svcs, serviceName)
delete(svcs, serviceId)
delete(sm.serviceMap, protocol)

return nil
Expand Down
17 changes: 4 additions & 13 deletions common/rpc_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ func (s *TestService) MethodTwo(arg1, arg2, arg3 interface{}) (interface{}, erro
func (s *TestService) MethodThree() error {
return nil
}
func (s *TestService) Service() string {
func (s *TestService) Reference() string {
return "com.test.Path"
}
func (s *TestService) Version() string {
return ""
}
func (s *TestService) MethodMapper() map[string]string {
return map[string]string{
"MethodTwo": "methodTwo",
Expand All @@ -65,22 +62,16 @@ func (s *testService) Method3(ctx context.Context, args []interface{}, rsp *stru
func (s *testService) Method4(ctx context.Context, args []interface{}, rsp *struct{}) *testService {
return nil
}
func (s *testService) Service() string {
func (s *testService) Reference() string {
return "com.test.Path"
}
func (s *testService) Version() string {
return ""
}

type TestService1 struct {
}

func (s *TestService1) Service() string {
func (s *TestService1) Reference() string {
return "com.test.Path1"
}
func (s *TestService1) Version() string {
return ""
}

func TestServiceMap_Register(t *testing.T) {
// lowercase
Expand Down Expand Up @@ -180,7 +171,7 @@ func TestSuiteMethod(t *testing.T) {

// wrong number of in return
s1 := &testService{}
method, ok = reflect.TypeOf(s1).MethodByName("Version")
method, ok = reflect.TypeOf(s1).MethodByName("Reference")
assert.True(t, ok)
methodType = suiteMethod(method)
assert.Nil(t, methodType)
Expand Down
23 changes: 12 additions & 11 deletions config/base_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func Test_refresh(t *testing.T) {
c := &BaseConfig{}
mockMap := map[string]string{}
mockMap["dubbo.registries.shanghai_reg1.protocol"] = "mock100"
mockMap["dubbo.reference.MockService.MockService.retries"] = "10"
mockMap["dubbo.MockService.MockService.GetUser.retries"] = "10"
mockMap["dubbo.reference.com.MockService.MockService.retries"] = "10"
mockMap["dubbo.com.MockService.MockService.GetUser.retries"] = "10"
mockMap["dubbo.consumer.check"] = "false"
mockMap["dubbo.application.name"] = "dubbo"

Expand Down Expand Up @@ -88,7 +88,7 @@ func Test_refresh(t *testing.T) {
},
References: map[string]*ReferenceConfig{
"MockService": {
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Protocol: "mock",
Cluster: "failover",
Loadbalance: "random",
Expand All @@ -98,13 +98,14 @@ func Test_refresh(t *testing.T) {
Methods: []*MethodConfig{
{
InterfaceId: "MockService",
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser",
Retries: 2,
Loadbalance: "random",
},
{InterfaceId: "MockService",
InterfaceName: "MockService",
{
InterfaceId: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser1",
Retries: 2,
Loadbalance: "random",
Expand All @@ -128,8 +129,8 @@ func Test_refreshProvider(t *testing.T) {
c := &BaseConfig{}
mockMap := map[string]string{}
mockMap["dubbo.registries.shanghai_reg1.protocol"] = "mock100"
mockMap["dubbo.service.MockService.MockService.retries"] = "10"
mockMap["dubbo.MockService.MockService.GetUser.retries"] = "10"
mockMap["dubbo.service.com.MockService.MockService.retries"] = "10"
mockMap["dubbo.com.MockService.MockService.GetUser.retries"] = "10"
mockMap["dubbo.consumer.check"] = "false"
mockMap["dubbo.application.name"] = "dubbo"
mockMap["dubbo.protocols.jsonrpc1.name"] = "jsonrpc"
Expand Down Expand Up @@ -183,7 +184,7 @@ func Test_refreshProvider(t *testing.T) {
},
Services: map[string]*ServiceConfig{
"MockService": {
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Protocol: "mock",
Cluster: "failover",
Loadbalance: "random",
Expand All @@ -193,13 +194,13 @@ func Test_refreshProvider(t *testing.T) {
Methods: []*MethodConfig{
{
InterfaceId: "MockService",
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser",
Retries: 2,
Loadbalance: "random",
},
{InterfaceId: "MockService",
InterfaceName: "MockService",
InterfaceName: "com.MockService",
Name: "GetUser1",
Retries: 2,
Loadbalance: "random",
Expand Down
22 changes: 12 additions & 10 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ func Load() {
logger.Errorf("[consumer config center refresh] %#v", err)
}
refMap = make(map[string]*ReferenceConfig)
for _, ref := range consumerConfig.References {
rpcService := GetConsumerService(ref.InterfaceName)
for key, ref := range consumerConfig.References {
rpcService := GetConsumerService(key)

if rpcService == nil {
logger.Warnf("%s is not exsist!", ref.InterfaceName)
logger.Warnf("%s is not exsist!", key)
continue
}
ref.id = key
ref.Refer()
ref.Implement(rpcService)
refMap[ref.InterfaceName] = ref
refMap[key] = ref

}
//wait for invoker is available, if wait over default 3s, then panic
Expand Down Expand Up @@ -122,17 +123,18 @@ func Load() {
logger.Errorf("[provider config center refresh] %#v", err)
}
srvMap = make(map[string]*ServiceConfig)
for _, svs := range providerConfig.Services {
rpcService := GetProviderService(svs.InterfaceName)
for key, svs := range providerConfig.Services {
rpcService := GetProviderService(key)
if rpcService == nil {
logger.Warnf("%s is not exsist!", svs.InterfaceName)
logger.Warnf("%s is not exsist!", key)
continue
}
svs.id = key
svs.Implement(rpcService)
if err := svs.Export(); err != nil {
panic(fmt.Sprintf("service %s export failed! ", svs.InterfaceName))
panic(fmt.Sprintf("service %s export failed! ", key))
}
srvMap[svs.InterfaceName] = svs
srvMap[key] = svs
}
}
}
Expand All @@ -144,5 +146,5 @@ func GetRPCService(name string) common.RPCService {

// create rpc service for consumer
func RPCService(service common.RPCService) {
providerConfig.Services[service.Service()].Implement(service)
providerConfig.Services[service.Reference()].Implement(service)
}
10 changes: 6 additions & 4 deletions config/config_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ func TestLoad(t *testing.T) {

Load()

assert.Equal(t, ms, GetRPCService(ms.Service()))
assert.Equal(t, ms, GetRPCService(ms.Reference()))
ms2 := &struct {
MockService
}{}
RPCService(ms2)
assert.NotEqual(t, ms2, GetRPCService(ms2.Service()))
assert.NotEqual(t, ms2, GetRPCService(ms2.Reference()))

conServices = map[string]common.RPCService{}
proServices = map[string]common.RPCService{}
common.ServiceMap.UnRegister("mock", "MockService")
consumerConfig = nil
providerConfig = nil
}

func TestWithNoRegLoad(t *testing.T) {
doInit()
doinit()
Expand All @@ -99,19 +100,20 @@ func TestWithNoRegLoad(t *testing.T) {

Load()

assert.Equal(t, ms, GetRPCService(ms.Service()))
assert.Equal(t, ms, GetRPCService(ms.Reference()))
ms2 := &struct {
MockService
}{}
RPCService(ms2)
assert.NotEqual(t, ms2, GetRPCService(ms2.Service()))
assert.NotEqual(t, ms2, GetRPCService(ms2.Reference()))

conServices = map[string]common.RPCService{}
proServices = map[string]common.RPCService{}
common.ServiceMap.UnRegister("mock", "MockService")
consumerConfig = nil
providerConfig = nil
}

func TestConfigLoaderWithConfigCenter(t *testing.T) {
extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory {
return &config_center.MockDynamicConfigurationFactory{}
Expand Down
6 changes: 1 addition & 5 deletions config/mock_rpcservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ import (

type MockService struct{}

func (*MockService) Service() string {
func (*MockService) Reference() string {
return "MockService"
}

func (*MockService) Version() string {
return "1.0"
}

func (*MockService) GetUser(ctx context.Context, itf []interface{}, str *struct{}) error {
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion config/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
type ReferenceConfig struct {
context context.Context
pxy *proxy.Proxy
id string
InterfaceName string `required:"true" yaml:"interface" json:"interface,omitempty" property:"interface"`
Check *bool `yaml:"check" json:"check,omitempty" property:"check"`
Url string `yaml:"url" json:"url,omitempty" property:"url"`
Expand Down Expand Up @@ -76,7 +77,7 @@ func (refconfig *ReferenceConfig) UnmarshalYAML(unmarshal func(interface{}) erro
}

func (refconfig *ReferenceConfig) Refer() {
url := common.NewURLWithOptions(common.WithPath(refconfig.InterfaceName), common.WithProtocol(refconfig.Protocol), common.WithParams(refconfig.getUrlMap()))
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.
if refconfig.Url != "" {
Expand Down
Loading

0 comments on commit 4fa45b5

Please sign in to comment.