refactor(Druid - DataSource): 系统表与业务表分库读写 | 支持动态数据源切换
+ 使用AbstractRoutingDataSource实现动态数据源 + 数据库 sys 与 业务表划分为两部分独立数据库以实现多数据分区 + 添加过滤器在请求时识别请求类型通过determineCurrentLookupKey决定最终数据源 + ContextHolder 存储数据源标识确保全局统一 + 修改了FeedList模块的缓存初始化时机确保在第一次请求时初始化而非构造时
This commit is contained in:
24
zhyc-admin/src/main/java/com/zhyc/Event/FarmLoginEvent.java
Normal file
24
zhyc-admin/src/main/java/com/zhyc/Event/FarmLoginEvent.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package com.zhyc.Event;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class FarmLoginEvent {
|
||||||
|
|
||||||
|
private final String farmCode;
|
||||||
|
private final Long userId;
|
||||||
|
|
||||||
|
public FarmLoginEvent(String farmCode, Long userId) {
|
||||||
|
this.farmCode = farmCode;
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFarmCode() {
|
||||||
|
return farmCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.zhyc.Routing.Filter;
|
||||||
|
|
||||||
|
import com.zhyc.framework.config.routing.DataSourceKeys;
|
||||||
|
import com.zhyc.framework.datasource.DynamicDataSourceContextHolder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
@Order(value = -100)
|
||||||
|
public class DataSourceRoutingFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
|
||||||
|
String uri = request.getRequestURI();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (uri.startsWith("/app") || uri.startsWith("/base") || uri.startsWith("/biosafety") || uri.startsWith("/common")
|
||||||
|
|| uri.startsWith("/dairyProducts") || uri.startsWith("/enums") || uri.startsWith("/feed") || uri.startsWith("/frozen") || uri.startsWith("/produce")
|
||||||
|
|| uri.startsWith("sale") || uri.startsWith("/stock") || uri.startsWith("/work") || uri.startsWith("/sheepfold_management")) {
|
||||||
|
log.debug("业务请求 : BUSINESS : {}", uri);
|
||||||
|
DynamicDataSourceContextHolder.setDataSourceType(DataSourceKeys.FARM_1);
|
||||||
|
} else {
|
||||||
|
log.debug("系统请求 : SYSTEM : {}", uri);
|
||||||
|
DynamicDataSourceContextHolder.setDataSourceType(DataSourceKeys.SYSTEM);
|
||||||
|
}
|
||||||
|
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
} finally {
|
||||||
|
// 业务结束后清楚数据源-防止线程复用导致错误
|
||||||
|
DynamicDataSourceContextHolder.clearDataSourceType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
zhyc-admin/src/main/java/com/zhyc/web/mvc/MvcConfig.java
Normal file
17
zhyc-admin/src/main/java/com/zhyc/web/mvc/MvcConfig.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.zhyc.web.mvc;
|
||||||
|
|
||||||
|
import com.zhyc.Routing.Filter.DataSourceRoutingFilter;
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class MvcConfig {
|
||||||
|
@Bean
|
||||||
|
public FilterRegistrationBean<DataSourceRoutingFilter> myFilter() {
|
||||||
|
FilterRegistrationBean<DataSourceRoutingFilter> registrationBean = new FilterRegistrationBean<>();
|
||||||
|
registrationBean.setFilter(new DataSourceRoutingFilter());
|
||||||
|
registrationBean.addUrlPatterns("/*");
|
||||||
|
return registrationBean;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,16 +9,15 @@ spring:
|
|||||||
# url: jdbc:mysql://localhost:3306/zhyc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
# url: jdbc:mysql://localhost:3306/zhyc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||||
# username: root
|
# username: root
|
||||||
# password: 123456
|
# password: 123456
|
||||||
url: jdbc:mysql://118.182.97.76:3306/zhyc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
url: jdbc:mysql://118.182.97.76:3306/zhyc_sys?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||||
username: zhyc
|
username: zhyc
|
||||||
password: yszh123
|
password: yszh123
|
||||||
# 从库数据源
|
# 从库数据源
|
||||||
slave:
|
farm01:
|
||||||
# 从数据源开关/默认关闭
|
# 从数据源开关/默认关闭
|
||||||
enabled: false
|
url: jdbc:mysql://118.182.97.76:3306/zhyc_sheep01?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
|
||||||
url:
|
username: zhyc
|
||||||
username:
|
password: yszh123
|
||||||
password:
|
|
||||||
# 初始连接数
|
# 初始连接数
|
||||||
initialSize: 5
|
initialSize: 5
|
||||||
# 最小连接池数量
|
# 最小连接池数量
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import javax.servlet.ServletException;
|
|||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
import javax.servlet.ServletResponse;
|
import javax.servlet.ServletResponse;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
@@ -19,7 +21,6 @@ import com.alibaba.druid.pool.DruidDataSource;
|
|||||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||||
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
|
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
|
||||||
import com.alibaba.druid.util.Utils;
|
import com.alibaba.druid.util.Utils;
|
||||||
import com.zhyc.common.enums.DataSourceType;
|
|
||||||
import com.zhyc.common.utils.spring.SpringUtils;
|
import com.zhyc.common.utils.spring.SpringUtils;
|
||||||
import com.zhyc.framework.config.properties.DruidProperties;
|
import com.zhyc.framework.config.properties.DruidProperties;
|
||||||
import com.zhyc.framework.datasource.DynamicDataSource;
|
import com.zhyc.framework.datasource.DynamicDataSource;
|
||||||
@@ -39,6 +40,14 @@ public class DruidConfig
|
|||||||
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||||
return druidProperties.dataSource(dataSource);
|
return druidProperties.dataSource(dataSource);
|
||||||
}
|
}
|
||||||
|
@Bean
|
||||||
|
@ConfigurationProperties("spring.datasource.druid.farm01")
|
||||||
|
public DataSource farm1DataSource(DruidProperties druidProperties) {
|
||||||
|
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
|
||||||
|
return druidProperties.dataSource(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties("spring.datasource.druid.slave")
|
@ConfigurationProperties("spring.datasource.druid.slave")
|
||||||
@@ -51,12 +60,16 @@ public class DruidConfig
|
|||||||
|
|
||||||
@Bean(name = "dynamicDataSource")
|
@Bean(name = "dynamicDataSource")
|
||||||
@Primary
|
@Primary
|
||||||
public DynamicDataSource dataSource(DataSource masterDataSource)
|
public DataSource dynamicDataSource(
|
||||||
{
|
@Qualifier("masterDataSource") DataSource systemDataSource,
|
||||||
|
@Qualifier("farm1DataSource") DataSource farm1DataSource) {
|
||||||
|
|
||||||
|
// 存储数据源路由
|
||||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||||
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
|
targetDataSources.put("system", systemDataSource);
|
||||||
setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
|
targetDataSources.put("farm01", farm1DataSource);
|
||||||
return new DynamicDataSource(masterDataSource, targetDataSources);
|
|
||||||
|
return new DynamicDataSource(systemDataSource, targetDataSources);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.zhyc.framework.config.routing;
|
||||||
|
|
||||||
|
public interface DataSourceKeys {
|
||||||
|
String SYSTEM = "system";
|
||||||
|
String FARM_1 = "farm01";
|
||||||
|
String FARM_2 = "farm02";
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.zhyc.framework.web.service;
|
package com.zhyc.framework.web.service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import com.zhyc.framework.datasource.DynamicDataSourceContextHolder;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import com.zhyc.module.feed.service.impl.SgFeedListServiceImpl;
|
import com.zhyc.module.feed.service.impl.SgFeedListServiceImpl;
|
||||||
@@ -39,7 +40,7 @@ public class SgFeedListController extends BaseController {
|
|||||||
private final ISgFeedListService sgFeedListService;
|
private final ISgFeedListService sgFeedListService;
|
||||||
|
|
||||||
|
|
||||||
public static boolean refresh = true;
|
public static final AtomicBoolean refresh = new AtomicBoolean(true);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public SgFeedListController(ISgFeedListService sgFeedListService) {
|
public SgFeedListController(ISgFeedListService sgFeedListService) {
|
||||||
@@ -57,9 +58,8 @@ public class SgFeedListController extends BaseController {
|
|||||||
刷新缓存
|
刷新缓存
|
||||||
当配方管理表出现更新 或 饲喂计划表出现增删改时会将refresh置为true 通知此处进行刷新
|
当配方管理表出现更新 或 饲喂计划表出现增删改时会将refresh置为true 通知此处进行刷新
|
||||||
*/
|
*/
|
||||||
if (refresh) {
|
if (refresh.compareAndSet(true, false)) {
|
||||||
sgFeedListService.SyncFeedList();
|
sgFeedListService.syncFeedListSafely();
|
||||||
refresh = false;
|
|
||||||
}
|
}
|
||||||
startPage();
|
startPage();
|
||||||
List<SgFeedList> list = sgFeedListService.selectSgFeedListList(sgFeedList);
|
List<SgFeedList> list = sgFeedListService.selectSgFeedListList(sgFeedList);
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ public class SgFeedPlanController extends BaseController {
|
|||||||
// 计算其他字段值
|
// 计算其他字段值
|
||||||
setPlan(sgFeedPlan);
|
setPlan(sgFeedPlan);
|
||||||
// 通知配料清单刷新数据
|
// 通知配料清单刷新数据
|
||||||
SgFeedListController.refresh = true;
|
SgFeedListController.refresh.set(true);
|
||||||
return toAjax(sgFeedPlanService.insertSgFeedPlan(sgFeedPlan));
|
return toAjax(sgFeedPlanService.insertSgFeedPlan(sgFeedPlan));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ public class SgFeedPlanController extends BaseController {
|
|||||||
// 根据修改后的值重新计算
|
// 根据修改后的值重新计算
|
||||||
setPlan(sgFeedPlan);
|
setPlan(sgFeedPlan);
|
||||||
// 通知配料清单刷新数据
|
// 通知配料清单刷新数据
|
||||||
SgFeedListController.refresh = true;
|
SgFeedListController.refresh.set(true);
|
||||||
return toAjax(sgFeedPlanService.updateSgFeedPlan(sgFeedPlan));
|
return toAjax(sgFeedPlanService.updateSgFeedPlan(sgFeedPlan));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +109,7 @@ public class SgFeedPlanController extends BaseController {
|
|||||||
@DeleteMapping("/{createDates}")
|
@DeleteMapping("/{createDates}")
|
||||||
public AjaxResult remove(@PathVariable Date[] createDates) {
|
public AjaxResult remove(@PathVariable Date[] createDates) {
|
||||||
// 通知配料清单刷新数据
|
// 通知配料清单刷新数据
|
||||||
SgFeedListController.refresh = true;
|
SgFeedListController.refresh.set(true);
|
||||||
return toAjax(sgFeedPlanService.deleteSgFeedPlanByCreateDates(createDates));
|
return toAjax(sgFeedPlanService.deleteSgFeedPlanByCreateDates(createDates));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,8 +93,7 @@ public class SgFormulaManagementController extends BaseController {
|
|||||||
@PostMapping
|
@PostMapping
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public AjaxResult add(@RequestBody SgFormulaManagement sgFormulaManagement) {
|
public AjaxResult add(@RequestBody SgFormulaManagement sgFormulaManagement) {
|
||||||
if (null == sgFormulaManagement)
|
if (null == sgFormulaManagement) throw new RuntimeException("ERROR: 数据为空");
|
||||||
throw new RuntimeException("ERROR: 数据为空");
|
|
||||||
if (Objects.equals(sgFormulaManagement.getBatchId(), "0")) {
|
if (Objects.equals(sgFormulaManagement.getBatchId(), "0")) {
|
||||||
SgFormulaManagement exist = sgFormulaManagementService.selectSgFormulaManagementByFormulaId(sgFormulaManagement.getFormulaId());
|
SgFormulaManagement exist = sgFormulaManagementService.selectSgFormulaManagementByFormulaId(sgFormulaManagement.getFormulaId());
|
||||||
if (exist != null) {
|
if (exist != null) {
|
||||||
@@ -145,7 +144,7 @@ public class SgFormulaManagementController extends BaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 通知配料清单刷新数据
|
// 通知配料清单刷新数据
|
||||||
SgFeedListController.refresh = true;
|
SgFeedListController.refresh.set(true);
|
||||||
return toAjax(sgFormulaManagementService.updateSgFormulaManagement(sgFormulaManagement));
|
return toAjax(sgFormulaManagementService.updateSgFormulaManagement(sgFormulaManagement));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,7 +171,7 @@ public class SgFormulaManagementController extends BaseController {
|
|||||||
sgFormulaManagement.setBatchId(batchId);
|
sgFormulaManagement.setBatchId(batchId);
|
||||||
|
|
||||||
// 通知配料清单刷新数据
|
// 通知配料清单刷新数据
|
||||||
SgFeedListController.refresh = true;
|
SgFeedListController.refresh.set(true);
|
||||||
return toAjax(sgFormulaManagementService.deleteSgFormulaManagement(sgFormulaManagement));
|
return toAjax(sgFormulaManagementService.deleteSgFormulaManagement(sgFormulaManagement));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,5 +59,7 @@ public interface ISgFeedListService {
|
|||||||
*/
|
*/
|
||||||
int deleteSgFeedListById(Long id);
|
int deleteSgFeedListById(Long id);
|
||||||
|
|
||||||
|
void syncFeedListSafely();
|
||||||
|
|
||||||
void SyncFeedList();
|
void SyncFeedList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ import org.springframework.stereotype.Service;
|
|||||||
import com.zhyc.module.feed.mapper.SgFeedListMapper;
|
import com.zhyc.module.feed.mapper.SgFeedListMapper;
|
||||||
import com.zhyc.module.feed.domain.SgFeedList;
|
import com.zhyc.module.feed.domain.SgFeedList;
|
||||||
import com.zhyc.module.feed.service.ISgFeedListService;
|
import com.zhyc.module.feed.service.ISgFeedListService;
|
||||||
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import static com.zhyc.module.feed.controller.SgFeedListController.refresh;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配料清单Service业务层处理
|
* 配料清单Service业务层处理
|
||||||
@@ -30,7 +34,15 @@ public class SgFeedListServiceImpl implements ISgFeedListService {
|
|||||||
this.sgFeedListMapper = sgFeedListMapper;
|
this.sgFeedListMapper = sgFeedListMapper;
|
||||||
this.sgFormulaManagementService = sgFormulaManagementService;
|
this.sgFormulaManagementService = sgFormulaManagementService;
|
||||||
this.sgFeedPlanService = sgFeedPlanService;
|
this.sgFeedPlanService = sgFeedPlanService;
|
||||||
// 构造时将数据库初始数据写入缓存
|
// 第一次请求时更新缓存
|
||||||
|
refresh.set(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在数据源已切换后调用
|
||||||
|
*/
|
||||||
|
public synchronized void initCacheIfNecessary() {
|
||||||
|
// 初次访问时将数据库初始数据写入缓存
|
||||||
List<SgFeedList> feedListsFromDataBase = this.selectSgFeedListList(new SgFeedList());
|
List<SgFeedList> feedListsFromDataBase = this.selectSgFeedListList(new SgFeedList());
|
||||||
for (SgFeedList sgFeedListItem : feedListsFromDataBase) {
|
for (SgFeedList sgFeedListItem : feedListsFromDataBase) {
|
||||||
String key = sgFeedListItem.getFormulaId() + "_" + sgFeedListItem.getFormulaBatchId() + "_" + sgFeedListItem.getDeployDate();
|
String key = sgFeedListItem.getFormulaId() + "_" + sgFeedListItem.getFormulaBatchId() + "_" + sgFeedListItem.getDeployDate();
|
||||||
@@ -157,6 +169,16 @@ public class SgFeedListServiceImpl implements ISgFeedListService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void syncFeedListSafely() {
|
||||||
|
try {
|
||||||
|
SyncFeedList();
|
||||||
|
} catch (Exception e) {
|
||||||
|
refresh.set(true); // 初始化失败,下次还能重试
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||||
public void SyncFeedList() {
|
public void SyncFeedList() {
|
||||||
HashMap<String, SgFeedList> cacheTemp = new HashMap<>(sgFeedListMap);
|
HashMap<String, SgFeedList> cacheTemp = new HashMap<>(sgFeedListMap);
|
||||||
// 清空旧缓存
|
// 清空旧缓存
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<if test="isGranular != null "> and is_granular = #{isGranular}</if>
|
<if test="isGranular != null "> and is_granular = #{isGranular}</if>
|
||||||
</where>
|
</where>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="selectSgMaterialByMaterialId" parameterType="String" resultMap="SgMaterialResult">
|
<select id="selectSgMaterialByMaterialId" parameterType="String" resultMap="SgMaterialResult">
|
||||||
<include refid="selectSgMaterialVo"/>
|
<include refid="selectSgMaterialVo"/>
|
||||||
where material_id = #{materialId}
|
where material_id = #{materialId}
|
||||||
|
|||||||
Reference in New Issue
Block a user