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); + } }