configuration center is a configuration management center based on Zookeeper. It includes the following components.
configuration-center-api
provide an API to create/view/update/delete the config.
configuration-center-sdk
provide a Java SDK tool to get/listen config, which can be used for non-spring application.
spring-boot-starter-configuration-center
provide a library that integrated with Spring, which can be used for Spring boot application.
There should be a project configuration-center-ui
to provide a ui interface to operate the config easily. But sorry, I am not familiar with front-end development.
Assume there is an application called customer-api
in dev
environment.
Let's get zookeeper started and create some config for customer-api application.
Ideally we are supposed to create config data via configuration-center-api
, but here let's make things simple, create data in zookeeper directly.
- Start your zookeeper server
- Run below command in zookeeper server to create test data.
create /configuration-center ""
create /configuration-center/customer-api ""
create /configuration-center/customer-api/dev ""
create /configuration-center/customer-api/dev/name "Tom"
As you can see, we created a config name=Tom
for customer-api
in dev
environment.
The following documentation will show you how to get and listen config for non spring application & spring boot application.
- import
configuration-center-sdk
library - create
ConfigurationTemplate
configurationTemplate = new ConfigurationTemplate.Builder()
.connectionString("localhost:2181")
.application("customer-api")
.environment("dev")
.sessionTimeoutMs(10000)
.connectionTimeoutMs(10000)
.build();
- we can get the config like below.
String name = configurationTemplate.get("name")
- Also we can listen the config update as per code below, every time
name
config updated, callback method will be invoked.
configurationTemplate.listen(this, "name", value -> {
System.out.println(value);
});
- import
spring-boot-starter-configuration-center
library - add configuration center associated config in application.yml
configuration-center:
application: "customer-api"
environment: "dev"
connectionString: "localhost:2181"
- add annotation
@Config
on bean field for which you want to get value from zookeeper
@RestController
public class TestController {
@Config(value = "name")
private String name;
@RequestMapping("/name")
public String name() {
return name;
}
}
@Config(refresh=true)
supports that real time refresh the field value as long as you update the config in zookeeper.
@RestController
public class TestController {
@Config(value = "name", refresh = true)
private String name;
@RequestMapping("/name")
public String name() {
return name;
}
}
Basically configuration struct in zookeeper consists of four layers.
Top layer is configuration-center namespace, the second layer is different applications, the third layer is application's different environments, the last layer is detailed properties in specific environment.
Configuration center has a complete failover strategy. it does not impact the application even if configuration center is down.
- local memory cache, configuration from zookeeper would be stored in memory.
- local cache folder, application would persistent the memory data into file. when application lost connection with configuration center and application is restarted, since there is no memory cache, application can read config from local cache file.
- local failover folder, normally failover folder is empty. when application has been lost connection with configuration center for a long time, and configurations has been changed a lot, config in local cache file is out of date. in this case, it's able to add config manually in local failover file. and application will ignore cache folder, only read config from failover folder instead.