Diamond 配置生成

/**
* ===============================================================================
* The Shenma Internet Financial Information Software License
*
* Copyright (c) 2017 Shenma Internet Financial Information Service Co., Ltd.
*
* @Company:  xushenjk www.xushen.com
*
* @Project: shenma-samp-server

* @Author:  Kaiyun.Fu
* @Created at 2017年12月2日下午9:06:52
* ===============================================================================
*/

package com.shenm.account.server.test;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

/**
*
* @author gaohailiang
* @Created at 2017年12月2日下午9:06:52
*/
@Slf4j
public class ResourceTest {

@Test 
     public void testClasspathAsteriskPrefix()throws IOException{

         ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
        
         //将加载多个绝对匹配的所有Resource 
         //将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分 
         //然后进行遍历模式匹配 
         // classpath*:*-test.properties
         //Resource[] resources = (Resource[]) resolver.getResources("classpath*:META-INF/INDEX.LIST");  
        // Resource[] resources = (Resource[]) resolver.getResources("classpath*:*-test.properties");  
         Resource[] resources = resolver.getResources("classpath*:application-*.properties");
         for(Resource resource:resources){
        log.info(resource.getFilename());
         }
        // Assert.assertTrue(resources.length > 1);   
         //将加载多个模式匹配的Resource 
        // resources = (Resource[]) resolver.getResources("classpath*:META-INF/*.LIST"); 
       //  Assert.assertTrue(resources.length > 1);    
         
     } 

private static final String SAMP = "shenma-samp";
private static final String EVN = "application-production";

/**
  * 配置文件转Json 用于上传到配置中心
* @throws IOException
*/
@Test
public void printJson() throws IOException{
ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:"+EVN+".properties");
        
// Properties propertie = new Properties();

         List<Properties> properties = new ArrayList<Properties>();
for (Resource rsrc : resources) {
log.info("Filename = "+rsrc.getFilename());
log.info("Description="+rsrc.getDescription());
// if(!rsrc.getDescription().contains(SAMP)){
// log.info(rsrc.getFilename()+ "不是  SAMP 工程配置文件,不解析.--"+rsrc.getDescription());
// continue;
// }
EncodedResource enrsrc = new EncodedResource(rsrc);
Properties tmpProperties = PropertiesLoaderUtils.loadProperties(enrsrc);
properties.add(tmpProperties);
}
List<XdiaConfig> list = new ArrayList<XdiaConfig>();
for (Properties p : properties) {
Enumeration<?> enu = p.propertyNames();
while (enu.hasMoreElements()) {
String key = (String) enu.nextElement();
XdiaConfig xc = new XdiaConfig(key, p.getProperty(key), key);
list.add(xc);
//propertie.setProperty(key, p.getProperty(key));
}
}
log.info("配置中心初始化全量Key Value:"+ JSON.toJSONString(list));
}
}

class XdiaConfig {

private String key;

private String value;

private String description;

/**
* @param key
* @param value
*/
public XdiaConfig(String key, String value, String description) {
super();
this.key = key;
this.value = value;
this.description = description;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}

spring 配置

<bean id="xDiamondConfig"
class="io.github.xdiamond.client.spring.XDiamondConfigFactoryBean">
<property name="serverHost" value="${com.shenma.xdiamond.serverHost}" />
<property name="serverPort" value="${com.shenma.xdiamond.serverPort}" />
<property name="groupId" value="${com.shenma.xdiamond.groupId}" />
<property name="artifactId" value="${com.shenma.xdiamond.artifactId}" />
<property name="version" value="${com.shenma.xdiamond.version}" />
<property name="bSyncToSystemProperties" value="${com.shenma.xdiamond.bSyncToSystemProperties}"/>
<property name="profile" value="${com.shenma.xdiamond.profile}" />
<property name="secretKey" value="${com.shenma.xdiamond.secretKey}"></property>
</bean>

猜你喜欢

转载自terry0501.iteye.com/blog/2413187