From 4d7e4aae1a60b01147181c6c0507f420de6437f8 Mon Sep 17 00:00:00 2001 From: wenjy Date: Tue, 16 Jul 2024 15:35:25 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E6=B7=BB=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=94=A8=E6=88=B7=E6=95=B0=E6=8D=AE=E6=BA=90API?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mesnac/system/api/RemoteUserService.java | 8 +++- .../factory/RemoteUserFallbackFactory.java | 10 ++++- .../mesnac/ems/config/DynamicDatasource.java | 40 +++++++++++++++++++ .../system/controller/SysUserController.java | 28 +++++++++---- 4 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 mesnac-modules/mesnac-ems/src/main/java/com/mesnac/ems/config/DynamicDatasource.java diff --git a/mesnac-api/mesnac-api-system/src/main/java/com/mesnac/system/api/RemoteUserService.java b/mesnac-api/mesnac-api-system/src/main/java/com/mesnac/system/api/RemoteUserService.java index b724cb2..6f104ee 100644 --- a/mesnac-api/mesnac-api-system/src/main/java/com/mesnac/system/api/RemoteUserService.java +++ b/mesnac-api/mesnac-api-system/src/main/java/com/mesnac/system/api/RemoteUserService.java @@ -13,9 +13,12 @@ import com.mesnac.system.api.domain.SysUser; import com.mesnac.system.api.factory.RemoteUserFallbackFactory; import com.mesnac.system.api.model.LoginUser; +import java.util.List; +import java.util.Map; + /** * 用户服务 - * + * * @author ruoyi */ @FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteUserFallbackFactory.class) @@ -40,4 +43,7 @@ public interface RemoteUserService */ @PostMapping("/user/register") public R registerUserInfo(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); + + @PostMapping("/user/getPoolNameList") + public R>> getPoolNameList(@RequestBody SysUser sysUser, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); } diff --git a/mesnac-api/mesnac-api-system/src/main/java/com/mesnac/system/api/factory/RemoteUserFallbackFactory.java b/mesnac-api/mesnac-api-system/src/main/java/com/mesnac/system/api/factory/RemoteUserFallbackFactory.java index 7ff39ea..fc6ae4f 100644 --- a/mesnac-api/mesnac-api-system/src/main/java/com/mesnac/system/api/factory/RemoteUserFallbackFactory.java +++ b/mesnac-api/mesnac-api-system/src/main/java/com/mesnac/system/api/factory/RemoteUserFallbackFactory.java @@ -9,9 +9,12 @@ import com.mesnac.system.api.RemoteUserService; import com.mesnac.system.api.domain.SysUser; import com.mesnac.system.api.model.LoginUser; +import java.util.List; +import java.util.Map; + /** * 用户服务降级处理 - * + * * @author ruoyi */ @Component @@ -36,6 +39,11 @@ public class RemoteUserFallbackFactory implements FallbackFactory>> getPoolNameList(SysUser sysUser, String source) { + return R.fail("获取数据源信息失败:" + throwable.getMessage()); + } }; } } diff --git a/mesnac-modules/mesnac-ems/src/main/java/com/mesnac/ems/config/DynamicDatasource.java b/mesnac-modules/mesnac-ems/src/main/java/com/mesnac/ems/config/DynamicDatasource.java new file mode 100644 index 0000000..4b85e3c --- /dev/null +++ b/mesnac-modules/mesnac-ems/src/main/java/com/mesnac/ems/config/DynamicDatasource.java @@ -0,0 +1,40 @@ +package com.mesnac.ems.config; + +import com.baomidou.dynamic.datasource.creator.DataSourceProperty; +import com.mesnac.common.core.constant.SecurityConstants; +import com.mesnac.common.core.domain.R; +import com.mesnac.common.datasource.creator.DynamicDatasourceCreator; +import com.mesnac.system.api.RemoteUserService; +import com.mesnac.system.api.domain.SysUser; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; + +@Component +public class DynamicDatasource { + @Resource + private DynamicDatasourceCreator dynamicDatasourceCreator; + + @Resource + private RemoteUserService remoteUserService; + + @PostConstruct + public void init() { + SysUser sysUser = new SysUser(); + sysUser.setUserId(1L); + R>> dateSources = remoteUserService.getPoolNameList(sysUser, SecurityConstants.INNER); + for (Map dateSource : dateSources.getData()) { + DataSourceProperty sdp = new DataSourceProperty(); + sdp.setUrl(dateSource.get("url")); + sdp.setUsername(dateSource.get("userName")); + sdp.setPassword(dateSource.get("password")); + sdp.setDriverClassName(dateSource.get("driveClassName")); + sdp.setPoolName(dateSource.get("poolName"));// 这是数据源的key + sdp.setLazy(false); + dynamicDatasourceCreator.createDynamicDataSource(sdp); + } + } +} diff --git a/mesnac-modules/mesnac-system/src/main/java/com/mesnac/system/controller/SysUserController.java b/mesnac-modules/mesnac-system/src/main/java/com/mesnac/system/controller/SysUserController.java index f0a6c47..6a42bc8 100644 --- a/mesnac-modules/mesnac-system/src/main/java/com/mesnac/system/controller/SysUserController.java +++ b/mesnac-modules/mesnac-system/src/main/java/com/mesnac/system/controller/SysUserController.java @@ -8,6 +8,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; import com.mesnac.common.core.constant.CacheConstants; +import com.mesnac.common.core.constant.SecurityConstants; import com.mesnac.common.core.text.Convert; import com.mesnac.common.redis.service.RedisService; import com.mesnac.system.domain.SysUserDatasource; @@ -16,14 +17,7 @@ import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import com.mesnac.common.core.domain.R; import com.mesnac.common.core.utils.StringUtils; @@ -462,4 +456,22 @@ public class SysUserController extends BaseController } return result; } + + /** + * + * @param sysUser + * @param source + * @return + */ + @InnerAuth + @PostMapping("/getPoolNameList") + public R>> getPoolNameList(@RequestBody SysUser sysUser,@RequestHeader(SecurityConstants.FROM_SOURCE) String source) { + if (StringUtils.isNull(sysUser)) { + return R.fail("用户信息为空"); + } + + List> poolNameList = userService.getPoolNameList(sysUser); + + return R.ok(poolNameList); + } }