配置

Example

系统内置

  • 文本配置
  • 框架默认开启动态配置
  • 框架默认内志支持文本更改日志级别,动态生效

代码示例

动态配置获取单个配置信息

  1. package main
  2. import (
  3. "github.com/gotomicro/ego"
  4. "github.com/gotomicro/ego/core/econf"
  5. "github.com/gotomicro/ego/core/elog"
  6. "time"
  7. )
  8. // export EGO_DEBUG=true && go run main.go --config=config.toml
  9. func main() {
  10. if err := ego.New(ego.WithHang(true)).Invoker(func() error {
  11. go func() {
  12. // 循环打印配置
  13. for {
  14. time.Sleep(3 * time.Second)
  15. peopleName := econf.GetString("people.name")
  16. elog.Info("people info", elog.String("name", peopleName), elog.String("type", "onelineByFileWatch"))
  17. }
  18. }()
  19. return nil
  20. }).Run(); err != nil {
  21. elog.Panic("startup", elog.FieldErr(err))
  22. }
  23. }

动态配置获取结构体配置信息

  1. package main
  2. import (
  3. "github.com/gotomicro/ego"
  4. "github.com/gotomicro/ego/core/econf"
  5. "github.com/gotomicro/ego/core/elog"
  6. "time"
  7. )
  8. // export EGO_DEBUG=true && go run main.go --config=config.toml
  9. func main() {
  10. if err := ego.New(ego.WithHang(true)).Invoker(func() error {
  11. p := People{}
  12. // 初始化
  13. err := econf.UnmarshalKey("people", &p)
  14. if err != nil {
  15. panic(err.Error())
  16. }
  17. // 监听
  18. econf.OnChange(func(config *econf.Configuration) {
  19. err := config.UnmarshalKey("people", &p)
  20. if err != nil {
  21. elog.Panic("unmarshal", elog.FieldErr(err))
  22. }
  23. })
  24. go func() {
  25. // 循环打印配置
  26. for {
  27. time.Sleep(1 * time.Second)
  28. elog.Info("people info", elog.String("name", p.Name), elog.String("type", "structByFileWatch"))
  29. }
  30. }()
  31. return nil
  32. }).Run(); err != nil {
  33. elog.Panic("startup", elog.FieldErr(err))
  34. }
  35. }
  36. type People struct {
  37. Name string
  38. }