-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] config feature roadmap #3254
Comments
It seems like you've got this under control, if you want help or have specific questions, let me know what I can do for you!
|
基于proto生成结构体 并发安全的配置热更新,支持配置validate option选项 |
Generate structure based on proto, concurrent and safe configuration hot update, support configuration of validate option option |
@xiongpan828
|
|
|
The config package focuses more on how to parse, merge, and finally serialize configurations into user structures
|
注入到 我现在的做法(参考): package main
import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos-ecosystem/components/v2/config"
"github.com/go-kratos-ecosystem/components/v2/env"
"github.com/go-kratos-ecosystem/components/v2/provider"
"github.com/go-kratos-ecosystem/components/v2/timezone"
)
type Config struct {
Host string
}
func main() {
ctx, err := provider.Pipe(context.Background(),
// 注入到 context 中
config.Provider(&Config{
Host: "localhost",
}),
// 注入其他的 Provider
timezone.Provider(timezone.Local("Asia/Shanghai")),
env.Provider(env.Prod),
)
if err != nil {
panic(err)
}
app := kratos.New(
kratos.Context(ctx),
kratos.Server(),
)
if err := app.Run(); err != nil {
panic(err)
}
}
// DemoService is a demo service.
func DemoService(ctx context.Context) error {
cfg, ok := config.FromContext[*Config](ctx)
if !ok {
return fmt.Errorf("config not found")
}
_ = cfg
return nil
} 参考资料: |
👍👍👍谢谢 get了 |
菜鸟咨询:能否进行配置文件没配置的部分给默认值 (比较推荐的方式) 😁 |
Rookie consultation: Is it possible to give default values to the unconfigured parts of the configuration file (a more recommended method) 😁 |
我的做法:(不过用了泛型,可能会有点性能损耗,我自己测了下,损耗不大。结合实际并发情况使用吧。) 相关包代码: package values
// If returns trueVal if condition is true, otherwise falseVal
//
// Example:
//
// If(true, "foo", "bar") // "foo"
// If(false, "foo", "bar") // "bar"
func If[T any](condition bool, trueVal T, falseVal T) T {
if condition {
return trueVal
}
return falseVal
}
// With calls the given callbacks with the given value then return the value.
//
// Example:
//
// With("foo", func(s string) string {
// return s + "bar"
// }, func(s string) string {
// return s + "baz"
// }) // "foobarbaz"
func With[T any](value T, callbacks ...func(T) T) T {
for _, callback := range callbacks {
if callback != nil {
value = callback(value)
}
}
return value
}
type Proxy[T any] struct {
value T
}
func NewProxy[T any](value T) *Proxy[T] {
return &Proxy[T]{
value: value,
}
}
func (p *Proxy[T]) When(condition bool, callbacks ...func(T) T) *Proxy[T] {
if condition {
return p.With(callbacks...)
}
return p
}
func (p *Proxy[T]) Value() T {
return p.value
} |
This is not a good proposal and should not be provided by the config package |
这个确实,适合用工具或者有个类似 bootstrap 统一进行初始化的包实现好点。 |
can't reading bool value from environment config |
希望支持byte序列化,因为配置有从配置中心读取到程序中,目前的Scan方法只能读取文件path |
We hope to support byte serialization because the configuration is read from the configuration center into the program. The current Scan method can only read the file path. |
Load 支持多次调用,因为有时候比如用配置中心,先拿到本地的配置然后才会连上配置中心接着 load |
Load supports multiple calls, because sometimes, for example, when using the configuration center, you first get the local configuration and then connect to the configuration center and then load |
希望支持先 path 然后 在 scan , 类似 https://github.com/go-micro/go-micro/blob/master/config/config.go#L82 config.Get("cos", "user", "domain").Scan(&domain) |
Hope to support path first and then scan, similar to https://github.com/go-micro/go-micro/blob/master/config/config.go#L82 config.Get("cos", "user", "domain").Scan(&domain) |
1.Need to load the configuration from the config center and write it to a local configuration file, such as configmap, in case the config center goes down and the service doesn't start? |
Dear Kratos friends, the config package has been widely used since its inception.
Received some feedback on issues and feature requirements during this period.
The current plan is to expand the config package functionality. Please feel free to comment below.
env
variable replacement and default value functionality Issue with configuration file to render the values from environment variables #3198The text was updated successfully, but these errors were encountered: