... ... @@ -96,6 +96,7 @@ public class HttpUriContants {
* nginxswitch
*/
public static final String VIEW_NGINX_CONF= "/nginxswitch/viewNginxConf";
public static final String VIEW_TOCHANGE_NGINX_CONF= "/nginxswitch/viewToChangeNginxConf";
public static final String SWITCH_NGINX= "/nginxswitch/switchNginxConf";
/**
... ...
... ... @@ -35,9 +35,9 @@ public class NginxSwitchCtrl {
*/
@RequestMapping(value = "toNginxSwitch")
public ModelAndView viewNginxConf() {
BaseResponse<Map<String,List<Map<String, Object>>>> response = httpRestClient.exchangeForget(HttpUriContants.VIEW_NGINX_CONF, new ParameterizedTypeReference<BaseResponse<Map<String,List<Map<String, Object>>>>>() {
BaseResponse<Map<String, List<Map<String, Object>>>> response = httpRestClient.exchangeForget(HttpUriContants.VIEW_NGINX_CONF, new ParameterizedTypeReference<BaseResponse<Map<String, List<Map<String, Object>>>>>() {
}, null);
Map<String,List<Map<String, Object>>> map = response.getData();
Map<String, List<Map<String, Object>>> map = response.getData();
ModelAndView mdv = new ModelAndView("switch/nginxSwitch");
mdv.addObject("awsList", map.get("awsList"));
mdv.addObject("qcloudList", map.get("qcloudList"));
... ... @@ -51,11 +51,25 @@ public class NginxSwitchCtrl {
*/
@RequestMapping(value = "switchNginxConf")
@ResponseBody
public BaseResponse switchNginxConf(String cloudName, String target) {
Map map = new HashMap<>();
map.put("cloudName",cloudName);
map.put("target",target);
public BaseResponse switchNginxConf() {
Map<String, String> map = new HashMap<>();
return httpRestClient.defaultGet(HttpUriContants.SWITCH_NGINX, BaseResponse.class, map);
}
/**
* 修改配置
*
* @return 2016年5月12日下午1:49:48
*/
@RequestMapping(value = "viewToChangeNginxConf")
@ResponseBody
public BaseResponse viewToChangeNginxConf(String cloudName, String target, String onlineOrGray, String noChangeIps) {
Map<String, String> map = new HashMap<>();
map.put("cloudName", cloudName);
map.put("target", target);
map.put("onlineOrGray", onlineOrGray);
map.put("ips", noChangeIps);
return httpRestClient.defaultGet(HttpUriContants.VIEW_TOCHANGE_NGINX_CONF, BaseResponse.class, map);
}
}
... ...
... ... @@ -124,27 +124,27 @@
<button class="btn btn-sm ">
线上切换
</button>
<button class="btn btn-sm btn-success" onclick="initSwitch('aws','qcloud')">
<button class="btn btn-sm btn-success" onclick="initSwitch('aws','qcloud','online','${awsList}')">
<i class="icon-cogs bigger-110"></i>
切向qcloud
</button>
<button class="btn btn-sm btn-success" onclick="initSwitch('aws','aws')">
<button class="btn btn-sm btn-success" onclick="initSwitch('aws','aws','online','${awsList}')">
<i class="icon-cogs bigger-110"></i>
切向aws
</button><br><br>
<button class="btn btn-sm ">
灰度切换
</button>
<button class="btn btn-sm btn-success" onclick="initSwitch('gray','qcloud')">
<button class="btn btn-sm btn-success" onclick="initSwitch('aws','qcloud','gray','${awsList}')">
<i class="icon-cogs bigger-110"></i>
切向qcloud
</button>
</button>
<button class="btn btn-sm btn-success" onclick="initSwitch('gray','aws')">
<button class="btn btn-sm btn-success" onclick="initSwitch('aws','aws','gray','${awsList}')">
<i class="icon-cogs bigger-110"></i>
切向aws
</button>
<button class="btn btn-sm btn-success" onclick="initSwitch('gray','gray')">
<button class="btn btn-sm btn-success" onclick="initSwitch('aws','gray','gray','${awsList}')">
<i class="icon-cogs bigger-110"></i>
切向gray
</button>
... ... @@ -180,11 +180,11 @@
<button class="btn btn-sm ">
线上切换
</button>
<button class="btn btn-sm btn-success" onclick="initSwitch('qcloud','qcloud')">
<button class="btn btn-sm btn-success" onclick="initSwitch('qcloud','qcloud','online','${qcloudList}')">
<i class="icon-cogs bigger-110"></i>
切向qcloud
</button>
<button class="btn btn-sm btn-success" onclick="initSwitch('qcloud','aws')">
<button class="btn btn-sm btn-success" onclick="initSwitch('qcloud','aws','online','${qcloudList}')">
<i class="icon-cogs bigger-110"></i>
切向aws
</button>
... ... @@ -192,15 +192,15 @@
<button class="btn btn-sm ">
灰度切换
</button>&nbsp;&nbsp;
<button class="btn btn-sm btn-success" onclick="initSwitch('gray','qcloud')">
<button class="btn btn-sm btn-success" onclick="initSwitch('qcloud','qcloud','gray','${qcloudList}')">
<i class="icon-cogs bigger-110"></i>
切向qcloud
</button>
<button class="btn btn-sm btn-success" onclick="initSwitch('gray','aws')">
<button class="btn btn-sm btn-success" onclick="initSwitch('qcloud','aws','gray','${qcloudList}')">
<i class="icon-cogs bigger-110"></i>
切向aws
</button>
<button class="btn btn-sm btn-success" onclick="initSwitch('gray','gray')">
<button class="btn btn-sm btn-success" onclick="initSwitch('qcloud','gray','gray','${qcloudList}')">
<i class="icon-cogs bigger-110"></i>
切向gray
</button>
... ... @@ -229,9 +229,5 @@
</div>
<script src="<%=basePath %>script/common/genarate_left_panel.js"></script>
<script>
$("#li_switch").addClass("active open");
$("#li_nginxswitch").addClass("active");
</script>
</body>
</html>
... ...
var dialog1;
var dialog2;
var dialog3;
$(function () {
$("#validateTokenRltInfo").hide();
dialog1 = $("<div>").appendTo($("body"));
dialog2 = $("<div>").appendTo($("body"));
dialog3 = $("<div>").appendTo($("body"));
});
function initSwitch(cloudName, target) {
var dialog = $("<div>").appendTo($("body"));
dialog.dialog({
function initSwitch(cloudName, target, onlineOrGray, list) {
var arr = getNoChangeIpArr(onlineOrGray, list);
if (undefined === arr || null === arr || 0 === arr.length) {
prompt("提示", "无可切换的gateway!");
return;
}
dialog2.dialog({
title: "你确定切换吗",
backdrop: "static",
content: "你确定要将" + cloudName + "上的流量切向" + target + "吗?",
content: "你确定要将" + cloudName + "上的" + onlineOrGray + "流量切向" + target + "吗?",
buttons: [{
text: "否",
className: "btn-danger",
onclick: function () {
$(dialog).dialog("hide");
$(dialog2).dialog("hide");
}
}, {
text: "是",
... ... @@ -20,38 +31,109 @@ function initSwitch(cloudName, target) {
onclick: function () {
var param = {
cloudName: cloudName,
target: target
}
$.ajax({
type: 'post',
url: 'switchNginxConf',
data: param,
dataType: 'text',
success: function (resp) {
$(dialog).dialog("hide");
var data = JSON.parse(resp);
var dialog2 = $("<div>").appendTo($("body"));
dialog2.dialog({
title: "切换结果",
backdrop: "static",
content: data.data,
buttons: [{
text: "确定",
className: "btn-danger",
onclick: function () {
window.location.href = getUrlBasePath() + "/nginxswitch/toNginxSwitch";
}
}]
});
},
error: function (data) {
layer.msg("Token异常", {icon: 2});
}
});
target: target,
onlineOrGray: onlineOrGray,
noChangeIps: JSON.stringify(arr)
};
sendAjax("post", "viewToChangeNginxConf", param, "text", viewToChangeSuccess, errorFunc);
}
}]
});
}
function viewToChangeSuccess(resp) {
$(dialog2).dialog("hide");
var data = JSON.parse(resp);
dialog3.dialog({
title: "切换结果",
backdrop: "static",
content: data.data,
buttons: [{
text: "否",
className: "btn-danger",
onclick: function () {
$(dialog3).dialog("hide");
}
}, {
text: "确定",
className: "btn-danger",
onclick: function () {
sendAjax("post", "switchNginxConf", {}, "text", switchSuccess, errorFunc);
}
}]
});
}
function switchSuccess() {
window.location.href = getUrlBasePath() + "/nginxswitch/toNginxSwitch";
}
/**
* 获取不切换的服务器ip(由于线上和灰度同在nginx.conf配置中,模板须设计两个占位符,所以切换某一处时,须将不切换的ip提交到后台)
* @param onlineOrGray 切换线上/灰度
* @param list 线上和灰度的upstream配置列表
* @returns {Array} 不切换服务器的ip列表
*/
function getNoChangeIpArr(onlineOrGray, list) {
if (undefined === list || null === list || 0 === list.length) {
return [];
}
var arr = [];
var p = "";
if ("online" === onlineOrGray) {
p = "grayapigateway";
} else {
p = "apigateway";
}
for (var o in list) {
if (undefined === o || null === o || undefined === o.name || null === o.name || p !== o.name) {
continue;
}
if (undefined === o.server || null === o.server || 0 === o.server.length) {
continue;
}
for (var ob in o.server) {
if (undefined === ob || null === ob || '' === ob) {
continue;
}
arr.push(ob.split(":")[0].replace(" ", "").replace("server", ""));
}
}
return arr;
}
/**
* 提示函数
* @param title
* @param content
*/
function prompt(title, content) {
dialog1.dialog({
title: title,
backdrop: "static",
content: content,
buttons: [{
text: "确定",
className: "btn-success",
onclick: function () {
$(dialog1).dialog("hide");
}
}]
});
}
function sendAjax(type, url, data, dataType, success, error) {
$.ajax({
type: type,
url: url,
data: data,
dataType: dataType,
success: success,
error: error
});
}
function errorFunc() {
layer.msg("Token异常", {icon: 2});
}
\ No newline at end of file
... ...