Qconf配置管理工具-Java使用文档
前一篇文章已经介绍如何安装和基于命令行的方式使用QConf,这篇文章主要介绍一下如何生成Java客户端,以及怎样与Java工程进行结合使用,官方说明地址:——QConf Java Doc。
- QConf支持的客户端有:
- C++
- Go
- Java
- Lua
- Perl
- Php
- Python
- Shell
编译Java客户端
前提条件:已安装JDK(建议使用1.7版本)及设置Java环境变量
进入Java对应的编译目录
cd /opt/QConf-1.1.0/driver/java
根据自己的JDK版本修改Makefile文件
vim Makefile
'''根据自己的JDK路径进行修改'''
#PARAM
CC = g++
JNI_DIR = /usr/java/jdk1.7.0_55/include/ #将jdk1.7.0_55替换为自己的jdk版本
JNI_MD_DIR = /usr/java/jdk1.7.0_55/include/linux/ #将jdk1.7.0_55替换为自己的jdk版本
QCONF_HEAD_DIR = /usr/local/include/qconf
LIBOBJS = /usr/local/qconf/lib/libqconf.a
编译并生成jar文件
make
执行编译后报错了,以下为报错提示
mv libqconf_java.so qconf_jar
ant -buildfile qconf_jar/build.xml
make: ant: Command not found
make: *** [clean] Error 127
机器没有安装ant,QConf编译Java客户端应该是使用ant工具进行编译的,因此需要安装一下ant:
ANT的安装非常简单,去找度娘吧
再次执行make编译,下图可以看到jar包已经生成了;
到此顺利编译出jar包了;
Java Api使用介绍
项目开发一直在使用maven集成环境,并且有nexus私服,因此将生成的jar包上传到公司内部私服中,项目pom进行引入;
- API
String getConf(String path, String idc);
- 根据idc获取对应的配置信息
ArrayList<String> getBatchKeys(String path, String idc);
- 获取对应目录下的所有节点key
Map<String, String> getBatchConf(String path, String idc);
- 获取对应目录下的所有节点key与value
官方API说明:
API Doc
QConf access functions
getConf
String getConf(String path, String idc)
Description
get configure value
Parameters
path - key of configuration.
idc - Optional,from which idc to get the value,get from local idc if omit
Return Value
value of the configuation, throw Exception net.qihoo.qconf.QconfException if failed
Example
String value = Qconf.getConf("demo/confs");
getBatchKeys()
ArrayList<String> getBatchKeys(String path, String idc);
Description
get all children nodes'key
Parameters
path - key of configuration.
idc - Optional,from which idc to get the keys,get from local idc if omit
Return Value
python list of the node's keys, throw Exception net.qihoo.qconf.QconfException if failed
Example
ArrayList keys = Qconf.getBatchKeys("demo/confs");
getBatchConf
Map<String, String> getBatchConf(String path, String idc);
Description
get all children nodes' key and value
Parameters
path - key of configuration.
idc - Optional, from which idc to get the children configurations,get from local idc if omit
Return Value
python dictionary of the children configuration, throw Exception net.qihoo.qconf.QconfException if failed
Example
Map confs = Qconf.getBatchConf("demo/confs");
getAllHost
ArrayList<String> getAllHost(String key, String idc);
Description
get all available services under given key
Parameters
path - key of configuration. idc - Optional, from which idc to get the services,get from local idc if omit
Return Value
python list of all available services, throw Exception net.qihoo.qconf.QconfException if failed
Example
ArrayList hosts = Qconf.getAllHost("demo/hosts");
getHost
String host = Qconf.getHost(String key, String idc);
Description
get one available service
Parameters
path - key of configuration.
idc - Optional,from which idc to get the host,get from local idc if omit
Return Value
available host, throw Exception net.qihoo.qconf.QconfException if failed
Example
String host = Qconf.getHost("demo/hosts");