Toggle navigation
Toggle navigation
This project
Loading...
Sign in
ops
/
monitor-service
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
1
Download as
Email Patches
Plain Diff
Browse Files
Authored by
qinchao
7 years ago
Commit
d79c2a93bb7ae3310632513eba0c6d056e9cf4c9
1 parent
bc3f1b2a
lb切换
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
136 additions
and
110 deletions
monitor-service-switch/src/main/java/com/monitor/switchs/NginxSwitchCtrl.java
monitor-service-switch/src/main/java/com/monitor/switchs/NginxSwitchCtrl.java
View file @
d79c2a9
...
...
@@ -25,6 +25,7 @@ import java.io.File;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.util.*
;
import
java.util.concurrent.CompletableFuture
;
import
java.util.stream.Collectors
;
/**
...
...
@@ -36,7 +37,7 @@ public class NginxSwitchCtrl {
private
Logger
logger
=
LoggerFactory
.
getLogger
(
"switchLogger"
);
private
long
sleepTime
=
10
*
1000L
;
private
long
sleepTime
OneSecond
=
1
*
1000L
;
@Value
(
"${system.envi}"
)
private
String
env
;
...
...
@@ -69,136 +70,160 @@ public class NginxSwitchCtrl {
if
(
lbList
==
null
||
lbList
.
size
()<=
0
){
return
new
BaseResponse
(
201
,
"源lb为空,请检测参数:"
+
cloudName
);
}
Map
<
String
,
String
>
innerListenerMap
=
getLbListerner
(
true
);
Map
<
String
,
String
>
outerListenerMap
=
getLbListerner
(
false
);
for
(
LbModel
lb:
lbList
){
if
(
lb
.
isClbFlag
()){
switchClb
(
targetMap
,
lb
);
switchClb
(
targetMap
,
lb
);
}
else
{
Map
<
String
,
String
>
listenerMap
=
outerListenerMap
;
if
(
lb
.
isInnerFlag
())
{
listenerMap
=
innerListenerMap
;
}
switchAlb
(
targetMap
,
lb
);
}
}
/// end for
QcloudForwardLbModel
forwardLbModel
=
getAlbFromQcloud
(
lb
);
if
(
forwardLbModel
==
null
)
{
return
new
BaseResponse
(
201
,
"获取alb信息出现异常,切换失败:"
+
lb
.
getKey
());
}
List
<
AlbModelClass
>
dealAlb
=
new
ArrayList
<>();
for
(
String
protoTypeAndPort
:
listenerMap
.
keySet
())
{
AlbModelClass
albModelClass
=
new
AlbModelClass
();
String
[]
protoAndPortArray
=
protoTypeAndPort
.
split
(
","
);
String
protocolType
=
protoAndPortArray
[
0
];
int
port
=
Integer
.
parseInt
(
protoAndPortArray
[
1
]);
String
domain
=
listenerMap
.
get
(
protoTypeAndPort
);
List
<
String
>
locationIdList
=
new
ArrayList
<>();
for
(
QcloudForwardLbModelData
data
:
forwardLbModel
.
getData
())
{
if
(
StringUtils
.
equals
(
protocolType
,
data
.
getProtocolType
())
&&
port
==
data
.
getLoadBalancerPort
())
{
String
listenerId
=
data
.
getListenerId
();
for
(
QcloudForwardLbModelDataRule
rule
:
data
.
getRules
())
{
if
(
StringUtils
.
equals
(
domain
,
rule
.
getDomain
()))
{
locationIdList
.
add
(
rule
.
getLocationId
());
List
<
List
<
QcloudLbModelBackend
>>
ls
=
getNeedChangedWei
(
rule
.
getBackends
(),
targetMap
);
List
<
QcloudLbModelBackend
>
needAddWeight
=
ls
.
get
(
0
);
List
<
QcloudLbModelBackend
>
needRemoveWeight
=
ls
.
get
(
1
);
albModelClass
.
setListenerId
(
listenerId
);
albModelClass
.
setLocationIdList
(
locationIdList
);
albModelClass
.
setNeedAddWeight
(
needAddWeight
);
albModelClass
.
setNeedRemoveWeight
(
needRemoveWeight
);
dealAlb
.
add
(
albModelClass
);
return
new
BaseResponse
();
}
/**
* 发起异步调用
*/
/*private void startRunAsyLbSwitch(List<LbModel> lbList, Map<String,Integer> targetMap){
CompletableFuture.runAsync(new Runnable() {
@Override
public void run() {
for(LbModel lb:lbList){
if(lb.isClbFlag()){
switchClb(targetMap,lb);
}else {
switchAlb(targetMap,lb);
}
}/// end for
}
});
}*/
private
BaseResponse
switchAlb
(
Map
<
String
,
Integer
>
targetMap
,
LbModel
lb
){
Map
<
String
,
String
>
innerListenerMap
=
getLbListerner
(
true
);
Map
<
String
,
String
>
outerListenerMap
=
getLbListerner
(
false
);
Map
<
String
,
String
>
listenerMap
=
outerListenerMap
;
if
(
lb
.
isInnerFlag
())
{
listenerMap
=
innerListenerMap
;
}
QcloudForwardLbModel
forwardLbModel
=
getAlbFromQcloud
(
lb
);
if
(
forwardLbModel
==
null
)
{
return
new
BaseResponse
(
201
,
"获取alb信息出现异常,切换失败:"
+
lb
.
getKey
());
}
List
<
AlbModelClass
>
dealAlb
=
new
ArrayList
<>();
for
(
String
protoTypeAndPort
:
listenerMap
.
keySet
())
{
AlbModelClass
albModelClass
=
new
AlbModelClass
();
String
[]
protoAndPortArray
=
protoTypeAndPort
.
split
(
","
);
String
protocolType
=
protoAndPortArray
[
0
];
int
port
=
Integer
.
parseInt
(
protoAndPortArray
[
1
]);
String
domain
=
listenerMap
.
get
(
protoTypeAndPort
);
List
<
String
>
locationIdList
=
new
ArrayList
<>();
for
(
QcloudForwardLbModelData
data
:
forwardLbModel
.
getData
())
{
if
(
StringUtils
.
equals
(
protocolType
,
data
.
getProtocolType
())
&&
port
==
data
.
getLoadBalancerPort
())
{
String
listenerId
=
data
.
getListenerId
();
for
(
QcloudForwardLbModelDataRule
rule
:
data
.
getRules
())
{
if
(
StringUtils
.
equals
(
domain
,
rule
.
getDomain
()))
{
locationIdList
.
add
(
rule
.
getLocationId
());
List
<
List
<
QcloudLbModelBackend
>>
ls
=
getNeedChangedWei
(
rule
.
getBackends
(),
targetMap
);
List
<
QcloudLbModelBackend
>
needAddWeight
=
ls
.
get
(
0
);
List
<
QcloudLbModelBackend
>
needRemoveWeight
=
ls
.
get
(
1
);
albModelClass
.
setListenerId
(
listenerId
);
albModelClass
.
setLocationIdList
(
locationIdList
);
albModelClass
.
setNeedAddWeight
(
needAddWeight
);
albModelClass
.
setNeedRemoveWeight
(
needRemoveWeight
);
dealAlb
.
add
(
albModelClass
);
}
}
}
}
}
}
}
for
(
AlbModelClass
albModelClass
:
dealAlb
)
{
//操作weight,
List
<
QcloudLbModelBackend
>
needAddWeight
=
albModelClass
.
getNeedAddWeight
();
if
(
needAddWeight
.
size
()
>
0
)
{
List
<
String
>
unInstanceIdList
=
new
ArrayList
<>();
List
<
Integer
>
weightList
=
new
ArrayList
<>();
List
<
Integer
>
portList
=
new
ArrayList
<>();
for
(
QcloudLbModelBackend
backend
:
needAddWeight
)
{
unInstanceIdList
.
add
(
backend
.
getUnInstanceId
());
weightList
.
add
(
backend
.
getUpdateWeight
());
portList
.
add
(
backend
.
getPort
());
}
//alb 切换权重
QcloudSdkUtil
.
modifyForwardSeventhBackends
(
lb
.
getLbId
(),
albModelClass
.
getListenerId
(),
albModelClass
.
getLocationIdList
(),
unInstanceIdList
,
portList
,
weightList
);
try
{
// 括号内的参数是毫秒值,线程休眠
Thread
.
sleep
(
sleepTimeOneSecond
*
2
);
//2s
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
boolean
recheckFlag
=
false
;
for
(
AlbModelClass
albModelClass
:
dealAlb
)
{
List
<
QcloudLbModelBackend
>
needRemoveWeight
=
albModelClass
.
getNeedRemoveWeight
();
if
(
needRemoveWeight
.
size
()
>
0
)
{
recheckFlag
=
true
;
break
;
}
}
if
(
recheckFlag
)
{
//切掉流量之前需要校验
QcloudForwardLbModel
checkBeforeMoveBackends
=
getAlbFromQcloud
(
lb
);
boolean
checkPass
=
false
;
for
(
QcloudForwardLbModelData
flbModel
:
checkBeforeMoveBackends
.
getData
())
{
for
(
AlbModelClass
albModelClass
:
dealAlb
)
{
//操作weight,
List
<
QcloudLbModelBackend
>
needAddWeight
=
albModelClass
.
getNeedAddWeight
();
if
(
needAddWeight
.
size
()
>
0
)
{
if
(
StringUtils
.
equals
(
flbModel
.
getListenerId
(),
albModelClass
.
getListenerId
()))
{
List
<
String
>
unInstanceIdList
=
new
ArrayList
<>();
List
<
Integer
>
weightList
=
new
ArrayList
<>();
List
<
Integer
>
portList
=
new
ArrayList
<>();
for
(
QcloudLbModelBackend
backend
:
needAddWeight
)
{
for
(
QcloudLbModelBackend
backend
:
albModelClass
.
getNeedRemoveWeight
()
)
{
unInstanceIdList
.
add
(
backend
.
getUnInstanceId
());
weightList
.
add
(
backend
.
getUpdateWeight
()
);
weightList
.
add
(
0
);
portList
.
add
(
backend
.
getPort
());
}
//alb 切换权重
QcloudSdkUtil
.
modifyForwardSeventhBackends
(
lb
.
getLbId
(),
albModelClass
.
getListenerId
(),
albModelClass
.
getLocationIdList
(),
unInstanceIdList
,
portList
,
weightList
);
try
{
// 括号内的参数是毫秒值,线程休眠
Thread
.
sleep
(
sleepTime
);
//10s
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
boolean
recheckFlag
=
false
;
for
(
AlbModelClass
albModelClass
:
dealAlb
)
{
List
<
QcloudLbModelBackend
>
needRemoveWeight
=
albModelClass
.
getNeedRemoveWeight
();
if
(
needRemoveWeight
.
size
()
>
0
)
{
recheckFlag
=
true
;
break
;
}
}
if
(
recheckFlag
)
{
//切掉流量之前需要校验
QcloudForwardLbModel
checkBeforeMoveBackends
=
getAlbFromQcloud
(
lb
);
boolean
checkPass
=
false
;
for
(
QcloudForwardLbModelData
flbModel
:
checkBeforeMoveBackends
.
getData
())
{
for
(
AlbModelClass
albModelClass
:
dealAlb
)
{
if
(
StringUtils
.
equals
(
flbModel
.
getListenerId
(),
albModelClass
.
getListenerId
()))
{
List
<
String
>
unInstanceIdList
=
new
ArrayList
<>();
List
<
Integer
>
weightList
=
new
ArrayList
<>();
List
<
Integer
>
portList
=
new
ArrayList
<>();
for
(
QcloudLbModelBackend
backend
:
albModelClass
.
getNeedRemoveWeight
())
{
unInstanceIdList
.
add
(
backend
.
getUnInstanceId
());
weightList
.
add
(
0
);
portList
.
add
(
backend
.
getPort
());
}
for
(
QcloudForwardLbModelDataRule
rule
:
flbModel
.
getRules
())
{
if
(
albModelClass
.
getLocationIdList
().
contains
(
rule
.
getLocationId
()))
{
for
(
QcloudLbModelBackend
backend
:
rule
.
getBackends
())
{
if
(
backend
.
getWeight
()
>
0
&&
!
unInstanceIdList
.
contains
(
backend
.
getUnInstanceId
()))
{
checkPass
=
true
;
break
;
}
}
}
}
if
(
checkPass
)
{
//alb 切换权重
QcloudSdkUtil
.
modifyForwardSeventhBackends
(
lb
.
getLbId
(),
albModelClass
.
getListenerId
(),
albModelClass
.
getLocationIdList
(),
unInstanceIdList
,
portList
,
weightList
);
try
{
// 括号内的参数是毫秒值,线程休眠
Thread
.
sleep
(
sleepTime
*
2
);
//10s
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
for
(
QcloudForwardLbModelDataRule
rule
:
flbModel
.
getRules
())
{
if
(
albModelClass
.
getLocationIdList
().
contains
(
rule
.
getLocationId
()))
{
for
(
QcloudLbModelBackend
backend
:
rule
.
getBackends
())
{
if
(
backend
.
getWeight
()
>
0
&&
!
unInstanceIdList
.
contains
(
backend
.
getUnInstanceId
()))
{
checkPass
=
true
;
break
;
}
}
}
}
if
(
checkPass
)
{
//alb 切换权重
QcloudSdkUtil
.
modifyForwardSeventhBackends
(
lb
.
getLbId
(),
albModelClass
.
getListenerId
(),
albModelClass
.
getLocationIdList
(),
unInstanceIdList
,
portList
,
weightList
);
try
{
// 括号内的参数是毫秒值,线程休眠
Thread
.
sleep
(
sleepTimeOneSecond
*
5
);
//10s
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
}
return
new
BaseResponse
();
}
...
...
@@ -228,7 +253,7 @@ public class NginxSwitchCtrl {
try
{
// 括号内的参数是毫秒值,线程休眠
Thread
.
sleep
(
sleepTime
);
//10s
Thread
.
sleep
(
sleepTime
OneSecond
*
2
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
...
...
@@ -254,12 +279,13 @@ public class NginxSwitchCtrl {
//clb 切换权重
QcloudSdkUtil
.
modifyLoadBalancerBackends
(
lb
.
getLbId
(),
unInstanceIdList
,
weightList
);
try
{
// clb不需要等待
/*try {
// 括号内的参数是毫秒值,线程休眠
Thread
.
sleep
(
sleepTime
*
2
);
//20
s
Thread.sleep(sleepTime
OneSecond*5);//2
s
} catch (InterruptedException e) {
e.printStackTrace();
}
}
*/
}
}
return
new
BaseResponse
();
...
...
qinchao
@qinchao
7 years ago
Guest
mentioned in commit
72e94594
Please
register
or
login
to post a comment