Authored by qinchao

nginx切换

... ... @@ -6,4 +6,7 @@ public class HostTagConstant {
public static final String tag_az1="az1";
public static final String tag_az2="az2";
public static final String tag_huidu_cn="灰度";
public static final String tag_luaSwitch="lua-switch";
public static final String tag_lbSwitch="lb-switch";
}
... ...
... ... @@ -2,6 +2,7 @@ package com.monitor.switchs;
import com.google.common.collect.Maps;
import com.model.HostInfo;
import com.monitor.common.HostTagConstant;
import com.monitor.model.response.BaseResponse;
import com.monitor.mysql.mapper.HostInfoMapper;
import org.apache.commons.collections.CollectionUtils;
... ... @@ -43,7 +44,7 @@ public class LbSwitchCtrl {
@ResponseBody
public BaseResponse viewConf() throws Exception {
//直连的配置放在腾讯云nginx上
List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("lb-switch");
List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_lbSwitch);
if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
return new BaseResponse(500, "没有找到直连nginx服务器", new HashMap<String, String>());
}
... ... @@ -83,7 +84,7 @@ public class LbSwitchCtrl {
String path = request.getSession().getServletContext().getRealPath("/") + "lua/lb.lua";
logger.info("lb.lua template path {}", path);
//lua dns 只放在腾讯云nginx服务器上
List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag("lb-switch");
List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_lbSwitch);
if (CollectionUtils.isEmpty(nginxHosts)) {
return new BaseResponse(500, "在cmdb中未找到直连对应的nginx服务器!");
}
... ...
package com.monitor.switchs;
import com.model.HostInfo;
import com.monitor.common.HostTagConstant;
import com.monitor.model.response.BaseResponse;
import com.monitor.mysql.mapper.HostInfoMapper;
import com.yoho.ops.cmdb.qcloud.util.QcloudDnspotUtil;
... ... @@ -39,7 +40,7 @@ public class LuaSwitchCtrl {
@ResponseBody
public String viewConf() throws Exception {
try{
List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("lua-switch");
List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_luaSwitch);
if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
logger.error(" LuaSwitchCtrl - viewConf - init - error=没有找到lua切换nginx服务器");
return "没有找到lua切换nginx服务器";
... ... @@ -86,7 +87,7 @@ public class LuaSwitchCtrl {
@ResponseBody
public BaseResponse viewCurrentConf() {
try{
List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("lua-switch");
List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_luaSwitch);
String result = exe("view", qcloudNginxHosts.get(0).getHostIp(), null);
return new BaseResponse(200, "success", result);
}catch (Exception e){
... ... @@ -105,7 +106,7 @@ public class LuaSwitchCtrl {
public BaseResponse switchConf(HttpServletRequest request) {
try{
//lua dns 只放在腾讯云nginx服务器上
List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag("lua-switch");
List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_luaSwitch);
if (CollectionUtils.isEmpty(nginxHosts)) {
return new BaseResponse(500, "腾讯云在cmdb中未找到对应的nginx服务器!");
}
... ...
... ... @@ -257,12 +257,23 @@ public class NginxSwitchCtrl {
@ResponseBody
public BaseResponse switchNginxConf(String cloudName, HttpServletRequest httpRequest) {
try{
int nginxCloudType = StringUtils.equals("aws", cloudName) ? 1 : (StringUtils.equals("qcloud", cloudName) ? 2 : 0);
int nginxCloudType= 0;
String azTag="";
if(StringUtils.equals("aws", cloudName)){
nginxCloudType=1;
}else if(StringUtils.equals("qcloud", cloudName)){
nginxCloudType=2;
azTag="az1";
}else if(cloudName!=null&&cloudName.contains("qcloud")&&cloudName.contains("az2")){
nginxCloudType=2;
azTag="az2";
}
if (0 == nginxCloudType) {
return new BaseResponse(500, "没有匹配到要切换的云");
}
//获取对应cloud的机器 aws 1 qcloud 2
List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", nginxCloudType);
List<HostInfo> nginxHosts = getUpstreamTagNginxHostList( nginxCloudType,azTag);
if (CollectionUtils.isEmpty(nginxHosts)) {
return new BaseResponse(500, cloudName + "在cmdb中未找到对应的nginx服务器!");
}
... ... @@ -270,7 +281,7 @@ public class NginxSwitchCtrl {
for (HostInfo hostInfo : nginxHosts) {
ips.add(hostInfo.getHostIp());
}
String tempPath = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf";
//String tempPath = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf";
return new BaseResponse(200, "success", exe(" -eswitch -n" + StringUtils.join(ips, ",") + " -s" + cloudName));
}catch (Exception e){
logger.error(" NginxSwitchCtrl - switchNginxConf - 3 - error" , e);
... ...