Config

    • The local file data source is implemented by default
    • Users can customize the data source implementation
    • Supports configuration hot-reloading, and change the existing Value through Atomic
    • Supports custom data-source decoding implementation
    • Ability to get the value of environment variables or existing fields through the $ placeholder

    In the Kratos project, we recommend using proto to define the configuration file by default. The main benefits are:

    • A unified template configuration can be defined
    • Add corresponding configuration verification
    • Better management of configuration
    • Cross-language support

    Configuration file

    Proto declaration

    1. syntax = "proto3";
    2. package kratos.api;
    3. option go_package = "github.com/go-kratos/kratos-layout/internal/conf;conf";
    4. import "google/protobuf/duration.proto";
    5. message Bootstrap {
    6. Server server = 1;
    7. Data data = 2;
    8. }
    9. message Server {
    10. message HTTP {
    11. string network = 1;
    12. string addr = 2;
    13. google.protobuf.Duration timeout = 3;
    14. }
    15. message GRPC {
    16. string network = 1;
    17. string addr = 2;
    18. google.protobuf.Duration timeout = 3;
    19. HTTP http = 1;
    20. GRPC grpc = 2;
    21. }
    22. message Data {
    23. message Database {
    24. string source = 2;
    25. }
    26. message Redis {
    27. string network = 1;
    28. string addr = 2;
    29. google.protobuf.Duration read_timeout = 3;
    30. google.protobuf.Duration write_timeout = 4;
    31. }
    32. Database database = 1;
    33. Redis redis = 2;
    34. }

    One or more config sources can be applied.

    • file
    • env
    1. c := config.New(
    2. config.WithSource(
    3. file.NewSource(path),
    4. ),
    5. config.WithDecoder(func(kv *config.KeyValue, v map[string]interface{}) error {
    6. // kv.Key
    7. // kv.Value
    8. // kv.Metadata
    9. // Configuration center can use the metadata to determine the type of the config.
    10. return yaml.Unmarshal(kv.Value, v)
    11. }),
    12. config.WithResolver(func(map[string]interface{}) error {
    13. // The default resolver provides processing
    14. // for two $(key:default) and $key placeholders.
    15. //
    16. // Customize the processing method after loading the configuration data...
    17. )
    18. // load config source
    19. log.Fatal(err)
    20. }
    21. // Get the corresponding value
    22. name, err := c.Value("service").String()
    23. /*
    24. // The structure generated by the proto file can also be directly declared for analysis
    25. var v struct {
    26. Service string `json:"service"`
    27. Version string `json:"version"`
    28. }
    29. */
    30. var bc conf.Bootstrap
    31. if err := c.Scan(&bc); err != nil {
    32. log.Fatal(err)
    33. }
    34. // watch the changing of the value
    35. c.Watch("service.name", func(key string, value config.Value) {
    36. // callback of this event
    37. })

    Kratos can read the value of environment variable or existing field through placeholders in the configuration file

    1. c := config.New(
    2. config.WithSource(
    3. // Add environment variables prefixed with KRATOS_
    4. env.NewSource("KRATOS_"),
    5. // Add configuration file
    6. file.NewSource(path),
    7. ))
    8. // 加载配置源:
    9. if err := c.Load(); err != nil {
    10. log.Fatal(err)
    11. }
    12. port, err := c.Value("PORT").String()