fast()多数据源调整
parent
d16f1e5c69
commit
43d3d167dc
|
@ -5,7 +5,7 @@ package com.muyu.common.core.exception;
|
|||
*
|
||||
* @author muyu
|
||||
*/
|
||||
public final class ServiceException extends RuntimeException {
|
||||
public class ServiceException extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu</artifactId>
|
||||
<version>3.6.3</version>
|
||||
<relativePath>../../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>muyu-goods-enterprise-remote</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-goods-enterprise-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,4 @@
|
|||
package muyu.goods.enterprise.remote;
|
||||
|
||||
public interface RemoteEnterpriseManageService {
|
||||
}
|
|
@ -76,11 +76,7 @@ public class EnterpriseServiceImpl implements IEnterpriseService
|
|||
if (i>0){
|
||||
Enterprise enterprise1 = enterpriseMapper.queryDateEnterprise();
|
||||
enterpriseConfig.index(enterprise1);
|
||||
//新建的企业绑定mysql服务
|
||||
HttpClient.http(enterprise1);
|
||||
Sources sources = Sources.index(enterprise1.getId(),"enterprise" + enterprise1.getId(), String.valueOf((int) (enterprise.getId() + 3306)));
|
||||
System.out.println(sources);
|
||||
enterpriseMapper.indexSources(sources);
|
||||
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
@ -110,7 +106,22 @@ public class EnterpriseServiceImpl implements IEnterpriseService
|
|||
Enterprise enterprise = new Enterprise();
|
||||
enterprise.setId(id);
|
||||
enterprise.setAuthenticationDate(DateUtils.getNowDate());
|
||||
return enterpriseMapper.authentication(enterprise);
|
||||
int i = enterpriseMapper.authentication(enterprise);
|
||||
if (i>0) {
|
||||
Enterprise enterprise1 = selectEnterpriseById(id);
|
||||
//新建的企业绑定mysql服务
|
||||
try {
|
||||
HttpClient.http(enterprise1);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Sources sources = Sources.index(enterprise1.getId(),"enterprise" + enterprise1.getId(), String.valueOf((int) (enterprise.getId() + 3306)));
|
||||
System.out.println(sources);
|
||||
enterpriseMapper.indexSources(sources);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
<modules>
|
||||
<module>muyu-goods-enterprise-common</module>
|
||||
<module>muyu-goods-enterprise-server</module>
|
||||
<module>muyu-goods-enterprise-remote</module>
|
||||
<module>muyu-goods-enterprise-client</module>
|
||||
</modules>
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package com.muyu.cloud.controller;
|
||||
|
||||
import com.muyu.cloud.datasource.DynamicDataSourceHolder;
|
||||
import com.muyu.cloud.datasource.config.contents.DataSourceInfo;
|
||||
import com.muyu.cloud.datasource.util.DataSourceService;
|
||||
import com.muyu.cloud.domain.Status;
|
||||
import com.muyu.cloud.service.CloudService;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
@ -11,6 +13,8 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Log4j2
|
||||
@RestController
|
||||
@RequestMapping("cloud")
|
||||
|
@ -36,4 +40,9 @@ public class CloudController {
|
|||
dataSourceService.addDataSource(DataSourceInfo.dataIpBuild(ip));
|
||||
return null;
|
||||
}
|
||||
|
||||
@PostMapping("list")
|
||||
public Result<List<Status>> list() {
|
||||
return Result.success(cloudService.list());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,4 +40,8 @@ public class DynamicDataSourceHolder {
|
|||
log.info("移除数据源:{}",DYNAMIC_DATASOURCE_KEY.get());
|
||||
DYNAMIC_DATASOURCE_KEY.remove();
|
||||
}
|
||||
|
||||
public boolean hashKey(String header) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package com.muyu.cloud.datasource.config;
|
||||
|
||||
import com.muyu.cloud.datasource.DynamicDataSourceHolder;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import org.aspectj.lang.annotation.After;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
|
@ -27,9 +26,8 @@ public class DataSourceAsp {
|
|||
*/
|
||||
@Before("pointcut()")
|
||||
public void beforeMethod() {
|
||||
System.out.println(SecurityUtils.getUserId());
|
||||
Long storeId = SecurityUtils.getLoginUser().getUserid();
|
||||
DynamicDataSourceHolder.setDynamicDataSourceKey("test_"+storeId);
|
||||
// Long storeId = SecurityUtils.getLoginUser().getUserid();
|
||||
// DynamicDataSourceHolder.setDynamicDataSourceKey("test_"+storeId);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
package com.muyu.cloud.datasource.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
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;
|
||||
|
@ -33,8 +30,8 @@ public class DruidConfig {
|
|||
* @return
|
||||
*/
|
||||
private List<DataSourceInfo> getDataSourceInfoList(){
|
||||
// System.out.println("获取当前登录信息");
|
||||
// System.out.println(SecurityUtils.getLoginUser());
|
||||
System.out.println("获取当前登录信息");
|
||||
System.out.println(SecurityUtils.getLoginUser());
|
||||
List<String> databaseNameList = new ArrayList<>(){{
|
||||
add("3310");
|
||||
add("3311");
|
||||
|
@ -82,7 +79,7 @@ public class DruidConfig {
|
|||
System.out.println(dataSourceMap);
|
||||
//设置动态数据源
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
// dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
|
||||
// dynamicDataSource.setDefaultTargetDataSource(dynamicDataSource);
|
||||
dynamicDataSource.setTargetDataSources(dataSourceMap);
|
||||
//将数据源信息备份在defineTargetDataSources中
|
||||
dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
|
||||
|
|
|
@ -24,7 +24,16 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
|
|||
private Map<Object, Object> defineTargetDataSources;
|
||||
|
||||
/**
|
||||
* 谈价新的数据源
|
||||
* 判定建是否出站
|
||||
* @param key
|
||||
* @return 在 true 否false
|
||||
*/
|
||||
public boolean hashKey(String key) {
|
||||
return defineTargetDataSources.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加新的数据源
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package com.muyu.cloud.datasource.config.contents;
|
||||
|
||||
public class SaasConstant {
|
||||
public final static String SAAS_KEY = "saas";
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.muyu.cloud.datasource.exception;
|
||||
|
||||
import com.muyu.common.core.exception.ServiceException;
|
||||
|
||||
public class SaasException extends ServiceException{
|
||||
|
||||
public SaasException(String message, Integer code) {
|
||||
super(message, code);
|
||||
}
|
||||
public SaasException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public SaasException() {
|
||||
super();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.muyu.cloud.datasource.interceptor;
|
||||
|
||||
import com.muyu.cloud.datasource.DynamicDataSourceHolder;
|
||||
import com.muyu.cloud.datasource.config.DynamicDataSource;
|
||||
import com.muyu.cloud.datasource.exception.SaasException;
|
||||
import com.muyu.cloud.datasource.config.contents.SaasConstant;
|
||||
import com.muyu.common.core.utils.ServletUtils;
|
||||
import com.muyu.common.core.utils.SpringUtils;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.AsyncHandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 拦截器
|
||||
*/
|
||||
public class DataScopeInterceptor implements AsyncHandlerInterceptor{// 继承头部拦截
|
||||
|
||||
/**
|
||||
* 之前
|
||||
*/
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
//进行跨域检测
|
||||
if (!(handler instanceof HandlerMethod)){
|
||||
return true;
|
||||
}
|
||||
// 获取请求头
|
||||
String header = ServletUtils.getHeader(request, SaasConstant.SAAS_KEY);
|
||||
if (header == null) {
|
||||
throw new SaasException("saas非法访问");//提交异常
|
||||
}else {
|
||||
DynamicDataSource dynamicDataSource = SpringUtils.getBean(DynamicDataSource.class);
|
||||
if (!dynamicDataSource.hashKey(header)){//判定是否出站
|
||||
throw new SaasException("saas非法访问");
|
||||
}
|
||||
}
|
||||
DynamicDataSourceHolder.setDynamicDataSourceKey(header);
|
||||
return true;//默认返回true
|
||||
}
|
||||
|
||||
/**
|
||||
* 之后
|
||||
*/
|
||||
@Override
|
||||
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
//用于清空
|
||||
DynamicDataSourceHolder.removeDynamicDataSourceKey();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.cloud.datasource.interceptor;
|
||||
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* 拦截器配置
|
||||
*
|
||||
* @author muyu
|
||||
*/
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
/**
|
||||
* 不需要拦截地址
|
||||
*/
|
||||
public static final String[] excludeUrls = {"/login", "/logout", "/refresh"};
|
||||
|
||||
@Override
|
||||
public void addInterceptors (InterceptorRegistry registry) {
|
||||
registry.addInterceptor(getHeaderInterceptor())
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns(excludeUrls)
|
||||
.order(-10);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义请求头拦截器
|
||||
*/
|
||||
public DataScopeInterceptor getHeaderInterceptor () {
|
||||
return new DataScopeInterceptor();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.muyu.cloud.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName(value = "status")
|
||||
public class Status {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private Integer age;
|
||||
}
|
|
@ -1,8 +1,18 @@
|
|||
package com.muyu.cloud.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.cloud.domain.Status;
|
||||
import com.muyu.common.core.constant.ScheduleConstants;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface CloudMapper {
|
||||
public interface CloudMapper extends BaseMapper<Status> {
|
||||
Object selSource();
|
||||
|
||||
// List<Status> list();
|
||||
|
||||
<T> List<Status> list(QueryWrapper<T> tQueryWrapper);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
package com.muyu.cloud.service;
|
||||
|
||||
import com.muyu.cloud.domain.Status;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CloudService {
|
||||
Object selSource();
|
||||
|
||||
List<Status> list();
|
||||
}
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package com.muyu.cloud.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.muyu.cloud.domain.Status;
|
||||
import com.muyu.cloud.mapper.CloudMapper;
|
||||
import com.muyu.cloud.service.CloudService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CloudServiceImpl implements CloudService {
|
||||
@Autowired
|
||||
|
@ -13,4 +17,9 @@ public class CloudServiceImpl implements CloudService {
|
|||
public Object selSource() {
|
||||
return cloudMapper.selSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Status> list() {
|
||||
return cloudMapper.list(new QueryWrapper<>());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
com.muyu.cloud.Client.ManyConfig
|
||||
com.muyu.cloud.datasource.interceptor.WebMvcConfig
|
||||
|
|
|
@ -6,6 +6,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
|
||||
|
||||
<select id="selSource" resultType="java.lang.Object">
|
||||
select * from ry_goods
|
||||
select * from status
|
||||
</select>
|
||||
<select id="list" resultType="com.muyu.cloud.domain.Status">
|
||||
select *
|
||||
from status
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
1
pom.xml
1
pom.xml
|
@ -218,6 +218,7 @@
|
|||
<module>muyu-modules/muyu-goods-enterprise</module>
|
||||
<module>muyu-modules/muyu-business</module>
|
||||
<module>muyu-modules/muyu-moudels-many-datasource</module>
|
||||
<module>muyu-modules/muyu-goods-enterprise/muyu-goods-enterprise-remote</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
|
Loading…
Reference in New Issue