Setaria 配置管理
Setaria 一套简单的配置管理 API, 可支持文件, 分布式配置实现, 同时支持配置热加载.
在一个项目中参数配置是非常基本也是非常重要的部分, 我们的项目都有各自的实现对配置的处理方式, Setaria 期望能统一配置实现标准. Setaria 支持不同格式的文件配置, 同时会监听文件在变化, 在应用运行时无需重启应用, Setaria 会自动重载配置至内存中. Setaria 也支持分布式配置管理并提供一套配置管理后台应用程序用于运维/开发人员方便的管理分布式配置, 在控制台程序中也可监控客户机的配置状态.
Setaria Client 使用
<dependency>
<groupId>com.weghst.setaria</groupId>
<artifactId>setaria-client</artifactId>
<version>1.2.0</version>
</dependency>
compile 'com.weghst.setaria:setaria-client:1.2.0'
普通 Java 程序中使用 Setaria
SetariaBean setariaBean = new SetariaBean();
setariaBean.addResource("classpath:test-fileSetariaConfig.properties");
setariaBean.addResource("classpath:test-fileSetariaConfig.properties", false);
SetariaConfig setariaConfig = new PropertiesSetariaConfig(setariaBean);
setariaConfig.init();
SetariaConfigContext.setSetariaConfig(setariaConfig);
setariaConfig.destroy();
SetariaBean
是 Setaria 参数配置 Bean. SetariaBean
支持多文件配置, 同时支持忽略不存在的配置文件, Setaria 会监听配置文件的变化, 如果你在应用运行时修改了配置 Setaria 会自动重载配置.
Java Web 程序中使用 Setaria
<context-param>
<description>Setaria 配置实现类</description>
<param-name>setaria.config.implementation</param-name>
<param-value>com.weghst.setaria.client.DistributedSetariaConfig</param-value>
</context-param>
<context-param>
<description>[可选配置] Setaria 配置参数配置文件, 默认寻找 classpath:setaria.json 文件</description>
<param-name>setaria.config.location</param-name>
<param-value>classpath:setaria.json</param-value>
</context-param>
<listener>
<listener-class>com.weghst.setaria.client.web.SetariaConfigContextListener</listener-class>
</listener>
setaria.json
SetariaConfigContextListener
默认会寻找 setaria.json
配置文件来获取 Setaria 初始化所需要的参数.
{
"setaria.config.resources": [
{
"location": "classpath:test-config.json"
},
{
"location": "~/setaria/test-config.json",
"ignoreNotFound": true
}
],
"setaria.config.zookeeper.connectString": "localhost:2181",
"setaria.config.zookeeper.sessionTimeout": 3000,
"setaria.config.zookeeper.basePath": "/setaria",
"setaria.config.zookeeper.app": "pine",
"setaria.config.zookeeper.env": "test"
}
Note
|
setaria.config.resources 是文件配置参数, setaria.config.zookeeper.* 是分布式配置参数, 根据当前的配置模式选择参数. |
Setaria Java API
通过原生的 Java API 获取配置属性值, com.weghst.setaria.client.Configs
是 Setaria 提供获取配置属性值的 Java 原生 API.
Setaria Spring 使用
配置 Setaria 的 BeanFactoryPostProcessor
.
<!--
必须配置 ConfigValueBeanFactoryPostProcessor 才可使用 @ConfigValue @Value 以及 Spring Xml 获取 Setaria 的配置属性值
-->
<bean class="com.weghst.setaria.client.spring.ConfigValueBeanFactoryPostProcessor"/>
@com.weghst.setaria.client.annotation.ConfigValue
由 Setaria 提供的配置属性获取注解, 使用该注解获取配置属性值, 当配置属性值发生变化时 Setaria 会自动更新所对应的 Bean 对象, 同时该注解也支持 Spring 表达式.
@ConfigValue("${samples.first:Default Value}")
private String first;
@org.springframework.beans.factory.annotation.Value
通过 Spring 原生的配置注解获取配置属性值, @Value
与 @ConfigValue
唯一的区别是 @Value
Setaria 不会在运行时*自动更新*配置属性值.
@Value("${samples.first:Default Value}")
private String first;
Spring Xml 获取配置属性值. 通过 Spring Xml 注入的配置属性值不会在运行时*自动更新*其值.
<bean id="springXmlHelloBean" class="com.weghst.setaria.samples.SpringXmlHelloBean">
<property name="first" value="${samples.first}"/>
<property name="second" value="${samples.second}"/>
</bean>