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
3a042fc0
Commit
3a042fc0
authored
Jun 22, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
ef9d7726
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
36 deletions
+49
-36
DbSyncController.java
...java/cn/qg/holmes/controller/effect/DbSyncController.java
+26
-25
DatabaseSyncServiceImpl.java
...g/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
+23
-11
No files found.
src/main/java/cn/qg/holmes/controller/effect/DbSyncController.java
View file @
3a042fc0
...
...
@@ -2,15 +2,17 @@ package cn.qg.holmes.controller.effect;
import
cn.qg.holmes.common.JsonResult
;
import
cn.qg.holmes.service.effect.DatabaseSyncService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.Map
;
@Slf4j
@CrossOrigin
@RestController
@RequestMapping
(
"/db
/
sync"
)
@RequestMapping
(
"/dbsync"
)
public
class
DbSyncController
{
@Autowired
...
...
@@ -28,35 +30,34 @@ public class DbSyncController {
@Value
(
"${dbsync.mysql.password}"
)
private
String
password
;
@GetMapping
(
"/db"
)
public
JsonResult
syncSingleDatabase
(
@RequestParam
String
namespace
,
@RequestParam
String
dbName
)
{
long
startTime
=
System
.
currentTimeMillis
();
databaseSyncService
.
getDbInfoFromSource
(
ip
,
port
,
username
,
password
,
dbName
);
Map
<
String
,
String
>
map
=
databaseSyncService
.
getMysqlInfoByNamespace
(
namespace
);
String
destIp
=
map
.
get
(
"ip"
);
String
destPort
=
map
.
get
(
"port"
);
databaseSyncService
.
syncDbToDest
(
destIp
,
destPort
,
"qa"
,
"qatest"
,
dbName
);
long
endTime
=
System
.
currentTimeMillis
();
return
JsonResult
.
buildSuccessResult
((
endTime
-
startTime
)
/
1000
);
}
@GetMapping
(
"/table"
)
@GetMapping
(
"/one"
)
public
JsonResult
syncSingleTable
(
@RequestParam
String
namespace
,
@RequestParam
String
dbName
,
@RequestParam
String
tableName
)
{
long
startTime
=
System
.
currentTimeMillis
();
Map
<
String
,
String
>
map
=
databaseSyncService
.
getMysqlInfoByNamespace
(
namespace
);
String
destIp
=
map
.
get
(
"ip"
);
String
destPort
=
map
.
get
(
"port"
);
databaseSyncService
.
getSingleTableFromSource
(
ip
,
port
,
username
,
password
,
dbName
,
tableName
);
databaseSyncService
.
syncSingleTableToDest
(
destIp
,
destPort
,
"qa"
,
"qatest"
,
dbName
,
tableName
);
long
endTime
=
System
.
currentTimeMillis
();
return
JsonResult
.
buildSuccessResult
((
endTime
-
startTime
)
/
1000
);
try
{
Map
<
String
,
String
>
map
=
databaseSyncService
.
getMysqlInfoByNamespace
(
namespace
);
String
destIp
=
map
.
get
(
"ip"
);
String
destPort
=
map
.
get
(
"port"
);
log
.
info
(
"获取到{}环境的Mysql地址为:{}"
,
namespace
,
destIp
+
":"
+
destPort
);
if
(
tableName
.
equalsIgnoreCase
(
"all"
))
{
log
.
info
(
"开始同步{}库下所有的表"
,
dbName
);
databaseSyncService
.
getDbInfoFromSource
(
ip
,
port
,
username
,
password
,
dbName
);
databaseSyncService
.
syncDbToDest
(
destIp
,
destPort
,
"qa"
,
"qatest"
,
dbName
);
}
else
{
log
.
info
(
"开始同步{}库下{}表"
,
dbName
,
tableName
);
databaseSyncService
.
getSingleTableFromSource
(
ip
,
port
,
username
,
password
,
dbName
,
tableName
);
databaseSyncService
.
syncSingleTableToDest
(
destIp
,
destPort
,
"qa"
,
"qatest"
,
dbName
,
tableName
);
}
return
JsonResult
.
buildSuccessResult
(
"同步成功!"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
JsonResult
.
buildErrorStateResult
(
"同步失败!"
,
false
);
}
}
/**
* 获取数据库列表
* @return
*/
@GetMapping
(
"/db
list
"
)
@GetMapping
(
"/db
s
"
)
public
JsonResult
getDatabaseList
()
{
return
JsonResult
.
buildSuccessResult
(
databaseSyncService
.
getDatabaseList
(
ip
,
port
,
username
,
password
));
}
...
...
@@ -64,10 +65,10 @@ public class DbSyncController {
/**
* 获取某个库下的表列表
* @param dbName
* @param dbName
数据库名
* @return
*/
@GetMapping
(
"/table
list
"
)
@GetMapping
(
"/table
s
"
)
public
JsonResult
getTableList
(
@RequestParam
String
dbName
)
{
return
JsonResult
.
buildSuccessResult
(
databaseSyncService
.
getTableListByDb
(
ip
,
port
,
username
,
password
,
dbName
));
}
...
...
src/main/java/cn/qg/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
View file @
3a042fc0
...
...
@@ -34,10 +34,10 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
/**
* 从中间库获取数据库相关信息
* @param ip
mysql
ip
* @param port
mysql
port
* @param username
mysql
用户名
* @param password
mysql
密码
* @param ip
中间库
ip
* @param port
中间库
port
* @param username
中间库
用户名
* @param password
中间库
密码
* @param dbName 数据库名
* @return
*/
...
...
@@ -48,7 +48,6 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
String
createTableKey
=
dbSyncPrefix
+
dbName
+
":create"
;
String
insertTableKey
=
dbSyncPrefix
+
dbName
+
":insert"
;
String
dbRedisKey
=
dbSyncPrefix
+
dbName
;
Connection
connection
=
null
;
PreparedStatement
preparedStatement
=
null
;
...
...
@@ -144,6 +143,15 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
return
true
;
}
/**
* 同步数据库信息到目标地址
* @param ip 目标库ip
* @param port 目标库端口
* @param username 目标库用户名
* @param password 目标库密码
* @param dbName 目标库名称
* @return
*/
@Override
public
boolean
syncDbToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
)
{
String
driver
=
"com.mysql.jdbc.Driver"
;
...
...
@@ -181,9 +189,6 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
List
<
Object
>
createTableRedisValue
=
redisUtils
.
lGet
(
createTableKey
,
0
,
redisUtils
.
lGetListSize
(
createTableKey
));
Statement
statement
=
newConnection
.
createStatement
();
for
(
Object
sql:
createTableRedisValue
)
{
// log.info("开始同步表结构:\n {}", sql.toString());
// preparedStatement = newConnection.prepareStatement(sql.toString());
// preparedStatement.execute();
statement
.
addBatch
(
sql
.
toString
());
}
statement
.
executeBatch
();
...
...
@@ -193,9 +198,6 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
String
insertTableRedisValue
=
redisUtils
.
get
(
insertTableKey
).
toString
();
log
.
info
(
"开始同步表数据!"
);
for
(
String
insertSql:
insertTableRedisValue
.
split
(
"\n"
))
{
// log.info(insertSql);
// preparedStatement = newConnection.prepareStatement(insertSql);
// preparedStatement.execute();
statement
.
addBatch
(
insertSql
);
}
statement
.
executeBatch
();
...
...
@@ -204,6 +206,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
// 判断是否需要update
if
(
dbName
.
equals
(
"cash_loan_flow"
))
{
for
(
String
sql:
cashLoanFlowSql
.
split
(
"\n"
))
{
log
.
info
(
sql
);
statement
.
addBatch
(
sql
);
}
statement
.
executeBatch
();
...
...
@@ -440,6 +443,15 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
return
result
;
}
/**
* 获取某个数据库下表列表
* @param ip mysql ip
* @param port mysql port
* @param username mysql 用户名
* @param password mysql 密码
* @param dbName 数据库名
* @return
*/
@Override
public
List
<
Object
>
getTableListByDb
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
)
{
String
driver
=
"com.mysql.jdbc.Driver"
;
...
...
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