forked from apache/dubbo-go
/
config_loader.go
492 lines (445 loc) · 14.5 KB
/
config_loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"flag"
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"sync"
"time"
)
import (
hessian "github.com/apache/dubbo-go-hessian2"
perrors "github.com/pkg/errors"
)
import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
_ "dubbo.apache.org/dubbo-go/v3/common/observer/dispatcher"
"dubbo.apache.org/dubbo-go/v3/common/yaml"
"dubbo.apache.org/dubbo-go/v3/registry"
)
var (
consumerConfig *ConsumerConfig
providerConfig *ProviderConfig
// baseConfig = providerConfig.BaseConfig or consumerConfig
baseConfig *BaseConfig
sslEnabled = false
// configAccessMutex is used to make sure that xxxxConfig will only be created once if needed.
// it should be used combine with double-check to avoid the race condition
configAccessMutex sync.Mutex
maxWait = 3
confRouterFile string
confBaseFile string
uniformVirtualServiceConfigPath string
uniformDestRuleConfigPath string
)
// loaded consumer & provider config from xxx.yml, and log config from xxx.xml
// Namely: dubbo.consumer.xml & dubbo.provider.xml in java dubbo
func DefaultInit() []LoaderInitOption {
var (
confConFile string
confProFile string
)
fs := flag.NewFlagSet("config", flag.ContinueOnError)
fs.StringVar(&confConFile, "conConf", os.Getenv(constant.CONF_CONSUMER_FILE_PATH), "default client config path")
fs.StringVar(&confProFile, "proConf", os.Getenv(constant.CONF_PROVIDER_FILE_PATH), "default server config path")
fs.StringVar(&confRouterFile, "rouConf", os.Getenv(constant.CONF_ROUTER_FILE_PATH), "default router config path")
fs.StringVar(&uniformVirtualServiceConfigPath, "vsConf", os.Getenv(constant.CONF_VIRTUAL_SERVICE_FILE_PATH), "default virtual service of uniform router config path")
fs.StringVar(&uniformDestRuleConfigPath, "drConf", os.Getenv(constant.CONF_DEST_RULE_FILE_PATH), "default destination rule of uniform router config path")
fs.Parse(os.Args[1:])
for len(fs.Args()) != 0 {
fs.Parse(fs.Args()[1:])
}
// If user did not set the environment variables or flags,
// we provide default value
if confConFile == "" {
confConFile = constant.DEFAULT_CONSUMER_CONF_FILE_PATH
}
if confProFile == "" {
confProFile = constant.DEFAULT_PROVIDER_CONF_FILE_PATH
}
if confRouterFile == "" {
confRouterFile = constant.DEFAULT_ROUTER_CONF_FILE_PATH
}
return []LoaderInitOption{RouterInitOption(confRouterFile), BaseInitOption(""), ConsumerInitOption(confConFile), ProviderInitOption(confProFile)}
}
// setDefaultValue set default value for providerConfig or consumerConfig if it is null
func setDefaultValue(target interface{}) {
switch target.(type) {
case *ProviderConfig:
p := target.(*ProviderConfig)
if len(p.Protocols) == 0 {
p.Protocols[constant.DEFAULT_PROTOCOL] = &ProtocolConfig{
Name: constant.DEFAULT_PROTOCOL,
Port: strconv.Itoa(constant.DEFAULT_PORT),
}
}
if p.ApplicationConfig == nil {
p.ApplicationConfig = NewDefaultApplicationConfig()
}
default:
c := target.(*ConsumerConfig)
if c.ApplicationConfig == nil {
c.ApplicationConfig = NewDefaultApplicationConfig()
}
}
}
func checkRegistries(registries map[string]*RegistryConfig, singleRegistry *RegistryConfig) {
if len(registries) == 0 && singleRegistry != nil {
registries[constant.DEFAULT_KEY] = singleRegistry
}
}
func checkApplicationName(config *ApplicationConfig) {
if config == nil || len(config.Name) == 0 {
errMsg := "application config must not be nil, pls check your configuration"
logger.Errorf(errMsg)
panic(errMsg)
}
}
func loadConsumerConfig() {
if consumerConfig == nil {
logger.Warnf("consumerConfig is nil!")
return
}
// init other consumer config
conConfigType := consumerConfig.ConfigType
for key, value := range extension.GetDefaultConfigReader() {
if conConfigType != nil {
if v, ok := conConfigType[key]; ok {
value = v
}
}
if err := extension.GetConfigReaders(value).ReadConsumerConfig(consumerConfig.fileStream); err != nil {
logger.Errorf("ReadConsumerConfig error: %#v for %s", perrors.WithStack(err), value)
}
}
checkApplicationName(consumerConfig.ApplicationConfig)
if err := configCenterRefreshConsumer(); err != nil {
logger.Errorf("[consumer config center refresh] %#v", err)
}
// start the metadata report if config set
if err := startMetadataReport(GetApplicationConfig().MetadataType, GetBaseConfig().MetadataReportConfig); err != nil {
logger.Errorf("Provider starts metadata report error, and the error is {%#v}", err)
return
}
checkRegistries(consumerConfig.Registries, consumerConfig.Registry)
for key, ref := range consumerConfig.References {
if ref.Generic {
genericService := NewGenericService(key)
SetConsumerService(genericService)
}
rpcService := GetConsumerService(key)
if rpcService == nil {
logger.Warnf("%s does not exist!", key)
continue
}
ref.id = key
ref.Refer(rpcService)
ref.Implement(rpcService)
}
// Write current configuration to cache file.
if consumerConfig.CacheFile != "" {
if data, err := yaml.MarshalYML(consumerConfig); err != nil {
logger.Errorf("Marshal consumer config err: %s", err.Error())
} else {
if err := ioutil.WriteFile(consumerConfig.CacheFile, data, 0666); err != nil {
logger.Errorf("Write consumer config cache file err: %s", err.Error())
}
}
}
// wait for invoker is available, if wait over default 3s, then panic
var count int
for {
checkok := true
for _, refconfig := range consumerConfig.References {
if (refconfig.Check != nil && *refconfig.Check) ||
(refconfig.Check == nil && consumerConfig.Check != nil && *consumerConfig.Check) ||
(refconfig.Check == nil && consumerConfig.Check == nil) { // default to true
if refconfig.invoker != nil && !refconfig.invoker.IsAvailable() {
checkok = false
count++
if count > maxWait {
errMsg := fmt.Sprintf("Failed to check the status of the service %v. No provider available for the service to the consumer use dubbo version %v", refconfig.InterfaceName, constant.Version)
logger.Error(errMsg)
panic(errMsg)
}
time.Sleep(time.Second * 1)
break
}
if refconfig.invoker == nil {
logger.Warnf("The interface %s invoker not exist, may you should check your interface config.", refconfig.InterfaceName)
}
}
}
if checkok {
break
}
}
}
func loadProviderConfig() {
if providerConfig == nil {
logger.Warnf("providerConfig is nil!")
return
}
// init other provider config
proConfigType := providerConfig.ConfigType
for key, value := range extension.GetDefaultConfigReader() {
if proConfigType != nil {
if v, ok := proConfigType[key]; ok {
value = v
}
}
if err := extension.GetConfigReaders(value).ReadProviderConfig(providerConfig.fileStream); err != nil {
logger.Errorf("ReadProviderConfig error: %#v for %s", perrors.WithStack(err), value)
}
}
checkApplicationName(providerConfig.ApplicationConfig)
if err := configCenterRefreshProvider(); err != nil {
logger.Errorf("[provider config center refresh] %#v", err)
}
// start the metadata report if config set
if err := startMetadataReport(GetApplicationConfig().MetadataType, GetBaseConfig().MetadataReportConfig); err != nil {
logger.Errorf("Provider starts metadata report error, and the error is {%#v}", err)
return
}
checkRegistries(providerConfig.Registries, providerConfig.Registry)
// Write the current configuration to cache file.
if providerConfig.CacheFile != "" {
if data, err := yaml.MarshalYML(providerConfig); err != nil {
logger.Errorf("Marshal provider config err: %s", err.Error())
} else {
if err := ioutil.WriteFile(providerConfig.CacheFile, data, 0666); err != nil {
logger.Errorf("Write provider config cache file err: %s", err.Error())
}
}
}
for key, svs := range providerConfig.Services {
rpcService := GetProviderService(key)
if rpcService == nil {
logger.Warnf("%s does not exist!", key)
continue
}
svs.id = key
svs.Implement(rpcService)
svs.Protocols = providerConfig.Protocols
if err := svs.Export(); err != nil {
panic(fmt.Sprintf("service %s export failed! err: %#v", key, err))
}
}
registerServiceInstance()
}
// registerServiceInstance register service instance
func registerServiceInstance() {
url := selectMetadataServiceExportedURL()
if url == nil {
return
}
instance, err := createInstance(url)
if err != nil {
panic(err)
}
p := extension.GetProtocol(constant.REGISTRY_KEY)
var rp registry.RegistryFactory
var ok bool
if rp, ok = p.(registry.RegistryFactory); !ok {
panic("dubbo registry protocol{" + reflect.TypeOf(p).String() + "} is invalid")
}
rs := rp.GetRegistries()
for _, r := range rs {
var sdr registry.ServiceDiscoveryHolder
if sdr, ok = r.(registry.ServiceDiscoveryHolder); !ok {
continue
}
err := sdr.GetServiceDiscovery().Register(instance)
if err != nil {
panic(err)
}
}
// todo publish metadata to remote
if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil {
remoteMetadataService.PublishMetadata(GetApplicationConfig().Name)
}
}
// nolint
func createInstance(url *common.URL) (registry.ServiceInstance, error) {
appConfig := GetApplicationConfig()
port, err := strconv.ParseInt(url.Port, 10, 32)
if err != nil {
return nil, perrors.WithMessage(err, "invalid port: "+url.Port)
}
host := url.Ip
if len(host) == 0 {
host = common.GetLocalIp()
}
// usually we will add more metadata
metadata := make(map[string]string, 8)
metadata[constant.METADATA_STORAGE_TYPE_PROPERTY_NAME] = appConfig.MetadataType
return ®istry.DefaultServiceInstance{
ServiceName: appConfig.Name,
Host: host,
Port: int(port),
ID: host + constant.KEY_SEPARATOR + url.Port,
Enable: true,
Healthy: true,
Metadata: metadata,
}, nil
}
// selectMetadataServiceExportedURL get already be exported url
func selectMetadataServiceExportedURL() *common.URL {
var selectedUrl *common.URL
metaDataService, err := extension.GetLocalMetadataService("")
if err != nil {
logger.Warn(err)
return nil
}
urlList, err := metaDataService.GetExportedURLs(constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE, constant.ANY_VALUE)
if err != nil {
panic(err)
}
if len(urlList) == 0 {
return nil
}
for _, url := range urlList {
selectedUrl = url
// rest first
if url.Protocol == "rest" {
break
}
}
return selectedUrl
}
func initRouter() {
if uniformDestRuleConfigPath != "" && uniformVirtualServiceConfigPath != "" {
if err := RouterInit(uniformVirtualServiceConfigPath, uniformDestRuleConfigPath); err != nil {
logger.Warnf("[routerConfig init] %#v", err)
}
}
}
// Load Dubbo Init
func Load() {
options := DefaultInit()
LoadWithOptions(options...)
}
func LoadWithOptions(options ...LoaderInitOption) {
// register metadata info and service info
hessian.RegisterPOJO(&common.MetadataInfo{})
hessian.RegisterPOJO(&common.ServiceInfo{})
hessian.RegisterPOJO(&common.URL{})
for _, option := range options {
option.init()
}
for _, option := range options {
option.apply()
}
// init router
initRouter()
// init the shutdown callback
GracefulShutdownInit()
}
// GetRPCService get rpc service for consumer
func GetRPCService(name string) common.RPCService {
return consumerConfig.References[name].GetRPCService()
}
// RPCService create rpc service for consumer
func RPCService(service common.RPCService) {
consumerConfig.References[service.Reference()].Implement(service)
}
// GetMetricConfig find the MetricConfig
// if it is nil, create a new one
// we use double-check to reduce race condition
// In general, it will be locked 0 or 1 time.
// So you don't need to worry about the race condition
func GetMetricConfig() *MetricConfig {
if GetBaseConfig().MetricConfig == nil {
configAccessMutex.Lock()
defer configAccessMutex.Unlock()
if GetBaseConfig().MetricConfig == nil {
GetBaseConfig().MetricConfig = &MetricConfig{}
}
}
return GetBaseConfig().MetricConfig
}
// GetApplicationConfig find the application config
// if not, we will create one
// Usually applicationConfig will be initialized when system start
// we use double-check to reduce race condition
// In general, it will be locked 0 or 1 time.
// So you don't need to worry about the race condition
func GetApplicationConfig() *ApplicationConfig {
if GetBaseConfig().ApplicationConfig == nil {
configAccessMutex.Lock()
defer configAccessMutex.Unlock()
if GetBaseConfig().ApplicationConfig == nil {
GetBaseConfig().ApplicationConfig = &ApplicationConfig{}
}
}
return GetBaseConfig().ApplicationConfig
}
// GetProviderConfig find the provider config
// if not found, create new one
func GetProviderConfig() ProviderConfig {
if providerConfig == nil {
if providerConfig == nil {
return ProviderConfig{}
}
}
return *providerConfig
}
// GetConsumerConfig find the consumer config
// if not found, create new one
// we use double-check to reduce race condition
// In general, it will be locked 0 or 1 time.
// So you don't need to worry about the race condition
func GetConsumerConfig() ConsumerConfig {
if consumerConfig == nil {
if consumerConfig == nil {
return ConsumerConfig{}
}
}
return *consumerConfig
}
func GetBaseConfig() *BaseConfig {
if baseConfig == nil {
configAccessMutex.Lock()
defer configAccessMutex.Unlock()
if baseConfig == nil {
baseConfig = &BaseConfig{
MetricConfig: &MetricConfig{},
ConfigCenterConfig: &ConfigCenterConfig{},
Remotes: make(map[string]*RemoteConfig),
ApplicationConfig: &ApplicationConfig{},
ServiceDiscoveries: make(map[string]*ServiceDiscoveryConfig),
}
}
}
return baseConfig
}
func GetSslEnabled() bool {
return sslEnabled
}
func SetSslEnabled(enabled bool) {
sslEnabled = enabled
}
func IsProvider() bool {
return providerConfig != nil
}