Skip to content

Commit

Permalink
re
Browse files Browse the repository at this point in the history
  • Loading branch information
knightliao committed Jul 1, 2016
2 parents fdfc567 + 9a8d48d commit d8b6d6e
Show file tree
Hide file tree
Showing 79 changed files with 883 additions and 334 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Distributed Configuration Management Platform(分布式配置管理平台)

https://github.com/knightliao/disconf-demos-java

## 开发协作

请在dev上进行提交代码

## 项目信息

- CLIENT 端:
Expand All @@ -35,8 +39,8 @@ https://github.com/knightliao/disconf-demos-java

disconf.git branches and Maven version:

- dev(develop branch): 2.6.35-SNAPSHOT
- master(stable branch):2.6.34
- dev(develop branch): 2.6.35
- master(stable branch):2.6.35
- [更新日志](https://github.com/knightliao/disconf/wiki/updates)
- 在Maven Central Repository里查看 [com.baidu.disconf](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.baidu.disconf%22 )

Expand Down Expand Up @@ -181,8 +185,7 @@ APP+环境+版本+ZK查询:

## 群·联系·讨论

- disconf技术QQ群: 239203866
- disconf技术QQ二群: 280712860
- disconf技术QQ群: 239203866 ; disconf技术QQ二群: 280712860
- [媒体报道与网友教程](https://github.com/knightliao/disconf/wiki/%E5%AA%92%E4%BD%93%E6%8A%A5%E9%81%93%E4%B8%8E%E7%BD%91%E5%8F%8B%E6%95%99%E7%A8%8B)

## 关于
Expand Down
2 changes: 1 addition & 1 deletion disconf-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-base</artifactId>
<version>2.6.34</version>
<version>2.6.35</version>
</parent>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringValueResolver;

/**
* 具有 reloadable 的 property bean
Expand Down Expand Up @@ -388,6 +389,14 @@ protected void processProperties(ConfigurableListableBeanFactory beanFactoryToPr
}
}
}

StringValueResolver stringValueResolver = new PlaceholderResolvingStringValueResolver(props);

// New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
beanFactoryToProcess.resolveAliases(stringValueResolver);

// New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
beanFactoryToProcess.addEmbeddedValueResolver(stringValueResolver);
}

/**
Expand Down Expand Up @@ -449,6 +458,23 @@ protected String resolveStringValue(String strVal) throws BeansException {
}
}

/**
*
*/
protected class PlaceholderResolvingStringValueResolver implements StringValueResolver {

private final Properties props;

public PlaceholderResolvingStringValueResolver(Properties props) {
this.props = props;
}

@Override
public String resolveStringValue(String strVal) throws BeansException {
return parseStringValue(strVal, this.props, new HashSet());
}
}

/**
* the application context is needed to find the beans again during reconfiguration
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class DisconfCenterFile extends DisconfCenterBaseModel {
// 文件名
private String fileName;

// 复制到指定的路径下
private String copy2TargetDirPath;

// 文件类型
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ public static void verifyUserConfig() throws Exception {
}
}
DisClientConfig.getInstance().setIgnoreDisconfKeySet(keySet);
LOGGER.info("SERVER ignore: " + DisClientConfig.getInstance().getIgnoreDisconfKeySet());
LOGGER.info("SERVER disconf.ignore: " + DisClientConfig.getInstance().getIgnoreDisconfKeySet());

// 重试
LOGGER.debug("SERVER conf_server_url_retry_times: " + DisClientConfig
LOGGER.debug("SERVER disconf.conf_server_url_retry_times: " + DisClientConfig
.getInstance().CONF_SERVER_URL_RETRY_TIMES);

LOGGER.debug("SERVER conf_server_url_retry_sleep_seconds: " +
LOGGER.debug("SERVER disconf.conf_server_url_retry_sleep_seconds: " +
DisClientConfig.getInstance().confServerUrlRetrySleepSeconds);

LOGGER.debug("SERVER disconf.enable_local_download_dir_in_class_path: " + DisClientConfig
.getInstance().enableLocalDownloadDirInClassPath);
// 是否将文件放在classpath目录下
if (DisClientConfig.getInstance().enableLocalDownloadDirInClassPath) {

Expand Down Expand Up @@ -141,21 +143,23 @@ public static void verifySysConfig() throws Exception {

throw new Exception("settings: CONF_SERVER_STORE_ACTION cannot find");
}
LOGGER.debug("SERVER CONF_SERVER_STORE_ACTION: " + DisClientSysConfig.getInstance().CONF_SERVER_STORE_ACTION);
LOGGER.debug("SERVER disconf.conf_server_store_action: " + DisClientSysConfig
.getInstance().CONF_SERVER_STORE_ACTION);

// CONF_SERVER_ZOO_ACTION
if (StringUtils.isEmpty(DisClientSysConfig.getInstance().CONF_SERVER_ZOO_ACTION)) {

throw new Exception("settings: CONF_SERVER_ZOO_ACTION cannot find");
}
LOGGER.debug("SERVER CONF_SERVER_ZOO_ACTION: " + DisClientSysConfig.getInstance().CONF_SERVER_ZOO_ACTION);
LOGGER.debug(
"SERVER disconf.conf_server_zoo_action: " + DisClientSysConfig.getInstance().CONF_SERVER_ZOO_ACTION);

// CONF_SERVER_MASTER_NUM_ACTION
if (StringUtils.isEmpty(DisClientSysConfig.getInstance().CONF_SERVER_MASTER_NUM_ACTION)) {

throw new Exception("settings: CONF_SERVER_MASTER_NUM_ACTION cannot find");
}
LOGGER.debug("SERVER CONF_SERVER_MASTER_NUM_ACTION Action URL: " +
LOGGER.debug("SERVER disconf.conf_server_master_num_action: " +
DisClientSysConfig.getInstance().CONF_SERVER_MASTER_NUM_ACTION);

//
Expand All @@ -167,7 +171,7 @@ public static void verifySysConfig() throws Exception {
}

// LOCAL_DOWNLOAD_DIR
LOGGER.debug("SERVER LOCAL_DOWNLOAD_DIR: " + DisClientSysConfig.getInstance().LOCAL_DOWNLOAD_DIR);
LOGGER.debug("SERVER disconf.local_download_dir: " + DisClientSysConfig.getInstance().LOCAL_DOWNLOAD_DIR);
OsUtil.makeDirs(DisClientSysConfig.getInstance().LOCAL_DOWNLOAD_DIR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ public void storeOneFile(DisconfCenterBaseModel disconfCenterBaseModel) {

if (confFileMap.containsKey(fileName)) {

LOGGER.error("There are two same fileName key!!!! " + "first: " + confFileMap.get(fileName).toString() +
"\n, Second: " + disconfCenterFile.toString());
LOGGER.warn("There are two same fileName key!!!! " + fileName);
DisconfCenterFile existCenterFile = confFileMap.get(fileName);

// 如果是 同时使用了 注解式 和 非注解式 两种方式,则当修改时也要 进行 XML 式 reload
if (disconfCenterFile.isTaggedWithNonAnnotationFile()) {
existCenterFile.setIsTaggedWithNonAnnotationFile(true);
}

} else {
confFileMap.put(fileName, disconfCenterFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,10 @@ public void inject2Store(String fileName, DisconfValue disconfValue) {
}
}

// 注解式
// 使用过 XML式配置
if (disconfCenterFile.isTaggedWithNonAnnotationFile()) {

//
// 非注解式 或者 注解式的域集合为空,则将 文件内容写到配置库里
//

if (disconfCenterFile.getObject() == null &&
disconfCenterFile.getSupportFileTypeEnum().equals(SupportFileTypeEnum.PROPERTIES)) {
if (disconfCenterFile.getSupportFileTypeEnum().equals(SupportFileTypeEnum.PROPERTIES)) {
// 如果是采用XML进行配置的,则需要利用spring的reload将数据reload到bean里
ReloadConfigurationMonitor.reload();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,77 @@
package com.baidu.disconf.client.test.support.utils;

import org.junit.Test;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

/**
* TestAntPathMatcher
* Created by knightliao on 15/11/13.
* 两种较为通用的spring 资源访问方式
*/
public class TestAntPathMatcher extends TestCase {
public class TestAntPathMatcher {

@Test
public void testUrlMatch() {

PathMatcher matcher = new AntPathMatcher();

// 完全路径url方式路径匹配
// String requestPath="http://localhost:8080/pub/login.jsp"; //请求路径
public void ResourceLoaderTest() throws Exception {
/* 资源地址表达式
* classpath:相对于类的根路径,可访问jar或zip中的资源哦
* classpath*:和上面类似,只不过上面是加载找到的第一个资源,这个是全部加载
* file:文件系统目录中加载,可以是绝对,也可以是相对
* http:// 不用多说了吧
* ftp:// 不用多说了吧
*
* ant风格:可以使用通配符
* ?:匹配一个字符
* *:匹配多个字符
* **:匹配多层路径
* */
ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
Resource[] rs = rpr.getResources("classpath:testXml.xml");
for (Resource one : rs) {
showResourceInfo(one, true);
}
System.out.println("=============================");

// String patternPath="**/login.jsp"; //路径匹配模式
//file:访问文件系统(绝对 和 相对路径方式)
//绝对路径 类似于FileSystemResource
rs = rpr.getResources("file:src/test/resources/res/testXml.xml");
//相对路径 相对于当前项目路径
//rs=rpr.getResources("file:src/aop.xml");
for (Resource one : rs) {
showResourceInfo(one, true);
}
System.out.println("=============================");

// 不完整路径uri方式路径匹配
// String requestPath="/app/pub/login.do"; //请求路径
// String patternPath="/**/login.do"; //路径匹配模式
// 模糊路径方式匹配
// String requestPath="/app/pub/login.do"; //请求路径
// String patternPath="/**/*.do"; //路径匹配模式
// 包含模糊单字符路径匹配

String requestPath = "/app/pub/login.do"; // 请求路径
String patternPath = "/**/lo?in.do"; // 路径匹配模式
//http:方式
rs = rpr.getResources("http://www.baidu.com/img/bdlogo.gif");
//为了测试的简便,这里直接取第一个资源
byte[] gifByte = IOUtils.toByteArray(rs[0].getInputStream());
FileUtils.writeByteArrayToFile(new File("tmp/bdlogo1.gif"), gifByte);
}

boolean result = matcher.match(patternPath, requestPath);
assertTrue(result);
/**
* 公共的现实文件信息方法
*
* @param one spring resource对象
* @param isShowContent 是否打印文件内容(非文本下不建议使用)
*/
private void showResourceInfo(Resource one, boolean isShowContent) {
try {
System.out.println("文件名称:" + one.getFilename());
System.out.println("是否存在:" + one.exists());
if (isShowContent) {
System.out.println("文件绝对路径:" + one.getFile().getAbsolutePath());
System.out.println("文件内容:");
System.out.println(IOUtils.toString(one.getInputStream(), "GB2312"));
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
7 changes: 7 additions & 0 deletions disconf-client/src/test/resources/res/testXml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
2 changes: 1 addition & 1 deletion disconf-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-base</artifactId>
<version>2.6.34</version>
<version>2.6.35</version>
</parent>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.baidu.disconf.core.common.path;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.baidu.disconf.core.common.constants.Constants;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static String getRemoteUrlParameter(String urlPrefix, String app, String
*/
private static Map<String, String> getConfServerBasePathMap(String app, String version, String env, String key) {

Map<String, String> parameterMap = new HashMap<String, String>();
Map<String, String> parameterMap = new LinkedHashMap<String, String>();

parameterMap.put(Constants.VERSION, version);
parameterMap.put(Constants.APP, app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ private FileUtils() {

/**
* 关闭文件流
*
* @param w 上午10:27:27 created by Darwin(Tianxin)
*/
public static final void closeWriter(Writer w) {
if (w != null) {
Expand All @@ -41,8 +39,6 @@ public static final void closeWriter(Writer w) {

/**
* 关闭文件流
*
* @param r 上午10:27:27 created by Darwin(Tianxin)
*/
public static final void closeReader(Reader r) {
if (r != null) {
Expand All @@ -56,8 +52,6 @@ public static final void closeReader(Reader r) {

/**
* 关闭文件流
*
* @param os 上午10:27:27 created by Darwin(Tianxin)
*/
public static final void closeOutputStream(OutputStream os) {
if (os != null) {
Expand All @@ -71,8 +65,6 @@ public static final void closeOutputStream(OutputStream os) {

/**
* 关闭文件流
*
* @param is 上午10:27:27 created by Darwin(Tianxin)
*/
public static final void closeInputStream(InputStream is) {
if (is != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static String getHostName() throws Exception {

} catch (UnknownHostException e) {

throw new Exception();
throw new Exception(e);
}
}

Expand All @@ -49,7 +49,7 @@ public static String getHostIp() throws Exception {

} catch (UnknownHostException e) {

throw new Exception();
throw new Exception(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public void getZooHostsUrlTest() {
public void getRemoteUrlParameterTest() {

String fileUrl =
DisconfWebPathMgr.getRemoteUrlParameter("test", "app", "version", "env", "key", DisConfigTypeEnum.FILE);
DisconfWebPathMgr.getRemoteUrlParameter("test", "app", "version", "env", "key", DisConfigTypeEnum.FILE);
System.out.println(fileUrl);
Assert.assertEquals("test/file?app=app&env=env&type=0&key=key&version=version", fileUrl);
Assert.assertEquals("test/file?version=version&app=app&env=env&key=key&type=0", fileUrl);

String itemUrl =
DisconfWebPathMgr.getRemoteUrlParameter("test", "app", "version", "env", "key", DisConfigTypeEnum.ITEM);
DisconfWebPathMgr.getRemoteUrlParameter("test", "app", "version", "env", "key", DisConfigTypeEnum.ITEM);
System.out.println(fileUrl);
Assert.assertEquals("test/item?app=app&env=env&type=1&key=key&version=version", itemUrl);
Assert.assertEquals("test/item?version=version&app=app&env=env&key=key&type=1", itemUrl);
}
}
Loading

0 comments on commit d8b6d6e

Please sign in to comment.