diff --git a/common/rpc_service.go b/common/rpc_service.go index 224f8c8e1e..1a7cbc8b7c 100644 --- a/common/rpc_service.go +++ b/common/rpc_service.go @@ -34,14 +34,43 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/logger" ) -// RPCService +// RPCService the type alias of interface{} +type RPCService = interface{} + +// ReferencedRPCService // rpc service interface -type RPCService interface { +type ReferencedRPCService interface { // Reference: // rpc service id or reference id Reference() string } +// GetReference return the reference id of the service. +// If the service implemented the ReferencedRPCService interface, +// it will call the Reference method. If not, it will +// return the struct name as the reference id. +func GetReference(service RPCService) string { + if s, ok := service.(ReferencedRPCService); ok { + return s.Reference() + } + + ref := "" + sType := reflect.TypeOf(service) + kind := sType.Kind() + switch kind { + case reflect.Struct: + ref = sType.Name() + case reflect.Ptr: + sName := sType.Elem().Name() + if sName != "" { + ref = sName + } else { + ref = sType.Elem().Field(0).Name + } + } + return ref +} + // AsyncCallbackService callback interface for async type AsyncCallbackService interface { // Callback: callback diff --git a/common/rpc_service_test.go b/common/rpc_service_test.go index 8dc984f7a5..6143e472a5 100644 --- a/common/rpc_service_test.go +++ b/common/rpc_service_test.go @@ -218,3 +218,36 @@ func TestSuiteMethod(t *testing.T) { methodType = suiteMethod(method) assert.Nil(t, methodType) } + +type ServiceWithoutRef struct{} + +func TestGetReference(t *testing.T) { + s0 := &TestService{} + ref0 := GetReference(s0) + assert.Equal(t, referenceTestPath, ref0) + + //s1 := TestService{} + //ref1 := GetReference(s1) + //assert.Equal(t, referenceTestPath, ref1) + + s2 := &struct { + TestService + }{} + ref2 := GetReference(s2) + assert.Equal(t, referenceTestPath, ref2) + + expectedReference := "ServiceWithoutRef" + s3 := &ServiceWithoutRef{} + ref3 := GetReference(s3) + assert.Equal(t, expectedReference, ref3) + + s4 := ServiceWithoutRef{} + ref4 := GetReference(s4) + assert.Equal(t, expectedReference, ref4) + + s5 := &struct { + ServiceWithoutRef + }{} + ref5 := GetReference(s5) + assert.Equal(t, expectedReference, ref5) +} diff --git a/config/config_loader.go b/config/config_loader.go index 039d8fccb6..a87abf4aa5 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -401,7 +401,8 @@ func GetRPCService(name string) common.RPCService { // RPCService create rpc service for consumer func RPCService(service common.RPCService) { - consumerConfig.References[service.Reference()].Implement(service) + ref := common.GetReference(service) + consumerConfig.References[ref].Implement(service) } // GetMetricConfig find the MetricConfig diff --git a/config/service.go b/config/service.go index 12cc91e90b..a487abab07 100644 --- a/config/service.go +++ b/config/service.go @@ -28,12 +28,14 @@ var ( // SetConsumerService is called by init() of implement of RPCService func SetConsumerService(service common.RPCService) { - conServices[service.Reference()] = service + ref := common.GetReference(service) + conServices[ref] = service } // SetProviderService is called by init() of implement of RPCService func SetProviderService(service common.RPCService) { - proServices[service.Reference()] = service + ref := common.GetReference(service) + proServices[ref] = service } // GetConsumerService gets ConsumerService by @name diff --git a/metadata/service/local_service.go b/metadata/service/local_service.go index 135dae04fd..1230e3a1eb 100644 --- a/metadata/service/local_service.go +++ b/metadata/service/local_service.go @@ -30,7 +30,7 @@ import ( // MetadataService is used to define meta data related behaviors // usually the implementation should be singleton type MetadataService interface { - common.RPCService + common.ReferencedRPCService // ServiceName will get the service's name in meta service , which is application name ServiceName() (string, error) // ExportURL will store the exported url in metadata @@ -94,7 +94,7 @@ func (mts *BaseMetadataService) ServiceName() (string, error) { return mts.serviceName, nil } -// Version will return the version of metadata service +// Reference will return the reference id of metadata service func (mts *BaseMetadataService) Reference() string { return constant.SIMPLE_METADATA_SERVICE_NAME }