Setaria Server Core Project

Setaria 分布式统一配置服务端核心功能程序

License

License

GroupId

GroupId

com.weghst.setaria
ArtifactId

ArtifactId

setaria-core
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Setaria Server Core Project
Setaria 分布式统一配置服务端核心功能程序
Project URL

Project URL

https://github.com/weghst/setaria
Source Code Management

Source Code Management

https://github.com/weghst/setaria

Download setaria-core

How to add to project

<!-- https://jarcasting.com/artifacts/com.weghst.setaria/setaria-core/ -->
<dependency>
    <groupId>com.weghst.setaria</groupId>
    <artifactId>setaria-core</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.weghst.setaria/setaria-core/
implementation 'com.weghst.setaria:setaria-core:1.0.0'
// https://jarcasting.com/artifacts/com.weghst.setaria/setaria-core/
implementation ("com.weghst.setaria:setaria-core:1.0.0")
'com.weghst.setaria:setaria-core:jar:1.0.0'
<dependency org="com.weghst.setaria" name="setaria-core" rev="1.0.0">
  <artifact name="setaria-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.weghst.setaria', module='setaria-core', version='1.0.0')
)
libraryDependencies += "com.weghst.setaria" % "setaria-core" % "1.0.0"
[com.weghst.setaria/setaria-core "1.0.0"]

Dependencies

compile (16)

Group / Artifact Type Version
org.slf4j : jcl-over-slf4j jar 1.7.12
org.apache.curator : curator-recipes jar 2.9.1
org.flywaydb : flyway-core jar 4.0
org.mybatis.scripting : mybatis-freemarker jar 1.1.1
org.aspectj : aspectjweaver jar 1.8.6
com.google.guava : guava jar 19.0
org.springframework : spring-jdbc jar 4.2.3.RELEASE
org.slf4j : slf4j-api jar 1.7.12
commons-codec : commons-codec jar 1.10
org.springframework : spring-context-support jar 4.2.3.RELEASE
org.springframework : spring-context jar 4.2.3.RELEASE
com.fasterxml.jackson.core : jackson-databind jar 2.6.5
org.apache.commons : commons-lang3 jar 3.4
org.glassfish : javax.json jar 1.0.4
org.mybatis : mybatis-spring jar 1.2.3
com.google.code.findbugs : jsr305 jar 3.0.0

test (6)

Group / Artifact Type Version
org.mockito : mockito-core jar 1.10.19
org.springframework : spring-test jar 4.2.3.RELEASE
com.alibaba : druid jar 1.0.16
mysql : mysql-connector-java jar 5.1.37
ch.qos.logback : logback-classic jar 1.1.3
org.testng : testng jar 6.9.6

Project Modules

There are no modules declared in this project.

Setaria 配置管理

Setaria 一套简单的配置管理 API, 可支持文件, 分布式配置实现, 同时支持配置热加载.

在一个项目中参数配置是非常基本也是非常重要的部分, 我们的项目都有各自的实现对配置的处理方式, Setaria 期望能统一配置实现标准. Setaria 支持不同格式的文件配置, 同时会监听文件在变化, 在应用运行时无需重启应用, Setaria 会自动重载配置至内存中. Setaria 也支持分布式配置管理并提供一套配置管理后台应用程序用于运维/开发人员方便的管理分布式配置, 在控制台程序中也可监控客户机的配置状态.

Setaria Client 使用

Maven
<dependency>
  <groupId>com.weghst.setaria</groupId>
  <artifactId>setaria-client</artifactId>
  <version>1.2.0</version>
</dependency>
Gradle
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>

Versions

Version
1.0.0