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
:
package boot
import (
"github.com/gogf/gf/contrib/config/polaris/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func init() {
var (
ctx = gctx.GetInitCtx()
namespace = "default"
fileGroup = "TestGroup"
fileName = "config.yaml"
path = "manifest/config/polaris.yaml"
logDir = "/tmp/polaris/log"
)
// Create polaris Client that implements gcfg.Adapter.
adapter, err := polaris.New(ctx, polaris.Config{
Namespace: namespace,
FileGroup: fileGroup,
FileName: fileName,
Path: path,
LogDir: logDir,
Watch: true,
})
if err != nil {
g.Log().Fatalf(ctx, `%+v`, err)
}
// Change the adapter of default configuration instance.
g.Cfg().SetAdapter(adapter)
}
Where:
Namespace
specifies the namespace in thepolaris
configuration.FileGroup
specifies the file group inpolaris
.FileName
specifies the name of the configuration file to read inpolaris
.Path
specifies the server-side configuration ofpolaris
, including the connection address, listening address, component output log path, etc.
The configuration file of Polaris
is as follows:
global:
serverConnector:
addresses:
- 127.0.0.1:8091
config:
configConnector:
addresses:
- 127.0.0.1:8093
consumer:
localCache:
persistDir: "/tmp/polaris/backup"
Then introduce the boot
package at the top of main.go
, ensuring that its import is before all other components:
package main
import (
_ "github.com/gogf/gf/example/config/polaris/boot"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
var ctx = gctx.GetInitCtx()
// Available checks.
g.Dump(g.Cfg().Available(ctx))
// All key-value configurations.
g.Dump(g.Cfg().Data(ctx))
// Retrieve certain value by key.
g.Dump(g.Cfg().MustGet(ctx, "server.address"))
}
Note the import statement at the top:
_ "github.com/gogf/gf/example/config/polaris/boot"
Common Components
Component Name | Documentation | Remark |
---|---|---|
file | Built-in framework | Default implementation |
apollo | https://github.com/gogf/gf/tree/master/contrib/config/apollo | |
kubecm | https://github.com/gogf/gf/tree/master/contrib/config/kubecm | Commonly used in container deployment environments |
nacos | https://github.com/gogf/gf/tree/master/contrib/config/nacos | |
polaris | https://github.com/gogf/gf/tree/master/contrib/config/polaris | |
consul | https://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
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
$ go run main.go
true
{}
"failed to update local value: config file is empty"
panic: failed to update local value: config file is empty
goroutine 1 [running]:
github.com/gogf/gf/v2/os/gcfg.(*Config).MustGet(0x0?, {0x1c1c4f8?, 0xc0000c2000?}, {0x1ac11ad?, 0x0?}, {0x0?, 0xc000002340?, 0xc000064738?})
/Users/john/Workspace/gogf/gf/os/gcfg/gcfg.go:167 +0x5e
main.main()
/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
.
Run the example again
$ go run main.go
true
{
"server": {
"openapiPath": "/api.json",
"swaggerPath": "/swagger",
"address": ":8199",
},
}
<nil>
":8199"
We can see that the configuration data in polaris
has been correctly retrieved.