test:(修改数据源)
parent
9db5a3bbc4
commit
a5a8af2236
|
@ -1,92 +0,0 @@
|
|||
package com.muyu.config.role;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.muyu.common.core.utils.SpringUtils;
|
||||
import com.muyu.config.DynamicDataSource;
|
||||
import com.muyu.config.domain.model.DataSourceInfo;
|
||||
import com.muyu.config.domain.model.EntInfo;
|
||||
import com.muyu.config.factory.DruidDataSourceFactory;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @ClassDescription:
|
||||
* @JdkVersion: 17
|
||||
* @Author: zhangxu
|
||||
* @Created: 2024/6/4 9:05
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
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_4587")
|
||||
.ip("115.159.67.205")
|
||||
.port(3307)
|
||||
.build();
|
||||
DataSourceInfo dataSourceInfo = DataSourceInfo.getDataSourceInfo(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("115.159.67.205")
|
||||
.port(3307)
|
||||
.build()
|
||||
);
|
||||
return list;
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public DynamicDataSource dynamicDataSource(DruidDataSourceFactory druidDataSourceFactory) {
|
||||
Map<Object, Object> map = new HashMap<>();
|
||||
dataSourceInfoList().stream()
|
||||
.map(entInfo -> DataSourceInfo.getDataSourceInfo(entInfo.getEntCode(),entInfo.getIp(),entInfo.getPort()))
|
||||
.forEach(dataSourceInfo -> {
|
||||
map.put(dataSourceInfo.getKey(),druidDataSourceFactory.create(dataSourceInfo));
|
||||
});
|
||||
|
||||
//设置动态数据源
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
dynamicDataSource.setTargetDataSources(map);
|
||||
dynamicDataSource.setDefaultTargetDataSource(map);
|
||||
return dynamicDataSource;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package com.muyu.networking.interceptor;
|
||||
|
||||
|
||||
import com.muyu.networking.service.DataSourceService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.AsyncHandlerInterceptor;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @ClassName SaasInterceptor
|
||||
* @Author GuanTieLin
|
||||
* @Date 2024/6/3 21:09
|
||||
*/
|
||||
@Component
|
||||
public class SaasInterceptor implements AsyncHandlerInterceptor {
|
||||
|
||||
@Autowired
|
||||
private DataSourceService changeDataSourceService;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
// 解决跨域问题
|
||||
//1.如果请求不是动态的,handler对象不是HandlerMethod的实例(静态页面).放行
|
||||
//2.如果请求是跨域请求(请求方法是:OPTIONS),handler对象不是HandlerMethod
|
||||
if (!(handler instanceof HandlerMethod)) {
|
||||
return true;
|
||||
}
|
||||
String datasourceCode = request.getHeader("datasource-code");
|
||||
if(datasourceCode==null){
|
||||
throw new RuntimeException("请求不合法");
|
||||
}
|
||||
changeDataSourceService.changeDS(Long.valueOf(datasourceCode));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
// 切换为默认数据源
|
||||
changeDataSourceService.toDefaultDS();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,17 +1,17 @@
|
|||
package com.muyu.networking.mapper;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.domain.datasources.SongInfo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* songMapper接口
|
||||
*
|
||||
* @author
|
||||
* @date
|
||||
*/
|
||||
@Mapper
|
||||
public interface SongMapper extends BaseMapper<SongInfo> { // song_info对象
|
||||
}
|
||||
|
||||
//package com.muyu.networking.mapper;
|
||||
//
|
||||
//
|
||||
//import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
//import com.muyu.domain.datasources.SongInfo;
|
||||
//import org.apache.ibatis.annotations.Mapper;
|
||||
//
|
||||
///**
|
||||
// * songMapper接口
|
||||
// *
|
||||
// * @author
|
||||
// * @date
|
||||
// */
|
||||
//@Mapper
|
||||
//public interface SongMapper extends BaseMapper<SongInfo> { // song_info对象
|
||||
//}
|
||||
//
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
package com.muyu.networking.opFen;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* @ClassDescription:
|
||||
* @JdkVersion: 17
|
||||
* @Author: zhangxu
|
||||
* @Created: 2024/5/27 15:11
|
||||
*/
|
||||
@FeignClient("muyu-system")
|
||||
public interface SysUserNet {
|
||||
|
||||
@PostMapping("/system/user")
|
||||
public Result add (@Validated @RequestBody SysUser user);
|
||||
}
|
|
@ -1,23 +1,23 @@
|
|||
package com.muyu.networking.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.domain.datasources.SongInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 多数据源
|
||||
* songService接口
|
||||
*
|
||||
* @author
|
||||
* @date 2024-05-16
|
||||
*/
|
||||
public interface SongService extends IService<SongInfo> { // song_info对象
|
||||
/**
|
||||
* 查询song列表
|
||||
*
|
||||
* @return song集合
|
||||
*/
|
||||
public List<SongInfo> list(); // song_info对象
|
||||
|
||||
}
|
||||
|
||||
//package com.muyu.networking.service;
|
||||
//
|
||||
//import com.baomidou.mybatisplus.extension.service.IService;
|
||||
//import com.muyu.domain.datasources.SongInfo;
|
||||
//
|
||||
//import java.util.List;
|
||||
//
|
||||
///** 多数据源
|
||||
// * songService接口
|
||||
// *
|
||||
// * @author
|
||||
// * @date 2024-05-16
|
||||
// */
|
||||
//public interface SongService extends IService<SongInfo> { // song_info对象
|
||||
// /**
|
||||
// * 查询song列表
|
||||
// *
|
||||
// * @return song集合
|
||||
// */
|
||||
// public List<SongInfo> list(); // song_info对象
|
||||
//
|
||||
//}
|
||||
//
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
package com.muyu.networking.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.domain.datasources.SongInfo;
|
||||
import com.muyu.networking.mapper.SongMapper;
|
||||
import com.muyu.networking.service.SongService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 多数据源
|
||||
* songService业务层处理
|
||||
*
|
||||
* @author
|
||||
* @date 2024-05-16
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SongServiceImpl extends ServiceImpl<SongMapper, SongInfo> implements SongService { // song_info对象 | songService接口
|
||||
|
||||
/**
|
||||
* 查询song列表
|
||||
*
|
||||
* @return song
|
||||
*/
|
||||
@Override // 查询song列表
|
||||
public List<SongInfo> list() { // song_info对象
|
||||
return this.list(new LambdaQueryWrapper<>());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//package com.muyu.networking.service.impl;
|
||||
//
|
||||
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
//
|
||||
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
//import com.muyu.domain.datasources.SongInfo;
|
||||
//import com.muyu.networking.mapper.SongMapper;
|
||||
//import com.muyu.networking.service.SongService;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.springframework.stereotype.Service;
|
||||
//
|
||||
//import java.util.List;
|
||||
//
|
||||
///** 多数据源
|
||||
// * songService业务层处理
|
||||
// *
|
||||
// * @author
|
||||
// * @date 2024-05-16
|
||||
// */
|
||||
//@Slf4j
|
||||
//@Service
|
||||
//public class SongServiceImpl extends ServiceImpl<SongMapper, SongInfo> implements SongService { // song_info对象 | songService接口
|
||||
//
|
||||
// /**
|
||||
// * 查询song列表
|
||||
// *
|
||||
// * @return song
|
||||
// */
|
||||
// @Override // 查询song列表
|
||||
// public List<SongInfo> list() { // song_info对象
|
||||
// return this.list(new LambdaQueryWrapper<>());
|
||||
// }
|
||||
//
|
||||
//}
|
||||
//
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
//package com.muyu.vehicle.controller;
|
||||
//
|
||||
//
|
||||
//import com.muyu.common.core.domain.Result;
|
||||
//import com.muyu.domain.datasources.SongInfo;
|
||||
//
|
||||
//import com.muyu.vehicle.service.SongService;
|
||||
//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;
|
||||
//
|
||||
///** 多数据源
|
||||
// * songController
|
||||
// *
|
||||
// * @author 张zhangxu
|
||||
// * @date 2024-05-16
|
||||
// */
|
||||
//@RestController
|
||||
//@RequestMapping("/song")
|
||||
//public class SongController {
|
||||
//
|
||||
// @Autowired
|
||||
// private SongService songService; // songService接口
|
||||
//
|
||||
// /**
|
||||
// * 查询歌曲列表
|
||||
// */
|
||||
// @GetMapping("/list")
|
||||
// public Result<List<SongInfo>> list() { // song_info对象
|
||||
// return Result.success(songService.list()); // 查询song列表
|
||||
// }
|
||||
//
|
||||
//}
|
||||
//
|
|
@ -0,0 +1,36 @@
|
|||
//package com.muyu.vehicle.interceptor;
|
||||
//
|
||||
//import org.springframework.context.annotation.Bean;
|
||||
//import org.springframework.context.annotation.Configuration;
|
||||
//import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
//
|
||||
///**
|
||||
// * 拦截器配置
|
||||
// *
|
||||
// * @author zhangxu
|
||||
// */
|
||||
//@Configuration
|
||||
//public class WebMvcConfig implements WebMvcConfigurer {
|
||||
// /**
|
||||
// * 不需要拦截地址
|
||||
// */
|
||||
// public static final String[] excludeUrls = {"/login", "/logout", "/refresh","/enterprise"};
|
||||
//
|
||||
// @Override
|
||||
// public void addInterceptors (InterceptorRegistry registry) {
|
||||
// registry.addInterceptor(getSaasInterceptor()) // 自定义请求头拦截器
|
||||
// .addPathPatterns("/networking/**")
|
||||
// .excludePathPatterns(excludeUrls) // 不需要拦截地址
|
||||
// .order(-10);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 自定义请求头拦截器
|
||||
// */
|
||||
// @Bean
|
||||
// public SaasInterceptor getSaasInterceptor() {
|
||||
// return new SaasInterceptor();
|
||||
// }
|
||||
//}
|
||||
//
|
|
@ -0,0 +1,17 @@
|
|||
package com.muyu.vehicle.mapper;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.domain.datasources.SongInfo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* songMapper接口
|
||||
*
|
||||
* @author
|
||||
* @date
|
||||
*/
|
||||
@Mapper
|
||||
public interface SongMapper extends BaseMapper<SongInfo> { // song_info对象
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.muyu.vehicle.service;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.domain.datasources.SongInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 多数据源
|
||||
* songService接口
|
||||
*
|
||||
* @author
|
||||
* @date 2024-05-16
|
||||
*/
|
||||
public interface SongService extends IService<SongInfo> { // song_info对象
|
||||
/**
|
||||
* 查询song列表
|
||||
*
|
||||
* @return song集合
|
||||
*/
|
||||
public List<SongInfo> list(); // song_info对象
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue