
kratos-config
In-memory config source implementation to use with Kratos framework.
CHINESE README
δΈζθ―΄ζ
Features
- Byte Slice Source: Load config from byte data instead of files
- Multiple Formats: Support JSON, YAML, XML, Proto plus additional formats
- Dynamic Updates: Runtime config updates with watch mechanism (DataSource)
- Static Mode: Lightweight static config without watch overhead (DataStatic)
- Type Safe: Complete support to Kratos config.Source interface
- Thread Safe: Concurrent-safe config updates
Installation
go get github.com/yylego/kratos-config/configkratos
Quick Start
Static Config (Recommended when config is immutable)
package main
import (
"fmt"
"github.com/go-kratos/kratos/v3/config"
"github.com/yylego/kratos-config/configkratos"
"github.com/yylego/must"
"github.com/yylego/rese"
)
type AppConfig struct {
Name string `json:"name"`
Version string `json:"version"`
}
func main() {
jsonData := []byte(`{"name":"my-app","version":"1.0.0"}`)
source := configkratos.NewJsonStatic(jsonData)
cfg := config.New(config.WithSource(source))
defer rese.F0(cfg.Close)
must.Done(cfg.Load())
var app AppConfig
must.Done(cfg.Scan(&app))
fmt.Printf("App: %s, Version: %s\n", app.Name, app.Version)
}
β¬οΈ Source: Source
Dynamic Config (Support runtime updates)
package main
import (
"fmt"
"time"
"github.com/go-kratos/kratos/v3/config"
"github.com/yylego/kratos-config/configkratos"
"github.com/yylego/must"
"github.com/yylego/rese"
)
type ServerConfig struct {
Port int `json:"port"`
}
func main() {
jsonData := []byte(`{"port":8080}`)
source := configkratos.NewJsonSource(jsonData)
cfg := config.New(config.WithSource(source))
defer rese.F0(cfg.Close)
must.Done(cfg.Load())
var server ServerConfig
must.Done(cfg.Scan(&server))
fmt.Printf("Start with port: %d\n", server.Port)
must.Done(cfg.Watch("port", func(key string, value config.Value) {
var port int
must.Done(value.Scan(&port))
fmt.Printf("Port update: %d\n", port)
}))
time.Sleep(time.Second)
newData := []byte(`{"port":9090}`)
must.Done(source.Update(newData))
time.Sleep(time.Second)
}
β¬οΈ Source: Source
package main
import (
"fmt"
"github.com/go-kratos/kratos/v3/config"
"github.com/yylego/kratos-config/configkratos"
"github.com/yylego/must"
"github.com/yylego/rese"
)
type DatabaseConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}
func main() {
yamlData := []byte(`host: localhost
port: 5432`)
source := configkratos.NewYamlStatic(yamlData)
cfg := config.New(config.WithSource(source))
defer rese.F0(cfg.Close)
must.Done(cfg.Load())
var res DatabaseConfig
must.Done(cfg.Scan(&res))
fmt.Printf("Database: %s:%d\n", res.Host, res.Port)
}
β¬οΈ Source: Source
API Reference
DataSource (Dynamic Config)
Create Source
// Generic function with format option
source := configkratos.NewDataSource(data, "json")
// Convenient functions to specific formats
jsonSource := configkratos.NewJsonSource(data)
yamlSource := configkratos.NewYamlSource(data)
Update Config
newData := []byte(`{"key":"new-value"}`)
err := source.Update(newData)
DataStatic (Static Config)
Create Static Source
// Generic function with format parameter
source := configkratos.NewDataStatic(data, "yaml")
// Convenient functions to specific formats
jsonSource := configkratos.NewJsonStatic(data)
yamlSource := configkratos.NewYamlStatic(data)
json - JSON format
yaml - YAML format
xml - XML format
proto - Protocol Buffers
form - URL encoded form
- Other formats registered in Kratos encoding
Use Cases
- Unit Testing: Mock config data without creating temp files
- Embedded Config: Package config data into binary
- Dynamic Config: Runtime config updates from remote sources
- Config Aggregation: Combine multiple config sources
Design Philosophy
DataSource vs DataStatic
- DataSource: Full-featured with watch mechanism, suitable to scenarios requiring runtime updates
- DataStatic: Lightweight without watch overhead, suitable to scenarios with immutable config
The dual implementation gives flexibility in choosing the best approach to different scenarios.
π License
MIT License. See LICENSE.
π€ Contributing
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Found a mistake? Open an issue on GitHub with reproduction steps
- π‘ Have a feature idea? Create an issue to discuss the suggestion
- π Documentation confusing? Report it so we can improve
- π Need new features? Share the use cases to help us understand requirements
- β‘ Performance issue? Help us optimize through reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo to get new releases and features
- π Success stories? Share how this package improved the workflow
- π¬ Feedback? We welcome suggestions and comments
π§ Development
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/kratos-config.git).
- Navigate: Navigate to the cloned project (
cd kratos-config)
- Branch: Create a feature branch (
git checkout -b feature/xxx).
- Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...) and follow Go code style conventions
- Documentation: Update documentation to support client-facing changes and use significant commit messages
- Stage: Stage changes (
git add .)
- Commit: Commit changes (
git commit -m "Add feature xxx") ensuring backward compatible code
- Push: Push to the branch (
git push origin feature/xxx).
- PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
π Support
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! πππ
GitHub Stars
