fix():多数据源
parent
6f2496fe9c
commit
02f4b99e79
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
@ -21,12 +21,6 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-customer-business-server</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package com.muyu.cloud.many.datasource.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.cloud.many.datasource.config.factory.DruidDataSourceFactory;
|
||||
import com.muyu.cloud.many.datasource.config.domain.model.DataSourceInfo;
|
||||
import com.muyu.cloud.many.datasource.config.domain.model.EnterPriseInfo;
|
||||
import com.muyu.cloud.many.datasource.config.factory.DruidDataSourceFactory;
|
||||
import com.muyu.cloud.many.datasource.config.role.DynamicDataSource;
|
||||
import com.muyu.common.core.utils.SpringUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
@ -21,7 +21,6 @@ import java.util.Map;
|
|||
/**
|
||||
* 多数据源 ManyDataSource
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* Date 2024/6/3 20:01
|
||||
*/
|
||||
@Component
|
||||
|
@ -33,12 +32,12 @@ public class ManyDataSource {
|
|||
public void init(){
|
||||
new Thread(()->{
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
Thread.sleep(10000);
|
||||
} catch (InterruptedException ignored) {}
|
||||
DruidDataSourceFactory druidDataSourceFactory= SpringUtils.getBean(DruidDataSourceFactory.class);
|
||||
DynamicDataSource dynamicDataSource= SpringUtils.getBean(DynamicDataSource.class);
|
||||
EnterPriseInfo enterPriseInfo = EnterPriseInfo.builder()
|
||||
.entCode("jiang_0604")
|
||||
.entCode("jiang_0530")
|
||||
.ip("101.34.248.9")
|
||||
.port(3308)
|
||||
.build();
|
||||
|
@ -54,9 +53,9 @@ public class ManyDataSource {
|
|||
List<EnterPriseInfo> list = new ArrayList<>();
|
||||
list.add(
|
||||
EnterPriseInfo.builder()
|
||||
.entCode("jiang_0604")
|
||||
.entCode("jiang_0606")
|
||||
.ip("101.34.248.9")
|
||||
.port(3307)
|
||||
.port(3309)
|
||||
.build()
|
||||
);
|
||||
return list;
|
||||
|
@ -76,6 +75,7 @@ public class ManyDataSource {
|
|||
});
|
||||
//设置动态数据源
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
// dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
|
||||
dynamicDataSource.setTargetDataSources(dataSourceMap);
|
||||
//将数据源信息备份在defineTargetDataSources中
|
||||
dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.cloud.many.datasource.config;
|
||||
package com.muyu.cloud.many.datasource.config.contents;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
|
@ -7,9 +7,9 @@ package com.muyu.cloud.many.datasource.config;
|
|||
*/
|
||||
public class DatasourceContent {
|
||||
|
||||
public final static String DATASOURCE_URL = "jdbc:mysql://{}:{}/vehicle_networking?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
|
||||
public final static String DATASOURCE_URL = "jdbc:mysql://{}:{}/etltest?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
|
||||
|
||||
public final static String USER_NAME = "root";
|
||||
|
||||
public final static String PASSWORD = "L041120D";
|
||||
public final static String PASSWORD = "Jp991103";
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.muyu.cloud.many.datasource.config.contents;
|
||||
|
||||
/**
|
||||
* SaaS常量 SaaSConstant
|
||||
*
|
||||
* Date 2024/6/4 18:34
|
||||
*/
|
||||
public class SaaSConstant {
|
||||
|
||||
public final static String SAAS_KEY="enterprise-code";
|
||||
}
|
|
@ -1,93 +0,0 @@
|
|||
package com.muyu.cloud.many.datasource.config.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.muyu.common.core.annotation.Excel;
|
||||
import com.muyu.common.core.web.domain.BaseEntity;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 车辆录入对象 vehicle
|
||||
*
|
||||
* @author muyu
|
||||
* @date 2024-05-27
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName("vehicle")
|
||||
public class Vehicle extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 车辆id */
|
||||
@TableId(value = "id",type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/** 车辆vin */
|
||||
private String vin;
|
||||
|
||||
/** 品牌 */
|
||||
private String brand;
|
||||
|
||||
/** 型号 */
|
||||
private String model;
|
||||
|
||||
/** 生产日期 */
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
|
||||
private Date productionDate;
|
||||
|
||||
/** 车身类型 */
|
||||
private String bodyType;
|
||||
|
||||
/** 车身颜色 */
|
||||
private String color;
|
||||
|
||||
/** 发动机排量 */
|
||||
private BigDecimal engineCapacity;
|
||||
|
||||
/** 燃油类型 */
|
||||
private String fuelType;
|
||||
|
||||
/** 变速器类型 */
|
||||
private String transmission;
|
||||
|
||||
/** 驱动方式 */
|
||||
private String driveType;
|
||||
|
||||
/** 行驶里程 */
|
||||
@Excel(name = "行驶里程")
|
||||
@ApiModelProperty(name = "行驶里程", value = "行驶里程")
|
||||
private BigDecimal mileage;
|
||||
|
||||
/** 注册日期 */
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
|
||||
private Date registrationDate;
|
||||
|
||||
/** 车牌号码 */
|
||||
private String licenseNumber;
|
||||
|
||||
/** 持有者 */
|
||||
private String holder;
|
||||
|
||||
/** 车辆类型 */
|
||||
private String vehicleType;
|
||||
|
||||
|
||||
}
|
|
@ -7,7 +7,8 @@ import lombok.Builder;
|
|||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import static com.muyu.cloud.many.datasource.config.DatasourceContent.*;
|
||||
import static com.muyu.cloud.many.datasource.config.contents.DatasourceContent.*;
|
||||
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
|
@ -36,14 +37,15 @@ public class DataSourceInfo {
|
|||
private String userName;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
* 用户密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
|
||||
public static DataSourceInfo hostAndPortBuild(String key,String host, Integer port){
|
||||
return DataSourceInfo.builder()
|
||||
.key(key)
|
||||
.url(StringUtils.format(DATASOURCE_URL, host,port))
|
||||
.url(StringUtils.format(DATASOURCE_URL, host, port,port))
|
||||
.password(PASSWORD)
|
||||
.userName(USER_NAME)
|
||||
.build();
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package com.muyu.cloud.many.datasource.config.exception;
|
||||
|
||||
import com.muyu.common.core.exception.ServiceException;
|
||||
|
||||
/**
|
||||
* SaaS异常类 SaaSException
|
||||
*
|
||||
* Date 2024/6/4 18:45
|
||||
*/
|
||||
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.many.datasource.config.interceptor;
|
||||
|
||||
import com.muyu.cloud.many.datasource.config.contents.SaaSConstant;
|
||||
import com.muyu.cloud.many.datasource.config.exception.SaaSException;
|
||||
import com.muyu.cloud.many.datasource.config.holder.DynamicDataSourceHolder;
|
||||
import com.muyu.cloud.many.datasource.config.role.DynamicDataSource;
|
||||
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;
|
||||
|
||||
/**
|
||||
* SaaS拦截器 SaaSInterceptor
|
||||
*
|
||||
* Date 2024/6/4 14:39
|
||||
*/
|
||||
public class SaaSInterceptor implements AsyncHandlerInterceptor {
|
||||
|
||||
/**
|
||||
* 之前
|
||||
*/
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
if (!(handler instanceof HandlerMethod)){
|
||||
return true;
|
||||
}
|
||||
|
||||
String SaasKey = ServletUtils.getHeader(request, SaaSConstant.SAAS_KEY);
|
||||
|
||||
if (SaasKey==null){
|
||||
throw new SaaSException("SaaS非法访问");
|
||||
} else {
|
||||
DynamicDataSource dynamicDataSource = SpringUtils.getBean(DynamicDataSource.class);
|
||||
if (!dynamicDataSource.hashKye(SaasKey)){
|
||||
throw new SaaSException("SaaS非法访问");
|
||||
}
|
||||
}
|
||||
DynamicDataSourceHolder.setDynamicDataSourceKey(SaasKey);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 之后
|
||||
*/
|
||||
@Override
|
||||
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
DynamicDataSourceHolder.removeDynamicDataSourceKey();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.cloud.many.datasource.config.interceptor;
|
||||
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* 拦截器配置
|
||||
*
|
||||
* @author muyu
|
||||
*/
|
||||
public class WebMvcSaaSConfig 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 SaaSInterceptor getHeaderInterceptor () {
|
||||
return new SaaSInterceptor();
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
package com.muyu.cloud.many.datasource.config.role;
|
||||
|
||||
import com.muyu.cloud.many.datasource.config.holder.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;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 数据源切面
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class DataSourceAsp {
|
||||
|
||||
@Pointcut("execution(public * com.muyu.customer.business.controller.*Controller.*(..))")
|
||||
public void pointcut(){
|
||||
|
||||
}
|
||||
|
||||
@Before("pointcut()")
|
||||
public void beforeMethod(){
|
||||
Long storeId = SecurityUtils.getLoginUser().getUserid();
|
||||
DynamicDataSourceHolder.setDynamicDataSourceKey("test_"+storeId);
|
||||
}
|
||||
|
||||
@After("pointcut()")
|
||||
public void afterMethod(){
|
||||
DynamicDataSourceHolder.removeDynamicDataSourceKey();
|
||||
}
|
||||
|
||||
}
|
|
@ -13,6 +13,7 @@ import java.util.Map;
|
|||
* 动态数据源
|
||||
* 调用AddDefineDataSource组件的addDefineDynamicDataSource()方法,获取原来targetdatasources的map,并将新的数据源信息添加到map中,并替换targetdatasources中的map
|
||||
* 切换数据源时可以使用@DataSource(value = "数据源名称"),或者DynamicDataSourceContextHolder.setContextKey("数据源名称")
|
||||
* @author Dongzl
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
|
@ -21,6 +22,16 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
|
|||
//备份所有数据源信息,备份的是个 指针!!!
|
||||
private Map<Object, Object> defineTargetDataSources;
|
||||
|
||||
/**
|
||||
* 判定键是否出站了
|
||||
* @param key 键
|
||||
* @return 存在结束 true 存在 false 不存在
|
||||
*/
|
||||
public boolean hashKye(String key){
|
||||
return defineTargetDataSources.containsKey(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加数据库
|
||||
* @param key 键
|
||||
|
@ -28,6 +39,7 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
|
|||
*/
|
||||
public void put(String key, DruidDataSource value){
|
||||
defineTargetDataSources.put(key,value);
|
||||
this.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.cloud.many.datasource.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.muyu.cloud.many.datasource.domain.Vehicle;
|
||||
import com.muyu.cloud.many.datasource.mapper.VehicleMapper;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 车辆控制层 VehicleController
|
||||
*
|
||||
* Date 2024/6/4 14:08
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/vehicle")
|
||||
public class VehicleController {
|
||||
@Autowired
|
||||
private VehicleMapper vehicleMapper;
|
||||
|
||||
@GetMapping("/list/all")
|
||||
public Result<List<Vehicle>> findAll () {
|
||||
return Result.success(vehicleMapper.selectList(new QueryWrapper<>()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package com.muyu.cloud.many.datasource.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.muyu.common.core.annotation.Excel;
|
||||
import com.muyu.common.core.web.domain.BaseEntity;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import io.swagger.models.auth.In;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 车辆录入对象 vehicle
|
||||
*
|
||||
* @author muyu
|
||||
* @date 2024-05-27
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName(value = "user")
|
||||
public class Vehicle extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String name;
|
||||
private Integer age;
|
||||
|
||||
// /** 车辆id */
|
||||
// @TableId(value = "id",type = IdType.AUTO)
|
||||
// private Long id;
|
||||
//
|
||||
// /** 车辆vin */
|
||||
// private String vin;
|
||||
//
|
||||
// /** 品牌 */
|
||||
// private String brand;
|
||||
//
|
||||
// /** 型号 */
|
||||
// private String model;
|
||||
//
|
||||
// /** 生产日期 */
|
||||
// @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
|
||||
// private Date productionDate;
|
||||
//
|
||||
// /** 车身类型 */
|
||||
// private String bodyType;
|
||||
//
|
||||
// /** 车身颜色 */
|
||||
// private String color;
|
||||
//
|
||||
// /** 发动机排量 */
|
||||
// private BigDecimal engineCapacity;
|
||||
//
|
||||
// /** 燃油类型 */
|
||||
// private String fuelType;
|
||||
//
|
||||
// /** 变速器类型 */
|
||||
// private String transmission;
|
||||
//
|
||||
// /** 驱动方式 */
|
||||
// private String driveType;
|
||||
//
|
||||
// /** 行驶里程 */
|
||||
// @Excel(name = "行驶里程")
|
||||
// @ApiModelProperty(name = "行驶里程", value = "行驶里程")
|
||||
// private BigDecimal mileage;
|
||||
//
|
||||
// /** 注册日期 */
|
||||
// @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
|
||||
// private Date registrationDate;
|
||||
//
|
||||
// /** 车牌号码 */
|
||||
// private String licenseNumber;
|
||||
//
|
||||
// /** 持有者 */
|
||||
// private String holder;
|
||||
//
|
||||
// /** 车辆类型 */
|
||||
// private String vehicleType;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.muyu.cloud.many.datasource.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.cloud.many.datasource.domain.Vehicle;
|
||||
|
||||
/**
|
||||
* 车辆mapper VehicleMapper
|
||||
*
|
||||
* Date 2024/6/4 14:07
|
||||
*/
|
||||
public interface VehicleMapper extends BaseMapper<Vehicle> {
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
com.muyu.cloud.many.datasource.config.interceptor.WebMvcSaaSConfig
|
Loading…
Reference in New Issue