dubbo.apache.org/dubbo-go/v3@v3.1.1/config/provider_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 "fmt" 22 "strings" 23 ) 24 25 import ( 26 "github.com/creasty/defaults" 27 28 "github.com/dubbogo/gost/log/logger" 29 30 tripleConstant "github.com/dubbogo/triple/pkg/common/constant" 31 32 perrors "github.com/pkg/errors" 33 ) 34 35 import ( 36 "dubbo.apache.org/dubbo-go/v3/common" 37 "dubbo.apache.org/dubbo-go/v3/common/constant" 38 aslimiter "dubbo.apache.org/dubbo-go/v3/filter/adaptivesvc/limiter" 39 ) 40 41 // ProviderConfig is the default configuration of service provider 42 type ProviderConfig struct { 43 Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` 44 // Deprecated Register whether registration is required 45 Register bool `yaml:"register" json:"register" property:"register"` 46 // RegistryIDs is registry ids list 47 RegistryIDs []string `yaml:"registry-ids" json:"registry-ids" property:"registry-ids"` 48 // protocol 49 ProtocolIDs []string `yaml:"protocol-ids" json:"protocol-ids" property:"protocol-ids"` 50 // TracingKey is tracing ids list 51 TracingKey string `yaml:"tracing-key" json:"tracing-key" property:"tracing-key"` 52 // Services services 53 Services map[string]*ServiceConfig `yaml:"services" json:"services,omitempty" property:"services"` 54 ProxyFactory string `default:"default" yaml:"proxy" json:"proxy,omitempty" property:"proxy"` 55 FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf"` 56 ConfigType map[string]string `yaml:"config_type" json:"config_type,omitempty" property:"config_type"` 57 // adaptive service 58 AdaptiveService bool `yaml:"adaptive-service" json:"adaptive-service" property:"adaptive-service"` 59 AdaptiveServiceVerbose bool `yaml:"adaptive-service-verbose" json:"adaptive-service-verbose" property:"adaptive-service-verbose"` 60 61 rootConfig *RootConfig 62 } 63 64 func (ProviderConfig) Prefix() string { 65 return constant.ProviderConfigPrefix 66 } 67 68 func (c *ProviderConfig) check() error { 69 if err := defaults.Set(c); err != nil { 70 return err 71 } 72 return verify(c) 73 } 74 75 func (c *ProviderConfig) Init(rc *RootConfig) error { 76 if c == nil { 77 return nil 78 } 79 buildDebugMsg := func() string { 80 if len(c.Services) == 0 { 81 return "empty" 82 } 83 providerNames := make([]string, 0, len(c.Services)) 84 for k := range c.Services { 85 providerNames = append(providerNames, k) 86 } 87 return strings.Join(providerNames, ", ") 88 } 89 logger.Debugf("Registered provider services are %v", buildDebugMsg()) 90 91 c.RegistryIDs = translateIds(c.RegistryIDs) 92 if len(c.RegistryIDs) <= 0 { 93 c.RegistryIDs = rc.getRegistryIds() 94 } 95 c.ProtocolIDs = translateIds(c.ProtocolIDs) 96 97 if c.TracingKey == "" && len(rc.Tracing) > 0 { 98 for k := range rc.Tracing { 99 c.TracingKey = k 100 break 101 } 102 } 103 for key, serviceConfig := range c.Services { 104 if serviceConfig.Interface == "" { 105 service := GetProviderService(key) 106 // try to use interface name defined by pb 107 supportPBPackagerNameSerivce, ok := service.(common.TriplePBService) 108 if !ok { 109 logger.Errorf("Service with reference = %s is not support read interface name from it."+ 110 "Please run go install github.com/dubbogo/dubbogo-cli/cmd/protoc-gen-go-triple@latest to update your "+ 111 "protoc-gen-go-triple and re-generate your pb file again."+ 112 "If you are not using pb serialization, please set 'interface' field in service config.", key) 113 continue 114 } else { 115 // use interface name defined by pb 116 serviceConfig.Interface = supportPBPackagerNameSerivce.XXX_InterfaceName() 117 } 118 } 119 if err := serviceConfig.Init(rc); err != nil { 120 return err 121 } 122 123 serviceConfig.adaptiveService = c.AdaptiveService 124 } 125 126 for k, v := range rc.Protocols { 127 if v.Name == tripleConstant.TRIPLE { 128 // Auto create grpc based health check service. 129 healthService := NewServiceConfigBuilder(). 130 SetProtocolIDs(k). 131 SetNotRegister(true). 132 SetInterface(constant.HealthCheckServiceInterface). 133 Build() 134 if err := healthService.Init(rc); err != nil { 135 return err 136 } 137 c.Services[constant.HealthCheckServiceTypeName] = healthService 138 139 // Auto create reflection service configure only when provider with triple service is configured. 140 tripleReflectionService := NewServiceConfigBuilder(). 141 SetProtocolIDs(k). 142 SetNotRegister(true). 143 SetInterface(constant.ReflectionServiceInterface). 144 Build() 145 if err := tripleReflectionService.Init(rc); err != nil { 146 return err 147 } 148 // Maybe only register once, If setting this service, break from traversing Protocols. 149 c.Services[constant.ReflectionServiceTypeName] = tripleReflectionService 150 break 151 } 152 } 153 154 if err := c.check(); err != nil { 155 return err 156 } 157 // enable adaptive service verbose 158 if c.AdaptiveServiceVerbose { 159 if !c.AdaptiveService { 160 return perrors.Errorf("The adaptive service is disabled, " + 161 "adaptive service verbose should be disabled either.") 162 } 163 logger.Infof("adaptive service verbose is enabled.") 164 logger.Debugf("debug-level info could be shown.") 165 aslimiter.Verbose = true 166 } 167 return nil 168 } 169 170 func (c *ProviderConfig) Load() { 171 for registeredTypeName, service := range GetProviderServiceMap() { 172 serviceConfig, ok := c.Services[registeredTypeName] 173 if !ok { 174 if registeredTypeName == constant.ReflectionServiceTypeName || 175 registeredTypeName == constant.HealthCheckServiceTypeName { 176 // do not auto generate reflection or health check server's configuration. 177 continue 178 } 179 // service doesn't config in config file, create one with default 180 supportPBPackagerNameSerivce, ok := service.(common.TriplePBService) 181 if !ok { 182 logger.Warnf( 183 "The provider service %s is ignored: neither the config is found, nor it is a valid Triple service.", 184 registeredTypeName) 185 continue 186 } 187 serviceConfig = NewServiceConfigBuilder().Build() 188 // use interface name defined by pb 189 serviceConfig.Interface = supportPBPackagerNameSerivce.XXX_InterfaceName() 190 if err := serviceConfig.Init(rootConfig); err != nil { 191 logger.Errorf("Service with refKey = %s init failed with error = %s") 192 } 193 serviceConfig.adaptiveService = c.AdaptiveService 194 } 195 serviceConfig.id = registeredTypeName 196 serviceConfig.Implement(service) 197 if err := serviceConfig.Export(); err != nil { 198 logger.Errorf(fmt.Sprintf("service with registeredTypeName = %s export failed! err: %#v", registeredTypeName, err)) 199 } 200 } 201 } 202 203 // newEmptyProviderConfig returns ProviderConfig with default ApplicationConfig 204 func newEmptyProviderConfig() *ProviderConfig { 205 newProviderConfig := &ProviderConfig{ 206 Services: make(map[string]*ServiceConfig), 207 RegistryIDs: make([]string, 8), 208 ProtocolIDs: make([]string, 8), 209 } 210 return newProviderConfig 211 } 212 213 type ProviderConfigBuilder struct { 214 providerConfig *ProviderConfig 215 } 216 217 func NewProviderConfigBuilder() *ProviderConfigBuilder { 218 return &ProviderConfigBuilder{providerConfig: newEmptyProviderConfig()} 219 } 220 221 func (pcb *ProviderConfigBuilder) SetFilter(filter string) *ProviderConfigBuilder { 222 pcb.providerConfig.Filter = filter 223 return pcb 224 } 225 226 func (pcb *ProviderConfigBuilder) SetRegister(register bool) *ProviderConfigBuilder { 227 pcb.providerConfig.Register = register 228 return pcb 229 } 230 231 func (pcb *ProviderConfigBuilder) SetRegistryIDs(RegistryIDs ...string) *ProviderConfigBuilder { 232 pcb.providerConfig.RegistryIDs = RegistryIDs 233 return pcb 234 } 235 236 func (pcb *ProviderConfigBuilder) SetServices(services map[string]*ServiceConfig) *ProviderConfigBuilder { 237 pcb.providerConfig.Services = services 238 return pcb 239 } 240 241 func (pcb *ProviderConfigBuilder) AddService(serviceID string, serviceConfig *ServiceConfig) *ProviderConfigBuilder { 242 if pcb.providerConfig.Services == nil { 243 pcb.providerConfig.Services = make(map[string]*ServiceConfig) 244 } 245 pcb.providerConfig.Services[serviceID] = serviceConfig 246 return pcb 247 } 248 249 func (pcb *ProviderConfigBuilder) SetProxyFactory(proxyFactory string) *ProviderConfigBuilder { 250 pcb.providerConfig.ProxyFactory = proxyFactory 251 return pcb 252 } 253 254 func (pcb *ProviderConfigBuilder) SetFilterConf(filterConf interface{}) *ProviderConfigBuilder { 255 pcb.providerConfig.FilterConf = filterConf 256 return pcb 257 } 258 259 func (pcb *ProviderConfigBuilder) SetConfigType(configType map[string]string) *ProviderConfigBuilder { 260 pcb.providerConfig.ConfigType = configType 261 return pcb 262 } 263 264 func (pcb *ProviderConfigBuilder) AddConfigType(key, value string) *ProviderConfigBuilder { 265 if pcb.providerConfig.ConfigType == nil { 266 pcb.providerConfig.ConfigType = make(map[string]string) 267 } 268 pcb.providerConfig.ConfigType[key] = value 269 return pcb 270 } 271 272 func (pcb *ProviderConfigBuilder) SetRootConfig(rootConfig *RootConfig) *ProviderConfigBuilder { 273 pcb.providerConfig.rootConfig = rootConfig 274 return pcb 275 } 276 277 func (pcb *ProviderConfigBuilder) Build() *ProviderConfig { 278 return pcb.providerConfig 279 }