dubbo.apache.org/dubbo-go/v3@v3.1.1/metadata/service/exporter/configurable/exporter.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 configurable
    19  
    20  import (
    21  	"sync"
    22  )
    23  
    24  import (
    25  	"github.com/dubbogo/gost/log/logger"
    26  )
    27  
    28  import (
    29  	"dubbo.apache.org/dubbo-go/v3/common"
    30  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    31  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    32  	"dubbo.apache.org/dubbo-go/v3/config"
    33  	_ "dubbo.apache.org/dubbo-go/v3/metadata/mapping/metadata"
    34  	"dubbo.apache.org/dubbo-go/v3/metadata/service"
    35  	"dubbo.apache.org/dubbo-go/v3/metadata/service/exporter"
    36  	_ "dubbo.apache.org/dubbo-go/v3/metadata/service/remote"
    37  	_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo"
    38  )
    39  
    40  // MetadataServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface
    41  type MetadataServiceExporter struct {
    42  	ServiceConfig   *config.ServiceConfig
    43  	lock            sync.RWMutex
    44  	metadataService service.MetadataService
    45  }
    46  
    47  func init() {
    48  	extension.SetMetadataServiceExporter(constant.DefaultKey, NewMetadataServiceExporter)
    49  }
    50  
    51  // NewMetadataServiceExporter will return a service_exporter.MetadataServiceExporter with the specified  metadata service
    52  func NewMetadataServiceExporter(metadataService service.MetadataService) exporter.MetadataServiceExporter {
    53  	return &MetadataServiceExporter{
    54  		metadataService: metadataService,
    55  	}
    56  }
    57  
    58  // Export will export the metadataService
    59  func (exporter *MetadataServiceExporter) Export(url *common.URL) error {
    60  	if !exporter.IsExported() {
    61  		version, _ := exporter.metadataService.Version()
    62  		exporter.lock.Lock()
    63  		defer exporter.lock.Unlock()
    64  		exporter.ServiceConfig = config.NewServiceConfigBuilder().
    65  			SetServiceID(constant.SimpleMetadataServiceName).
    66  			SetProtocolIDs(constant.DefaultProtocol).
    67  			AddRCProtocol(constant.DefaultProtocol, config.NewProtocolConfigBuilder().
    68  				SetName(constant.DefaultProtocol).SetPort(getMetadataPort()).
    69  				Build()).
    70  			SetRegistryIDs("N/A").
    71  			SetInterface(constant.MetadataServiceName).
    72  			SetGroup(config.GetApplicationConfig().Name).
    73  			SetVersion(version).
    74  			SetProxyFactoryKey(constant.DefaultKey).
    75  			SetMetadataType(config.GetApplicationConfig().MetadataType).
    76  			Build()
    77  		exporter.ServiceConfig.Implement(exporter.metadataService)
    78  		err := exporter.ServiceConfig.Export()
    79  
    80  		logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.ServiceConfig.GetExportedUrls())
    81  		return err
    82  	}
    83  	logger.Warnf("[Metadata Service] The MetadataService has been exported : %v ", exporter.ServiceConfig.GetExportedUrls())
    84  	return nil
    85  }
    86  
    87  func getMetadataPort() string {
    88  	rootConfig := config.GetRootConfig()
    89  	port := rootConfig.Application.MetadataServicePort
    90  	if port == "" {
    91  		protocolConfig, ok := rootConfig.Protocols[constant.DefaultProtocol]
    92  		if ok {
    93  			port = protocolConfig.Port
    94  		} else {
    95  			logger.Warnf("[Metadata Service] Dubbo-go %s version's MetadataService only support dubbo protocol,"+
    96  				"MetadataService will use random port",
    97  				constant.Version)
    98  		}
    99  	}
   100  	return port
   101  }
   102  
   103  // Unexport will unexport the metadataService
   104  func (exporter *MetadataServiceExporter) Unexport() {
   105  	if exporter.IsExported() {
   106  		exporter.ServiceConfig.Unexport()
   107  	}
   108  }
   109  
   110  // GetExportedURLs will return the urls that export use.
   111  // Noticeļ¼The exported url is not same as url in registry , for example it lack the ip.
   112  func (exporter *MetadataServiceExporter) GetExportedURLs() []*common.URL {
   113  	return exporter.ServiceConfig.GetExportedUrls()
   114  }
   115  
   116  // IsExported will return is metadataServiceExporter exported or not
   117  func (exporter *MetadataServiceExporter) IsExported() bool {
   118  	exporter.lock.RLock()
   119  	defer exporter.lock.RUnlock()
   120  	return exporter.ServiceConfig != nil && exporter.ServiceConfig.IsExport()
   121  }