Commit 64a7f9fe by qiuweili123

增加durid扩展属性

parent 08950868
...@@ -5,13 +5,17 @@ import com.secoo.mall.common.core.errorcode.CommonErrorCode; ...@@ -5,13 +5,17 @@ import com.secoo.mall.common.core.errorcode.CommonErrorCode;
import com.secoo.mall.common.core.exception.BusinessException; import com.secoo.mall.common.core.exception.BusinessException;
import com.secoo.mall.common.core.exception.SystemInternalException; import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.util.json.FastJsonUtil; import com.secoo.mall.common.util.json.FastJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map; import java.util.Map;
@Slf4j
public class BeanUtil extends BeanUtils { public class BeanUtil extends BeanUtils {
private static final String[] DEFUALT_IGNORE_KEY = {"class", "empty"}; private static final String[] DEFUALT_IGNORE_KEY = {"class", "empty"};
...@@ -54,8 +58,37 @@ public class BeanUtil extends BeanUtils { ...@@ -54,8 +58,37 @@ public class BeanUtil extends BeanUtils {
try { try {
copyProperties(dest, orig); copyProperties(dest, orig);
} catch (Exception e) { } catch (Exception e) {
log.error("copyProps e:", e);
throw new BusinessException(CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION); throw new BusinessException(CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION);
} }
} }
public static void copyProps(Object dest, Object orig, boolean ignoreNullFlag) {
PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(orig, name) &&
PropertyUtils.isWriteable(dest, name)) {
try {
Object value = PropertyUtils.getProperty(orig, name);
if (ignoreNullFlag) {
if (value != null) {
copyProperty(dest, name, value);
}
} else {
copyProperty(dest, name, value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
}
} }
...@@ -2,6 +2,8 @@ package com.secoo.mall.datasource.config; ...@@ -2,6 +2,8 @@ package com.secoo.mall.datasource.config;
import lombok.Data; import lombok.Data;
import java.util.Properties;
@Data @Data
public class MatrixDataSourceConfig implements DataSourceConfig { public class MatrixDataSourceConfig implements DataSourceConfig {
...@@ -22,6 +24,7 @@ public class MatrixDataSourceConfig implements DataSourceConfig { ...@@ -22,6 +24,7 @@ public class MatrixDataSourceConfig implements DataSourceConfig {
private long idleTimeout = IDLE_TIMEOUT; private long idleTimeout = IDLE_TIMEOUT;
private long maxLifetime = MAX_LIFETIME; private long maxLifetime = MAX_LIFETIME;
public void setName(String name) { public void setName(String name) {
this.name = POOL_NAME_PRIFIX + name; this.name = POOL_NAME_PRIFIX + name;
} }
...@@ -76,4 +79,5 @@ public class MatrixDataSourceConfig implements DataSourceConfig { ...@@ -76,4 +79,5 @@ public class MatrixDataSourceConfig implements DataSourceConfig {
this.maxPoolSize = maxPoolSize; this.maxPoolSize = maxPoolSize;
} }
} }
...@@ -7,6 +7,8 @@ import lombok.Data; ...@@ -7,6 +7,8 @@ import lombok.Data;
public class MatrixDataSourceProperties extends MatrixDataSourceConfig { public class MatrixDataSourceProperties extends MatrixDataSourceConfig {
public static final String PREFIX = "spring.datasource.matrix"; public static final String PREFIX = "spring.datasource.matrix";
public MatrixDataSourceProperties() { public MatrixDataSourceProperties() {
} }
......
...@@ -15,10 +15,8 @@ import lombok.extern.slf4j.Slf4j; ...@@ -15,10 +15,8 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.reflect.MethodUtils; import org.apache.commons.lang3.reflect.MethodUtils;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.HashMap; import java.lang.reflect.Field;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -26,6 +24,7 @@ import java.util.stream.Collectors; ...@@ -26,6 +24,7 @@ import java.util.stream.Collectors;
*/ */
@Slf4j @Slf4j
public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends AbsDataSourceProvider<T> { public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends AbsDataSourceProvider<T> {
/** /**
* 从appollo加载配置信息 * 从appollo加载配置信息
* *
...@@ -47,6 +46,8 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends ...@@ -47,6 +46,8 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends
Map<String, List<String>> propertyMap = propertyNames.stream().filter(pro -> pro.contains(MatrixDataSourceProperties.PREFIX)).map(pro -> pro.replace(MatrixDataSourceProperties.PREFIX, "")) Map<String, List<String>> propertyMap = propertyNames.stream().filter(pro -> pro.contains(MatrixDataSourceProperties.PREFIX)).map(pro -> pro.replace(MatrixDataSourceProperties.PREFIX, ""))
.collect(Collectors.groupingBy(str -> str.split(DataSourceConstant.PROPETY_SPLIT_CHAR)[1])); .collect(Collectors.groupingBy(str -> str.split(DataSourceConstant.PROPETY_SPLIT_CHAR)[1]));
Map<String, Class> fieldMap = getFieldMap();
try { try {
for (Map.Entry<String, List<String>> entry : propertyMap.entrySet()) { for (Map.Entry<String, List<String>> entry : propertyMap.entrySet()) {
String dsName = entry.getKey(); String dsName = entry.getKey();
...@@ -54,11 +55,15 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends ...@@ -54,11 +55,15 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends
matrixDataSourceProperties.setName(dsName); matrixDataSourceProperties.setName(dsName);
for (String property : entry.getValue()) { for (String property : entry.getValue()) {
String propertyFullPath = MatrixDataSourceProperties.PREFIX + property; String propertyFullPath = MatrixDataSourceProperties.PREFIX + property;
String value = decryptPropertyItemValue(appConfig.getProperty(propertyFullPath, "")); Object value = decryptPropertyItemValue(appConfig.getProperty(propertyFullPath, ""));
//得到实际的属性,首字母大写 //得到实际的属性,首字母大写
String beanProperty = StringUtil.capitalize(property.split(DataSourceConstant.PROPETY_SPLIT_CHAR)[2]); String beanProperty = property.split(DataSourceConstant.PROPETY_SPLIT_CHAR)[2];
//转换为真实类型数据
if(fieldMap.containsKey(beanProperty)){
value = MethodUtils.invokeExactStaticMethod(fieldMap.get(beanProperty), "valueOf", value);
}
MethodUtils.invokeExactMethod(matrixDataSourceProperties, "set" + beanProperty, value); MethodUtils.invokeExactMethod(matrixDataSourceProperties, "set" + StringUtil.capitalize(beanProperty), value);
} }
log.info("init datasource name:{}", dsName); log.info("init datasource name:{}", dsName);
list.add(matrixDataSourceProperties); list.add(matrixDataSourceProperties);
...@@ -77,4 +82,22 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends ...@@ -77,4 +82,22 @@ public class ApolloDataSourceProvider<T extends MatrixDataSourceConfig> extends
String acccessToken = appConfig.getProperty("config.app.security.accessToken", "default"); String acccessToken = appConfig.getProperty("config.app.security.accessToken", "default");
SysUtil.setProperty("accessToken", acccessToken); SysUtil.setProperty("accessToken", acccessToken);
} }
private Map<String, Class> getFieldMap() {
List<Field> fieldList = new ArrayList<>();
Class tempClass = getEntryClass();
while (tempClass != null) {
fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
tempClass = tempClass.getSuperclass();
}
Map<String, Class> map = new HashMap<>();
//过滤String
fieldList = fieldList.stream().filter(field -> !field.getType().getSimpleName().contains("String")).collect(Collectors.toList());
for (Field field : fieldList) {
map.put(field.getName(), field.getType());
}
return map;
}
} }
package com.secoo.mall.datasource.factory; package com.secoo.mall.datasource.factory;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.secoo.mall.common.util.bean.BeanUtil;
import com.secoo.mall.datasource.properties.MatrixDruidDataSourceProperties; import com.secoo.mall.datasource.properties.MatrixDruidDataSourceProperties;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -13,6 +14,7 @@ public class DruidDataSourceFactory extends AbsDataSourceFactory<DruidDataSource ...@@ -13,6 +14,7 @@ public class DruidDataSourceFactory extends AbsDataSourceFactory<DruidDataSource
@Override @Override
protected DruidDataSource convertAndCreate(MatrixDruidDataSourceProperties config) { protected DruidDataSource convertAndCreate(MatrixDruidDataSourceProperties config) {
DruidDataSource dataSource = new DruidDataSource(); DruidDataSource dataSource = new DruidDataSource();
BeanUtil.copyProps(dataSource, config, true);
dataSource.setName(config.getName()); dataSource.setName(config.getName());
dataSource.setUrl(config.getUrl()); dataSource.setUrl(config.getUrl());
dataSource.setUsername(config.getUsername()); dataSource.setUsername(config.getUsername());
...@@ -22,7 +24,6 @@ public class DruidDataSourceFactory extends AbsDataSourceFactory<DruidDataSource ...@@ -22,7 +24,6 @@ public class DruidDataSourceFactory extends AbsDataSourceFactory<DruidDataSource
dataSource.setPhyTimeoutMillis(config.getConnectionTimeout()); dataSource.setPhyTimeoutMillis(config.getConnectionTimeout());
dataSource.setTimeBetweenEvictionRunsMillis(config.getIdleTimeout()); dataSource.setTimeBetweenEvictionRunsMillis(config.getIdleTimeout());
dataSource.setMaxEvictableIdleTimeMillis(config.getMaxLifetime()); dataSource.setMaxEvictableIdleTimeMillis(config.getMaxLifetime());
try { try {
dataSource.init(); dataSource.init();
} catch (SQLException e) { } catch (SQLException e) {
......
package com.secoo.mall.datasource.properties; package com.secoo.mall.datasource.properties;
import com.alibaba.druid.pool.DruidAbstractDataSource;
import lombok.Data; import lombok.Data;
import java.util.Properties;
@Data @Data
public class MatrixDruidDataSourceProperties extends MatrixDataSourceProperties { public class MatrixDruidDataSourceProperties extends MatrixDataSourceProperties {
public static final String PREFIX = MatrixDataSourceProperties.PREFIX + ".druid"; public static final String PREFIX = MatrixDataSourceProperties.PREFIX + ".druid";
private Long timeBetweenEvictionRunsMillis= DruidAbstractDataSource.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
private Long timeBetweenLogStatsMillis;
private Integer statSqlMaxSize;
private Long minEvictableIdleTimeMillis;
private Long maxEvictableIdleTimeMillis;
private Boolean testWhileIdle;
private Boolean testOnBorrow;
private Boolean testOnReturn;
private String validationQuery;
private Integer validationQueryTimeout;
private Boolean useGlobalDataSourceStat;
private Boolean asyncInit;
private String filters;
private Boolean clearFiltersEnable;
private Boolean resetStatEnable;
private Integer notFullTimeoutRetryCount;
private Integer maxWaitThreadCount;
private Boolean failFast;
private Long phyTimeoutMillis;
private Boolean keepAlive;
private Boolean poolPreparedStatements;
private Boolean initVariants;
private Boolean initGlobalVariants;
private Boolean useUnfairLock;
private Boolean killWhenSocketReadTimeout;
private Properties connectionProperties;
private Integer maxPoolPreparedStatementPerConnectionSize;
private String initConnectionSqls;
private Boolean sharePreparedStatements=false;
private Integer connectionErrorRetryAttempts=1;
private Boolean breakAfterAcquireFailure;
private Boolean removeAbandoned;
private Integer removeAbandonedTimeoutMillis;
private Boolean logAbandoned;
private String publicKey;
} }
...@@ -50,12 +50,6 @@ public class MatrixMybatisAutoConfiguration { ...@@ -50,12 +50,6 @@ public class MatrixMybatisAutoConfiguration {
log.info("Init MatrixDataSouceAutoConfiguration"); log.info("Init MatrixDataSouceAutoConfiguration");
} }
@Bean
@ConditionalOnMissingBean(DataSourceProvider.class)
public DataSourceProvider dataSourceProvider() {
return new ApolloDruidDataSourceProvider();
}
/** /**
* 如果没有声明,则使用默认的 * 如果没有声明,则使用默认的
* *
...@@ -63,16 +57,10 @@ public class MatrixMybatisAutoConfiguration { ...@@ -63,16 +57,10 @@ public class MatrixMybatisAutoConfiguration {
*/ */
@Bean @Bean
@ConditionalOnMissingBean(DataSource.class) @ConditionalOnMissingBean(DataSource.class)
public DataSource dataSource(DataSourceProvider dataSourceProvider) { public DataSource dataSource() {
return new MatrixDataSource("01-rw"); return new MatrixDataSource("01-rw");
} }
@Bean
@ConditionalOnMissingBean(DataSourceFactory.class)
public DataSourceFactory dataSourceFactory() {
return new DruidDataSourceFactory();
}
@Bean @Bean
@ConditionalOnMissingBean(SqlSessionFactory.class) @ConditionalOnMissingBean(SqlSessionFactory.class)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment