text(测试多数据源)
commit
386a6dd0cf
|
@ -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;
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,6 +5,9 @@ import org.springframework.beans.BeansException;
|
|||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
|
@ -111,4 +114,18 @@ public final class SpringUtils implements BeanFactoryPostProcessor {
|
|||
public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
SpringUtils.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
public static void registerBean(String beanName, Object bean) {
|
||||
if (beanFactory instanceof AnnotationConfigApplicationContext) {
|
||||
AnnotationConfigApplicationContext annotationContext = (AnnotationConfigApplicationContext) beanFactory;
|
||||
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) annotationContext.getBeanFactory();
|
||||
|
||||
GenericBeanDefinition definition = new GenericBeanDefinition();
|
||||
definition.setBeanClass(bean.getClass());
|
||||
beanFactory.registerBeanDefinition(beanName, definition);
|
||||
beanFactory.registerSingleton(beanName, bean);
|
||||
} else {
|
||||
throw new IllegalStateException("注入失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
<?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-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>muyu-common-saas</artifactId>
|
||||
<description>SaaS公共依赖</description>
|
||||
|
||||
<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-common-datasource</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 鉴权依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,55 @@
|
|||
package com.muyu.clw.common.many.datasource;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.clw.common.many.datasource.domain.model.DataSourceInfo;
|
||||
import com.muyu.clw.common.saas.domain.model.EntInfo;
|
||||
import com.muyu.clw.common.many.datasource.factory.DruidDataSourceFactory;
|
||||
import com.muyu.clw.common.many.datasource.role.DynamicDataSource;
|
||||
import com.muyu.common.core.utils.SpringUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 多数据源
|
||||
*
|
||||
* @ClassName ManyDataSource
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/6/4 14:10
|
||||
*/
|
||||
|
||||
@Log4j2
|
||||
@Component
|
||||
public class ManyDataSource {
|
||||
public static List<EntInfo> dataSourceInfoList(EntInfo entInfo) {
|
||||
List<EntInfo> list = new ArrayList<>();
|
||||
list.add(entInfo);
|
||||
return list;
|
||||
}
|
||||
@Bean
|
||||
@Primary
|
||||
public static DynamicDataSource dynamicDataSource(DruidDataSourceFactory druidDataSourceFactory,EntInfo entInfoList) {
|
||||
|
||||
Map<Object, Object> dataSourceMap = new HashMap<>();
|
||||
dataSourceInfoList(entInfoList)
|
||||
.stream()
|
||||
.map(entInfo -> DataSourceInfo.hostAndPortBuild(entInfo.getEntCode(),entInfo.getIp(),entInfo.getPort()))
|
||||
.forEach(dataSourceInfo -> {
|
||||
dataSourceMap.put(dataSourceInfo.getKey(), druidDataSourceFactory.create(dataSourceInfo));
|
||||
});
|
||||
//设置动态数据源
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
// dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
|
||||
dynamicDataSource.setTargetDataSources(dataSourceMap);
|
||||
//将数据源信息备份在defineTargetDataSources中
|
||||
dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
|
||||
return dynamicDataSource;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.many.config.contents;
|
||||
package com.muyu.clw.common.many.datasource.constents;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.many.config.domai.model;
|
||||
package com.muyu.clw.common.many.datasource.domain.model;
|
||||
|
||||
import com.muyu.common.core.utils.StringUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
@ -6,7 +6,8 @@ import lombok.Data;
|
|||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import static com.muyu.many.config.contents.DatasourceContent.*;
|
||||
import static com.muyu.clw.common.many.datasource.constents.DatasourceContent.*;
|
||||
|
||||
|
||||
/**
|
||||
* @ClassName DataSourceInfo
|
|
@ -1,7 +1,7 @@
|
|||
package com.muyu.many.config.factory;
|
||||
package com.muyu.clw.common.many.datasource.factory;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.many.config.domai.model.DataSourceInfo;
|
||||
import com.muyu.clw.common.many.datasource.domain.model.DataSourceInfo;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.many.config.holder;
|
||||
package com.muyu.clw.common.many.datasource.holder;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.Assert;
|
|
@ -1,8 +1,7 @@
|
|||
package com.muyu.many.config.role;
|
||||
package com.muyu.clw.common.many.datasource.role;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.many.config.domai.model.DataSourceInfo;
|
||||
import com.muyu.many.config.holder.DynamicDataSourceHolder;
|
||||
import com.muyu.clw.common.many.datasource.holder.DynamicDataSourceHolder;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
@ -26,6 +25,14 @@ 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
|
|
@ -0,0 +1,14 @@
|
|||
package com.muyu.clw.common.saas.contents;
|
||||
|
||||
/**
|
||||
* SAAS常量
|
||||
*
|
||||
* @ClassName SaaSConstant
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/6/5 11:24
|
||||
*/
|
||||
|
||||
|
||||
public class SaaSConstant {
|
||||
public final static String SAAS_KEY = "ent_code";
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.muyu.many.config.domai.model;
|
||||
package com.muyu.clw.common.saas.domain.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
|
@ -0,0 +1,24 @@
|
|||
package com.muyu.clw.common.saas.exception;
|
||||
|
||||
import com.muyu.common.core.exception.ServiceException;
|
||||
|
||||
/**
|
||||
* @ClassName SaaSException
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/6/5 11:29
|
||||
*/
|
||||
|
||||
|
||||
public class SaaSException extends ServiceException {
|
||||
public SaaSException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public SaaSException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public SaaSException(String message, Integer code) {
|
||||
super(message, code);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.muyu.clw.common.saas.interceptor;
|
||||
|
||||
import com.muyu.clw.common.saas.contents.SaaSConstant;
|
||||
import com.muyu.clw.common.saas.exception.SaaSException;
|
||||
import com.muyu.clw.common.many.datasource.holder.DynamicDataSourceHolder;
|
||||
import com.muyu.clw.common.many.datasource.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数据拦截
|
||||
*
|
||||
* @ClassName SaaSInterceptor
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/6/5 10:04
|
||||
*/
|
||||
|
||||
|
||||
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.hashKey(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.clw.common.saas.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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
com.muyu.clw.common.saas.interceptor.WebMvcSaaSConfig
|
||||
com.muyu.clw.common.many.datasource.factory.DruidDataSourceFactory
|
|
@ -18,6 +18,7 @@
|
|||
<module>muyu-common-datascope</module>
|
||||
<module>muyu-common-datasource</module>
|
||||
<module>muyu-common-system</module>
|
||||
<module>muyu-common-saas</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>muyu-common</artifactId>
|
||||
|
|
|
@ -1,110 +0,0 @@
|
|||
<?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-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>muyu-common-many-dataSourceMany</artifactId>
|
||||
|
||||
<description>动态多数据源</description>
|
||||
<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>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Actuator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Swagger UI -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${swagger.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataSource -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-datasource</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataScope -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-datascope</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common Log -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-log</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common Swagger -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-swagger</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- 加入maven deploy插件,当在deploy时,忽略些model-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -1,30 +0,0 @@
|
|||
package com.muyu.many;
|
||||
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration;
|
||||
import com.muyu.common.security.annotation.EnableCustomConfig;
|
||||
import com.muyu.common.security.annotation.EnableMyFeignClients;
|
||||
import com.muyu.common.swagger.annotation.EnableCustomSwagger2;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
|
||||
/**
|
||||
* 多数据源启动类
|
||||
*
|
||||
* @ClassName DataSourcemanyApplication
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/6/3 18:40
|
||||
*/
|
||||
|
||||
@EnableCustomConfig
|
||||
@EnableCustomSwagger2
|
||||
@EnableMyFeignClients
|
||||
@SpringBootApplication(exclude = {
|
||||
DynamicDataSourceAutoConfiguration.class,
|
||||
DataSourceAutoConfiguration.class
|
||||
})
|
||||
public class DataSourcemanyApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DataSourcemanyApplication.class,args);
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
package com.muyu.many.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.common.core.utils.SpringUtils;
|
||||
import com.muyu.many.config.domai.model.DataSourceInfo;
|
||||
import com.muyu.many.config.domai.model.EntInfo;
|
||||
import com.muyu.many.config.factory.DruidDataSourceFactory;
|
||||
import com.muyu.many.config.role.DynamicDataSource;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 多数据源
|
||||
*
|
||||
* @ClassName ManyDataSource
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/6/4 14:10
|
||||
*/
|
||||
|
||||
@Log4j2
|
||||
@Component
|
||||
public class ManyDataSource {
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
new Thread(() ->{
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException ignored) {}
|
||||
DruidDataSourceFactory druidDataSourceFactory = SpringUtils.getBean(DruidDataSourceFactory.class);
|
||||
DynamicDataSource dynamicDataSource = SpringUtils.getBean(DynamicDataSource.class);;
|
||||
EntInfo entInfo = EntInfo.builder()
|
||||
.entCode("ent_4588")
|
||||
.ip("123.56.102.11")
|
||||
.port(3308)
|
||||
.build();
|
||||
DataSourceInfo dataSourceInfo = DataSourceInfo.hostAndPortBuild(entInfo.getEntCode(), entInfo.getIp(), entInfo.getPort());
|
||||
DruidDataSource druidDataSource = druidDataSourceFactory.create(dataSourceInfo);
|
||||
dynamicDataSource.put(dataSourceInfo.getKey(), druidDataSource);
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
private List<EntInfo> dataSourceInfoList() {
|
||||
List<EntInfo> list = new ArrayList<>();
|
||||
list.add(
|
||||
EntInfo.builder()
|
||||
.entCode("ent_4587")
|
||||
.ip("123.56.102.11")
|
||||
.port(3310)
|
||||
.build()
|
||||
);
|
||||
return list;
|
||||
}
|
||||
@Bean
|
||||
@Primary
|
||||
public DynamicDataSource dynamicDataSource(DruidDataSourceFactory druidDataSourceFactory) {
|
||||
|
||||
Map<Object, Object> dataSourceMap = new HashMap<>();
|
||||
dataSourceInfoList()
|
||||
.stream()
|
||||
.map(entInfo -> DataSourceInfo.hostAndPortBuild(entInfo.getEntCode(),entInfo.getIp(),entInfo.getPort()))
|
||||
.forEach(dataSourceInfo -> {
|
||||
dataSourceMap.put(dataSourceInfo.getKey(), druidDataSourceFactory.create(dataSourceInfo));
|
||||
});
|
||||
//设置动态数据源
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
// dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
|
||||
dynamicDataSource.setTargetDataSources(dataSourceMap);
|
||||
//将数据源信息备份在defineTargetDataSources中
|
||||
dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
|
||||
return dynamicDataSource;
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.muyu.many.server.controller;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.many.config.holder.DynamicDataSourceHolder;
|
||||
import com.muyu.many.server.domain.Enterprise;
|
||||
import com.muyu.many.server.service.EnterpriseService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 企业表 Controller层
|
||||
*
|
||||
* @ClassName EnterpriseController
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/6/4 20:53
|
||||
*/
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/enterprise")
|
||||
public class EnterpriseController {
|
||||
@Autowired
|
||||
private EnterpriseService enterpriseService;
|
||||
|
||||
@GetMapping("/all/{code}")
|
||||
public Result<List<Enterprise>> all(@PathVariable("code") String code){
|
||||
DynamicDataSourceHolder.setDynamicDataSourceKey(code);
|
||||
return enterpriseService.enterpriseList(code);
|
||||
}
|
||||
}
|
|
@ -1,96 +0,0 @@
|
|||
package com.muyu.many.server.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.muyu.common.core.web.domain.BaseEntity;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 企业表
|
||||
*
|
||||
* @ClassName Enterprise
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/6/4 20:47
|
||||
*/
|
||||
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TableName(value = "enterprise")
|
||||
public class Enterprise extends BaseEntity {
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId(value = "id",type = IdType.AUTO)
|
||||
private Long id;
|
||||
/**
|
||||
* 企业名称
|
||||
*/
|
||||
private String enterpriseName;
|
||||
/**
|
||||
* 法定代表人
|
||||
*/
|
||||
private String legalPerson;
|
||||
/**
|
||||
* 经营执照凭证号码
|
||||
*/
|
||||
private String businessLicenseNumber;
|
||||
/**
|
||||
* 企业成立时间
|
||||
*/
|
||||
private Date establishmentDate;
|
||||
/**
|
||||
* 经营范围
|
||||
*/
|
||||
private String businessScope;
|
||||
/**
|
||||
* 注册地址
|
||||
*/
|
||||
private String address;
|
||||
/**
|
||||
* 企业联系方式
|
||||
*/
|
||||
private String contactPhone;
|
||||
/**
|
||||
* 公司邮箱
|
||||
*/
|
||||
private String email;
|
||||
/**
|
||||
* 企业当前状态
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 企业入驻平台时期
|
||||
*/
|
||||
private Date registrationDate;
|
||||
/**
|
||||
* 企业认证id
|
||||
*/
|
||||
private Long certificationId;
|
||||
/**
|
||||
* 认证时间
|
||||
*/
|
||||
private Date authenticationDate;
|
||||
/**
|
||||
* 服务级别
|
||||
*/
|
||||
private Integer serviceLevel;
|
||||
/**
|
||||
* 开通服务id
|
||||
*/
|
||||
private Long open_serverId;
|
||||
/**
|
||||
* 增值服务id
|
||||
*/
|
||||
private Long add_serverId;
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package com.muyu.many.server.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.many.server.domain.Enterprise;
|
||||
|
||||
/**
|
||||
* Enterprise 企业表的Mapper接口层
|
||||
*
|
||||
* @author AnNan.Wang
|
||||
* @ClassName: EnterpriseMapper
|
||||
* @createTime: 2024/6/4 20:58
|
||||
*/
|
||||
public interface EnterpriseMapper extends BaseMapper<Enterprise> {
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package com.muyu.many.server.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.many.server.domain.Enterprise;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Enterprise 企业表Service层接口
|
||||
*
|
||||
* @author AnNan.Wang
|
||||
* @ClassName: EnterpriseService
|
||||
* @createTime: 2024/6/4 20:54
|
||||
*/
|
||||
public interface EnterpriseService extends IService<Enterprise> {
|
||||
Result<List<Enterprise>> enterpriseList(String code);
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.muyu.many.server.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.many.server.domain.Enterprise;
|
||||
import com.muyu.many.server.mapper.EnterpriseMapper;
|
||||
import com.muyu.many.server.service.EnterpriseService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Enterprise 企业表的EnterpriseService业务实现层
|
||||
*
|
||||
* @ClassName EnterpriseServiceImpl
|
||||
* @Author AnNan.Wang
|
||||
* @Date 2024/6/4 20:57
|
||||
*/
|
||||
|
||||
@Service
|
||||
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterprise>
|
||||
implements EnterpriseService{
|
||||
@Autowired
|
||||
private EnterpriseMapper enterpriseMapper;
|
||||
|
||||
|
||||
@Override
|
||||
public Result<List<Enterprise>> enterpriseList(String code) {
|
||||
return Result.success(
|
||||
enterpriseMapper.selectList(null)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
Spring Boot Version: ${spring-boot.version}
|
||||
Spring Application Name: ${spring.application.name}
|
|
@ -1,28 +0,0 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 9204
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
# 应用名称
|
||||
name: ruoyi-many
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 101.34.243.166:8848
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 101.34.243.166:8848
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
logging:
|
||||
level:
|
||||
com.muyu.system.mapper: DEBUG
|
|
@ -1,74 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<!-- 日志存放路径 -->
|
||||
<property name="log.path" value="logs/muyu-many"/>
|
||||
<!-- 日志输出格式 -->
|
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
|
||||
|
||||
<!-- 控制台输出 -->
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 系统日志输出 -->
|
||||
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/info.log</file>
|
||||
<!-- 循环政策:基于时间创建日志文件 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 日志文件名格式 -->
|
||||
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 60天 -->
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<!-- 过滤的级别 -->
|
||||
<level>INFO</level>
|
||||
<!-- 匹配时的操作:接收(记录) -->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/error.log</file>
|
||||
<!-- 循环政策:基于时间创建日志文件 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 日志文件名格式 -->
|
||||
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 60天 -->
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<!-- 过滤的级别 -->
|
||||
<level>ERROR</level>
|
||||
<!-- 匹配时的操作:接收(记录) -->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- 系统模块日志级别控制 -->
|
||||
<logger name="com.muyu" level="info"/>
|
||||
<!-- Spring日志级别控制 -->
|
||||
<logger name="org.springframework" level="warn"/>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
|
||||
<!--系统操作日志-->
|
||||
<root level="info">
|
||||
<appender-ref ref="file_info"/>
|
||||
<appender-ref ref="file_error"/>
|
||||
</root>
|
||||
</configuration>
|
|
@ -113,5 +113,9 @@
|
|||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-saas</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -5,14 +5,7 @@ import javax.servlet.http.HttpServletResponse;
|
|||
|
||||
import com.muyu.authentication.service.IEnterpriseService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import com.muyu.common.log.annotation.Log;
|
||||
import com.muyu.common.log.enums.BusinessType;
|
||||
import com.muyu.common.security.annotation.RequiresPermissions;
|
||||
|
@ -102,4 +95,11 @@ public class EnterpriseController extends BaseController
|
|||
{
|
||||
return toAjax(enterpriseService.deleteEnterpriseByIds(ids));
|
||||
}
|
||||
|
||||
@PostMapping("/AllList")
|
||||
public Result<List<Enterprise>> AllList(@RequestParam Long id){
|
||||
return Result.success(
|
||||
enterpriseService.AllList(id)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,4 +60,6 @@ public interface IEnterpriseService extends IService<Enterprise>
|
|||
* @return 结果
|
||||
*/
|
||||
public int deleteEnterpriseById(Long id);
|
||||
|
||||
List<Enterprise> AllList(Long id);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
package com.muyu.authentication.service.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.authentication.mapper.EnterpriseMapper;
|
||||
import com.muyu.clw.common.many.datasource.ManyDataSource;
|
||||
import com.muyu.clw.common.many.datasource.domain.model.DataSourceInfo;
|
||||
import com.muyu.clw.common.many.datasource.factory.DruidDataSourceFactory;
|
||||
import com.muyu.clw.common.saas.domain.model.EntInfo;
|
||||
import com.muyu.common.core.utils.DateUtils;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
|
@ -122,4 +127,26 @@ public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper,Enterpri
|
|||
{
|
||||
return enterpriseMapper.deleteById(id);
|
||||
}
|
||||
@Autowired
|
||||
private DruidDataSourceFactory druidDataSourceFactory;
|
||||
|
||||
|
||||
@Override
|
||||
public List<Enterprise> AllList(Long id) {
|
||||
Enterprise enterprise = enterpriseMapper.selectOne(
|
||||
new LambdaQueryWrapper<>() {{
|
||||
eq(Enterprise::getId, id);
|
||||
}}
|
||||
);
|
||||
String substring = enterprise.getContactPhone().substring(enterprise.getContactPhone().length() - 4);
|
||||
Integer enterpriseId = Math.toIntExact(enterprise.getId());
|
||||
|
||||
EntInfo build = EntInfo.builder()
|
||||
.entCode("ent_" + substring)
|
||||
.ip("123.56.102.11")
|
||||
.port(3306 + enterpriseId)
|
||||
.build();
|
||||
ManyDataSource.dynamicDataSource(druidDataSourceFactory,build);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
<module>muyu-modules-file</module>
|
||||
<module>muyu-company</module>
|
||||
<module>muyu-businessPlatform</module>
|
||||
<module>muyu-common-many-dataSourceMany</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>muyu-modules</artifactId>
|
||||
|
|
Loading…
Reference in New Issue