Configuration System Overview
Kratos configuration features:- Multiple sources (files, environment variables, remote config)
- Hot-reloading with watchers
- Format support (YAML, JSON, TOML, XML)
- Configuration merging and overrides
- Type-safe configuration binding
Basic Configuration
package conf
type Bootstrap struct {
Server *Server `json:"server"`
Data *Data `json:"data"`
Registry *Registry `json:"registry"`
}
type Server struct {
HTTP *HTTP `json:"http"`
GRPC *GRPC `json:"grpc"`
}
type HTTP struct {
Network string `json:"network"`
Addr string `json:"addr"`
Timeout string `json:"timeout"`
}
type GRPC struct {
Network string `json:"network"`
Addr string `json:"addr"`
Timeout string `json:"timeout"`
}
type Data struct {
Database *Database `json:"database"`
Redis *Redis `json:"redis"`
}
type Database struct {
Driver string `json:"driver"`
Source string `json:"source"`
}
type Redis struct {
Addr string `json:"addr"`
ReadTimeout string `json:"read_timeout"`
WriteTimeout string `json:"write_timeout"`
}
type Registry struct {
Consul *Consul `json:"consul"`
}
type Consul struct {
Address string `json:"address"`
Scheme string `json:"scheme"`
}
server:
http:
network: tcp
addr: 0.0.0.0:8000
timeout: 1s
grpc:
network: tcp
addr: 0.0.0.0:9000
timeout: 1s
data:
database:
driver: mysql
source: root:root@tcp(127.0.0.1:3306)/test?parseTime=True
redis:
addr: 127.0.0.1:6379
read_timeout: 0.2s
write_timeout: 0.2s
registry:
consul:
address: 127.0.0.1:8500
scheme: http
package main
import (
"flag"
"os"
"github.com/go-kratos/kratos/v2/config"
"github.com/go-kratos/kratos/v2/config/file"
"github.com/go-kratos/kratos/v2/log"
"yourproject/internal/conf"
)
var (
flagconf string
)
func init() {
flag.StringVar(&flagconf, "conf", "../../configs", "config path, eg: -conf config.yaml")
}
func main() {
flag.Parse()
// Create config source
c := config.New(
config.WithSource(
file.NewSource(flagconf),
),
)
defer c.Close()
// Load configuration
if err := c.Load(); err != nil {
log.Fatal(err)
}
// Scan configuration into struct
var bc conf.Bootstrap
if err := c.Scan(&bc); err != nil {
log.Fatal(err)
}
// Use configuration
log.Infof("HTTP server running on %s", bc.Server.HTTP.Addr)
log.Infof("gRPC server running on %s", bc.Server.GRPC.Addr)
}
Multiple Configuration Sources
File + Environment Variables
Combine file and environment variable sources:Remote Configuration
Load configuration from remote sources (Consul, etcd, Kubernetes ConfigMap):Configuration Priority
Sources are merged in order, with later sources overriding earlier ones:Configuration Watching
Hot-Reloading
Watch for configuration changes:Watch Entire Configuration
Reload entire configuration on changes:Environment-Specific Configuration
Multiple Configuration Files
Organize configurations by environment:Configuration Validation
Validate on Load
Validate configuration after loading:Advanced Features
Value Interpolation
Reference other configuration values:Custom Decoders
Register custom decoders for specific formats:Configuration Resolver
Resolve placeholders in configuration:Best Practices
Secrets Management
Never commit secrets to version control. Use environment variables or secret managers.
Defaults
Provide sensible defaults for all configuration values.
Documentation
Document all configuration options with comments in your config files.
Validation
Always validate configuration on startup to fail fast.
Example: Complete Configuration Setup
cmd/server/main.go
Next Steps
Service Discovery
Configure service discovery and registration
Deployment
Deploy your configured service