Introduction

The GoFrame framework provides a configuration management component designed with a decoupled and interface-oriented approach, allowing flexible integration with various third-party configuration management centers. The component default implementation is based on local system files. For more implementations, refer to community components: https://github.com/gogf/gf/tree/master/contrib/config

The community components provide implementations for various popular configuration centers, such as polaris, apollo, nacos, consul, and container orchestration kubernetes configmap.

Component Activation

The activation of the configuration management component is achieved through package initialization. Since the configuration management feature is quite fundamental, it is necessary to ensure that the community package is introduced at the very top of the main package to avoid pitfalls. Here we take polaris as an example, for how to use the community component: https://github.com/gogf/gf/tree/master/contrib/config/polaris

An independent import package is needed, such as boot:

  1. package boot
  2. import (
  3. "github.com/gogf/gf/contrib/config/polaris/v2"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. )
  7. func init() {
  8. var (
  9. ctx = gctx.GetInitCtx()
  10. namespace = "default"
  11. fileGroup = "TestGroup"
  12. fileName = "config.yaml"
  13. path = "manifest/config/polaris.yaml"
  14. logDir = "/tmp/polaris/log"
  15. )
  16. // Create polaris Client that implements gcfg.Adapter.
  17. adapter, err := polaris.New(ctx, polaris.Config{
  18. Namespace: namespace,
  19. FileGroup: fileGroup,
  20. FileName: fileName,
  21. Path: path,
  22. LogDir: logDir,
  23. Watch: true,
  24. })
  25. if err != nil {
  26. g.Log().Fatalf(ctx, `%+v`, err)
  27. }
  28. // Change the adapter of default configuration instance.
  29. g.Cfg().SetAdapter(adapter)
  30. }

Where:

  • Namespace specifies the namespace in the polaris configuration.
  • FileGroup specifies the file group in polaris.
  • FileName specifies the name of the configuration file to read in polaris.
  • Path specifies the server-side configuration of polaris, including the connection address, listening address, component output log path, etc.

The configuration file of Polaris is as follows:

  1. global:
  2. serverConnector:
  3. addresses:
  4. - 127.0.0.1:8091
  5. config:
  6. configConnector:
  7. addresses:
  8. - 127.0.0.1:8093
  9. consumer:
  10. localCache:
  11. persistDir: "/tmp/polaris/backup"

Then introduce the boot package at the top of main.go, ensuring that its import is before all other components:

  1. package main
  2. import (
  3. _ "github.com/gogf/gf/example/config/polaris/boot"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. )
  7. func main() {
  8. var ctx = gctx.GetInitCtx()
  9. // Available checks.
  10. g.Dump(g.Cfg().Available(ctx))
  11. // All key-value configurations.
  12. g.Dump(g.Cfg().Data(ctx))
  13. // Retrieve certain value by key.
  14. g.Dump(g.Cfg().MustGet(ctx, "server.address"))
  15. }

Note the import statement at the top:

  1. _ "github.com/gogf/gf/example/config/polaris/boot"

Common Components

Component NameDocumentationRemark
fileBuilt-in frameworkDefault implementation
apollohttps://github.com/gogf/gf/tree/master/contrib/config/apollo
kubecmhttps://github.com/gogf/gf/tree/master/contrib/config/kubecmCommonly used in container deployment environments
nacoshttps://github.com/gogf/gf/tree/master/contrib/config/nacos
polarishttps://github.com/gogf/gf/tree/master/contrib/config/polaris
consulhttps://github.com/gogf/gf/tree/master/contrib/config/consul

For more components, refer to: https://github.com/gogf/gf/tree/master/contrib/config

Usage Example

https://github.com/gogf/gf/tree/master/example/config/polaris

Run polaris

  1. docker run -d --name polaris -p 8080:8080 -p 8090:8090 -p 8091:8091 -p 8093:8093 loads/polaris-server-standalone:1.11.2

Run the example

  1. $ go run main.go
  2. true
  3. {}
  4. "failed to update local value: config file is empty"
  5. panic: failed to update local value: config file is empty
  6. goroutine 1 [running]:
  7. github.com/gogf/gf/v2/os/gcfg.(*Config).MustGet(0x0?, {0x1c1c4f8?, 0xc0000c2000?}, {0x1ac11ad?, 0x0?}, {0x0?, 0xc000002340?, 0xc000064738?})
  8. /Users/john/Workspace/gogf/gf/os/gcfg/gcfg.go:167 +0x5e
  9. main.main()
  10. /Users/john/Workspace/gogf/gf/example/config/polaris/main.go:20 +0x1b8

As seen, the MustGet method execution at the end reports an error because there is no specified namespace, configuration group, and configuration file in polaris. Even if no data is retrieved, it returns an error due to configuration issues. Because the Must* method is used here, when an error is returned upon execution, it will panic directly instead of returning an error.

So let’s add some test data in the polaris backend and try again.

Add Test Data

Log in to http://127.0.0.1:8080/#/login with default username polaris and password polaris.

Service Configuration - 图1

Service Configuration - 图2

Run the example again

  1. $ go run main.go
  2. true
  3. {
  4. "server": {
  5. "openapiPath": "/api.json",
  6. "swaggerPath": "/swagger",
  7. "address": ":8199",
  8. },
  9. }
  10. <nil>
  11. ":8199"

We can see that the configuration data in polaris has been correctly retrieved.