feat() 连接池初版V1.1
parent
c23040359e
commit
064873ecc7
|
@ -5,8 +5,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 自定义feign注解
|
||||
* 添加basePackages路径
|
||||
* 自定义feign注解 添加basePackages路径
|
||||
*
|
||||
* @author Chao
|
||||
*/
|
||||
|
|
|
@ -5,8 +5,8 @@ import com.etl.data.source.clinet.test.DataSourceConfig;
|
|||
import com.etl.data.source.clinet.test.Singleton;
|
||||
import com.etl.data.source.domain.DataSource;
|
||||
import com.etl.data.source.remote.DataSourceRemoteService;
|
||||
import com.etl.data.type.remote.DataTypeRemoteService;
|
||||
import com.etl.data.type.domain.DataType;
|
||||
import com.etl.data.type.remote.DataTypeRemoteService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
|
@ -14,9 +14,13 @@ import org.springframework.boot.ApplicationRunner;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
*
|
||||
* 客户端初始化连接池
|
||||
*
|
||||
* @author Chao
|
||||
* @ClassName: DataSourceClientRunner 描述
|
||||
|
@ -32,26 +36,50 @@ public class DataSourceClientRunner implements ApplicationRunner {
|
|||
private DataTypeRemoteService dataTypeRemoteService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
public void run(ApplicationArguments args){
|
||||
try {
|
||||
Result<List<DataSource>> dataSourceResultList = dataSourceRemoteService.dataSourceList();
|
||||
if (dataSourceResultList.getData() == null || dataSourceResultList.getData().isEmpty()){
|
||||
log.error("数据源列表为空");
|
||||
return;
|
||||
}
|
||||
List<DataSource> dataSourceList = dataSourceResultList.getData();
|
||||
|
||||
// 创建数据源Map
|
||||
HashMap<String, javax.sql.DataSource> stringDataSourceHashMap = new HashMap<>();
|
||||
log.info("数据源列表查询成功当前数据为{}" ,dataSourceList);
|
||||
// 获取数据源类型列表
|
||||
Result<List<DataType>> dataTypeResultList = dataTypeRemoteService.list();
|
||||
if (dataTypeResultList.getData() == null || dataTypeResultList.getData().isEmpty()){
|
||||
log.error("数据源类型列表为空");
|
||||
return;
|
||||
}
|
||||
List<DataType> dataTypeList = dataTypeResultList.getData();
|
||||
log.info("数据源类型列表查询成功当前数据为{}" ,dataTypeList);
|
||||
// 将数据源类型转换为Map ## Function.identity() 这个方法是返回一个自己
|
||||
Map<Long, DataType> dataTypeMap = dataTypeList.stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
DataType::getId, Function.identity()
|
||||
)
|
||||
);
|
||||
// 创建数据源Map
|
||||
HashMap<String, javax.sql.DataSource> stringDataSourceHashMap = new HashMap<>();
|
||||
// 遍历数据源列表
|
||||
for (DataSource datum : dataSourceList){
|
||||
// 过滤相应的数据源类型
|
||||
DataType type = dataTypeList.stream()
|
||||
.filter(
|
||||
dataType -> dataType.getId().equals(datum.getTypeId())
|
||||
).findFirst()
|
||||
.get();
|
||||
javax.sql.DataSource dataSource = DataSourceConfig.dataSource(datum,type);
|
||||
Optional<DataType> dataType = Optional.ofNullable(dataTypeMap.get(datum.getTypeId()));
|
||||
if (dataType.isPresent()){
|
||||
javax.sql.DataSource dataSource = DataSourceConfig.dataSource(datum, dataType.get());
|
||||
stringDataSourceHashMap.put(datum.getDataSourceIp()+"-"+datum.getDataSourceDatabaseName(),dataSource);
|
||||
}else {
|
||||
log.error("未找到数据源 ID 为 {} 的数据类型", datum.getTypeId());
|
||||
}
|
||||
Singleton instance = Singleton.getInstance(stringDataSourceHashMap);
|
||||
Map<String, javax.sql.DataSource> map = instance.getMap();
|
||||
log.info("数据源连接池初始化成功{}",map);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("初始化数据源连接池失败{}",e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Singleton.getInstance(stringDataSourceHashMap);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,23 +6,42 @@ import lombok.Data;
|
|||
import javax.sql.DataSource;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 单例模式
|
||||
*
|
||||
* @author Han
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class Singleton {
|
||||
private Map<String , DataSource> map ;
|
||||
//使用volatile修饰变量,具有原子性、可见性和防止指令重排的特性。
|
||||
|
||||
private volatile Map<String, DataSource> map;
|
||||
private volatile static Singleton singleton;
|
||||
|
||||
private static Singleton singleton;
|
||||
|
||||
/**
|
||||
* 加入了同步代码,解决线程不安全问题
|
||||
* 使用双重校验锁来保证安全,使用volatile禁止指令重排序
|
||||
*
|
||||
* @param map
|
||||
* @return
|
||||
* @param map 连接池map
|
||||
* @return Singleton
|
||||
*/
|
||||
public static synchronized Singleton getInstance(Map<String ,DataSource> map) {
|
||||
public static Singleton getInstance(Map<String, DataSource> map) {
|
||||
//先进行实例判断,只有当不存在的时候才去创建实例。
|
||||
if (singleton == null) {
|
||||
//用synchronized 同步代码块
|
||||
//注意:此处不能用this,因为this不能和static同用。
|
||||
synchronized (Singleton.class) {
|
||||
if (singleton == null) {
|
||||
singleton = new Singleton(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
return singleton;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义私有构造器,表示只在类内部使用,只能在内部创建。
|
||||
*/
|
||||
private Singleton () {}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package com.etl.data.source.remote.factory;
|
||||
|
||||
import com.etl.common.core.domain.Result;
|
||||
import com.etl.data.source.domain.DataSource;
|
||||
import com.etl.data.source.remote.DataSourceRemoteService;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* 数据源熔断
|
||||
*
|
||||
|
@ -15,6 +18,6 @@ public class DataSourceFactory implements FallbackFactory<DataSourceRemoteServic
|
|||
|
||||
@Override
|
||||
public DataSourceRemoteService create(Throwable cause) {
|
||||
return Result::error;
|
||||
return () -> Result.success(new ArrayList<DataSource>());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package com.etl.data.type.remote.factory;
|
||||
|
||||
import com.etl.common.core.domain.Result;
|
||||
import com.etl.data.type.domain.DataType;
|
||||
import com.etl.data.type.remote.DataTypeRemoteService;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* 数据源类型熔断
|
||||
*
|
||||
|
@ -15,6 +18,6 @@ public class DataTypeFactory implements FallbackFactory<DataTypeRemoteService> {
|
|||
|
||||
@Override
|
||||
public DataTypeRemoteService create(Throwable cause) {
|
||||
return Result::error;
|
||||
return () -> Result.error(new ArrayList<DataType>());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue