Commit c66ade4f authored by liguofu@yeejoin.com's avatar liguofu@yeejoin.com

优化:【空工大】创建mysql数据源的逻辑

parent bad30f33
......@@ -13,6 +13,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
......@@ -28,8 +29,6 @@ import java.sql.Statement;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @Author: xl
......@@ -50,12 +49,15 @@ public class ClientHandler<path> implements Runnable {
private final String password;
public ClientHandler(Socket socket, String hostAndPort, String url, String username, String password) {
private final String dataSourcesGroupId;
public ClientHandler(Socket socket, String hostAndPort, String url, String username, String password, String dataSourcesGroupId) {
this.socket = socket;
this.hostAndPort = hostAndPort; //127.0.0.1:30009
this.url = url; // 数据库连接地址
this.username = username;// 数据库用户名
this.password = password;// 数据库密码
this.dataSourcesGroupId = dataSourcesGroupId;// 空工大数据源所属分类Id
}
public static final String DATABASE_NAME = "jd_bearing";
......@@ -139,7 +141,7 @@ public class ClientHandler<path> implements Runnable {
// String product = robotAuthentication.getProduct();
// String appKey = robotAuthentication.getAppKey();
String token = "ab2d46e6-243b-4ea5-8f2f-126e5b2d87ae"; // 如果token、失效,就去浏览器复制最新的token、
String token = "f5f5ebba-f68a-4093-a78e-388c925a6eaa"; // 如果token、失效,就去浏览器复制最新的token、
String product = "AMOS_STUDIO_WEB";
String appKey = "AMOS_STUDIO";
......@@ -236,20 +238,72 @@ public class ClientHandler<path> implements Runnable {
state.close();
conn.close();
}
//3.新增mass数据源
String connsUrl = "http://" + hostAndPort + "/maas/dsm/datasources";
Map<String, String> params = new HashMap<>();
params.put("name", tableName);
params.put("subtype", "mysql");
params.put("type", "DataBase");
params.put("groupid", "6bc573d8-9534-4a93-8315-46be74f4c311");
params.put("detail", "{\"hostname\":\"172.16.3.101\",\"port\":3306,\"username\":\"" + username + "\",\"password\":\"" + password + "\",\"name\":\"" + tableName + "\",\"database\":\"" + DATABASE_NAME + "\",\"dbtype\":\"mysql\"}");
HttpEntity<Map<String, String>> requestEntity2 = new HttpEntity<>(params, getHeader(token, product, appKey, hostAndPort, false));
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(connsUrl, HttpMethod.POST, requestEntity2, String.class);
// //查询数据库是否已创建
// String connId = "";
// String datasourcesUrl = "http://" + hostAndPort + "/maas/dsm/datasources/query";
// Map<String, String> datasourcesParams = new HashMap<>();
// datasourcesParams.put("pid",dataSourcesGroupId);
// 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);
// String datasourcesBody = datasourcesResponse.getBody();
// JSONObject datasourcesJsonObject = JSONObject.parseObject(datasourcesBody);
// List result = Collections.singletonList(datasourcesJsonObject.getString("result"));
//
//// if (conns != null) {
//// connId = (String) conns.get(0).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();
// JSONObject tablesJsonObject = JSONObject.parseObject(tablesBody);
// List result2 = Collections.singletonList(tablesJsonObject.getString("result"));
//3. 调用conn接口
// if (StringUtil.isEmpty(connId)) {
String connsUrl = "http://" + hostAndPort + "/maas/maas/desktop/conns";
Map<String, String> connsParams = new HashMap<>();
connsParams.put("name", "空工大1_" + DATABASE_NAME);
connsParams.put("connType", "mysql");
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\"}");
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);
String connsBody = connsResponse.getBody();
JSONObject connsJsonObject = JSONObject.parseObject(connsBody);
String connId = connsJsonObject.getString("result");
// }
if (StringUtil.isNotEmpty(connId)) {
//4.新增mass数据源
String tablesUrl = "http://" + hostAndPort + "/maas/maas/desktop/tables";
Map<String, String> tablesParams = new HashMap<>();
tablesParams.put("config", "{\"tableType\":\"Filter\",\"criterias\":[],\"tableName\":\"" + tableName + "\"}");
tablesParams.put("connId", connId);
tablesParams.put("name", tableName);
tablesParams.put("pid", dataSourcesGroupId);
HttpEntity<Map<String, String>> tablesRequestEntity = new HttpEntity<>(tablesParams, getHeader(token, product, appKey, hostAndPort, false));
ResponseEntity<String> tablesResponse = restTemplate.exchange(tablesUrl, HttpMethod.POST, tablesRequestEntity, String.class);
String tablesBody = tablesResponse.getBody();
JSONObject tablesJsonObject = JSONObject.parseObject(tablesBody);
String tablesResult = tablesJsonObject.getString("result");
String tablelistUrl = "http://" + hostAndPort + "/maas/maas/desktop/tablelist";
Map<String, String> tablelistParams = new HashMap<>();
tablesParams.put("connId", connId);
tablesParams.put("subcjectId", dataSourcesGroupId);
HttpEntity<Map<String, String>> tablelistRequestEntity = new HttpEntity<>(tablelistParams, getHeader(token, product, appKey, hostAndPort, false));
ResponseEntity<String> tablelistResponse = restTemplate.exchange(tablelistUrl, HttpMethod.POST, tablelistRequestEntity, String.class);
String tablelistBody = tablelistResponse.getBody();
JSONObject tablelistJsonObject = JSONObject.parseObject(tablelistBody);
String tablelistResult = tablelistJsonObject.getString("result");
}
}
}
......
......@@ -40,6 +40,10 @@ public class SocketConfig {
@Value("${spring.datasource.password}")
private String password;
@Value("${kgd.datasource.group_id}")
private String dataSourcesGroupId;
private static final ThreadPoolExecutor threadpool = new ThreadPoolExecutor(15, 15,
10L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
......@@ -60,7 +64,7 @@ public class SocketConfig {
clientSocket.setSoTimeout(10000);
// 创建新线程处理连接
log.info("接收到客户端socket: {}", clientSocket.getRemoteSocketAddress());
threadpool.execute(new ClientHandler(clientSocket, hostAndPort, url, username, password));
threadpool.execute(new ClientHandler(clientSocket, hostAndPort, url, username, password, dataSourcesGroupId));
}
} catch (IOException e) {
throw new RuntimeException(e);
......
......@@ -68,3 +68,6 @@ knife4j.basic.password=a1234560
management.security.enabled=true
spring.security.user.name=admin
spring.security.user.password=a1234560
## \u7A7A\u5DE5\u5927\u6570\u636E\u6E90\u6240\u5C5E\u5206\u7C7BId
kgd.datasource.group_id=534de50c-bb8c-4450-87a0-557aac745a94
\ 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