fast()多数据源
parent
15edaa28f1
commit
904481ca38
|
@ -22,19 +22,15 @@ public class Fence {
|
||||||
/**
|
/**
|
||||||
* 围栏类型
|
* 围栏类型
|
||||||
*/
|
*/
|
||||||
private String fenceType;
|
private String fenceType = "多边形";
|
||||||
/**
|
|
||||||
* 半径
|
|
||||||
*/
|
|
||||||
private Double radius;
|
|
||||||
/**
|
/**
|
||||||
* 驶入驶出
|
* 驶入驶出
|
||||||
*/
|
*/
|
||||||
private String eventType;
|
private String eventType = "N";
|
||||||
/**
|
/**
|
||||||
* 围栏状态
|
* 围栏状态
|
||||||
*/
|
*/
|
||||||
private String staut;
|
private String staut = "Y";
|
||||||
/**
|
/**
|
||||||
*坐标
|
*坐标
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -12,6 +12,4 @@ import lombok.ToString;
|
||||||
public class Path {
|
public class Path {
|
||||||
private String lat;
|
private String lat;
|
||||||
private String lng;
|
private String lng;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.business.common.psvm;
|
||||||
|
|
||||||
|
import com.business.common.domain.Path;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class Pences {
|
||||||
|
private Long groupsId;
|
||||||
|
private List<Path> list;
|
||||||
|
}
|
|
@ -3,10 +3,12 @@ package com.muyu.goods.controller;
|
||||||
import com.business.common.domain.Fence;
|
import com.business.common.domain.Fence;
|
||||||
import com.business.common.domain.FenceGroups;
|
import com.business.common.domain.FenceGroups;
|
||||||
import com.business.common.domain.Path;
|
import com.business.common.domain.Path;
|
||||||
|
import com.business.common.psvm.Pences;
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.common.core.web.controller.BaseController;
|
import com.muyu.common.core.web.controller.BaseController;
|
||||||
import com.muyu.goods.service.IMapService;
|
import com.muyu.goods.service.IMapService;
|
||||||
import com.muyu.goods.service.impl.MapService;
|
import com.muyu.goods.service.impl.MapService;
|
||||||
|
import org.apache.commons.codec.language.bm.Rule;
|
||||||
import org.apache.poi.ss.formula.functions.T;
|
import org.apache.poi.ss.formula.functions.T;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
@ -48,17 +50,42 @@ public class MapController extends BaseController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查看
|
* 获取电子围栏
|
||||||
* @param list
|
* @param list
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@PostMapping("getSel")
|
@PostMapping("getSel")
|
||||||
public Result<Path> getSel(@RequestBody List<Path> list){
|
public Result getSel(@RequestBody Pences list){
|
||||||
return success(iMapService.getSel(list));
|
return success(iMapService.getSel(list));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("setSel")
|
/**
|
||||||
public String setSel(Object r){
|
* 电子围栏
|
||||||
return iMapService.setSel(r);
|
* @param fenceId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("deleteFence/{fenceId}")
|
||||||
|
public Result deleteFence(@PathVariable Long fenceId){
|
||||||
|
return success(iMapService.deleteFence(fenceId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查看详情
|
||||||
|
* @param fenceId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("queryFence/{fenceId}")
|
||||||
|
public Result<Fence> queryFence(@PathVariable Long fenceId){
|
||||||
|
return success(iMapService.queryFence(fenceId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改状态
|
||||||
|
* @param fenceId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("updateFence/{fenceId}")
|
||||||
|
public Result updateFence(@PathVariable Long fenceId) {
|
||||||
|
return success(iMapService.updateFence(fenceId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,4 +12,12 @@ public interface MapMapper {
|
||||||
List<FenceGroups> selectFenceGroups();
|
List<FenceGroups> selectFenceGroups();
|
||||||
|
|
||||||
List<Fence> selectFence();
|
List<Fence> selectFence();
|
||||||
|
|
||||||
|
int indexFence(Fence fence);
|
||||||
|
|
||||||
|
int deleteFence(@Param("fenceId") Long fenceId);
|
||||||
|
|
||||||
|
Fence queryFence(@Param("fenceId") Long fenceId);
|
||||||
|
|
||||||
|
int updateFence(@Param("fenceId") Long fenceId, @Param("status") String status);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@ package com.muyu.goods.service;
|
||||||
import com.business.common.domain.Fence;
|
import com.business.common.domain.Fence;
|
||||||
import com.business.common.domain.FenceGroups;
|
import com.business.common.domain.FenceGroups;
|
||||||
import com.business.common.domain.Path;
|
import com.business.common.domain.Path;
|
||||||
|
import com.business.common.psvm.Pences;
|
||||||
import org.apache.poi.ss.formula.functions.T;
|
import org.apache.poi.ss.formula.functions.T;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -15,7 +17,11 @@ public interface IMapService {
|
||||||
|
|
||||||
FenceGroups selectFenceGroupsById(Long carId);
|
FenceGroups selectFenceGroupsById(Long carId);
|
||||||
|
|
||||||
List<Path> getSel(List<Path> list);
|
String getSel(Pences list);
|
||||||
|
|
||||||
String setSel(Object r);
|
String deleteFence(Long fenceId);
|
||||||
|
|
||||||
|
Fence queryFence(Long fenceId);
|
||||||
|
|
||||||
|
String updateFence(Long fenceId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,6 +79,7 @@ public class CarServiceImpl implements ICarService
|
||||||
int i = carMapper.insertCar(car);
|
int i = carMapper.insertCar(car);
|
||||||
if (i>0) {
|
if (i>0) {
|
||||||
Car car1 = carMapper.carByCar();
|
Car car1 = carMapper.carByCar();
|
||||||
|
System.out.println(car1);
|
||||||
mapMapper.indexFenceGroups(car1.getCarId(),"car"+car1.getCarId());
|
mapMapper.indexFenceGroups(car1.getCarId(),"car"+car1.getCarId());
|
||||||
}
|
}
|
||||||
return i;
|
return i;
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
package com.muyu.goods.service.impl;
|
package com.muyu.goods.service.impl;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.business.common.domain.Fence;
|
import com.business.common.domain.Fence;
|
||||||
import com.business.common.domain.FenceGroups;
|
import com.business.common.domain.FenceGroups;
|
||||||
import com.business.common.domain.Path;
|
import com.business.common.domain.Path;
|
||||||
|
import com.business.common.psvm.Pences;
|
||||||
import com.muyu.goods.mapper.MapMapper;
|
import com.muyu.goods.mapper.MapMapper;
|
||||||
import com.muyu.goods.service.IMapService;
|
import com.muyu.goods.service.IMapService;
|
||||||
import org.apache.poi.ss.formula.functions.T;
|
import org.apache.poi.ss.formula.functions.T;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
@ -29,7 +32,6 @@ public class MapService implements IMapService {
|
||||||
List<Fence> fenceList = selectFence().stream().filter(fa -> fa.getGroupId() == fenceGroups.getGroupsId()).collect(Collectors.toList());
|
List<Fence> fenceList = selectFence().stream().filter(fa -> fa.getGroupId() == fenceGroups.getGroupsId()).collect(Collectors.toList());
|
||||||
fenceGroups.setFenceList(fenceList);
|
fenceGroups.setFenceList(fenceList);
|
||||||
}
|
}
|
||||||
System.out.println(fenceGroupsList);
|
|
||||||
return fenceGroupsList;
|
return fenceGroupsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,10 +48,52 @@ public class MapService implements IMapService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Path> getSel(List<Path> list) {
|
public String getSel(Pences pences) {
|
||||||
System.out.println(list);
|
System.out.println(pences);
|
||||||
String jsonString = JSONObject.toJSONString(list);
|
if (pences.getGroupsId() == null){
|
||||||
Path path = JSONObject.parseObject(jsonString, Path.class);
|
return "数据不全";
|
||||||
return null;
|
}
|
||||||
|
FenceGroups fenceGroups = selectFenceGroups().stream().filter(g -> g.getGroupsId() == pences.getGroupsId()).collect(Collectors.toList()).get(0);
|
||||||
|
Fence fence = new Fence();
|
||||||
|
fence.setGroupId(pences.getGroupsId());
|
||||||
|
List<Path> list = pences.getList();
|
||||||
|
String jsonString = JSONArray.toJSONString(list);
|
||||||
|
fence.setPolygonPoints(jsonString);
|
||||||
|
List<Path> paths = JSONArray.parseArray(jsonString, Path.class);
|
||||||
|
fence.setFenceName("围栏"+ (fenceGroups.getFenceList().size()+1));
|
||||||
|
int i = mapMapper.indexFence(fence);
|
||||||
|
if (i> 0) {
|
||||||
|
return "传入电子围栏";
|
||||||
|
}
|
||||||
|
return "传入电子围栏失败";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String deleteFence(Long fenceId) {
|
||||||
|
int i = mapMapper.deleteFence(fenceId);
|
||||||
|
if (i>0){
|
||||||
|
return "完成";
|
||||||
|
}
|
||||||
|
return "无效";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Fence queryFence(Long fenceId) {
|
||||||
|
return mapMapper.queryFence(fenceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String updateFence(Long fenceId) {
|
||||||
|
Fence fence = queryFence(fenceId);
|
||||||
|
int i = 0;
|
||||||
|
if (fence.getStaut().equals("Y")){
|
||||||
|
i = mapMapper.updateFence(fenceId,"N");
|
||||||
|
}else {
|
||||||
|
i = mapMapper.updateFence(fenceId,"Y");
|
||||||
|
}
|
||||||
|
if (i>0){
|
||||||
|
return "成功";
|
||||||
|
}
|
||||||
|
return "失败";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,4 +25,4 @@ spring:
|
||||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
com.muyu.system.mapper: DEBUG
|
com.muyu.rule.mapper: DEBUG
|
||||||
|
|
|
@ -44,7 +44,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
</select>
|
</select>
|
||||||
<select id="carByCar" resultType="com.business.common.domain.Car">
|
<select id="carByCar" resultType="com.business.common.domain.Car">
|
||||||
<include refid="selectCarVo"></include>
|
<include refid="selectCarVo"></include>
|
||||||
ORDER BY id DESC LIMIT 1;
|
ORDER BY car_id DESC LIMIT 1;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<insert id="insertCar" parameterType="com.business.common.domain.Car" useGeneratedKeys="true" keyProperty="carId">
|
<insert id="insertCar" parameterType="com.business.common.domain.Car" useGeneratedKeys="true" keyProperty="carId">
|
||||||
|
|
|
@ -15,9 +15,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
<result property="groupId" column="group_id" />
|
<result property="groupId" column="group_id" />
|
||||||
<result property="fenceName" column="fence_name" />
|
<result property="fenceName" column="fence_name" />
|
||||||
<result property="fenceType" column="fence_type" />
|
<result property="fenceType" column="fence_type" />
|
||||||
<result property="radius" column="radius" />
|
|
||||||
<result property="eventType" column="event_type" />
|
<result property="eventType" column="event_type" />
|
||||||
<result property="status" column="status" />
|
<result property="staut" column="staut" />
|
||||||
<result property="polygonPoints" column="polygon_points" />
|
<result property="polygonPoints" column="polygon_points" />
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
|
@ -26,18 +25,51 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
</sql>
|
</sql>
|
||||||
|
|
||||||
<sql id="FencesVo">
|
<sql id="FencesVo">
|
||||||
select fence_id, group_id, fence_name, fence_type, radius, event_type, staut, polygon_points from fence
|
select fence_id, group_id, fence_name, fence_type, event_type, staut, polygon_points from fence
|
||||||
</sql>
|
</sql>
|
||||||
<insert id="indexFenceGroups">
|
<insert id="indexFenceGroups">
|
||||||
insert into fence_groups ( groups_id, car_id, groups_name )
|
insert into fence_groups ( groups_id, car_id, groups_name )
|
||||||
values (0,#{carId},#{groupsName});
|
values (0,#{carId},#{groupsName});
|
||||||
</insert>
|
</insert>
|
||||||
|
<insert id="indexFence">
|
||||||
|
insert into fence
|
||||||
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="groupId != null">group_id,</if>
|
||||||
|
<if test="fenceName != null">fence_name,</if>
|
||||||
|
<if test="fenceType != null">fence_type,</if>
|
||||||
|
<if test="eventType != null">event_type,</if>
|
||||||
|
<if test="staut != null">staut,</if>
|
||||||
|
<if test="polygonPoints != null">polygon_points,</if>
|
||||||
|
</trim>
|
||||||
|
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="groupId != null">#{groupId},</if>
|
||||||
|
<if test="fenceName != null">#{fenceName},</if>
|
||||||
|
<if test="fenceType != null">#{fenceType},</if>
|
||||||
|
<if test="eventType != null">#{eventType},</if>
|
||||||
|
<if test="staut != null">#{staut},</if>
|
||||||
|
<if test="polygonPoints != null">#{polygonPoints},</if>
|
||||||
|
</trim>
|
||||||
|
</insert>
|
||||||
|
<update id="updateFence">
|
||||||
|
update fence
|
||||||
|
set staut = #{status}
|
||||||
|
where fence_id = #{fenceId};
|
||||||
|
</update>
|
||||||
|
<delete id="deleteFence">
|
||||||
|
delete
|
||||||
|
from fence
|
||||||
|
where fence_id = #{fenceId};
|
||||||
|
</delete>
|
||||||
<select id="selectFenceGroups" resultType="com.business.common.domain.FenceGroups">
|
<select id="selectFenceGroups" resultType="com.business.common.domain.FenceGroups">
|
||||||
<include refid="FenceGroupsVo"></include>
|
<include refid="FenceGroupsVo"></include>
|
||||||
</select>
|
</select>
|
||||||
<select id="selectFence" resultType="com.business.common.domain.Fence">
|
<select id="selectFence" resultType="com.business.common.domain.Fence">
|
||||||
<include refid="FencesVo"></include>
|
<include refid="FencesVo"></include>
|
||||||
</select>
|
</select>
|
||||||
|
<select id="queryFence" resultType="com.business.common.domain.Fence">
|
||||||
|
<include refid="FencesVo"></include>
|
||||||
|
where fence_id = #{fenceId}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.muyu.goods.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@SuperBuilder
|
||||||
|
public class Sources {
|
||||||
|
/**
|
||||||
|
* 主键
|
||||||
|
*/
|
||||||
|
private Long id;
|
||||||
|
/**
|
||||||
|
* 名称
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
/**
|
||||||
|
* 连接库
|
||||||
|
*/
|
||||||
|
private String url = "ry_goods";
|
||||||
|
/**
|
||||||
|
* ip
|
||||||
|
*/
|
||||||
|
private String ip;
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
private String username = "root";
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
private String password = "Qq4152637";
|
||||||
|
|
||||||
|
public static Sources index(String name,String ip) {
|
||||||
|
return builder()
|
||||||
|
.name(name)
|
||||||
|
.url("ry_goods")
|
||||||
|
.ip(ip)
|
||||||
|
.username("root")
|
||||||
|
.password("Qq4152637")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -148,4 +148,9 @@ public class EnterpriseController extends BaseController
|
||||||
return success(enterpriseService.getLevel(id,serviceLevel));
|
return success(enterpriseService.getLevel(id,serviceLevel));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("bu/{id}")
|
||||||
|
public Result bu(@PathVariable Long id){
|
||||||
|
return success(enterpriseService.bu(id));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.muyu.goods.mapper;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import com.muyu.goods.domain.Enterprise;
|
import com.muyu.goods.domain.Enterprise;
|
||||||
|
import com.muyu.goods.domain.Sources;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,4 +70,5 @@ public interface EnterpriseMapper
|
||||||
int getLevel(@Param("id") Long id, @Param("serviceLevel") Integer serviceLevel);
|
int getLevel(@Param("id") Long id, @Param("serviceLevel") Integer serviceLevel);
|
||||||
|
|
||||||
|
|
||||||
|
void indexSources(Sources sources);
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,4 +77,6 @@ public interface IEnterpriseService
|
||||||
List<Enterprise> lists();
|
List<Enterprise> lists();
|
||||||
|
|
||||||
String getLevel(Long id, Integer serviceLevel);
|
String getLevel(Long id, Integer serviceLevel);
|
||||||
|
|
||||||
|
String bu(Long id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import com.muyu.common.core.utils.DateUtils;
|
||||||
import com.muyu.common.security.utils.SecurityUtils;
|
import com.muyu.common.security.utils.SecurityUtils;
|
||||||
import com.muyu.common.system.domain.LoginUser;
|
import com.muyu.common.system.domain.LoginUser;
|
||||||
import com.muyu.common.system.domain.SysUser;
|
import com.muyu.common.system.domain.SysUser;
|
||||||
|
import com.muyu.goods.domain.Sources;
|
||||||
import com.muyu.goods.pojo.HttpClient;
|
import com.muyu.goods.pojo.HttpClient;
|
||||||
import com.muyu.system.remote.RemoteSystemManageService;
|
import com.muyu.system.remote.RemoteSystemManageService;
|
||||||
import muyu.goods.enterprise.client.config.EnterpriseConfig;
|
import muyu.goods.enterprise.client.config.EnterpriseConfig;
|
||||||
|
@ -77,6 +78,9 @@ public class EnterpriseServiceImpl implements IEnterpriseService
|
||||||
enterpriseConfig.index(enterprise1);
|
enterpriseConfig.index(enterprise1);
|
||||||
//新建的企业绑定mysql服务
|
//新建的企业绑定mysql服务
|
||||||
HttpClient.http(enterprise1);
|
HttpClient.http(enterprise1);
|
||||||
|
Sources sources = Sources.index("enterprise" + enterprise1.getId(), String.valueOf((int) (enterprise.getId() + 3306)));
|
||||||
|
System.out.println(sources);
|
||||||
|
enterpriseMapper.indexSources(sources);
|
||||||
}
|
}
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@ -106,7 +110,6 @@ public class EnterpriseServiceImpl implements IEnterpriseService
|
||||||
Enterprise enterprise = new Enterprise();
|
Enterprise enterprise = new Enterprise();
|
||||||
enterprise.setId(id);
|
enterprise.setId(id);
|
||||||
enterprise.setAuthenticationDate(DateUtils.getNowDate());
|
enterprise.setAuthenticationDate(DateUtils.getNowDate());
|
||||||
|
|
||||||
return enterpriseMapper.authentication(enterprise);
|
return enterpriseMapper.authentication(enterprise);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,4 +181,18 @@ public class EnterpriseServiceImpl implements IEnterpriseService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String bu(Long id) {
|
||||||
|
Enterprise enterprise = selectEnterpriseById(id);
|
||||||
|
//补充企业绑定mysql服务
|
||||||
|
try {
|
||||||
|
HttpClient.http(enterprise);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return "成功";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,4 +25,4 @@ spring:
|
||||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
com.muyu.system.mapper: DEBUG
|
com.muyu.goods.mapper: DEBUG
|
||||||
|
|
|
@ -91,6 +91,23 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
0,
|
0,
|
||||||
</trim>
|
</trim>
|
||||||
</insert>
|
</insert>
|
||||||
|
<insert id="indexSources">
|
||||||
|
insert into sources
|
||||||
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="name != null">name,</if>
|
||||||
|
<if test="url != null">url,</if>
|
||||||
|
<if test="ip != null">ip,</if>
|
||||||
|
<if test="username != null">username,</if>
|
||||||
|
<if test="password != null">password,</if>
|
||||||
|
</trim>
|
||||||
|
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||||
|
<if test="name != null">#{name},</if>
|
||||||
|
<if test="url != null">#{url},</if>
|
||||||
|
<if test="ip != null">#{ip},</if>
|
||||||
|
<if test="username != null">#{username},</if>
|
||||||
|
<if test="password != null">#{password},</if>
|
||||||
|
</trim>
|
||||||
|
</insert>
|
||||||
|
|
||||||
<update id="updateEnterprise" parameterType="com.muyu.goods.domain.Enterprise">
|
<update id="updateEnterprise" parameterType="com.muyu.goods.domain.Enterprise">
|
||||||
update enterprise
|
update enterprise
|
||||||
|
|
|
@ -0,0 +1,112 @@
|
||||||
|
<?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</artifactId>
|
||||||
|
<version>3.6.3</version>
|
||||||
|
<relativePath>../../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>muyu-moudels-many-datasource</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>
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.muyu.cloud;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多数据源启动类
|
||||||
|
*/
|
||||||
|
@EnableCustomConfig
|
||||||
|
@EnableCustomSwagger2
|
||||||
|
@EnableMyFeignClients
|
||||||
|
@SpringBootApplication(exclude = {DynamicDataSourceAutoConfiguration.class, DataSourceAutoConfiguration.class})
|
||||||
|
public class CloudManyDataSourceApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(CloudManyDataSourceApplication.class,args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.muyu.cloud.common;
|
||||||
|
|
||||||
|
import com.muyu.cloud.datasource.DataSourceInfo;
|
||||||
|
import com.muyu.cloud.datasource.util.DataSourceService;
|
||||||
|
import com.muyu.cloud.service.CloudService;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@Log4j2
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("cloud")
|
||||||
|
public class CloudController {
|
||||||
|
@Autowired
|
||||||
|
private CloudService cloudService;
|
||||||
|
@Autowired
|
||||||
|
private DataSourceService dataSourceService;
|
||||||
|
|
||||||
|
@PostMapping("source")
|
||||||
|
public Result selSource() {
|
||||||
|
Object clout = cloudService.selSource();
|
||||||
|
return Result.success(clout);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增数据源
|
||||||
|
* @param ip
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/{ip}")
|
||||||
|
public Result selectIp(@PathVariable String ip){
|
||||||
|
dataSourceService.addDataSourceIp(DataSourceInfo.dataIpBuild(ip));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.muyu.cloud.datasource;
|
||||||
|
|
||||||
|
import com.muyu.common.core.utils.StringUtils;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import static com.muyu.cloud.datasource.contents.DatasourceContent.*;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 数据源实体类
|
||||||
|
* @Date 2023-8-1 上午 11:15
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class DataSourceInfo {
|
||||||
|
|
||||||
|
private String key;
|
||||||
|
|
||||||
|
private String ip;
|
||||||
|
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
private String userName;
|
||||||
|
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
|
||||||
|
public static DataSourceInfo databaseNameBuild(String databaseName){
|
||||||
|
return DataSourceInfo.builder()
|
||||||
|
.key(databaseName)
|
||||||
|
.url(StringUtils.format(DATASOURCE_URL, databaseName))
|
||||||
|
.password(PASSWORD)
|
||||||
|
.userName(USER_NAME)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DataSourceInfo dataIpBuild(String ip) {
|
||||||
|
return DataSourceInfo.builder()
|
||||||
|
.ip(ip)
|
||||||
|
.url(StringUtils.format(DATASOURCE_URL,ip))
|
||||||
|
.password(PASSWORD)
|
||||||
|
.userName(USER_NAME)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.muyu.cloud.datasource;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据源切换处理
|
||||||
|
*
|
||||||
|
* @author Dongzl
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class DynamicDataSourceHolder {
|
||||||
|
/**
|
||||||
|
* 保存动态数据源名称
|
||||||
|
*/
|
||||||
|
private static final ThreadLocal<String> DYNAMIC_DATASOURCE_KEY = new ThreadLocal<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置/切换数据源,决定当前线程使用哪个数据源
|
||||||
|
*/
|
||||||
|
public static void setDynamicDataSourceKey(String key){
|
||||||
|
log.info("数据源切换为:{}",key);
|
||||||
|
DYNAMIC_DATASOURCE_KEY.set(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取动态数据源名称,默认使用mater数据源
|
||||||
|
*/
|
||||||
|
public static String getDynamicDataSourceKey(){
|
||||||
|
String key = DYNAMIC_DATASOURCE_KEY.get();
|
||||||
|
Assert.notNull(key, "请携带数据标识");
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取动态数据源名称,默认使用ip数据源
|
||||||
|
*/
|
||||||
|
public static String getDynamicDataSourceIp(){
|
||||||
|
String ip = DYNAMIC_DATASOURCE_KEY.get();
|
||||||
|
Assert.notNull(ip, "请携带数据标识");
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除当前数据源
|
||||||
|
*/
|
||||||
|
public static void removeDynamicDataSourceKey(){
|
||||||
|
log.info("移除数据源:{}",DYNAMIC_DATASOURCE_KEY.get());
|
||||||
|
DYNAMIC_DATASOURCE_KEY.remove();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.muyu.cloud.datasource.config;
|
||||||
|
|
||||||
|
import com.muyu.cloud.datasource.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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 数据源切面
|
||||||
|
* @Date 2023-8-2 下午 08:26
|
||||||
|
*/
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class DataSourceAsp {
|
||||||
|
/**
|
||||||
|
* 调用表示层
|
||||||
|
*/
|
||||||
|
@Pointcut("execution(public * com.muyu.cloud.common.*Controller.*(..))")
|
||||||
|
public void pointcut () {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 的每一個方法執行之前 執行的處理
|
||||||
|
*/
|
||||||
|
@Before("pointcut()")
|
||||||
|
public void beforeMethod() {
|
||||||
|
// Long storeId = SecurityUtils.getLoginUser().getSysUser().getUserId();
|
||||||
|
// DynamicDataSourceHolder.setDynamicDataSourceKey("test_"+storeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 的每一個方法執行之后 執行的處理
|
||||||
|
* 无论正常还是异常终了
|
||||||
|
* 不能接受到返回值
|
||||||
|
*/
|
||||||
|
@After("pointcut()")
|
||||||
|
public void afterMethod() {
|
||||||
|
DynamicDataSourceHolder.removeDynamicDataSourceKey();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.muyu.cloud.datasource.config;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import com.muyu.cloud.datasource.DataSourceInfo;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 数据源配置
|
||||||
|
* @Date 2023-8-1 上午 11:05
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Configuration
|
||||||
|
public class DruidConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private List<DataSourceInfo> getDataSourceInfoList(){
|
||||||
|
List<String> databaseNameList = new ArrayList<>(){{
|
||||||
|
add("3310");
|
||||||
|
add("3311");
|
||||||
|
}};
|
||||||
|
return databaseNameList.stream().map(DataSourceInfo::dataIpBuild).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 根据传递的数据源信息测试数据库连接
|
||||||
|
* @Author Dongzl
|
||||||
|
*/
|
||||||
|
public DruidDataSource createDataSourceConnection(DataSourceInfo dataSourceInfo) {
|
||||||
|
DruidDataSource druidDataSource = new DruidDataSource();//配置连接池
|
||||||
|
druidDataSource.setUrl(dataSourceInfo.getUrl());//配置数据库的连接url
|
||||||
|
druidDataSource.setUsername(dataSourceInfo.getUserName());//配置用户名
|
||||||
|
druidDataSource.setPassword(dataSourceInfo.getPassword());//配置密码
|
||||||
|
druidDataSource.setBreakAfterAcquireFailure(true);//配置
|
||||||
|
druidDataSource.setConnectionErrorRetryAttempts(0);//配置
|
||||||
|
try {
|
||||||
|
druidDataSource.getConnection(2000);
|
||||||
|
log.info("{} -> 数据源连接成功", dataSourceInfo.getIp());
|
||||||
|
return druidDataSource;
|
||||||
|
} catch (SQLException throwables) {
|
||||||
|
log.error("数据源 {} 连接失败,用户名:{},密码 {}",dataSourceInfo.getUrl(),dataSourceInfo.getUserName(),dataSourceInfo.getPassword());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Primary
|
||||||
|
public DynamicDataSource dynamicDataSource() {
|
||||||
|
|
||||||
|
Map<Object, Object> dataSourceMap = new HashMap<>();
|
||||||
|
getDataSourceInfoList().forEach(dataSourceInfo -> {
|
||||||
|
dataSourceMap.put(dataSourceInfo.getKey(), createDataSourceConnection(dataSourceInfo));
|
||||||
|
});
|
||||||
|
//设置动态数据源
|
||||||
|
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||||
|
// dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
|
||||||
|
dynamicDataSource.setTargetDataSources(dataSourceMap);
|
||||||
|
//将数据源信息备份在defineTargetDataSources中
|
||||||
|
dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
|
||||||
|
return dynamicDataSource;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.muyu.cloud.datasource.config;
|
||||||
|
|
||||||
|
import com.muyu.cloud.datasource.DynamicDataSourceHolder;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态数据源
|
||||||
|
* 调用AddDefineDataSource组件的addDefineDynamicDataSource()方法,获取原来targetdatasources的map,并将新的数据源信息添加到map中,并替换targetdatasources中的map
|
||||||
|
* 切换数据源时可以使用@DataSource(value = "数据源名称"),或者DynamicDataSourceContextHolder.setContextKey("数据源名称")
|
||||||
|
* @author Dongzl
|
||||||
|
*/
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class DynamicDataSource extends AbstractRoutingDataSource {
|
||||||
|
//备份所有数据源信息,备份是指针
|
||||||
|
private Map<Object, Object> defineTargetDataSources;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 决定当前线程使用哪个数据源
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Object determineCurrentLookupKey() {
|
||||||
|
return DynamicDataSourceHolder.getDynamicDataSourceKey();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.muyu.cloud.datasource.config.factory;
|
||||||
|
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class DruidDataSourceFactory {
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.muyu.cloud.datasource.config.holder;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据源切换处理
|
||||||
|
*
|
||||||
|
* @author Dongzl
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class DynamicDataSourceHolder {
|
||||||
|
/**
|
||||||
|
* 保存动态数据源名称
|
||||||
|
*/
|
||||||
|
private static final ThreadLocal<String> DYNAMIC_DATASOURCE_KEY = new ThreadLocal<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置/切换数据源,决定当前线程使用哪个数据源
|
||||||
|
*/
|
||||||
|
public static void setDynamicDataSourceKey(String key){
|
||||||
|
log.info("数据源切换为:{}",key);
|
||||||
|
DYNAMIC_DATASOURCE_KEY.set(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取动态数据源名称,默认使用mater数据源
|
||||||
|
*/
|
||||||
|
public static String getDynamicDataSourceKey(){
|
||||||
|
String key = DYNAMIC_DATASOURCE_KEY.get();
|
||||||
|
Assert.notNull(key, "请携带数据标识");
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取动态数据源名称,默认使用ip数据源
|
||||||
|
*/
|
||||||
|
public static String getDynamicDataSourceIp(){
|
||||||
|
String ip = DYNAMIC_DATASOURCE_KEY.get();
|
||||||
|
Assert.notNull(ip, "请携带数据标识");
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除当前数据源
|
||||||
|
*/
|
||||||
|
public static void removeDynamicDataSourceKey(){
|
||||||
|
log.info("移除数据源:{}",DYNAMIC_DATASOURCE_KEY.get());
|
||||||
|
DYNAMIC_DATASOURCE_KEY.remove();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.muyu.cloud.datasource.config.role;
|
||||||
|
|
||||||
|
import com.muyu.cloud.datasource.DynamicDataSourceHolder;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态数据源
|
||||||
|
* 调用AddDefineDataSource组件的addDefineDynamicDataSource()方法,获取原来targetdatasources的map,并将新的数据源信息添加到map中,并替换targetdatasources中的map
|
||||||
|
* 切换数据源时可以使用@DataSource(value = "数据源名称"),或者DynamicDataSourceContextHolder.setContextKey("数据源名称")
|
||||||
|
* @author Dongzl
|
||||||
|
*/
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class DynamicDataSource extends AbstractRoutingDataSource {
|
||||||
|
//备份所有数据源信息,备份是指针
|
||||||
|
private Map<Object, Object> defineTargetDataSources;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 决定当前线程使用哪个数据源
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Object determineCurrentLookupKey() {
|
||||||
|
return DynamicDataSourceHolder.getDynamicDataSourceKey();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.muyu.cloud.datasource.contents;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 动态数据源常量类
|
||||||
|
* @Author Dongzl
|
||||||
|
* @Date 2022/8/18 16:33
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CommonConstant {
|
||||||
|
/**
|
||||||
|
* 默认数据源标识
|
||||||
|
*/
|
||||||
|
public static final String MASTER = "master";
|
||||||
|
/**
|
||||||
|
* 从数据源标识
|
||||||
|
*/
|
||||||
|
public static final String SLAVE = "slave";
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
package com.muyu.cloud.datasource.contents;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 数据源常量
|
||||||
|
* @Date 2023-8-1 上午 11:02
|
||||||
|
*/
|
||||||
|
public class DatasourceContent {
|
||||||
|
|
||||||
|
public final static String DATASOURCE_URL = "jdbc:mysql://129.211.23.219:{}/ry_goods?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
|
||||||
|
|
||||||
|
public final static String USER_NAME = "root";
|
||||||
|
|
||||||
|
public final static String PASSWORD = "Qq4152637";
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.muyu.cloud.datasource.util;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import com.muyu.cloud.datasource.DataSourceInfo;
|
||||||
|
import com.muyu.cloud.datasource.config.DruidConfig;
|
||||||
|
import com.muyu.cloud.datasource.config.DynamicDataSource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 数据源工具类
|
||||||
|
* @Author Dongzl
|
||||||
|
* @Date 2022/8/18 17:20
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class DataSourceService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DruidConfig druidConfig;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DynamicDataSource dynamicDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增数据源
|
||||||
|
* @param dataSourceInfo
|
||||||
|
*/
|
||||||
|
public void addDataSource(DataSourceInfo dataSourceInfo){
|
||||||
|
//获取数据源连接请求
|
||||||
|
addDefineDynamicDataSource(druidConfig.createDataSourceConnection(dataSourceInfo), dataSourceInfo.getKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增数据源
|
||||||
|
* @param dataSourceInfo
|
||||||
|
*/
|
||||||
|
public void addDataSourceIp(DataSourceInfo dataSourceInfo){
|
||||||
|
//获取数据源连接请求
|
||||||
|
addDefineDynamicDataSource(druidConfig.createDataSourceConnection(dataSourceInfo), dataSourceInfo.getIp());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 将新增的数据源加入到备份数据源map中
|
||||||
|
* @Author Dongzl
|
||||||
|
*/
|
||||||
|
public void addDefineDynamicDataSource(DruidDataSource druidDataSource, String dataSourceName){
|
||||||
|
Map<Object, Object> defineTargetDataSources = dynamicDataSource.getDefineTargetDataSources();
|
||||||
|
defineTargetDataSources.put(dataSourceName, druidDataSource);
|
||||||
|
dynamicDataSource.setTargetDataSources(defineTargetDataSources);
|
||||||
|
dynamicDataSource.afterPropertiesSet();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.muyu.cloud.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface CloudMapper {
|
||||||
|
Object selSource();
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.muyu.cloud.service;
|
||||||
|
|
||||||
|
public interface CloudService {
|
||||||
|
Object selSource();
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.muyu.cloud.service.impl;
|
||||||
|
|
||||||
|
import com.muyu.cloud.mapper.CloudMapper;
|
||||||
|
import com.muyu.cloud.service.CloudService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CloudServiceImpl implements CloudService {
|
||||||
|
@Autowired
|
||||||
|
private CloudMapper cloudMapper;
|
||||||
|
@Override
|
||||||
|
public Object selSource() {
|
||||||
|
return cloudMapper.selSource();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 9209
|
||||||
|
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: muyu-range
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 129.211.23.219:8848
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 129.211.23.219:8848
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
com.muyu.cloud.mapper: DEBUG
|
|
@ -0,0 +1,74 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||||
|
<!-- 日志存放路径 -->
|
||||||
|
<property name="log.path" value="logs/muyu-moudels-many-datasource"/>
|
||||||
|
<!-- 日志输出格式 -->
|
||||||
|
<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>
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?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.cloud.mapper.CloudMapper">
|
||||||
|
|
||||||
|
|
||||||
|
<select id="selSource" resultType="java.lang.Object">
|
||||||
|
select * from status
|
||||||
|
</select>
|
||||||
|
</mapper>
|
1
pom.xml
1
pom.xml
|
@ -217,6 +217,7 @@
|
||||||
<module>muyu-common</module>
|
<module>muyu-common</module>
|
||||||
<module>muyu-modules/muyu-goods-enterprise</module>
|
<module>muyu-modules/muyu-goods-enterprise</module>
|
||||||
<module>muyu-modules/muyu-business</module>
|
<module>muyu-modules/muyu-business</module>
|
||||||
|
<module>muyu-modules/muyu-moudels-many-datasource</module>
|
||||||
</modules>
|
</modules>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue