使用disconf的standalone demo程序(基于spring)
目录 com.example.disconf.demo.config 下
- code.properties: 分布式配置文件
- coefficients.properties: 分布式配置文件
- redis.properties: 分布式配置文件
- remote.properties: 分布式配置文件
- empty.properties: 空的分布式配置文件
- static.properties: 静态配置文件示例
- testXml.xml: xml配置文件示例
以 code.properties
举例:
@Service
@DisconfFile(filename = "code.properties", copy2TargetDirPath = "disconf")
public class CodeConfig {
private String codeError = "";
@DisconfFileItem(name = "syserror.paramtype", associateField = "codeError")
public String getCodeError() {
return codeError;
}
public void setCodeError(String codeError) {
this.codeError = codeError;
}
}
com.example.disconf.demo.config.Coefficients.discount
/**
* 金融系数文件
*/
@Service
@DisconfFile(filename = "coefficients.properties")
public class Coefficients {
public static final String key = "discountRate";
@Value(value = "2.0d")
private Double discount;
}
- com.example.disconf.demo.config.JedisConfig: 配置类与回调类 是同一个类的示例
- com.example.disconf.demo.service.callbacks.*: 配置类与配置项 的回调类示例
- RemoteServiceUpdateCallback:
- SimpleRedisServiceUpdateCallback
- TestXmlConfigCallback: XML的回调函数
示例:
/**
* 更新Redis配置时的回调函数
*
* @author liaoqiqi
* @version 2014-6-17
*/
@Service
@Scope("singleton")
@DisconfUpdateService(classes = {JedisConfig.class}, itemKeys = {Coefficients.key})
public class SimpleRedisServiceUpdateCallback implements IDisconfUpdate {
protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleRedisServiceUpdateCallback.class);
@Autowired
private SimpleRedisService simpleRedisService;
/**
*
*/
public void reload() throws Exception {
simpleRedisService.changeJedis();
}
}
看这里 com.example.disconf.demo.task.DisconfDemoTask
- 支持spring注解式(bean)
- 支持静态类(非bean)
需要自动reload的
- autoconfig.properties
- autoconfig2.properties
- myserver_slave.properties
- testJson.json
- testXml2.xml
不需要自动reload的:
-
myserver.properties
classpath:/autoconfig.properties classpath:/autoconfig2.properties classpath:/myserver_slave.properties classpath:/testJson.json testXml2.xml
myserver.properties
- com.example.disconf.demo.service.AutoService
- com.example.disconf.demo.service.AutoService2
- com.example.disconf.demo.service.callbacks.*:
- AutoServiceCallback:
- TestJsonConfigCallback
示例:
/**
* 这是 autoconfig.properties 的回调函数类
* <p/>
* Created by knightliao on 15/3/21.
*/
@Service
@DisconfUpdateService(confFileKeys = {"autoconfig.properties", "autoconfig2.properties"})
public class AutoServiceCallback implements IDisconfUpdate {
protected static final Logger LOGGER = LoggerFactory.getLogger(AutoServiceCallback.class);
@Autowired
private AutoService autoService;
@Override
public void reload() throws Exception {
LOGGER.info("reload callback " + "autoconfig.properties or autoconfig2.properties" + autoService.getAuto());
}
}
mvn clean package
➜ disconf-standalone-demo git:(dev) ✗ cd target/starter-run
➜ starter-run git:(dev) ✗ ll
total 42680
-rw-r--r-- 1 knightliao staff 21831639 6 8 14:36 disconf-standalone-demo.jar
-rw-r--r-- 1 knightliao staff 1431 6 8 14:36 disconf.properties
-rw-r--r-- 1 knightliao staff 417 6 8 14:36 env
-rw-r--r-- 1 knightliao staff 1493 6 8 14:36 logback.xml
-rw-r--r-- 1 knightliao staff 1037 6 8 14:36 start.sh
-rw-r--r-- 1 knightliao staff 532 6 8 14:36 stop.sh
➜ starter-run git:(dev) ✗ sh start.sh
nohup java -server -Xms1024m -Xmx1024m -Xmn448m -Xss256K -XX:MaxPermSize=128m -XX:ReservedCodeCacheSize=64m -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=2 -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Dlogback.configurationFile=file:logback.xml -jar disconf-standalone-demo.jar >> log_1465367799.log 2>&1 &
➜ starter-run git:(dev) ✗ tail -f log_1465367799.log
disConfCommonModel=DisConfCommonModel [app=disconf_demo, version=1_0_0_0, env=rd]
disconfCommonCallbackModel=DisconfCommonCallbackModel{disconfConfUpdates=[com.example.disconf.demo.service.callbacks.SimpleRedisServiceUpdateCallback@50b7ae59], disconfUpdatesActiveBackups=[]}]]