gcfg组件采用了接口化设计,以实现高扩展性。通过接口化设计的方式,使用者可以自定义对接的配置获取方式,例如etcd, zookeeper, consule, kubernetes configmap, apollo等等。

接口定义

https://github.com/gogf/gf/blob/master/os/gcfg/gcfg_adaper.go

  1. // Adapter is the interface for configuration retrieving.
  2. type Adapter interface {
  3. // Available checks and returns the configuration service is available.
  4. // The optional parameter `resource` specifies certain configuration resource.
  5. //
  6. // It returns true if configuration file is present in default AdapterFile, or else false.
  7. // Note that this function does not return error as it just does simply check for backend configuration service.
  8. Available(ctx context.Context, resource ...string) (ok bool)
  9. // Get retrieves and returns value by specified `pattern`.
  10. Get(ctx context.Context, pattern string) (value interface{}, err error)
  11. // Data retrieves and returns all configuration data as map type.
  12. // Note that this function may lead lots of memory usage if configuration data is too large,
  13. // you can implement this function if necessary.
  14. Data(ctx context.Context) (data map[string]interface{}, err error)
  15. }

设置接口实现

配置对象可以通过SetAdapter方法设置当前使用的接口实现。

  1. // SetAdapter sets the adapter of current Config object.
  2. func (c *Config) SetAdapter(adapter Adapter)

获取接口实现

配置对象可以通过GetAdapter方法获取当前使用的接口实现。

  1. // GetAdapter returns the adapter of current Config object.
  2. func (c *Config) GetAdapter() Adapter