dubbo.apache.org/dubbo-go/v3@v3.1.1/metadata/service/local_service.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package service
    19  
    20  import (
    21  	"sync"
    22  )
    23  
    24  import (
    25  	"dubbo.apache.org/dubbo-go/v3/common"
    26  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    27  	"dubbo.apache.org/dubbo-go/v3/registry"
    28  )
    29  
    30  // MetadataService is used to define meta data related behaviors
    31  // usually the implementation should be singleton
    32  type MetadataService interface {
    33  	common.ReferencedRPCService
    34  	// ServiceName will get the service's name in meta service , which is application name
    35  	ServiceName() (string, error)
    36  	// ExportURL will store the exported url in metadata
    37  	ExportURL(url *common.URL) (bool, error)
    38  	// UnexportURL will delete the exported url in metadata
    39  	UnexportURL(url *common.URL) error
    40  	// SubscribeURL will store the subscribed url in metadata
    41  	SubscribeURL(url *common.URL) (bool, error)
    42  	// UnsubscribeURL will delete the subscribed url in metadata
    43  	UnsubscribeURL(url *common.URL) error
    44  	// PublishServiceDefinition will generate the target url's code info
    45  	PublishServiceDefinition(url *common.URL) error
    46  	// GetExportedURLs will get the target exported url in metadata
    47  	// the url should be unique
    48  	// due to dubbo-go only support return array []interface{} in RPCService, so we should declare the return type as []interface{}
    49  	// actually, it's []String
    50  	GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error)
    51  	// MethodMapper for rename dubbo method name
    52  	MethodMapper() map[string]string
    53  	// GetSubscribedURLs will get the target subscribed url in metadata the url should be unique
    54  	GetSubscribedURLs() ([]*common.URL, error)
    55  	// GetServiceDefinition will get the target service info store in metadata
    56  	GetServiceDefinition(interfaceName string, group string, version string) (string, error)
    57  	// GetServiceDefinitionByServiceKey will get the target service info store in metadata by service key
    58  	GetServiceDefinitionByServiceKey(serviceKey string) (string, error)
    59  	// RefreshMetadata will refresh the metadata
    60  	RefreshMetadata(exportedRevision string, subscribedRevision string) (bool, error)
    61  	// Version will return the metadata service version
    62  	Version() (string, error)
    63  	// GetMetadataInfo will return metadata info
    64  	GetMetadataInfo(revision string) (*common.MetadataInfo, error)
    65  	// GetExportedServiceURLs will return exported service urls
    66  	GetExportedServiceURLs() ([]*common.URL, error)
    67  	// GetMetadataServiceURL will return the url of metadata service
    68  	GetMetadataServiceURL() (*common.URL, error)
    69  	// SetMetadataServiceURL will save the url of metadata service
    70  	SetMetadataServiceURL(*common.URL) error
    71  }
    72  
    73  // BaseMetadataService is used for the event logic for struct who will implement interface MetadataService
    74  type BaseMetadataService struct {
    75  	serviceName string
    76  }
    77  
    78  func NewBaseMetadataService(serviceName string) BaseMetadataService {
    79  	return BaseMetadataService{
    80  		serviceName: serviceName,
    81  	}
    82  }
    83  
    84  func (mts *BaseMetadataService) MethodMapper() map[string]string {
    85  	return map[string]string{
    86  		"GetExportedURLs": "getExportedURLs",
    87  		"GetMetadataInfo": "getMetadataInfo",
    88  	}
    89  }
    90  
    91  // ServiceName can get the service's name in meta service , which is application name
    92  func (mts *BaseMetadataService) ServiceName() (string, error) {
    93  	return mts.serviceName, nil
    94  }
    95  
    96  // Reference will return the reference id of metadata service
    97  func (mts *BaseMetadataService) Reference() string {
    98  	return constant.SimpleMetadataServiceName
    99  }
   100  
   101  type MetadataServiceProxyFactory interface {
   102  	GetProxy(ins registry.ServiceInstance) MetadataService
   103  }
   104  
   105  type MetadataServiceProxyCreator func(ins registry.ServiceInstance) MetadataService
   106  
   107  type BaseMetadataServiceProxyFactory struct {
   108  	proxies sync.Map
   109  	creator MetadataServiceProxyCreator
   110  }
   111  
   112  func NewBaseMetadataServiceProxyFactory(creator MetadataServiceProxyCreator) *BaseMetadataServiceProxyFactory {
   113  	return &BaseMetadataServiceProxyFactory{
   114  		creator: creator,
   115  	}
   116  }
   117  
   118  func (b *BaseMetadataServiceProxyFactory) GetProxy(ins registry.ServiceInstance) MetadataService {
   119  	return b.creator(ins)
   120  }
   121  
   122  func getExportedServicesRevision(serviceInstance registry.ServiceInstance) string {
   123  	metaData := serviceInstance.GetMetadata()
   124  	return metaData[constant.ExportedServicesRevisionPropertyName]
   125  }
   126  
   127  func ConvertURLArrToIntfArr(urls []*common.URL) []interface{} {
   128  	if len(urls) == 0 {
   129  		return []interface{}{}
   130  	}
   131  
   132  	res := make([]interface{}, 0, len(urls))
   133  	for _, u := range urls {
   134  		res = append(res, u.String())
   135  	}
   136  	return res
   137  }