Config

In the Kratos project, multiple configuration sources can be specified, and config will be merged into key/value. Then the user obtains the corresponding key-value content through Scan or Value, the main features are:

  • 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

Define configuration through proto

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

  1. server:
  2. http:
  3. addr: 0.0.0.0:8000
  4. timeout: 1s
  5. grpc:
  6. addr: 0.0.0.0:9000
  7. timeout: 1s
  8. data:
  9. database:
  10. driver: mysql
  11. source: root:root@tcp(127.0.0.1:3306)/test
  12. redis:
  13. addr: 127.0.0.1:6379
  14. read_timeout: 0.2s
  15. write_timeout: 0.2s

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. }
  20. HTTP http = 1;
  21. GRPC grpc = 2;
  22. }
  23. message Data {
  24. message Database {
  25. string driver = 1;
  26. string source = 2;
  27. }
  28. message Redis {
  29. string network = 1;
  30. string addr = 2;
  31. google.protobuf.Duration read_timeout = 3;
  32. google.protobuf.Duration write_timeout = 4;
  33. }
  34. Database database = 1;
  35. Redis redis = 2;
  36. }

Build configuration

Head to the project’s root and execute the command below:

  1. make config

Usage

One or more config sources can be applied.

They will be merged into map[string]interface{}, then you could use Scan or Value to get the values.

  • 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. )
  19. // load config source
  20. if err := c.Load(); err != nil {
  21. log.Fatal(err)
  22. }
  23. // Get the corresponding value
  24. name, err := c.Value("service").String()
  25. /*
  26. // The structure generated by the proto file can also be directly declared for analysis
  27. var v struct {
  28. Service string `json:"service"`
  29. Version string `json:"version"`
  30. }
  31. */
  32. var bc conf.Bootstrap
  33. if err := c.Scan(&bc); err != nil {
  34. log.Fatal(err)
  35. }
  36. // watch the changing of the value
  37. c.Watch("service.name", func(key string, value config.Value) {
  38. // callback of this event
  39. })

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

  1. service:
  2. name: "kratos_app"
  3. http:
  4. server:
  5. # Use the value of service.name
  6. name: "${service.name}"
  7. # Replace with the environment variable PORT, if it does not exist, use the default value 8080
  8. port: "${PORT:8080}"
  9. # Use environment variable TIMEOUT to replace, no default value
  10. timeout: "$TIMEOUT"

When loading configuration sources from environment variables no need to load in advance, the analysis of the environment configuration will be performed after all sources are loaded

  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. // Get the value of the environment variable KRATOS_PORT
  13. port, err := c.Value("PORT").String()