Browse Source

完善动态数据库设置

lixing 3 years ago
parent
commit
b31d3da4b9

+ 45 - 11
apm-common/src/main/java/com/persagy/apm/common/handler/PoemsRequestBodyWrapper.java

@@ -22,14 +22,56 @@ import java.util.Map;
 
 /**
  * 将请求体中的用户信息放入线程存储对象中
+ *
+ * @author lixing
+ * @version V1.0 2021/9/9 8:18 下午
  */
 public class PoemsRequestBodyWrapper extends HttpServletRequestWrapper {
+    /**
+     * 存储body转换后的字符串
+     */
+    private String reqBodyStr;
 
     public PoemsRequestBodyWrapper(HttpServletRequest request) throws IOException {
         super(request);
+        // 初始化PoemsContext
+        initPoemsContext();
+    }
+
+    /**
+     * 初始化PoemsContext
+     *
+     * @author lixing
+     * @version V1.0 2021/9/9 8:17 下午
+     */
+    private void initPoemsContext() throws IOException{
+        //从输入流中取出body串, 如果为空,直接返回
+        reqBodyStr = IOUtils.toString(super.getInputStream(), "utf-8");
+        if (StringUtils.isEmpty(reqBodyStr)) {
+            return;
+        }
+
+        //reqBodyStr转为Map对象
+        Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr, new TypeReference<HashMap<String, Object>>() {
+        });
+        String userId = (String) paramMap.get("userId");
+        String loginDevice = (String) paramMap.get("loginDevice");
+        String pd = (String) paramMap.get("pd");
+        String groupCode = (String) paramMap.get("groupCode");
+        String projectId = (String) paramMap.get("projectId");
+
+        if (StringUtils.isBlank(groupCode)) {
+            throw new IllegalArgumentException("请求体中缺少groupCode");
+        }
+        if (StringUtils.isBlank(userId)) {
+            throw new IllegalArgumentException("请求体中缺少userId");
+        }
+        if (StringUtils.isBlank(pd)) {
+            throw new IllegalArgumentException("请求体中缺少pd");
+        }
+        PoemsContext.setContext(userId, loginDevice, pd, groupCode, projectId);
     }
 
-    // 注:一下两个方法一定要重写,否则数据还是空
     @Override
     public BufferedReader getReader() throws IOException {
         return new BufferedReader(new InputStreamReader(this.getInputStream()));
@@ -39,11 +81,10 @@ public class PoemsRequestBodyWrapper extends HttpServletRequestWrapper {
     public ServletInputStream getInputStream() throws IOException {
         //非json类型,直接返回
         if (super.getHeader(HttpHeaders.CONTENT_TYPE) == null ||
-				!super.getHeader(HttpHeaders.CONTENT_TYPE).contains(MediaType.APPLICATION_JSON_VALUE)) {
+                !super.getHeader(HttpHeaders.CONTENT_TYPE).contains(MediaType.APPLICATION_JSON_VALUE)) {
             return super.getInputStream();
         }
-        //从输入流中取出body串, 如果为空,直接返回
-        String reqBodyStr = IOUtils.toString(super.getInputStream(), "utf-8");
+
         if (StringUtils.isEmpty(reqBodyStr)) {
             return super.getInputStream();
         }
@@ -51,12 +92,6 @@ public class PoemsRequestBodyWrapper extends HttpServletRequestWrapper {
         //reqBodyStr转为Map对象
         Map<String, Object> paramMap = new ObjectMapper().readValue(reqBodyStr, new TypeReference<HashMap<String, Object>>() {
         });
-        String userId = (String) paramMap.get("userId");
-        String loginDevice = (String) paramMap.get("loginDevice");
-        String pd = (String) paramMap.get("pd");
-        String groupCode = (String) paramMap.get("groupCode");
-        String projectId = (String) paramMap.get("projectId");
-        PoemsContext.setContext(userId, loginDevice, pd, groupCode, projectId);
 
         //重新构造一个输入流对象
         byte[] bytes = JSON.toJSONString(paramMap).getBytes("utf-8");
@@ -85,5 +120,4 @@ public class PoemsRequestBodyWrapper extends HttpServletRequestWrapper {
             }
         };
     }
-
 }

+ 6 - 2
apm-mybatis/src/main/java/com/persagy/apm/mybatis/handler/DynamicDataSourceHandler.java

@@ -60,8 +60,12 @@ public class DynamicDataSourceHandler extends HandlerInterceptorAdapter {
         }
         // 默认为应用名
         String dbNameDefault = SpringHelper.getString("spring.application.name");
-        // 创建的数据源名称: 集团编码_应用名
-        String newDsName = PoemsContext.getContext().getGroupCode() + "_" + dbNameDefault;
+        String newDsName = dbNameDefault;
+        if (StringUtils.isNotBlank(PoemsContext.getContext().getGroupCode())) {
+            // 创建的数据源名称: 集团编码_应用名
+            newDsName = dbNameDefault + "_" + PoemsContext.getContext().getGroupCode();
+        }
+
         // 数据源中是否已存在
         if(!dataSource.getCurrentDataSources().keySet().contains(newDsName)) {
             // 设置新数据源