refactor(重构电子围栏)

master
031026 2024-06-13 22:25:43 +08:00
commit 997daaa5c1
9 changed files with 93 additions and 53 deletions

View File

@ -71,9 +71,9 @@ public class ManyDataSource {
//设置动态数据源 //设置动态数据源
DynamicDataSource dynamicDataSource = new DynamicDataSource(); DynamicDataSource dynamicDataSource = new DynamicDataSource();
// dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
dynamicDataSource.setTargetDataSources(dataSourceMap); dynamicDataSource.setTargetDataSources(dataSourceMap);
//将数据源信息备份在defineTargetDataSources中
dynamicDataSource.setDefineTargetDataSources(dataSourceMap); dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
return dynamicDataSource; return dynamicDataSource;
} }

View File

@ -7,7 +7,8 @@ package com.muyu.clw.common.many.datasource.constents;
*/ */
public class DatasourceContent { public class DatasourceContent {
public final static String DATASOURCE_URL = "jdbc:mysql://{}:{}/car?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&failOverReadOnly=false"; public final static String DATASOURCE_URL = "jdbc:mysql://{}:{}/car?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true&failOverReadOnly=false";
public final static String USER_NAME = "root"; public final static String USER_NAME = "root";

View File

@ -1,10 +1,12 @@
package com.muyu.clw.common.many.datasource.factory; package com.muyu.clw.common.many.datasource.factory;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.muyu.clw.common.many.datasource.domain.model.DataSourceInfo; import com.muyu.clw.common.many.datasource.domain.model.DataSourceInfo;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
/** /**
@ -30,7 +32,16 @@ public class DruidDataSourceFactory {
druidDataSource.setBreakAfterAcquireFailure(true); druidDataSource.setBreakAfterAcquireFailure(true);
druidDataSource.setConnectionErrorRetryAttempts(0); druidDataSource.setConnectionErrorRetryAttempts(0);
druidDataSource.setMaxActive(20); // 设置最大活动连接数为 20 druidDataSource.setMaxActive(20); // 设置最大活动连接数为 20
log.info( "{}->数据源连接成功", dataSourceInfo.getKey());
// try {
// Thread.sleep(4000);
// druidDataSource.getConnection();
// log.info("{}->数据库连接成功", dataSourceInfo.getKey());
//
// } catch (Exception e) {
// log.error("数据库连接失败: {}", e.getMessage());
// throw new RuntimeException("数据库连接失败", e);
// }
return druidDataSource; return druidDataSource;
} }
} }

View File

@ -42,7 +42,8 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
*/ */
public void put(String key, DruidDataSource value) { public void put(String key, DruidDataSource value) {
defineTargetDataSources.put(key, value); defineTargetDataSources.put(key, value);
log.info("都有哪些数据:{}",defineTargetDataSources); this.afterPropertiesSet();
log.info("defineTargetDataSources:{}",defineTargetDataSources);
} }
/** /**
* 线使 * 线使
@ -51,4 +52,8 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
protected Object determineCurrentLookupKey() { protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDynamicDataSourceKey(); return DynamicDataSourceHolder.getDynamicDataSourceKey();
} }
public Map<Object, Object> list(){
return defineTargetDataSources;
}
} }

View File

@ -31,24 +31,22 @@ public class FenceController extends BaseController
/** /**
* *
*/ */
@RequiresPermissions("many:fence:list")
@GetMapping("/list") @GetMapping("/list")
public Result<TableDataInfo<Fence>> list(Fence fence) public Result<TableDataInfo<Fence>> list(Fence fence,@RequestHeader("ent_code")String headerValue)
{ {
startPage(); startPage();
List<Fence> list = fenceService.selectFenceList(fence); List<Fence> list = fenceService.selectFenceList(fence,headerValue);
return getDataTable(list); return getDataTable(list);
} }
/** /**
* *
*/ */
@RequiresPermissions("many:fence:export")
@Log(title = "电子围栏", businessType = BusinessType.EXPORT) @Log(title = "电子围栏", businessType = BusinessType.EXPORT)
@PostMapping("/export") @PostMapping("/export")
public void export(HttpServletResponse response, Fence fence) public void export(HttpServletResponse response, Fence fence,@RequestHeader("ent_code")String headerValue)
{ {
List<Fence> list = fenceService.selectFenceList(fence); List<Fence> list = fenceService.selectFenceList(fence,headerValue);
ExcelUtil<Fence> util = new ExcelUtil<Fence>(Fence.class); ExcelUtil<Fence> util = new ExcelUtil<Fence>(Fence.class);
util.exportExcel(response, list, "电子围栏数据"); util.exportExcel(response, list, "电子围栏数据");
} }
@ -56,43 +54,39 @@ public class FenceController extends BaseController
/** /**
* *
*/ */
@RequiresPermissions("many:fence:query")
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
public Result getInfo(@PathVariable("id") Long id) public Result getInfo(@PathVariable("id") Long id,@RequestHeader("ent_code")String headerValue)
{ {
return success(fenceService.selectFenceById(id)); return success(fenceService.selectFenceById(id,headerValue));
} }
/** /**
* *
*/ */
@RequiresPermissions("many:fence:add")
@Log(title = "电子围栏", businessType = BusinessType.INSERT) @Log(title = "电子围栏", businessType = BusinessType.INSERT)
@PostMapping @PostMapping
public Result add(@RequestBody Fence fence) public Result add(@RequestBody Fence fence,@RequestHeader("ent_code")String headerValue)
{ {
return toAjax(fenceService.insertFence(fence)); return toAjax(fenceService.insertFence(fence,headerValue));
} }
/** /**
* *
*/ */
@RequiresPermissions("many:fence:edit")
@Log(title = "电子围栏", businessType = BusinessType.UPDATE) @Log(title = "电子围栏", businessType = BusinessType.UPDATE)
@PutMapping @PutMapping
public Result edit(@RequestBody Fence fence) public Result edit(@RequestBody Fence fence,@RequestHeader("ent_code")String headerValue)
{ {
return toAjax(fenceService.updateFence(fence)); return toAjax(fenceService.updateFence(fence,headerValue));
} }
/** /**
* *
*/ */
@RequiresPermissions("many:fence:remove")
@Log(title = "电子围栏", businessType = BusinessType.DELETE) @Log(title = "电子围栏", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}") @DeleteMapping("/{ids}")
public Result remove(@PathVariable Long[] ids) public Result remove(@PathVariable Long[] ids,@RequestHeader("ent_code")String headerValue)
{ {
return toAjax(fenceService.deleteFenceByIds(ids)); return toAjax(fenceService.deleteFenceByIds(ids,headerValue));
} }
} }

