dubbo.apache.org/dubbo-go/v3@v3.1.1/config/metadata_report_config.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 config 19 20 import ( 21 "github.com/dubbogo/gost/log/logger" 22 23 perrors "github.com/pkg/errors" 24 ) 25 26 import ( 27 "dubbo.apache.org/dubbo-go/v3/common" 28 "dubbo.apache.org/dubbo-go/v3/common/constant" 29 "dubbo.apache.org/dubbo-go/v3/common/extension" 30 "dubbo.apache.org/dubbo-go/v3/config/instance" 31 ) 32 33 // MetadataReportConfig is app level configuration 34 type MetadataReportConfig struct { 35 Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty"` 36 Address string `required:"true" yaml:"address" json:"address"` 37 Username string `yaml:"username" json:"username,omitempty"` 38 Password string `yaml:"password" json:"password,omitempty"` 39 Timeout string `yaml:"timeout" json:"timeout,omitempty"` 40 Group string `yaml:"group" json:"group,omitempty"` 41 Namespace string `yaml:"namespace" json:"namespace,omitempty"` 42 // metadataType of this application is defined by application config, local or remote 43 metadataType string 44 } 45 46 // Prefix dubbo.consumer 47 func (MetadataReportConfig) Prefix() string { 48 return constant.MetadataReportPrefix 49 } 50 51 func (mc *MetadataReportConfig) Init(rc *RootConfig) error { 52 if mc == nil { 53 return nil 54 } 55 mc.metadataType = rc.Application.MetadataType 56 return mc.StartMetadataReport() 57 } 58 59 func (mc *MetadataReportConfig) ToUrl() (*common.URL, error) { 60 res, err := common.NewURL(mc.Address, 61 common.WithUsername(mc.Username), 62 common.WithPassword(mc.Password), 63 common.WithLocation(mc.Address), 64 common.WithProtocol(mc.Protocol), 65 common.WithParamsValue(constant.TimeoutKey, mc.Timeout), 66 common.WithParamsValue(constant.MetadataReportGroupKey, mc.Group), 67 common.WithParamsValue(constant.MetadataReportNamespaceKey, mc.Namespace), 68 common.WithParamsValue(constant.MetadataTypeKey, mc.metadataType), 69 common.WithParamsValue(constant.ClientNameKey, clientNameID(mc, mc.Protocol, mc.Address)), 70 ) 71 if err != nil || len(res.Protocol) == 0 { 72 return nil, perrors.New("Invalid MetadataReport Config.") 73 } 74 res.SetParam("metadata", res.Protocol) 75 return res, nil 76 } 77 78 func (mc *MetadataReportConfig) IsValid() bool { 79 return len(mc.Protocol) != 0 80 } 81 82 // StartMetadataReport The entry of metadata report start 83 func (mc *MetadataReportConfig) StartMetadataReport() error { 84 if mc == nil || !mc.IsValid() { 85 return nil 86 } 87 if tmpUrl, err := mc.ToUrl(); err == nil { 88 instance.SetMetadataReportInstance(tmpUrl) 89 return nil 90 } else { 91 return perrors.Wrap(err, "Start MetadataReport failed.") 92 } 93 } 94 95 func publishServiceDefinition(url *common.URL) { 96 localService, err := extension.GetLocalMetadataService(constant.DefaultKey) 97 if err != nil { 98 logger.Warnf("get local metadata service failed, please check if you have imported _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/local\"") 99 return 100 } 101 localService.PublishServiceDefinition(url) 102 if url.GetParam(constant.MetadataTypeKey, "") != constant.RemoteMetadataStorageType { 103 return 104 } 105 if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil { 106 remoteMetadataService.PublishServiceDefinition(url) 107 } 108 } 109 110 // selectMetadataServiceExportedURL get already be exported url 111 func selectMetadataServiceExportedURL() *common.URL { 112 var selectedUrl *common.URL 113 metaDataService, err := extension.GetLocalMetadataService(constant.DefaultKey) 114 if err != nil { 115 logger.Warnf("get metadata service exporter failed, pls check if you import _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/local\"") 116 return nil 117 } 118 urlList, err := metaDataService.GetExportedURLs(constant.AnyValue, constant.AnyValue, constant.AnyValue, constant.AnyValue) 119 if err != nil { 120 panic(err) 121 } 122 if len(urlList) == 0 { 123 return nil 124 } 125 for _, url := range urlList { 126 selectedUrl = url 127 // rest first 128 if url.Protocol == "rest" { 129 break 130 } 131 } 132 return selectedUrl 133 } 134 135 type MetadataReportConfigBuilder struct { 136 metadataReportConfig *MetadataReportConfig 137 } 138 139 func NewMetadataReportConfigBuilder() *MetadataReportConfigBuilder { 140 return &MetadataReportConfigBuilder{metadataReportConfig: &MetadataReportConfig{}} 141 } 142 143 func (mrcb *MetadataReportConfigBuilder) SetProtocol(protocol string) *MetadataReportConfigBuilder { 144 mrcb.metadataReportConfig.Protocol = protocol 145 return mrcb 146 } 147 148 func (mrcb *MetadataReportConfigBuilder) SetAddress(address string) *MetadataReportConfigBuilder { 149 mrcb.metadataReportConfig.Address = address 150 return mrcb 151 } 152 153 func (mrcb *MetadataReportConfigBuilder) SetUsername(username string) *MetadataReportConfigBuilder { 154 mrcb.metadataReportConfig.Username = username 155 return mrcb 156 } 157 158 func (mrcb *MetadataReportConfigBuilder) SetPassword(password string) *MetadataReportConfigBuilder { 159 mrcb.metadataReportConfig.Password = password 160 return mrcb 161 } 162 163 func (mrcb *MetadataReportConfigBuilder) SetTimeout(timeout string) *MetadataReportConfigBuilder { 164 mrcb.metadataReportConfig.Timeout = timeout 165 return mrcb 166 } 167 168 func (mrcb *MetadataReportConfigBuilder) SetGroup(group string) *MetadataReportConfigBuilder { 169 mrcb.metadataReportConfig.Group = group 170 return mrcb 171 } 172 173 func (mrcb *MetadataReportConfigBuilder) Build() *MetadataReportConfig { 174 return mrcb.metadataReportConfig 175 }