fast()多数据源调整
parent
904481ca38
commit
d16f1e5c69
|
@ -14,6 +14,10 @@ public class Sources {
|
|||
* 主键
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 企业id
|
||||
*/
|
||||
private Long enterpriseId;
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
|
@ -35,8 +39,9 @@ public class Sources {
|
|||
*/
|
||||
private String password = "Qq4152637";
|
||||
|
||||
public static Sources index(String name,String ip) {
|
||||
public static Sources index(Long enterpriseId,String name,String ip) {
|
||||
return builder()
|
||||
.enterpriseId(enterpriseId)
|
||||
.name(name)
|
||||
.url("ry_goods")
|
||||
.ip(ip)
|
||||
|
|
|
@ -78,7 +78,7 @@ public class EnterpriseServiceImpl implements IEnterpriseService
|
|||
enterpriseConfig.index(enterprise1);
|
||||
//新建的企业绑定mysql服务
|
||||
HttpClient.http(enterprise1);
|
||||
Sources sources = Sources.index("enterprise" + enterprise1.getId(), String.valueOf((int) (enterprise.getId() + 3306)));
|
||||
Sources sources = Sources.index(enterprise1.getId(),"enterprise" + enterprise1.getId(), String.valueOf((int) (enterprise.getId() + 3306)));
|
||||
System.out.println(sources);
|
||||
enterpriseMapper.indexSources(sources);
|
||||
}
|
||||
|
|
|
@ -94,6 +94,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<insert id="indexSources">
|
||||
insert into sources
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="enterpriseId != null">enterprise_id,</if>
|
||||
<if test="name != null">name,</if>
|
||||
<if test="url != null">url,</if>
|
||||
<if test="ip != null">ip,</if>
|
||||
|
@ -101,6 +102,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="password != null">password,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="enterpriseId != null">#{enterpriseId},</if>
|
||||
<if test="name != null">#{name},</if>
|
||||
<if test="url != null">#{url},</if>
|
||||
<if test="ip != null">#{ip},</if>
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
package com.muyu.cloud.Client;
|
||||
|
||||
import com.muyu.cloud.Client.run.ManyConfigRunner;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Log4j2
|
||||
@ComponentScan
|
||||
@Import(value = ManyConfigRunner.class)
|
||||
public class ManyConfig {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.muyu.cloud.Client.run;
|
||||
|
||||
public class ManyConfigRunner {
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
package com.muyu.cloud.common;
|
||||
package com.muyu.cloud.controller;
|
||||
|
||||
import com.muyu.cloud.datasource.DataSourceInfo;
|
||||
import com.muyu.cloud.datasource.config.contents.DataSourceInfo;
|
||||
import com.muyu.cloud.datasource.util.DataSourceService;
|
||||
import com.muyu.cloud.service.CloudService;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
|
@ -33,7 +33,7 @@ public class CloudController {
|
|||
*/
|
||||
@PostMapping("/{ip}")
|
||||
public Result selectIp(@PathVariable String ip){
|
||||
dataSourceService.addDataSourceIp(DataSourceInfo.dataIpBuild(ip));
|
||||
dataSourceService.addDataSource(DataSourceInfo.dataIpBuild(ip));
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.muyu.cloud.datasource;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
|
@ -32,15 +33,6 @@ public class DynamicDataSourceHolder {
|
|||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取动态数据源名称,默认使用ip数据源
|
||||
*/
|
||||
public static String getDynamicDataSourceIp(){
|
||||
String ip = DYNAMIC_DATASOURCE_KEY.get();
|
||||
Assert.notNull(ip, "请携带数据标识");
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除当前数据源
|
||||
*/
|
||||
|
|
|
@ -16,10 +16,8 @@ import org.springframework.stereotype.Component;
|
|||
@Aspect
|
||||
@Component
|
||||
public class DataSourceAsp {
|
||||
/**
|
||||
* 调用表示层
|
||||
*/
|
||||
@Pointcut("execution(public * com.muyu.cloud.common.*Controller.*(..))")
|
||||
|
||||
@Pointcut("execution(public * com.muyu.cloud.controller.*Controller.*(..))")
|
||||
public void pointcut () {
|
||||
}
|
||||
|
||||
|
@ -29,8 +27,9 @@ public class DataSourceAsp {
|
|||
*/
|
||||
@Before("pointcut()")
|
||||
public void beforeMethod() {
|
||||
// Long storeId = SecurityUtils.getLoginUser().getSysUser().getUserId();
|
||||
// DynamicDataSourceHolder.setDynamicDataSourceKey("test_"+storeId);
|
||||
System.out.println(SecurityUtils.getUserId());
|
||||
Long storeId = SecurityUtils.getLoginUser().getUserid();
|
||||
DynamicDataSourceHolder.setDynamicDataSourceKey("test_"+storeId);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package com.muyu.cloud.datasource.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.cloud.datasource.DataSourceInfo;
|
||||
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
||||
import com.muyu.cloud.datasource.config.contents.DataSourceInfo;
|
||||
import com.muyu.cloud.datasource.config.contents.Sources;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
@ -22,16 +26,22 @@ import java.util.Map;
|
|||
@Configuration
|
||||
public class DruidConfig {
|
||||
|
||||
// @Autowired
|
||||
// private DynamicDataSource dynamicDataSource;
|
||||
/**
|
||||
*
|
||||
* 获取多数据源信息列表
|
||||
* @return
|
||||
*/
|
||||
private List<DataSourceInfo> getDataSourceInfoList(){
|
||||
// System.out.println("获取当前登录信息");
|
||||
// System.out.println(SecurityUtils.getLoginUser());
|
||||
List<String> databaseNameList = new ArrayList<>(){{
|
||||
add("3310");
|
||||
add("3311");
|
||||
}};
|
||||
return databaseNameList.stream().map(DataSourceInfo::dataIpBuild).toList();
|
||||
List<DataSourceInfo> list = databaseNameList.stream().map(DataSourceInfo::dataIpBuild).toList();
|
||||
System.out.println(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -39,12 +49,12 @@ public class DruidConfig {
|
|||
* @Author Dongzl
|
||||
*/
|
||||
public DruidDataSource createDataSourceConnection(DataSourceInfo dataSourceInfo) {
|
||||
DruidDataSource druidDataSource = new DruidDataSource();//配置连接池
|
||||
druidDataSource.setUrl(dataSourceInfo.getUrl());//配置数据库的连接url
|
||||
druidDataSource.setUsername(dataSourceInfo.getUserName());//配置用户名
|
||||
druidDataSource.setPassword(dataSourceInfo.getPassword());//配置密码
|
||||
druidDataSource.setBreakAfterAcquireFailure(true);//配置
|
||||
druidDataSource.setConnectionErrorRetryAttempts(0);//配置
|
||||
DruidDataSource druidDataSource = new DruidDataSource();
|
||||
druidDataSource.setUrl(dataSourceInfo.getUrl());
|
||||
druidDataSource.setUsername(dataSourceInfo.getUserName());
|
||||
druidDataSource.setPassword(dataSourceInfo.getPassword());
|
||||
druidDataSource.setBreakAfterAcquireFailure(true);
|
||||
druidDataSource.setConnectionErrorRetryAttempts(0);
|
||||
try {
|
||||
druidDataSource.getConnection(2000);
|
||||
log.info("{} -> 数据源连接成功", dataSourceInfo.getIp());
|
||||
|
@ -55,14 +65,21 @@ public class DruidConfig {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态操作数据源
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
public DynamicDataSource dynamicDataSource() {
|
||||
//获取列表 获取多数据源信息
|
||||
|
||||
Map<Object, Object> dataSourceMap = new HashMap<>();
|
||||
getDataSourceInfoList().forEach(dataSourceInfo -> {
|
||||
dataSourceMap.put(dataSourceInfo.getKey(), createDataSourceConnection(dataSourceInfo));
|
||||
dataSourceMap.put(dataSourceInfo.getIp(), createDataSourceConnection(dataSourceInfo));
|
||||
});
|
||||
System.out.println(dataSourceMap);
|
||||
//设置动态数据源
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
// dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package com.muyu.cloud.datasource.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.cloud.datasource.DynamicDataSourceHolder;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
||||
|
@ -11,18 +11,27 @@ import java.util.Map;
|
|||
|
||||
/**
|
||||
* 动态数据源
|
||||
* 多数据源规则
|
||||
* 调用AddDefineDataSource组件的addDefineDynamicDataSource()方法,获取原来targetdatasources的map,并将新的数据源信息添加到map中,并替换targetdatasources中的map
|
||||
* 切换数据源时可以使用@DataSource(value = "数据源名称"),或者DynamicDataSourceContextHolder.setContextKey("数据源名称")
|
||||
* @author Dongzl
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class DynamicDataSource extends AbstractRoutingDataSource {
|
||||
//备份所有数据源信息,备份是指针
|
||||
//备份所有数据源信息 主要是指针,
|
||||
private Map<Object, Object> defineTargetDataSources;
|
||||
|
||||
/**
|
||||
* 谈价新的数据源
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public void put(String key, DruidDataSource value){
|
||||
defineTargetDataSources.put(key,value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 决定当前线程使用哪个数据源
|
||||
*/
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.datasource;
|
||||
package com.muyu.cloud.datasource.config.contents;
|
||||
|
||||
import com.muyu.common.core.utils.StringUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
@ -31,19 +31,10 @@ public class DataSourceInfo {
|
|||
private String password;
|
||||
|
||||
|
||||
public static DataSourceInfo databaseNameBuild(String databaseName){
|
||||
return DataSourceInfo.builder()
|
||||
.key(databaseName)
|
||||
.url(StringUtils.format(DATASOURCE_URL, databaseName))
|
||||
.password(PASSWORD)
|
||||
.userName(USER_NAME)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static DataSourceInfo dataIpBuild(String ip) {
|
||||
public static DataSourceInfo dataIpBuild(String ip){
|
||||
return DataSourceInfo.builder()
|
||||
.ip(ip)
|
||||
.url(StringUtils.format(DATASOURCE_URL,ip))
|
||||
.url(StringUtils.format(DATASOURCE_URL, ip))
|
||||
.password(PASSWORD)
|
||||
.userName(USER_NAME)
|
||||
.build();
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.cloud.datasource.config.contents;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Sources {
|
||||
private Long id;
|
||||
private Long enterpriseId;
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 库名
|
||||
*/
|
||||
private String url;
|
||||
/**
|
||||
* ip值
|
||||
*/
|
||||
private String ip;
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
package com.muyu.cloud.datasource.config.factory;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class DruidDataSourceFactory {
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
package com.muyu.cloud.datasource.config.holder;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* 数据源切换处理
|
||||
*
|
||||
* @author Dongzl
|
||||
*/
|
||||
@Slf4j
|
||||
public class DynamicDataSourceHolder {
|
||||
/**
|
||||
* 保存动态数据源名称
|
||||
*/
|
||||
private static final ThreadLocal<String> DYNAMIC_DATASOURCE_KEY = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 设置/切换数据源,决定当前线程使用哪个数据源
|
||||
*/
|
||||
public static void setDynamicDataSourceKey(String key){
|
||||
log.info("数据源切换为:{}",key);
|
||||
DYNAMIC_DATASOURCE_KEY.set(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取动态数据源名称,默认使用mater数据源
|
||||
*/
|
||||
public static String getDynamicDataSourceKey(){
|
||||
String key = DYNAMIC_DATASOURCE_KEY.get();
|
||||
Assert.notNull(key, "请携带数据标识");
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取动态数据源名称,默认使用ip数据源
|
||||
*/
|
||||
public static String getDynamicDataSourceIp(){
|
||||
String ip = DYNAMIC_DATASOURCE_KEY.get();
|
||||
Assert.notNull(ip, "请携带数据标识");
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除当前数据源
|
||||
*/
|
||||
public static void removeDynamicDataSourceKey(){
|
||||
log.info("移除数据源:{}",DYNAMIC_DATASOURCE_KEY.get());
|
||||
DYNAMIC_DATASOURCE_KEY.remove();
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
package com.muyu.cloud.datasource.config.role;
|
||||
|
||||
import com.muyu.cloud.datasource.DynamicDataSourceHolder;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 动态数据源
|
||||
* 调用AddDefineDataSource组件的addDefineDynamicDataSource()方法,获取原来targetdatasources的map,并将新的数据源信息添加到map中,并替换targetdatasources中的map
|
||||
* 切换数据源时可以使用@DataSource(value = "数据源名称"),或者DynamicDataSourceContextHolder.setContextKey("数据源名称")
|
||||
* @author Dongzl
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class DynamicDataSource extends AbstractRoutingDataSource {
|
||||
//备份所有数据源信息,备份是指针
|
||||
private Map<Object, Object> defineTargetDataSources;
|
||||
|
||||
/**
|
||||
* 决定当前线程使用哪个数据源
|
||||
*/
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
return DynamicDataSourceHolder.getDynamicDataSourceKey();
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package com.muyu.cloud.datasource.util;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.cloud.datasource.DataSourceInfo;
|
||||
import com.muyu.cloud.datasource.config.contents.DataSourceInfo;
|
||||
import com.muyu.cloud.datasource.config.DruidConfig;
|
||||
import com.muyu.cloud.datasource.config.DynamicDataSource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -28,23 +28,13 @@ public class DataSourceService {
|
|||
private DynamicDataSource dynamicDataSource;
|
||||
|
||||
/**
|
||||
* 新增数据源
|
||||
* 添加数据源
|
||||
* @param dataSourceInfo
|
||||
*/
|
||||
public void addDataSource(DataSourceInfo dataSourceInfo){
|
||||
//获取数据源连接请求
|
||||
addDefineDynamicDataSource(druidConfig.createDataSourceConnection(dataSourceInfo), dataSourceInfo.getKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增数据源
|
||||
* @param dataSourceInfo
|
||||
*/
|
||||
public void addDataSourceIp(DataSourceInfo dataSourceInfo){
|
||||
//获取数据源连接请求
|
||||
addDefineDynamicDataSource(druidConfig.createDataSourceConnection(dataSourceInfo), dataSourceInfo.getIp());
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description: 将新增的数据源加入到备份数据源map中
|
||||
* @Author Dongzl
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
com.muyu.cloud.Client.ManyConfig
|
|
@ -6,6 +6,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
|
||||
|
||||
<select id="selSource" resultType="java.lang.Object">
|
||||
select * from status
|
||||
select * from ry_goods
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
Loading…
Reference in New Issue