Commit c0459159 authored by 刘凡's avatar 刘凡

优化:【空工大】新建mass数据源时,提前查询是否已经新增过

parent c6de9800
...@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject; ...@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil; import com.github.pagehelper.util.StringUtil;
import com.yeejoin.amos.boot.biz.common.utils.DateUtils; import com.yeejoin.amos.boot.biz.common.utils.DateUtils;
import com.yeejoin.amos.component.robot.AmosRequestContext; import com.yeejoin.amos.component.robot.AmosRequestContext;
import com.yeejoin.amos.feign.privilege.util.DesUtil;
import com.yeejoin.amos.kgd.message.Constant; import com.yeejoin.amos.kgd.message.Constant;
import com.yeejoin.amos.kgd.message.model.TableFieldModel; import com.yeejoin.amos.kgd.message.model.TableFieldModel;
import com.yeejoin.amos.kgd.message.model.TableModel; import com.yeejoin.amos.kgd.message.model.TableModel;
...@@ -13,7 +14,6 @@ import org.slf4j.LoggerFactory; ...@@ -13,7 +14,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
...@@ -49,18 +49,20 @@ public class ClientHandler<path> implements Runnable { ...@@ -49,18 +49,20 @@ public class ClientHandler<path> implements Runnable {
private final String password; private final String password;
private final String passwordSecretKey;
private final String dataSourcesGroupId; private final String dataSourcesGroupId;
public ClientHandler(Socket socket, String hostAndPort, String url, String username, String password, String dataSourcesGroupId) { public ClientHandler(Socket socket, String hostAndPort, String url, String username, String password, String passwordSecretKey, String dataSourcesGroupId) {
this.socket = socket; this.socket = socket;
this.hostAndPort = hostAndPort; //127.0.0.1:30009 this.hostAndPort = hostAndPort; //127.0.0.1:30009
this.url = url; // 数据库连接地址 this.url = url; // 数据库连接地址
this.username = username;// 数据库用户名 this.username = username;// 数据库用户名
this.password = password;// 数据库密码 this.password = password;// 数据库密码
this.passwordSecretKey = passwordSecretKey;// 数据库密码加密密钥
this.dataSourcesGroupId = dataSourcesGroupId;// 空工大数据源所属分类Id this.dataSourcesGroupId = dataSourcesGroupId;// 空工大数据源所属分类Id
} }
public static final String DATABASE_NAME = "jd_bearing";
/*String*/ /*String*/
public static final String TYPE_STRING = "String"; public static final String TYPE_STRING = "String";
/*Integer*/ /*Integer*/
...@@ -71,6 +73,8 @@ public class ClientHandler<path> implements Runnable { ...@@ -71,6 +73,8 @@ public class ClientHandler<path> implements Runnable {
public static final String TYPE_DATETIME = "DateTime"; public static final String TYPE_DATETIME = "DateTime";
/*BigDecimal*/ /*BigDecimal*/
public static final String TYPE_BIGDECIMAL = "BigDecimal"; public static final String TYPE_BIGDECIMAL = "BigDecimal";
/*longtext*/
public static final String TYPE_LONGTEXT = "longtext";
/*text*/ /*text*/
public static final String TYPE_TEXT = "text"; public static final String TYPE_TEXT = "text";
...@@ -106,11 +110,9 @@ public class ClientHandler<path> implements Runnable { ...@@ -106,11 +110,9 @@ public class ClientHandler<path> implements Runnable {
throw new Exception("导入失败,文件名称格式不正确!"); throw new Exception("导入失败,文件名称格式不正确!");
} }
InputStream ipsClone2 = inputStreamCacher.getInputStream(); InputStream ipsClone2 = inputStreamCacher.getInputStream();
InputStream contentIps = fileInputStreamFilter(ipsClone2, postfix); InputStream contentIps = fileInputStreamFilter(ipsClone2, postfix);
// 获取Excel表头内容 和 Excel表数据内容 // 获取Excel表头内容 和 Excel表数据内容
Map<String, Object> tableInfoMap = ExcelTool.readFromExcel(contentIps, filenameWithSuffix); Map<String, Object> tableInfoMap = ExcelTool.readFromExcel(contentIps, filenameWithSuffix);
...@@ -118,7 +120,10 @@ public class ClientHandler<path> implements Runnable { ...@@ -118,7 +120,10 @@ public class ClientHandler<path> implements Runnable {
if (StringUtil.isNotEmpty(tableName) && !tableInfoMap.isEmpty() && tableInfoMap.get("tableColumns") != null) { if (StringUtil.isNotEmpty(tableName) && !tableInfoMap.isEmpty() && tableInfoMap.get("tableColumns") != null) {
List<String> tableColumns = (List<String>) tableInfoMap.get("tableColumns"); List<String> tableColumns = (List<String>) tableInfoMap.get("tableColumns");
List<List<String>> tableDatas = (List<List<String>>) tableInfoMap.get("tableDatas"); List<List<String>> tableDatas = (List<List<String>>) tableInfoMap.get("tableDatas");
this.addMySqlDatasources(tableColumns, tableDatas, hostAndPort, tableName); //新增mysql数据库和表
this.addMySqlDatasources(tableColumns, tableDatas, tableName);
//新增 maas 数据源
this.addMaasDatasources(tableName);
} }
// 关闭连接 // 关闭连接
socket.close(); socket.close();
...@@ -134,7 +139,7 @@ public class ClientHandler<path> implements Runnable { ...@@ -134,7 +139,7 @@ public class ClientHandler<path> implements Runnable {
//新增My Sql数据源 //新增My Sql数据源
private void addMySqlDatasources(List<String> tableColumns, List<List<String>> dataList, String hostAndPort, String tableName) throws Exception { private void addMySqlDatasources(List<String> tableColumns, List<List<String>> dataList, String tableName) throws Exception {
AmosRequestContext robotAuthentication = SpringContextHelper.getBean(AmosRequestContext.class); AmosRequestContext robotAuthentication = SpringContextHelper.getBean(AmosRequestContext.class);
if (Objects.nonNull(robotAuthentication)) { if (Objects.nonNull(robotAuthentication)) {
// String token = robotAuthentication.getToken(); // String token = robotAuthentication.getToken();
...@@ -190,8 +195,7 @@ public class ClientHandler<path> implements Runnable { ...@@ -190,8 +195,7 @@ public class ClientHandler<path> implements Runnable {
for (int i = 0; i < tableColumns.size(); i++) { for (int i = 0; i < tableColumns.size(); i++) {
TableFieldModel tableFieldModel = new TableFieldModel(); TableFieldModel tableFieldModel = new TableFieldModel();
tableFieldModel.setFieldName(tableColumns.get(i)); tableFieldModel.setFieldName(tableColumns.get(i));
tableFieldModel.setFieldType(TYPE_STRING); tableFieldModel.setFieldType(TYPE_LONGTEXT);
tableFieldModel.setFieldLength("255");
tableFieldList.add(tableFieldModel); tableFieldList.add(tableFieldModel);
} }
} }
...@@ -238,47 +242,86 @@ public class ClientHandler<path> implements Runnable { ...@@ -238,47 +242,86 @@ public class ClientHandler<path> implements Runnable {
state.close(); state.close();
conn.close(); conn.close();
} }
}
}
// 新增 maas 数据源
private void addMaasDatasources(String tableName) {
AmosRequestContext robotAuthentication = SpringContextHelper.getBean(AmosRequestContext.class);
if (Objects.nonNull(robotAuthentication)) {
// String token = robotAuthentication.getToken();
// String product = robotAuthentication.getProduct();
// String appKey = robotAuthentication.getAppKey();
String token = "f5f5ebba-f68a-4093-a78e-388c925a6eaa"; // 如果token、失效,就去浏览器复制最新的token、
String product = "AMOS_STUDIO_WEB";
String appKey = "AMOS_STUDIO";
String[] parts = url.split("/|:");
String dbIp = parts[2];
int dbPort = Integer.parseInt(parts[3]);
String dbName = parts[4];
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
String defaultDatabaseName = "空工大_" + dbName;
// //查询数据库是否已创建 //查询数据库是否已创建
// String connId = ""; String connId = "";
// String datasourcesUrl = "http://" + hostAndPort + "/maas/dsm/datasources/query"; String datasourcesUrl = "http://" + hostAndPort + "/maas/dsm/datasources/query";
// Map<String, String> datasourcesParams = new HashMap<>(); Map<String, String> datasourcesParams = new HashMap<>();
// datasourcesParams.put("pid",dataSourcesGroupId); datasourcesParams.put("pid", dataSourcesGroupId);
// HttpEntity<Map<String, String>> datasourcesRequestEntity = new HttpEntity<>(datasourcesParams, getHeader(token, product, appKey, hostAndPort, false)); HttpEntity<Map<String, String>> datasourcesRequestEntity = new HttpEntity<>(datasourcesParams, getHeader(token, product, appKey, hostAndPort, false));
// ResponseEntity<String> datasourcesResponse = restTemplate.exchange(datasourcesUrl, HttpMethod.POST, datasourcesRequestEntity, String.class); ResponseEntity<String> datasourcesResponse = restTemplate.exchange(datasourcesUrl, HttpMethod.POST, datasourcesRequestEntity, String.class);
// String datasourcesBody = datasourcesResponse.getBody(); String datasourcesBody = datasourcesResponse.getBody();
// JSONObject datasourcesJsonObject = JSONObject.parseObject(datasourcesBody); JSONObject datasourcesJsonObject = JSONObject.parseObject(datasourcesBody);
// List result = Collections.singletonList(datasourcesJsonObject.getString("result")); JSONArray datasourcesResult = datasourcesJsonObject.getJSONArray("result");
// if (datasourcesResult != null) {
//// if (conns != null) { for (int i = 0; i < datasourcesResult.size(); i++) {
//// connId = (String) conns.get(0).get("id"); Map<String, Object> stringObjectMap = (Map<String, Object>) datasourcesResult.get(i);
//// } if (defaultDatabaseName.equals(stringObjectMap.get("name"))) {
// //查询数据库表是否已创建 connId = (String) stringObjectMap.get("id");
// String tablesUrl = "http://" + hostAndPort + "/maas/dsm/jdbc/tables"; }
// Map<String, String> tablesParams = new HashMap<>(); }
// HttpEntity<Map<String, String>> tablesRequestEntity = new HttpEntity<>(datasourcesParams, getHeader(token, product, appKey, hostAndPort, false)); }
// ResponseEntity<String> tablesResponse = restTemplate.exchange(datasourcesUrl, HttpMethod.POST, tablesRequestEntity, String.class); //查询数据库表是否已创建
// String tablesBody = tablesResponse.getBody(); boolean isCreateTable = false;
// JSONObject tablesJsonObject = JSONObject.parseObject(tablesBody); String jdbcTablesUrl = "http://" + hostAndPort + "/maas/dsm/jdbc/tables";
// List result2 = Collections.singletonList(tablesJsonObject.getString("result")); Map<String, String> jdbcTablesParams = new HashMap<>();
datasourcesParams.put("dsId", connId);
//3. 调用conn接口 datasourcesParams.put("dsType", "mysql");
// if (StringUtil.isEmpty(connId)) { datasourcesParams.put("resourceType", "mysql");
datasourcesParams.put("dsdetail", "{\"hostname\":\"" + dbIp + "\",\"port\":" + dbPort + ",\"name\":\"" + defaultDatabaseName + "\",\"database\":\"" + dbName + "\",\"username\":\"" + username + "\",\"password\":\"" + DesUtil.encode(password, passwordSecretKey) + "\",\"dbtype\":\"mysql\"}");
HttpEntity<Map<String, String>> jdbcTablesRequestEntity = new HttpEntity<>(jdbcTablesParams, getHeader(token, product, appKey, hostAndPort, false));
ResponseEntity<String> jdbcTablesResponse = restTemplate.exchange(jdbcTablesUrl, HttpMethod.POST, jdbcTablesRequestEntity, String.class);
String jdbcTablesBody = jdbcTablesResponse.getBody();
JSONObject jdbcTablesJsonObject = JSONObject.parseObject(jdbcTablesBody);
JSONArray jdbcTablesResult = jdbcTablesJsonObject.getJSONArray("result");
if (jdbcTablesResult != null) {
for (int i = 0; i < jdbcTablesResult.size(); i++) {
Map<String, Object> stringObjectMap = (Map<String, Object>) jdbcTablesResult.get(i);
if (tableName.equals(stringObjectMap.get("name"))) {
isCreateTable = true;
}
}
}
//3. 调用conn接口,数据库已创建过就不在创建
if (StringUtil.isEmpty(connId)) {
String connsUrl = "http://" + hostAndPort + "/maas/maas/desktop/conns"; String connsUrl = "http://" + hostAndPort + "/maas/maas/desktop/conns";
Map<String, String> connsParams = new HashMap<>(); Map<String, String> connsParams = new HashMap<>();
connsParams.put("name", "空工大1_" + DATABASE_NAME); connsParams.put("name", defaultDatabaseName);
connsParams.put("connType", "mysql"); connsParams.put("connType", "mysql");
connsParams.put("subjectid", dataSourcesGroupId); connsParams.put("subjectid", dataSourcesGroupId);
connsParams.put("detail", "{\"hostname\":\"172.16.3.101\",\"port\":3306,\"username\":\"" + username + "\",\"password\":\"U2FsdGVkX1/OWH77oSUJ2LeXv9x65yQi+yfUmOvLzjA=\",\"name\":\"空工大_" + DATABASE_NAME + "\",\"database\":\"" + DATABASE_NAME + "\",\"dbtype\":\"mysql\"}"); connsParams.put("detail", "{\"hostname\":\""+dbIp+"\",\"port\":"+dbPort+",\"username\":\"" + username + "\",\"password\":\"" + DesUtil.encode(password, passwordSecretKey) + "\",\"name\":\"" + defaultDatabaseName + "\",\"database\":\"" + dbName + "\",\"dbtype\":\"mysql\"}");
HttpEntity<Map<String, String>> connsRequestEntity = new HttpEntity<>(connsParams, getHeader(token, product, appKey, hostAndPort, false)); HttpEntity<Map<String, String>> connsRequestEntity = new HttpEntity<>(connsParams, getHeader(token, product, appKey, hostAndPort, false));
ResponseEntity<String> connsResponse = restTemplate.exchange(connsUrl, HttpMethod.POST, connsRequestEntity, String.class); ResponseEntity<String> connsResponse = restTemplate.exchange(connsUrl, HttpMethod.POST, connsRequestEntity, String.class);
String connsBody = connsResponse.getBody(); String connsBody = connsResponse.getBody();
JSONObject connsJsonObject = JSONObject.parseObject(connsBody); JSONObject connsJsonObject = JSONObject.parseObject(connsBody);
String connId = connsJsonObject.getString("result"); connId = connsJsonObject.getString("result");
// } }
if (StringUtil.isNotEmpty(connId)) { //数据库表已创建过就不在创建
if (StringUtil.isNotEmpty(connId) && !isCreateTable) {
//4.新增mass数据源 //4.新增mass数据源
String tablesUrl = "http://" + hostAndPort + "/maas/maas/desktop/tables"; String tablesUrl = "http://" + hostAndPort + "/maas/maas/desktop/tables";
Map<String, String> tablesParams = new HashMap<>(); Map<String, String> tablesParams = new HashMap<>();
...@@ -357,7 +400,7 @@ public class ClientHandler<path> implements Runnable { ...@@ -357,7 +400,7 @@ public class ClientHandler<path> implements Runnable {
} else if ("decimal".equals(fieldType)) { } else if ("decimal".equals(fieldType)) {
// 追加列 // 追加列
sb.append(" " + fieldType + "(" + field.getFieldLength() + "," + field.getDecimalPoint() + ") "); sb.append(" " + fieldType + "(" + field.getFieldLength() + "," + field.getDecimalPoint() + ") ");
} else if ("datetime".equals(fieldType)) { } else if ("datetime".equals(fieldType) || "longtext".equals(fieldType)) {
// 追加列 // 追加列
sb.append(" " + fieldType + " "); sb.append(" " + fieldType + " ");
} else { } else {
...@@ -503,6 +546,8 @@ public class ClientHandler<path> implements Runnable { ...@@ -503,6 +546,8 @@ public class ClientHandler<path> implements Runnable {
return "datetime"; return "datetime";
case TYPE_BIGDECIMAL: case TYPE_BIGDECIMAL:
return "decimal"; return "decimal";
case TYPE_LONGTEXT:
return "longtext";
case TYPE_TEXT: case TYPE_TEXT:
return "text"; return "text";
default: default:
......
...@@ -40,6 +40,10 @@ public class SocketConfig { ...@@ -40,6 +40,10 @@ public class SocketConfig {
@Value("${spring.datasource.password}") @Value("${spring.datasource.password}")
private String password; private String password;
@Value("${kgd.datasource.password.secretKey}")
private String passwordSecretKey;
@Value("${kgd.datasource.group_id}") @Value("${kgd.datasource.group_id}")
private String dataSourcesGroupId; private String dataSourcesGroupId;
...@@ -64,7 +68,7 @@ public class SocketConfig { ...@@ -64,7 +68,7 @@ public class SocketConfig {
clientSocket.setSoTimeout(10000); clientSocket.setSoTimeout(10000);
// 创建新线程处理连接 // 创建新线程处理连接
log.info("接收到客户端socket: {}", clientSocket.getRemoteSocketAddress()); log.info("接收到客户端socket: {}", clientSocket.getRemoteSocketAddress());
threadpool.execute(new ClientHandler(clientSocket, hostAndPort, url, username, password, dataSourcesGroupId)); threadpool.execute(new ClientHandler(clientSocket, hostAndPort, url, username, password, passwordSecretKey, dataSourcesGroupId));
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
...@@ -70,4 +70,6 @@ spring.security.user.name=admin ...@@ -70,4 +70,6 @@ spring.security.user.name=admin
spring.security.user.password=a1234560 spring.security.user.password=a1234560
## \u7A7A\u5DE5\u5927\u6570\u636E\u6E90\u6240\u5C5E\u5206\u7C7BId ## \u7A7A\u5DE5\u5927\u6570\u636E\u6E90\u6240\u5C5E\u5206\u7C7BId
kgd.datasource.group_id=534de50c-bb8c-4450-87a0-557aac745a94 kgd.datasource.group_id=534de50c-bb8c-4450-87a0-557aac745a94
\ No newline at end of file ## ??????????????
kgd.datasource.password.secretKey=yeejoin,maas,password
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment