gotomicro / ego

A simple and component-based microservice kit for go. 一个简单的基于组件的 Go 微服务套件。

Home Page:https://ego-org.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

组件依赖 econf 加载配置,能不能传入自定义配置给组件呢?

zhan3333 opened this issue · comments

commented

例如现在的新建 egin component 代码为:

server := egin.Load("server.http").Build()

而我现在使用的一个旧项目已经在线上运行,更改 config 结构不方便,能否通过下面这种方式传入自定义配置项呢?

server := egin.Load("", egin.Config{Port: 9001, Host: "0.0.0.0"}).Build()
commented

例如现在的新建 egin component 代码为:

server := egin.Load("server.http").Build()

而我现在使用的一个旧项目已经在线上运行,更改 config 结构不方便,能否通过下面这种方式传入自定义配置项呢?

server := egin.Load("", egin.Config{Port: 9001, Host: "0.0.0.0"}).Build()

在Build里面有option更改这些属性

egin.DefaultContainer().Build(...)

commented

image

master 分支的 egin options 并不支持直接更改 container.config, 只能更改一些定义好的 config option,并不能满足要求。
commented
image

master 分支的 egin options 并不支持直接更改 container.config, 只能更改一些定义好的 config option,并不能满足要求。

穷举所有的比较麻烦,所以不支持的,直接提pr。放进去就可以。

commented

👌

commented

看了 master 上的代码,其中 Build() 时已经有 econf.OnChange() 的依赖了, 用 option 的方式传入改动范围太大。后来者可以使用 econf.Apply() 方式接入 ego:

假设旧项目的配置文件不符合 ego component 配置格式,可以用 econf.Apply() 手动转换格式并应用,

// 项目配置应用到 econf
econf.Apply(map[string]interface{}{
      "server": map[string]interface{}{
	      "http": map[string]interface{}{
		      "port": config.Port,
	      },
      },
} 
// ego.New 中会默认去加载配置文件,这里关闭掉
app := ego.New(ego.WithDisableFlagConfig(true))