View File

@ -5,31 +5,33 @@ import com.alibaba.fastjson.JSON;
import com.muyu.authentication.controller.ManyEnterpriseController; import com.muyu.authentication.controller.ManyEnterpriseController;
import com.muyu.authentication.service.impl.ManyEnterpriseServiceImpl; import com.muyu.authentication.service.impl.ManyEnterpriseServiceImpl;
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.domain.model.DataSourceInfo;
import com.muyu.clw.common.many.datasource.factory.DruidDataSourceFactory; import com.muyu.clw.common.many.datasource.factory.DruidDataSourceFactory;
import com.muyu.clw.common.many.datasource.role.DynamicDataSource; import com.muyu.clw.common.many.datasource.role.DynamicDataSource;
import com.muyu.common.core.utils.SpringUtils; import com.muyu.common.core.utils.SpringUtils;
import com.muyu.many.domain.Enterprise ; import com.muyu.many.domain.Enterprise ;
import com.muyu.remote.RemoteManyEnterpriseService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Log4j2 @Log4j2
@Component @Component
public class MessageConsumer { public class MessageConsumer {
@Autowired @Autowired
private ManyEnterpriseController manyEnterpriseController; private DruidDataSourceFactory dataSourceFactory;
@Autowired
private DynamicDataSource druidDataSource;
@RabbitListener(queues = "text1") @RabbitListener(queues = "text1")
public void receiveMessage(String message) { public void receiveMessage(String message) {
// 在这里处理接收到的消息并将其写入MySQL数据库 // 在这里处理接收到的消息并将其写入MySQL数据库
log.info("message:{}",message); log.info("message:{}",message);
DruidDataSourceFactory dataSourceFactory = SpringUtils.getBean(DruidDataSourceFactory.class);
DynamicDataSource druidDataSource = SpringUtils.getBean(DynamicDataSource.class);
Enterprise parse = JSON.parseObject(message, Enterprise.class); Enterprise parse = JSON.parseObject(message, Enterprise.class);
String substring = parse.getContactPhone().substring(parse.getContactPhone().length() - 4); String substring = parse.getContactPhone().substring(parse.getContactPhone().length() - 4);
String host="123.56.102.11"; String host="123.56.102.11";
@ -39,10 +41,11 @@ public class MessageConsumer {
DataSourceInfo dataSourceInfo = DataSourceInfo.hostAndPortBuild(key, host, post); DataSourceInfo dataSourceInfo = DataSourceInfo.hostAndPortBuild(key, host, post);
log.info("dataSourceInfo:{}",dataSourceInfo);
DruidDataSource dataSource = dataSourceFactory.create(dataSourceInfo); DruidDataSource dataSource = dataSourceFactory.create(dataSourceInfo);
druidDataSource.put(key,dataSource); druidDataSource.put(key,dataSource);
} }
} }

View File

@ -20,7 +20,7 @@ public interface IFenceService
* @param id * @param id
* @return * @return
*/ */
public Fence selectFenceById(Long id); public Fence selectFenceById(Long id,String headerValue);
/** /**
* *
@ -28,7 +28,7 @@ public interface IFenceService
* @param fence * @param fence
* @return * @return
*/ */
public List<Fence> selectFenceList(Fence fence); public List<Fence> selectFenceList(Fence fence,String headerValue);
/** /**
* *
@ -36,7 +36,7 @@ public interface IFenceService
* @param fence * @param fence
* @return * @return
*/ */
public int insertFence(Fence fence); public int insertFence(Fence fence,String headerValue);
/** /**
* *
@ -44,7 +44,7 @@ public interface IFenceService
* @param fence * @param fence
* @return * @return
*/ */
public int updateFence(Fence fence); public int updateFence(Fence fence,String headerValue);
/** /**
* *
@ -52,7 +52,7 @@ public interface IFenceService
* @param ids * @param ids
* @return * @return
*/ */
public int deleteFenceByIds(Long[] ids); public int deleteFenceByIds(Long[] ids,String headerValue);
/** /**
* *
@ -60,5 +60,5 @@ public interface IFenceService
* @param id * @param id
* @return * @return
*/ */
public int deleteFenceById(Long id); public int deleteFenceById(Long id,String headerValue);
} }

