基本介绍

GoFrame框架提供了配置管理组件,采用了解耦化及接口化设计,可以很灵活地对接各种第三方配置管理中心。配置管理组件默认提供基于本地系统文件的实现,更多的实现请参考社区组件:https://github.com/gogf/gf/tree/master/contrib/config

社区组件提供了常用的多种配置中心实现,例如polaris, apollo, nacos, consul以及容器编排的kubernetes configmap

组件启用

配置管理组件的启用采用了包初始化的方式,并且由于配置管理功能比较底层,因此需要保证社区包在main包的最顶部引入,防止踩坑。我们这里以polaris为例,社区组件的使用说明:https://github.com/gogf/gf/tree/master/contrib/config/polaris

需要提供一个独立的引入包,例如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)
}

其中:

  • Namespace指定的是polaris配置中的命名空间。
  • FileGroup指定的是polaris中的文件分组。
  • FileName指定的是需要读取的polaris中的配置文件名称。
  • Path指定的是polaris服务端配置,包括polaris的链接地址、监听地址、组件输出日志路径等。

Polaris的配置文件如下:

global:
  serverConnector:
    addresses:
      - 127.0.0.1:8091
config:
  configConnector:
    addresses:
      - 127.0.0.1:8093
consumer:
  localCache:
    persistDir: "/tmp/polaris/backup"

随后在main.go顶部引入boot包,保证该包的引入在所有组件的最前面:

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"))
}

注意其中顶部import的引入语句:

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

常用组件

更多组件,请参考:https://github.com/gogf/gf/tree/master/contrib/config

使用示例

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

运行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

运行示例

$ 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

可以看到最后的MustGet方法执行有报错,这是因为polaris中并没有指定的命名空间、配置分组以及配置文件,即便查询不到任何数据但是涉及到配置问题因此返回错误了。由于这里使用的是Must*方法,那么在执行返回错误时将会直接panic而不是返回错误。

那么我们进入polaris的后台添加一些测试数据再试试。

添加测试数据

进入 http://127.0.0.1:8080/#/login 默认账号 polaris 密码 polaris

再次运行示例

$ go run main.go 
true
{
    "server": {
        "openapiPath": "/api.json",
        "swaggerPath": "/swagger",
        "address":     ":8199",
    },
}
<nil>
":8199"

可以看到已经正确查询到了polaris中的配置数据。








Content Menu

  • No labels

1 Comment

  1. 北极星安装文档:点击查看