Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
holmes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
holmes
Commits
bf27c9e0
Commit
bf27c9e0
authored
Oct 18, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增同步表结构接口
parent
61aed89a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
171 additions
and
0 deletions
+171
-0
DbSyncController.java
...java/cn/qg/holmes/controller/effect/DbSyncController.java
+21
-0
DatabaseSyncService.java
...java/cn/qg/holmes/service/effect/DatabaseSyncService.java
+6
-0
DatabaseSyncServiceImpl.java
...g/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
+144
-0
No files found.
src/main/java/cn/qg/holmes/controller/effect/DbSyncController.java
View file @
bf27c9e0
...
...
@@ -5,6 +5,7 @@ import cn.qg.holmes.service.effect.DatabaseSyncService;
import
cn.qg.holmes.service.k8s.K8sService
;
import
cn.qg.holmes.utils.JenkinsService
;
import
lombok.extern.slf4j.Slf4j
;
import
net.sf.jsqlparser.expression.JdbcNamedParameter
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
...
...
@@ -116,6 +117,26 @@ public class DbSyncController {
return
StringUtils
.
join
(
tableList
,
" "
);
}
/**
* 同步表结构
* @param ip
* @param port
* @param database
* @param table
* @return
*/
@GetMapping
(
"/table/structure"
)
public
JsonResult
handleTableStructure
(
@RequestParam
String
ip
,
@RequestParam
String
port
,
@RequestParam
String
database
,
@RequestParam
String
table
)
{
try
{
databaseSyncService
.
getSourceDbStructure
(
ip
,
port
,
username
,
password
,
database
,
table
);
databaseSyncService
.
syncDbStructureToDest
(
ip
,
port
,
"qa"
,
"qatest"
,
database
,
table
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
JsonResult
.
buildErrorStateResult
(
"创建表结构失败!"
,
false
);
}
return
JsonResult
.
buildSuccessResult
(
"创建"
+
ip
+
":"
+
port
+
"环境表结构成功!"
);
}
/**
* 老数据库同步,调用Jenkins job sync_database_schema
* @param dbName 数据库名
...
...
src/main/java/cn/qg/holmes/service/effect/DatabaseSyncService.java
View file @
bf27c9e0
...
...
@@ -15,4 +15,10 @@ public interface DatabaseSyncService {
List
<
String
>
getDatabaseList
(
String
ip
,
String
port
,
String
username
,
String
password
);
List
<
String
>
getTableListByDb
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
);
// 从同步库获取表结构
boolean
getSourceDbStructure
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
tableName
);
// 将同步库的表结构同步到环境
boolean
syncDbStructureToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
tableName
);
}
\ No newline at end of file
src/main/java/cn/qg/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
View file @
bf27c9e0
...
...
@@ -651,6 +651,150 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
}
}
/**
* 从同步库获取表结构
* @param ip
* @param port
* @param username
* @param password
* @param dbName
* @param tableName
* @return
*/
@Override
public
boolean
getSourceDbStructure
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
tableName
)
{
String
driver
=
"com.mysql.jdbc.Driver"
;
String
url
=
"jdbc:mysql://"
+
ip
+
":"
+
port
+
"/"
+
dbName
;
Connection
connection
=
null
;
Statement
statement
=
null
;
try
{
Class
.
forName
(
driver
);
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
connection
.
setAutoCommit
(
true
);
if
(
"all"
.
equals
(
tableName
))
{
String
tableListKey
=
dbSyncPrefix
+
dbName
+
":tableList"
;
List
<
String
>
tableList
=
getTableListByDb
(
ip
,
port
,
username
,
password
,
dbName
);
redisUtils
.
set
(
tableListKey
,
StringUtils
.
join
(
tableList
,
" "
),
600
);
for
(
String
table:
tableList
)
{
String
tableRedisKey
=
dbSyncPrefix
+
dbName
+
":"
+
table
+
":create"
;
if
(!
redisUtils
.
hasKey
(
tableRedisKey
))
{
String
sql
=
String
.
format
(
"SHOW CREATE TABLE %s"
,
table
);
statement
=
connection
.
createStatement
();
ResultSet
resultSet
=
statement
.
executeQuery
(
sql
);
while
(
resultSet
.
next
())
{
redisUtils
.
set
(
tableRedisKey
,
resultSet
.
getString
(
2
),
600
);
}
resultSet
.
close
();
statement
.
clearBatch
();
}
}
}
else
{
String
tableRedisKey
=
dbSyncPrefix
+
dbName
+
":"
+
tableName
+
":create"
;
if
(!
redisUtils
.
hasKey
(
tableRedisKey
))
{
String
sql
=
String
.
format
(
"SHOW CREATE TABLE %s"
,
tableName
);
ResultSet
resultSet
=
statement
.
executeQuery
(
sql
);
while
(
resultSet
.
next
())
{
redisUtils
.
set
(
tableRedisKey
,
resultSet
.
getString
(
2
),
600
);
}
resultSet
.
close
();
}
statement
.
clearBatch
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
finally
{
try
{
if
(
connection
!=
null
)
{
connection
.
close
();
}
if
(
statement
!=
null
)
{
statement
.
close
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
false
;
}
/**
* 将同步库的表结构同步到环境中
* @param ip
* @param port
* @param username
* @param password
* @param dbName
* @param tableName
* @return
*/
@Override
public
boolean
syncDbStructureToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
tableName
)
{
String
driver
=
"com.mysql.jdbc.Driver"
;
String
url
=
"jdbc:mysql://"
+
ip
+
":"
+
port
+
"/"
+
dbName
;
Connection
connection
=
null
;
Statement
statement
=
null
;
try
{
Class
.
forName
(
driver
);
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
connection
.
setAutoCommit
(
true
);
statement
=
connection
.
createStatement
();
if
(
"all"
.
equals
(
tableName
))
{
String
tableListKey
=
dbSyncPrefix
+
dbName
+
":tableList"
;
// List<String> tableList = Arrays.asList(redisUtils.get(tableListKey).toString().split(" "));
// 删除key
// redisUtils.del(tableListKey);
List
<
String
>
tableList
=
getTableListByDb
(
ip
,
port
,
username
,
password
,
dbName
);
for
(
String
table:
tableList
)
{
// 首先删除表
String
dropTableSql
=
"DROP TABLE IF EXISTS "
+
table
;
statement
.
execute
(
dropTableSql
);
statement
.
clearBatch
();
// 然后同步表结构
String
tableRedisKey
=
dbSyncPrefix
+
dbName
+
":"
+
table
+
":create"
;
String
createTableSql
=
redisUtils
.
get
(
tableRedisKey
).
toString
();
statement
.
execute
(
createTableSql
);
statement
.
clearBatch
();
}
}
else
{
// 首先删除表
String
dropTableSql
=
"DROP TABLE IF EXISTS"
+
tableName
;
statement
.
execute
(
dropTableSql
);
// 然后同步表结构
String
tableRedisKey
=
dbSyncPrefix
+
dbName
+
":"
+
tableName
+
":create"
;
String
createTableSql
=
redisUtils
.
get
(
tableRedisKey
).
toString
();
statement
.
execute
(
createTableSql
);
statement
.
clearBatch
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
finally
{
try
{
if
(
connection
!=
null
)
{
connection
.
close
();
}
if
(
statement
!=
null
)
{
statement
.
close
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
false
;
}
/**
* 获取表数据一行的所有值
* @param rs
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment