Authored by qinchao

nginx切换

@@ -6,4 +6,7 @@ public class HostTagConstant { @@ -6,4 +6,7 @@ public class HostTagConstant {
6 public static final String tag_az1="az1"; 6 public static final String tag_az1="az1";
7 public static final String tag_az2="az2"; 7 public static final String tag_az2="az2";
8 public static final String tag_huidu_cn="灰度"; 8 public static final String tag_huidu_cn="灰度";
  9 +
  10 + public static final String tag_luaSwitch="lua-switch";
  11 + public static final String tag_lbSwitch="lb-switch";
9 } 12 }
@@ -2,6 +2,7 @@ package com.monitor.switchs; @@ -2,6 +2,7 @@ package com.monitor.switchs;
2 2
3 import com.google.common.collect.Maps; 3 import com.google.common.collect.Maps;
4 import com.model.HostInfo; 4 import com.model.HostInfo;
  5 +import com.monitor.common.HostTagConstant;
5 import com.monitor.model.response.BaseResponse; 6 import com.monitor.model.response.BaseResponse;
6 import com.monitor.mysql.mapper.HostInfoMapper; 7 import com.monitor.mysql.mapper.HostInfoMapper;
7 import org.apache.commons.collections.CollectionUtils; 8 import org.apache.commons.collections.CollectionUtils;
@@ -43,7 +44,7 @@ public class LbSwitchCtrl { @@ -43,7 +44,7 @@ public class LbSwitchCtrl {
43 @ResponseBody 44 @ResponseBody
44 public BaseResponse viewConf() throws Exception { 45 public BaseResponse viewConf() throws Exception {
45 //直连的配置放在腾讯云nginx上 46 //直连的配置放在腾讯云nginx上
46 - List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("lb-switch"); 47 + List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_lbSwitch);
47 if (CollectionUtils.isEmpty(qcloudNginxHosts)) { 48 if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
48 return new BaseResponse(500, "没有找到直连nginx服务器", new HashMap<String, String>()); 49 return new BaseResponse(500, "没有找到直连nginx服务器", new HashMap<String, String>());
49 } 50 }
@@ -83,7 +84,7 @@ public class LbSwitchCtrl { @@ -83,7 +84,7 @@ public class LbSwitchCtrl {
83 String path = request.getSession().getServletContext().getRealPath("/") + "lua/lb.lua"; 84 String path = request.getSession().getServletContext().getRealPath("/") + "lua/lb.lua";
84 logger.info("lb.lua template path {}", path); 85 logger.info("lb.lua template path {}", path);
85 //lua dns 只放在腾讯云nginx服务器上 86 //lua dns 只放在腾讯云nginx服务器上
86 - List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag("lb-switch"); 87 + List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_lbSwitch);
87 if (CollectionUtils.isEmpty(nginxHosts)) { 88 if (CollectionUtils.isEmpty(nginxHosts)) {
88 return new BaseResponse(500, "在cmdb中未找到直连对应的nginx服务器!"); 89 return new BaseResponse(500, "在cmdb中未找到直连对应的nginx服务器!");
89 } 90 }
1 package com.monitor.switchs; 1 package com.monitor.switchs;
2 2
3 import com.model.HostInfo; 3 import com.model.HostInfo;
  4 +import com.monitor.common.HostTagConstant;
4 import com.monitor.model.response.BaseResponse; 5 import com.monitor.model.response.BaseResponse;
5 import com.monitor.mysql.mapper.HostInfoMapper; 6 import com.monitor.mysql.mapper.HostInfoMapper;
6 import com.yoho.ops.cmdb.qcloud.util.QcloudDnspotUtil; 7 import com.yoho.ops.cmdb.qcloud.util.QcloudDnspotUtil;
@@ -39,7 +40,7 @@ public class LuaSwitchCtrl { @@ -39,7 +40,7 @@ public class LuaSwitchCtrl {
39 @ResponseBody 40 @ResponseBody
40 public String viewConf() throws Exception { 41 public String viewConf() throws Exception {
41 try{ 42 try{
42 - List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("lua-switch"); 43 + List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_luaSwitch);
43 if (CollectionUtils.isEmpty(qcloudNginxHosts)) { 44 if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
44 logger.error(" LuaSwitchCtrl - viewConf - init - error=没有找到lua切换nginx服务器"); 45 logger.error(" LuaSwitchCtrl - viewConf - init - error=没有找到lua切换nginx服务器");
45 return "没有找到lua切换nginx服务器"; 46 return "没有找到lua切换nginx服务器";
@@ -86,7 +87,7 @@ public class LuaSwitchCtrl { @@ -86,7 +87,7 @@ public class LuaSwitchCtrl {
86 @ResponseBody 87 @ResponseBody
87 public BaseResponse viewCurrentConf() { 88 public BaseResponse viewCurrentConf() {
88 try{ 89 try{
89 - List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("lua-switch"); 90 + List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_luaSwitch);
90 String result = exe("view", qcloudNginxHosts.get(0).getHostIp(), null); 91 String result = exe("view", qcloudNginxHosts.get(0).getHostIp(), null);
91 return new BaseResponse(200, "success", result); 92 return new BaseResponse(200, "success", result);
92 }catch (Exception e){ 93 }catch (Exception e){
@@ -105,7 +106,7 @@ public class LuaSwitchCtrl { @@ -105,7 +106,7 @@ public class LuaSwitchCtrl {
105 public BaseResponse switchConf(HttpServletRequest request) { 106 public BaseResponse switchConf(HttpServletRequest request) {
106 try{ 107 try{
107 //lua dns 只放在腾讯云nginx服务器上 108 //lua dns 只放在腾讯云nginx服务器上
108 - List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag("lua-switch"); 109 + List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag(HostTagConstant.tag_luaSwitch);
109 if (CollectionUtils.isEmpty(nginxHosts)) { 110 if (CollectionUtils.isEmpty(nginxHosts)) {
110 return new BaseResponse(500, "腾讯云在cmdb中未找到对应的nginx服务器!"); 111 return new BaseResponse(500, "腾讯云在cmdb中未找到对应的nginx服务器!");
111 } 112 }
@@ -257,12 +257,23 @@ public class NginxSwitchCtrl { @@ -257,12 +257,23 @@ public class NginxSwitchCtrl {
257 @ResponseBody 257 @ResponseBody
258 public BaseResponse switchNginxConf(String cloudName, HttpServletRequest httpRequest) { 258 public BaseResponse switchNginxConf(String cloudName, HttpServletRequest httpRequest) {
259 try{ 259 try{
260 - int nginxCloudType = StringUtils.equals("aws", cloudName) ? 1 : (StringUtils.equals("qcloud", cloudName) ? 2 : 0); 260 + int nginxCloudType= 0;
  261 + String azTag="";
  262 + if(StringUtils.equals("aws", cloudName)){
  263 + nginxCloudType=1;
  264 + }else if(StringUtils.equals("qcloud", cloudName)){
  265 + nginxCloudType=2;
  266 + azTag="az1";
  267 + }else if(cloudName!=null&&cloudName.contains("qcloud")&&cloudName.contains("az2")){
  268 + nginxCloudType=2;
  269 + azTag="az2";
  270 + }
  271 +
261 if (0 == nginxCloudType) { 272 if (0 == nginxCloudType) {
262 return new BaseResponse(500, "没有匹配到要切换的云"); 273 return new BaseResponse(500, "没有匹配到要切换的云");
263 } 274 }
264 //获取对应cloud的机器 aws 1 qcloud 2 275 //获取对应cloud的机器 aws 1 qcloud 2
265 - List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", nginxCloudType); 276 + List<HostInfo> nginxHosts = getUpstreamTagNginxHostList( nginxCloudType,azTag);
266 if (CollectionUtils.isEmpty(nginxHosts)) { 277 if (CollectionUtils.isEmpty(nginxHosts)) {
267 return new BaseResponse(500, cloudName + "在cmdb中未找到对应的nginx服务器!"); 278 return new BaseResponse(500, cloudName + "在cmdb中未找到对应的nginx服务器!");
268 } 279 }
@@ -270,7 +281,7 @@ public class NginxSwitchCtrl { @@ -270,7 +281,7 @@ public class NginxSwitchCtrl {
270 for (HostInfo hostInfo : nginxHosts) { 281 for (HostInfo hostInfo : nginxHosts) {
271 ips.add(hostInfo.getHostIp()); 282 ips.add(hostInfo.getHostIp());
272 } 283 }
273 - String tempPath = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf"; 284 + //String tempPath = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf";
274 return new BaseResponse(200, "success", exe(" -eswitch -n" + StringUtils.join(ips, ",") + " -s" + cloudName)); 285 return new BaseResponse(200, "success", exe(" -eswitch -n" + StringUtils.join(ips, ",") + " -s" + cloudName));
275 }catch (Exception e){ 286 }catch (Exception e){
276 logger.error(" NginxSwitchCtrl - switchNginxConf - 3 - error" , e); 287 logger.error(" NginxSwitchCtrl - switchNginxConf - 3 - error" , e);