Configurations
Configurations allow to change microservice behavior, adjust to deployment environments, and set parameters to connect to infrastructure services and other microservices. For this, the Pip.Services toolkit provides a set of patterns that support design-time, deployment-time, and runtime configurations.
Design-time Configuration
This is the simplest type of configuration. It is initially defined by developers in their code and later used to set components by calling the configure method in components that implement the IConfigurable interface.
import { ConfigParams, IConfigurable } from "pip-services4-components-node";
let config = ConfigParams.fromTuples(
'param1', 'XYZ',
'param2', 345
);
component.configure(config);
/// Also, often components can have hard-coded presets.
/// The ConfigParams class has methods that allow to easily use them as defaults:
class MyComponent implements IConfigurable {
private _param1: string = 'ABC';
private _param2: number = 123;
public configure(config: ConfigParams) {
this._param1 = config.getAsStringWithDefault('param1', this._param1);
this._param2 = config.getAsIntegerWithDefault('param2', this._param2);
}
}
import (
cconf "github.com/pip-services4/pip-services4-go/pip-services4-components-go/config"
)
config := cconf.NewConfigParamsFromTuples(
"param1", "XYZ",
"param2", 345,
)
component.Configure(context.Background(), config)
/// Also, often components can have hard-coded presets.
/// The ConfigParams class has methods that allow to easily use them as defaults:
type MyComponent struct { // Implements IConfigurable
param1 string
param2 int
}
func NewMyComponent() MyComponent {
return MyComponent{
param1: "ABC",
param2: 123,
}
}
func (c *MyComponent) Configure(ctx context.Context, config *cconf.ConfigParams) {
c.param1 = config.GetAsStringWithDefault("param1", c.param1)
c.param2 = config.GetAsIntegerWithDefault("param2", c.param2)
}
from pip_services4_components.config import ConfigParams, IConfigurable
config = ConfigParams.from_tuples(
'param1', 'XYZ',
'param2', 345
)
# Also, often components can have hard-coded presets.
# The ConfigParams class has methods that allow to easily use them as defaults:
class MyComponent(IConfigurable):
_param1: str = 'ABC'
_param2: int = 123
def configure(self, config: ConfigParams):
self._param1 = config.get_as_string_with_default('param1', self._param1)
self._param2 = config.get_as_integer_with_default('param2', self._param2)
component = MyComponent()
component.configure(config)
Deployment-time Configuration
Deployment-time configuration parameters are set via environment variables or passed to a container via command-line arguments:
export MYCOMPONENT_ENABLED=true
export PARAM1=XYZ
node ./bin/main.js -p PARAM2=345
export MYCOMPONENT_ENABLED=true
export PARAM1=XYZ
go run ./bin/main.go -p PARAM2=345
export MYCOMPONENT_ENABLED=true
export PARAM1=XYZ
python ./bin/main.py -p PARAM2=345
Then those parameters are automatically injected into container configurations using Mustache templates:
{{#if MYCOMPONENT_ENABLED}}
descriptor: myservice:mycomponent:default:default:1.0
param1: {{PARAM1}}
param2: {{PARAM2}}
{{/if}}
The deployment-time parameters can change the composition of the microservice by including or excluding parameters or their configuration blocks, or setting specific parameters values that are passed to already known configure methods in components that implement the IConfigurable interface.
Runtime Configuration
Runtime configurations are typically stored in specialized services like etcd or special microservices. The Pip.Services toolkit has a standard IConfigReader interface that can be implemented by components that retrieve configurations from various sources. In this manner, they can pull configurations by calling the readConfig method or get notified when configurations change via the addChangeListener method.
The Pip.Services toolkit offers a few standard configuration readers:
- MemoryConfigReader: Stores a configuration in memory.
- FileConfigReader: Reads a configuration from a file.
- JsonConfigReader
- YamlConfigReader
References
For more information about configurations see: