in config/service_config.go [232:337]
func (s *ServiceConfig) Export() error {
// TODO: delay export
if s.unexported != nil && s.unexported.Load() {
err := perrors.Errorf("The service %v has already unexported!", s.Interface)
logger.Errorf(err.Error())
return err
}
if s.exported != nil && s.exported.Load() {
logger.Warnf("The service %v has already exported!", s.Interface)
return nil
}
regUrls := make([]*common.URL, 0)
if !s.NotRegister {
regUrls = LoadRegistries(s.RegistryIDs, s.RCRegistriesMap, common.PROVIDER)
}
urlMap := s.getUrlMap()
protocolConfigs := loadProtocol(s.ProtocolIDs, s.RCProtocolsMap)
if len(protocolConfigs) == 0 {
logger.Warnf("The service %v's '%v' protocols don't has right protocolConfigs, Please check your configuration center and transfer protocol ", s.Interface, s.ProtocolIDs)
return nil
}
var invoker protocol.Invoker
ports := getRandomPort(protocolConfigs)
nextPort := ports.Front()
for _, proto := range protocolConfigs {
// registry the service reflect
methods, err := common.ServiceMap.Register(s.Interface, proto.Name, s.Group, s.Version, s.rpcService)
if err != nil {
formatErr := perrors.Errorf("The service %v export the protocol %v error! Error message is %v.",
s.Interface, proto.Name, err.Error())
logger.Errorf(formatErr.Error())
return formatErr
}
port := proto.Port
if num, err := strconv.Atoi(proto.Port); err != nil || num <= 0 {
port = nextPort.Value.(string)
nextPort = nextPort.Next()
}
ivkURL := common.NewURLWithOptions(
common.WithPath(s.Interface),
common.WithProtocol(proto.Name),
common.WithIp(proto.Ip),
common.WithPort(port),
common.WithParams(urlMap),
common.WithParamsValue(constant.BeanNameKey, s.id),
common.WithParamsValue(constant.ApplicationTagKey, s.rc.Application.Tag),
//common.WithParamsValue(constant.SslEnabledKey, strconv.FormatBool(config.GetSslEnabled())),
common.WithMethods(strings.Split(methods, ",")),
common.WithToken(s.Token),
common.WithParamsValue(constant.MetadataTypeKey, s.metadataType),
// fix https://github.com/apache/dubbo-go/issues/2176
common.WithParamsValue(constant.MaxServerSendMsgSize, proto.MaxServerSendMsgSize),
common.WithParamsValue(constant.MaxServerRecvMsgSize, proto.MaxServerRecvMsgSize),
)
info := GetProviderServiceInfo(s.id)
if info != nil {
ivkURL.SetAttribute(constant.ServiceInfoKey, info)
}
if len(s.Tag) > 0 {
ivkURL.AddParam(constant.Tagkey, s.Tag)
}
// post process the URL to be exported
s.postProcessConfig(ivkURL)
// config post processor may set "export" to false
if !ivkURL.GetParamBool(constant.ExportKey, true) {
return nil
}
if len(regUrls) > 0 {
s.cacheMutex.Lock()
if s.cacheProtocol == nil {
logger.Debugf(fmt.Sprintf("First load the registry protocol, url is {%v}!", ivkURL))
s.cacheProtocol = extension.GetProtocol(constant.RegistryProtocol)
}
s.cacheMutex.Unlock()
for _, regUrl := range regUrls {
setRegistrySubURL(ivkURL, regUrl)
invoker = s.generatorInvoker(regUrl, info)
exporter := s.cacheProtocol.Export(invoker)
if exporter == nil {
return perrors.New(fmt.Sprintf("Registry protocol new exporter error, registry is {%v}, url is {%v}", regUrl, ivkURL))
}
s.exporters = append(s.exporters, exporter)
}
} else {
invoker = s.generatorInvoker(ivkURL, info)
exporter := extension.GetProtocol(protocolwrapper.FILTER).Export(invoker)
if exporter == nil {
return perrors.New(fmt.Sprintf("Filter protocol without registry new exporter error, url is {%v}", ivkURL))
}
s.exporters = append(s.exporters, exporter)
}
}
s.exported.Store(true)
return nil
}