/
config.go
executable file
·356 lines (304 loc) · 10.1 KB
/
config.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
package config
import (
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/go-chassis/go-chassis/core/archaius"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/config/model"
"github.com/go-chassis/go-chassis/core/config/schema"
"github.com/go-chassis/go-chassis/core/lager"
"github.com/go-chassis/go-chassis/pkg/util/fileutil"
"gopkg.in/yaml.v2"
)
// GlobalDefinition is having the information about region, load balancing, service center, config center,
// protocols, and handlers for the micro service
var GlobalDefinition *model.GlobalCfg
var lbConfig *model.LBWrapper
// MicroserviceDefinition is having the info about application id, provider info, description of the service,
// and description of the instance
var MicroserviceDefinition *model.MicroserviceCfg
// PaasLagerDefinition is having the information about loging
var PaasLagerDefinition *model.PassLagerCfg
// RouterDefinition is route rule config
var RouterDefinition *model.RouterConfig
//HystrixConfig is having info about isolation, circuit breaker, fallback properities of the micro service
var HystrixConfig *model.HystrixConfigWrapper
// NodeIP gives the information of node ip
var NodeIP string
// SelfServiceName is self micro service name
var SelfServiceName string
// SelfMetadata is gives meta data of the self micro service
var SelfMetadata map[string]string
// SelfVersion gives version of the self micro service
var SelfVersion string
// ErrNoName is used to represent the service name missing error
var ErrNoName = errors.New("micro service name is missing in description file")
// parse unmarshal configurations on respective structure
func parse() error {
err := readGlobalConfigFile()
if err != nil {
return err
}
err = ReadLBFromArchaius()
if err != nil {
return err
}
err = ReadHystrixFromArchaius()
if err != nil {
return err
}
err = readMicroserviceConfigFiles()
if err != nil {
return err
}
populateConfigCenterAddress()
populateServiceRegistryAddress()
populateMonitorServerAddress()
populateServiceEnvironment()
populateServiceName()
populateVersion()
populateTenant()
return nil
}
// populateServiceRegistryAddress populate service registry address
func populateServiceRegistryAddress() {
//Registry Address , higher priority for environment variable
registryAddrFromEnv := os.Getenv(common.EnvCSEEndpoint)
if registryAddrFromEnv == "" {
registryAddrFromEnv = archaius.GetString(common.CseRegistryAddress, "")
}
if registryAddrFromEnv != "" {
GlobalDefinition.Cse.Service.Registry.Registrator.Address = registryAddrFromEnv
GlobalDefinition.Cse.Service.Registry.ServiceDiscovery.Address = registryAddrFromEnv
GlobalDefinition.Cse.Service.Registry.ContractDiscovery.Address = registryAddrFromEnv
GlobalDefinition.Cse.Service.Registry.Address = registryAddrFromEnv
}
}
// populateConfigCenterAddress populate config center address
func populateConfigCenterAddress() {
//Config Center Address , higher priority for environment variable
configCenterAddrFromEnv := os.Getenv(common.EnvCSEEndpoint)
if configCenterAddrFromEnv == "" {
configCenterAddrFromEnv = archaius.GetString(common.CseConfigCenterAddress, "")
}
if configCenterAddrFromEnv != "" {
GlobalDefinition.Cse.Config.Client.ServerURI = configCenterAddrFromEnv
}
}
// populateMonitorServerAddress populate monitor server address
func populateMonitorServerAddress() {
//Monitor Center Address , higher priority for environment variable
monitorServerAddrFromEnv := archaius.GetString(common.CseMonitorServer, "")
if monitorServerAddrFromEnv != "" {
GlobalDefinition.Cse.Monitor.Client.ServerURI = monitorServerAddrFromEnv
}
}
// populateServiceEnvironment populate service environment
func populateServiceEnvironment() {
if e := archaius.GetString(common.Env, ""); e != "" {
MicroserviceDefinition.ServiceDescription.Environment = e
}
}
// populateServiceName populate service name
func populateServiceName() {
if e := archaius.GetString(common.ServiceName, ""); e != "" {
MicroserviceDefinition.ServiceDescription.Name = e
}
}
// populateVersion populate version
func populateVersion() {
if e := archaius.GetString(common.Version, ""); e != "" {
MicroserviceDefinition.ServiceDescription.Version = e
}
}
// populateTenant populate tenant
func populateTenant() {
if GlobalDefinition.Cse.Service.Registry.Tenant == "" {
GlobalDefinition.Cse.Service.Registry.Tenant = common.DefaultApp
}
}
// readGlobalConfigFile for to unmarshal the global config file(chassis.yaml) information
func readGlobalConfigFile() error {
globalDef := model.GlobalCfg{}
err := archaius.UnmarshalConfig(&globalDef)
if err != nil {
return err
}
GlobalDefinition = &globalDef
return nil
}
// readPanelConfig for to unmarshal the global config file(chassis.yaml) information
func readPanelConfig() error {
globalDef := model.GlobalCfg{}
err := archaius.UnmarshalConfig(&globalDef)
if err != nil {
return err
}
GlobalDefinition = &globalDef
return nil
}
// ReadLBFromArchaius for to unmarshal the global config file(chassis.yaml) information
func ReadLBFromArchaius() error {
lbMutex.Lock()
defer lbMutex.Unlock()
lbDef := model.LBWrapper{}
err := archaius.UnmarshalConfig(&lbDef)
if err != nil {
return err
}
lbConfig = &lbDef
return nil
}
type pathError struct {
Path string
Err error
}
func (e *pathError) Error() string { return e.Path + ": " + e.Err.Error() }
// parsePaasLagerConfig unmarshals the paas lager configuration file(lager.yaml)
func parsePaasLagerConfig(file string) error {
PaasLagerDefinition = &model.PassLagerCfg{}
err := unmarshalYamlFile(file, PaasLagerDefinition)
if err != nil && !os.IsNotExist(err) {
return &pathError{Path: file, Err: err}
}
return err
}
// parseRouterConfig is unmarshal the router configuration file(router.yaml)
func parseRouterConfig(file string) error {
RouterDefinition = &model.RouterConfig{}
err := unmarshalYamlFile(file, RouterDefinition)
if err != nil && !os.IsNotExist(err) {
return &pathError{Path: file, Err: err}
}
return err
}
func unmarshalYamlFile(file string, target interface{}) error {
content, err := ioutil.ReadFile(file)
if err != nil {
return err
}
return yaml.Unmarshal(content, target)
}
// ReadHystrixFromArchaius is unmarshal hystrix configuration file(circuit_breaker.yaml)
func ReadHystrixFromArchaius() error {
cbMutex.RLock()
defer cbMutex.RUnlock()
hystrixCnf := model.HystrixConfigWrapper{}
err := archaius.UnmarshalConfig(&hystrixCnf)
if err != nil {
return err
}
HystrixConfig = &hystrixCnf
return nil
}
// readMicroserviceConfigFiles read micro service configuration file
func readMicroserviceConfigFiles() error {
MicroserviceDefinition = &model.MicroserviceCfg{}
//find only one microservice yaml
microserviceNames := schema.GetMicroserviceNames()
defPath := fileutil.GetMicroserviceDesc()
data, err := ioutil.ReadFile(defPath)
if err != nil {
lager.Logger.Errorf(err, fmt.Sprintf("WARN: Missing microservice description file: %s", err.Error()))
if len(microserviceNames) == 0 {
return errors.New("Missing microservice description file")
}
msName := microserviceNames[0]
msDefPath := fileutil.MicroserviceDefinition(msName)
lager.Logger.Warnf(fmt.Sprintf("Try to find microservice description file in [%s]", msDefPath))
data, err := ioutil.ReadFile(msDefPath)
if err != nil {
return fmt.Errorf("Missing microservice description file: %s", err.Error())
}
ReadMicroserviceConfigFromBytes(data)
return nil
}
return ReadMicroserviceConfigFromBytes(data)
}
// ReadMicroserviceConfigFromBytes read micro service configurations from bytes
func ReadMicroserviceConfigFromBytes(data []byte) error {
microserviceDef := model.MicroserviceCfg{}
err := yaml.Unmarshal([]byte(data), µserviceDef)
if err != nil {
return err
}
if microserviceDef.ServiceDescription.Name == "" {
return ErrNoName
}
if microserviceDef.ServiceDescription.Version == "" {
microserviceDef.ServiceDescription.Version = common.DefaultVersion
}
MicroserviceDefinition = µserviceDef
return nil
}
//GetLoadBalancing return lb config
func GetLoadBalancing() *model.LoadBalancing {
if lbConfig != nil {
return lbConfig.Prefix.LBConfig
}
return nil
}
//GetHystrixConfig return cb config
func GetHystrixConfig() *model.HystrixConfig {
return HystrixConfig.HystrixConfig
}
// Init is initialize the configuration directory, lager, archaius, route rule, and schema
func Init() error {
err := parsePaasLagerConfig(fileutil.PaasLagerDefinition())
//initialize log in any case
if err != nil {
lager.Initialize("", "", "", "", true, 1, 10, 7)
if os.IsNotExist(err) {
lager.Logger.Infof("[%s] not exist", fileutil.PaasLagerDefinition())
} else {
return err
}
} else {
lager.Initialize(PaasLagerDefinition.Writers, PaasLagerDefinition.LoggerLevel,
PaasLagerDefinition.LoggerFile, PaasLagerDefinition.RollingPolicy,
PaasLagerDefinition.LogFormatText, PaasLagerDefinition.LogRotateDate,
PaasLagerDefinition.LogRotateSize, PaasLagerDefinition.LogBackupCount)
}
if err = parseRouterConfig(fileutil.RouterDefinition()); err != nil {
if os.IsNotExist(err) {
lager.Logger.Infof("[%s] not exist", fileutil.RouterDefinition())
} else {
return err
}
}
err = archaius.Init()
if err != nil {
return err
}
lager.Logger.Infof("archaius init success")
var schemaError error
//Upload schemas using environment variable SCHEMA_ROOT
schemaEnv := archaius.GetString(common.EnvSchemaRoot, "")
if schemaEnv != "" {
schemaError = schema.LoadSchema(schemaEnv, true)
} else {
schemaError = schema.LoadSchema(fileutil.GetConfDir(), false)
}
if schemaError != nil {
return schemaError
}
//set microservice names
msError := schema.SetMicroServiceNames(fileutil.GetConfDir())
if msError != nil {
return msError
}
NodeIP = archaius.GetString(common.EnvNodeIP, "")
err = parse()
if err != nil {
return err
}
SelfServiceName = MicroserviceDefinition.ServiceDescription.Name
SelfVersion = MicroserviceDefinition.ServiceDescription.Version
SelfMetadata = MicroserviceDefinition.ServiceDescription.Properties
if GlobalDefinition.AppID == "" {
GlobalDefinition.AppID = common.DefaultApp
}
return err
}