gf/contrib/config/apollo/README.MD
John Guo 127e8af6a6
add gcfg.Adapter implements using apollo service (#2165)
* version updates

* up

* add watch feature for package kubecm

* feat: support apollo as Adapter (#2143)

* feat: support apollo as Adapter

* feat: support apollo as Adapter

* feat: support apollo as Adapter

* feat: test apollo Adapter

* feat: test apollo Adapter

Co-authored-by: hongyihui <hongyihui@lixiang.com>
Co-authored-by: houseme <housemecn@gmail.com>
Co-authored-by: John Guo <john@johng.cn>

* add gcfg.Adapter implements using apollo service

* ci yaml update for apollo

Co-authored-by: hong0220 <hong0220@users.noreply.github.com>
Co-authored-by: hongyihui <hongyihui@lixiang.com>
Co-authored-by: houseme <housemecn@gmail.com>
2022-09-30 18:19:52 +08:00

1.6 KiB

apollo

Package apollo implements GoFrame gcfg.Adapter using apollo service.

Installation

go get -u github.com/gogf/gf/contrib/config/apollo/v2

Usage

Create a custom boot package

If you wish using configuration from apollo globally, it is strongly recommended creating a custom boot package in very top import, which sets the Adapter of default configuration instance before any other package boots.

package boot

import (
	"github.com/gogf/gf/contrib/config/apollo/v2"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
)

func init() {
	var (
		ctx     = gctx.GetInitCtx()
		appId   = "SampleApp"
		cluster = "default"
		ip      = "http://localhost:8080"
	)
	// Create apollo Client that implements gcfg.Adapter.
	adapter, err := apollo.New(ctx, apollo.Config{
		AppID:   appId,
		IP:      ip,
		Cluster: cluster,
	})
	if err != nil {
		g.Log().Fatalf(ctx, `%+v`, err)
	}
	// Change the adapter of default configuration instance.
	g.Cfg().SetAdapter(adapter)
}

Import boot package in top of main

It is strongly recommended import your boot package in top of your main.go.

Note the top import: _ "github.com/gogf/gf/example/config/apollo/boot" .

package main

import (
	_ "github.com/gogf/gf/example/config/apollo/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"))
}