View File

@ -2,6 +2,7 @@ package com.muyu.authentication.service.impl;
import com.muyu.authentication.mapper.FenceMapper; import com.muyu.authentication.mapper.FenceMapper;
import com.muyu.authentication.service.IFenceService; import com.muyu.authentication.service.IFenceService;
import com.muyu.clw.common.many.datasource.holder.DynamicDataSourceHolder;
import com.muyu.common.core.utils.DateUtils; import com.muyu.common.core.utils.DateUtils;
import com.muyu.many.domain.Fence; import com.muyu.many.domain.Fence;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -28,9 +29,12 @@ public class FenceServiceImpl implements IFenceService
* @return * @return
*/ */
@Override @Override
public Fence selectFenceById(Long id) public Fence selectFenceById(Long id,String headerValue)
{ {
return fenceMapper.selectFenceById(id); getMany(headerValue);
Fence fence = fenceMapper.selectFenceById(id);
getManyDelete();
return fence;
} }
/** /**
@ -40,9 +44,13 @@ public class FenceServiceImpl implements IFenceService
* @return * @return
*/ */
@Override @Override
public List<Fence> selectFenceList(Fence fence) public List<Fence> selectFenceList(Fence fence,String headerValue)
{ {
return fenceMapper.selectFenceList(fence); getMany(headerValue);
List<Fence> fences = fenceMapper.selectFenceList(fence);
getManyDelete();
return fences;
} }
/** /**
@ -52,10 +60,14 @@ public class FenceServiceImpl implements IFenceService
* @return * @return
*/ */
@Override @Override
public int insertFence(Fence fence) public int insertFence(Fence fence,String headerValue)
{ {
getMany(headerValue);
fence.setCreateTime(DateUtils.getNowDate()); fence.setCreateTime(DateUtils.getNowDate());
return fenceMapper.insertFence(fence); int i = fenceMapper.insertFence(fence);
getManyDelete();
return i;
} }
/** /**
@ -65,10 +77,14 @@ public class FenceServiceImpl implements IFenceService
* @return * @return
*/ */
@Override @Override
public int updateFence(Fence fence) public int updateFence(Fence fence,String headerValue)
{ {
getMany(headerValue);
fence.setUpdateTime(DateUtils.getNowDate()); fence.setUpdateTime(DateUtils.getNowDate());
return fenceMapper.updateFence(fence); int i = fenceMapper.updateFence(fence);
getManyDelete();
return i;
} }
/** /**
@ -78,9 +94,13 @@ public class FenceServiceImpl implements IFenceService
* @return * @return
*/ */
@Override @Override
public int deleteFenceByIds(Long[] ids) public int deleteFenceByIds(Long[] ids,String headerValue)
{ {
return fenceMapper.deleteFenceByIds(ids); getMany(headerValue);
int i = fenceMapper.deleteFenceByIds(ids);
getManyDelete();
return i;
} }
/** /**
@ -90,8 +110,20 @@ public class FenceServiceImpl implements IFenceService
* @return * @return
*/ */
@Override @Override
public int deleteFenceById(Long id) public int deleteFenceById(Long id,String headerValue)
{ {
return fenceMapper.deleteFenceById(id); getMany(headerValue);
int i = fenceMapper.deleteFenceById(id);
getManyDelete();
return i;
}
public void getMany(String entCode){
//切换数据库 切换到从数据库
DynamicDataSourceHolder.setDynamicDataSourceKey(entCode);
}
public void getManyDelete (){
//切换数据库 切换到从数据库
DynamicDataSourceHolder.removeDynamicDataSourceKey();
} }
} }

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.muyu.authentication.mapper.ManyEnterpriseMapper">
</mapper>