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
0cbde284
Commit
0cbde284
authored
Oct 12, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调试同步单张表
parent
d875f7c3
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
395 additions
and
239 deletions
+395
-239
DbSyncController.java
...java/cn/qg/holmes/controller/effect/DbSyncController.java
+21
-9
DatabaseSyncService.java
...java/cn/qg/holmes/service/effect/DatabaseSyncService.java
+4
-7
DatabaseSyncServiceImpl.java
...g/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
+346
-144
K8sService.java
src/main/java/cn/qg/holmes/service/k8s/K8sService.java
+24
-79
No files found.
src/main/java/cn/qg/holmes/controller/effect/DbSyncController.java
View file @
0cbde284
...
@@ -2,6 +2,7 @@ package cn.qg.holmes.controller.effect;
...
@@ -2,6 +2,7 @@ package cn.qg.holmes.controller.effect;
import
cn.qg.holmes.common.JsonResult
;
import
cn.qg.holmes.common.JsonResult
;
import
cn.qg.holmes.service.effect.DatabaseSyncService
;
import
cn.qg.holmes.service.effect.DatabaseSyncService
;
import
cn.qg.holmes.service.k8s.K8sService
;
import
cn.qg.holmes.utils.JenkinsService
;
import
cn.qg.holmes.utils.JenkinsService
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -35,22 +36,33 @@ public class DbSyncController {
...
@@ -35,22 +36,33 @@ public class DbSyncController {
@Value
(
"${dbsync.mysql.password}"
)
@Value
(
"${dbsync.mysql.password}"
)
private
String
password
;
private
String
password
;
@GetMapping
(
"/one"
)
@Autowired
public
JsonResult
syncSingleTable
(
@RequestParam
String
namespace
,
@RequestParam
String
dbName
,
@RequestParam
String
tableName
)
{
K8sService
k8sService
;
/**
* 同步数据库
* @param namespace 环境
* @param dbName 数据库名称
* @param tableName 表名称
* @param businessData 是否保留业务数据
* @return
*/
@GetMapping
(
"/new"
)
public
JsonResult
syncDatabase
(
@RequestParam
String
namespace
,
@RequestParam
String
dbName
,
@RequestParam
String
tableName
,
@RequestParam
boolean
businessData
)
{
try
{
try
{
long
startTime
=
System
.
currentTimeMillis
();
long
startTime
=
System
.
currentTimeMillis
();
Map
<
String
,
String
>
map
=
databaseSyncService
.
getMysqlInfo
ByNamespace
(
namespace
);
Map
<
String
,
String
>
map
=
k8sService
.
getMysqlAddress
ByNamespace
(
namespace
);
String
destIp
=
map
.
get
(
"
ip
"
);
String
destIp
=
map
.
get
(
"
host
"
);
String
destPort
=
map
.
get
(
"port"
);
String
destPort
=
map
.
get
(
"port"
);
log
.
info
(
"获取到{}环境的Mysql地址为:{}"
,
namespace
,
destIp
+
":"
+
destPort
);
log
.
info
(
"获取到{}环境的Mysql地址为:{}"
,
namespace
,
destIp
+
":"
+
destPort
);
if
(
tableName
.
equalsIgnoreCase
(
"all"
)
||
tableName
.
equals
(
""
))
{
if
(
tableName
.
equalsIgnoreCase
(
"all"
)
||
tableName
.
equals
(
""
))
{
log
.
info
(
"开始同步{}库下所有
的表"
,
dbName
);
log
.
info
(
"开始同步{}库下所有
表到{}环境,保留业务数据:{}"
,
dbName
,
namespace
,
businessData
);
databaseSyncService
.
getDbInfoFromSource
(
ip
,
port
,
username
,
password
,
dbName
);
databaseSyncService
.
getDbInfoFromSource
(
ip
,
port
,
username
,
password
,
dbName
);
databaseSyncService
.
syncDbToDest
(
destIp
,
destPort
,
"qa"
,
"qatest"
,
dbName
,
namespace
);
databaseSyncService
.
syncDbToDest
(
destIp
,
destPort
,
"qa"
,
"qatest"
,
dbName
,
namespace
,
businessData
);
}
else
{
}
else
{
log
.
info
(
"开始同步{}库下{}表
"
,
dbName
,
tableName
);
log
.
info
(
"开始同步{}库下{}表
到{}环境,保留业务数据:{}"
,
dbName
,
tableName
,
namespace
,
businessData
);
databaseSyncService
.
getSingleTableFromSource
(
ip
,
port
,
username
,
password
,
dbName
,
tableName
);
databaseSyncService
.
getSingleTableFromSource
(
ip
,
port
,
username
,
password
,
dbName
,
tableName
);
databaseSyncService
.
syncSingleTableToDest
(
destIp
,
destPort
,
"qa"
,
"qatest"
,
dbName
,
tableName
);
databaseSyncService
.
syncSingleTableToDest
(
destIp
,
destPort
,
"qa"
,
"qatest"
,
dbName
,
tableName
,
businessData
);
}
}
long
endTime
=
System
.
currentTimeMillis
();
long
endTime
=
System
.
currentTimeMillis
();
long
elapsedTime
=
(
endTime
-
startTime
)
/
1000
;
long
elapsedTime
=
(
endTime
-
startTime
)
/
1000
;
...
@@ -77,7 +89,7 @@ public class DbSyncController {
...
@@ -77,7 +89,7 @@ public class DbSyncController {
*/
*/
@GetMapping
(
"/tables"
)
@GetMapping
(
"/tables"
)
public
JsonResult
getTableList
(
@RequestParam
String
dbName
)
{
public
JsonResult
getTableList
(
@RequestParam
String
dbName
)
{
List
<
Object
>
tableList
=
databaseSyncService
.
getTableListByDb
(
ip
,
port
,
username
,
password
,
dbName
);
List
<
String
>
tableList
=
databaseSyncService
.
getTableListByDb
(
ip
,
port
,
username
,
password
,
dbName
);
tableList
.
add
(
0
,
"all"
);
tableList
.
add
(
0
,
"all"
);
return
JsonResult
.
buildSuccessResult
(
tableList
);
return
JsonResult
.
buildSuccessResult
(
tableList
);
}
}
...
...
src/main/java/cn/qg/holmes/service/effect/DatabaseSyncService.java
View file @
0cbde284
package
cn
.
qg
.
holmes
.
service
.
effect
;
package
cn
.
qg
.
holmes
.
service
.
effect
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
public
interface
DatabaseSyncService
{
public
interface
DatabaseSyncService
{
boolean
getDbInfoFromSource
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
);
boolean
getDbInfoFromSource
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
);
boolean
syncDbToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
namespace
);
boolean
syncDbToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
namespace
,
boolean
businessData
);
boolean
getSingleTableFromSource
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
tableName
);
boolean
getSingleTableFromSource
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
tableName
);
boolean
syncSingleTableToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
tableName
);
boolean
syncSingleTableToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
tableName
,
boolean
businessData
);
List
<
Object
>
getDatabaseList
(
String
ip
,
String
port
,
String
username
,
String
password
);
List
<
String
>
getDatabaseList
(
String
ip
,
String
port
,
String
username
,
String
password
);
Map
<
String
,
String
>
getMysqlInfoByNamespace
(
String
namespace
);
List
<
String
>
getTableListByDb
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
);
List
<
Object
>
getTableListByDb
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
);
}
}
\ No newline at end of file
src/main/java/cn/qg/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
View file @
0cbde284
This diff is collapsed.
Click to expand it.
src/main/java/cn/qg/holmes/service/k8s/K8sService.java
View file @
0cbde284
...
@@ -227,88 +227,33 @@ public class K8sService {
...
@@ -227,88 +227,33 @@ public class K8sService {
* @param namespace 环境
* @param namespace 环境
* @return
* @return
*/
*/
public
List
<
Map
<
String
,
Object
>>
getPodList
(
String
namespace
)
{
public
Map
<
String
,
String
>
getMysqlAddressByNamespace
(
String
namespace
)
{
List
<
Pod
>
podList
=
kubernetesClient
.
pods
().
inNamespace
(
namespace
).
list
().
getItems
();
// 获取Service
List
<
Map
<
String
,
Object
>>
result
=
new
ArrayList
<>();
Service
service
=
kubernetesClient
.
services
().
inNamespace
(
namespace
).
withName
(
"mysql"
).
get
();
for
(
Pod
pod
:
podList
)
{
Map
<
String
,
String
>
labels
=
new
HashMap
<>();
System
.
out
.
println
(
pod
);
labels
.
put
(
"qcloud-app"
,
"mysql"
);
if
(
pod
.
getStatus
().
getPhase
().
equals
(
"Running"
)
||
pod
.
getStatus
().
getPhase
().
equals
(
"Pending"
))
{
labels
.
put
(
"type"
,
"base"
);
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
List
<
Pod
>
podList
=
kubernetesClient
.
pods
().
inNamespace
(
namespace
).
withLabels
(
labels
).
list
().
getItems
();
// 端口映射
String
port
=
null
;
List
<
Map
<
String
,
Object
>>
portMappingList
=
new
ArrayList
<>();
String
host
=
null
;
ObjectMeta
podMetadata
=
pod
.
getMetadata
();
if
(
service
!=
null
)
{
String
serviceName
=
podMetadata
.
getLabels
().
get
(
"qcloud-app"
);
port
=
String
.
valueOf
(
service
.
getSpec
().
getPorts
().
get
(
0
).
getNodePort
());
// 端口暴露在Service
}
Service
service
=
kubernetesClient
.
services
().
inNamespace
(
namespace
).
withName
(
serviceName
).
get
();
if
(
podList
.
size
()
==
1
)
{
if
(
service
!=
null
)
{
host
=
podList
.
get
(
0
).
getStatus
().
getHostIP
();
map
.
put
(
"serviceType"
,
service
.
getSpec
().
getType
());
}
List
<
ServicePort
>
servicePortList
=
service
.
getSpec
().
getPorts
();
if
(
podList
.
size
()
>=
2
)
{
if
(
servicePortList
.
size
()
>
0
)
{
for
(
Pod
pod:
podList
)
{
for
(
ServicePort
servicePort
:
servicePortList
)
{
if
(
pod
.
getStatus
().
getHostIP
()
!=
null
)
{
if
(
servicePort
.
getNodePort
()
!=
null
)
{
host
=
pod
.
getStatus
().
getHostIP
();
map
.
put
(
"port_"
+
servicePort
.
getName
(),
servicePort
.
getNodePort
());
break
;
}
Map
<
String
,
Object
>
portMap
=
new
HashMap
<>();
portMap
.
put
(
"name"
,
servicePort
.
getName
());
portMap
.
put
(
"nodePort"
,
servicePort
.
getNodePort
());
portMap
.
put
(
"port"
,
servicePort
.
getPort
());
portMap
.
put
(
"protocol"
,
servicePort
.
getProtocol
());
portMap
.
put
(
"targetPort"
,
servicePort
.
getTargetPort
());
portMappingList
.
add
(
portMap
);
}
}
if
(
portMappingList
.
size
()
>
0
)
{
map
.
put
(
"portMappings"
,
portMappingList
);
}
}
// 格式化创建时间
try
{
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss'Z'"
);
df
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"UTC"
));
map
.
put
(
"createdAt"
,
df
.
parse
(
podMetadata
.
getCreationTimestamp
()));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
log
.
info
(
"时间转换异常!"
);
map
.
put
(
"createdAt"
,
podMetadata
.
getCreationTimestamp
());
}
// 从Ingress里获取host
Ingress
ingress
=
kubernetesClient
.
extensions
().
ingresses
().
inNamespace
(
namespace
).
withName
(
serviceName
).
get
();
if
(
ingress
!=
null
)
{
map
.
put
(
"host"
,
ingress
.
getSpec
().
getRules
().
get
(
0
).
getHost
());
}
map
.
put
(
"serviceName"
,
serviceName
);
map
.
put
(
"podName"
,
podMetadata
.
getName
());
map
.
put
(
"labels"
,
podMetadata
.
getLabels
());
map
.
put
(
"image"
,
pod
.
getStatus
().
getContainerStatuses
().
get
(
0
).
getImage
());
map
.
put
(
"imageId"
,
pod
.
getStatus
().
getContainerStatuses
().
get
(
0
).
getImageID
());
map
.
put
(
"lanIp"
,
pod
.
getStatus
().
getHostIP
());
map
.
put
(
"podIp"
,
pod
.
getStatus
().
getPodIP
());
map
.
put
(
"startTime"
,
pod
.
getStatus
().
getStartTime
());
// 状态判断
List
<
ContainerStatus
>
containerStatuses
=
pod
.
getStatus
().
getContainerStatuses
();
String
status
=
""
;
if
(
pod
.
getMetadata
().
getDeletionTimestamp
()
!=
null
)
{
status
=
"Terminating"
;
}
else
{
if
(
containerStatuses
.
size
()
==
0
)
{
status
=
"Pending"
;
}
else
{
if
(
containerStatuses
.
get
(
0
).
getState
().
getWaiting
()
!=
null
)
{
status
=
"ContainerCreating"
;
}
if
(
containerStatuses
.
get
(
0
).
getState
().
getRunning
()
!=
null
&&
!
containerStatuses
.
get
(
0
).
getReady
())
{
status
=
"Waiting"
;
}
if
(
containerStatuses
.
get
(
0
).
getState
().
getRunning
()
!=
null
&&
containerStatuses
.
get
(
0
).
getReady
())
{
status
=
"Normal"
;
}
}
}
}
map
.
put
(
"status"
,
status
);
result
.
add
(
map
);
}
}
}
}
return
result
;
Map
<
String
,
String
>
resultMap
=
new
HashMap
<>();
resultMap
.
put
(
"host"
,
host
);
resultMap
.
put
(
"port"
,
port
);
return
resultMap
;
}
}
...
...
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