Authored by qinchao

nginx切换

@@ -191,8 +191,8 @@ public class HostInfoCtrl { @@ -191,8 +191,8 @@ public class HostInfoCtrl {
191 List<Host> qcloudHosts = qcloudFetcher.getAllInstancesSet(); 191 List<Host> qcloudHosts = qcloudFetcher.getAllInstancesSet();
192 message += hostInfoService.compareHostInfo(qcloudHosts,2); 192 message += hostInfoService.compareHostInfo(qcloudHosts,2);
193 193
194 - List<Host> awsHosts = awsFetcher.getAllEc2Details();  
195 - message += hostInfoService.compareHostInfo(awsHosts, 1); 194 + /*List<Host> awsHosts = awsFetcher.getAllEc2Details();
  195 + message += hostInfoService.compareHostInfo(awsHosts, 1);*/
196 if(StringUtils.isNotBlank(message)){ 196 if(StringUtils.isNotBlank(message)){
197 return new BaseResponse(message); 197 return new BaseResponse(message);
198 } 198 }
@@ -714,15 +714,20 @@ public class QcloudSdkUtil { @@ -714,15 +714,20 @@ public class QcloudSdkUtil {
714 } 714 }
715 715
716 public static void main(String args[]){ 716 public static void main(String args[]){
717 - /*Calendar calendar = Calendar.getInstance(); 717 + Calendar calendar = Calendar.getInstance();
718 calendar.add(Calendar.MINUTE, -60); 718 calendar.add(Calendar.MINUTE, -60);
719 String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()); 719 String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
720 List<Double> dd=getMonitorData("outtraffic",startTime); 720 List<Double> dd=getMonitorData("outtraffic",startTime);
721 System.out.println(dd); 721 System.out.println(dd);
722 System.out.println(""); 722 System.out.println("");
723 List<Double> dd2=getMonitorData("intraffic",startTime); 723 List<Double> dd2=getMonitorData("intraffic",startTime);
724 - System.out.println(dd2);*/ 724 + System.out.println(dd2);
725 725
726 //String[] ips={"10.66.1.105","10.66.1.127","10.66.103.13","10.66.103.14","10.66.70.234","10.66.70.174"}; 726 //String[] ips={"10.66.1.105","10.66.1.127","10.66.103.13","10.66.103.14","10.66.70.234","10.66.70.174"};
  727 +
  728 +
  729 + /*List<Host> ls= getAllInstancesSet();
  730 + System.out.println(ls.size());
  731 + System.out.println(ls);*/
727 } 732 }
728 } 733 }
  1 +package com.monitor.common;
  2 +
  3 +public class HostTagConstant {
  4 + public static final String tag_upstreamSwitch="upstream-switch";
  5 + public static final String tag_gateway="gateway";
  6 + public static final String tag_az1="az1";
  7 + public static final String tag_az2="az2";
  8 + public static final String tag_huidu_cn="灰度";
  9 +}
@@ -2,6 +2,7 @@ package com.monitor.switchs; @@ -2,6 +2,7 @@ package com.monitor.switchs;
2 2
3 import com.google.common.collect.Lists; 3 import com.google.common.collect.Lists;
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;
@@ -22,6 +23,7 @@ import java.io.IOException; @@ -22,6 +23,7 @@ import java.io.IOException;
22 import java.util.*; 23 import java.util.*;
23 import java.util.regex.Matcher; 24 import java.util.regex.Matcher;
24 import java.util.regex.Pattern; 25 import java.util.regex.Pattern;
  26 +import java.util.stream.Collectors;
25 27
26 /** 28 /**
27 * @author zhengyouwei 2016年5月12日 下午1:49:55 29 * @author zhengyouwei 2016年5月12日 下午1:49:55
@@ -49,43 +51,105 @@ public class NginxSwitchCtrl { @@ -49,43 +51,105 @@ public class NginxSwitchCtrl {
49 try{ 51 try{
50 Map<String, List<Map<String, Object>>> map = new HashMap<>(); 52 Map<String, List<Map<String, Object>>> map = new HashMap<>();
51 53
52 - List<HostInfo> grayServerHosts=hostInfoMapper.selectHostInfosByTagList(Arrays.asList(new String[]{"灰度","gateway"}));  
53 - List<String> grayHostIps=new ArrayList<String>();  
54 - for(HostInfo host:grayServerHosts){  
55 - grayHostIps.add(host.getHostIp());  
56 - }  
57 //获取灰度机器的server。ip 54 //获取灰度机器的server。ip
58 - List<Map<String, Object>> grayHosts=new ArrayList<>();  
59 Map<String, Object> tmpmap=new HashMap<String, Object>(); 55 Map<String, Object> tmpmap=new HashMap<String, Object>();
60 - tmpmap.put("grayServerHosts", grayHostIps);  
61 - grayHosts.add(tmpmap);  
62 - map.put("grayServerHosts", grayHosts); 56 + tmpmap.put("grayServerHosts", getGatewayHostIPs(HostTagConstant.tag_huidu_cn));
  57 + tmpmap.put("az1ServerHosts", getGatewayHostIPs(HostTagConstant.tag_az1));
  58 + tmpmap.put("az2ServerHosts", getGatewayHostIPs(HostTagConstant.tag_az2));
  59 + List<Map<String, Object>> gatewayHosts=new ArrayList<>();
  60 + gatewayHosts.add(tmpmap);
  61 + map.put("gatewayServerHosts", gatewayHosts);
  62 +
63 if ("test".equals(env)) { 63 if ("test".equals(env)) {
64 List<Map<String, Object>> awsList = localTest("aws"); 64 List<Map<String, Object>> awsList = localTest("aws");
65 List<Map<String, Object>> qcloudList = localTest("qq"); 65 List<Map<String, Object>> qcloudList = localTest("qq");
66 map.put("awsList", awsList); 66 map.put("awsList", awsList);
67 map.put("qcloudList", qcloudList); 67 map.put("qcloudList", qcloudList);
  68 + map.put("qcloudaz2List", Lists.newArrayList());
68 return new BaseResponse(map); 69 return new BaseResponse(map);
69 } 70 }
70 71
71 - List<HostInfo> awsNginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 1);  
72 - if (CollectionUtils.isEmpty(awsNginxHosts)) {  
73 - map.put("awsList", Lists.newArrayList());  
74 - } else {  
75 - List<Map<String, Object>> awsList = CommodUtil.analyseConfigFile(exe(" -eview -n" + awsNginxHosts.get(0).getHostIp()));  
76 - map.put("awsList", awsList); 72 + map.put("awsList", getUpstreamTagNginxList(1,null));
  73 +
  74 + map.put("qcloudList", getUpstreamTagNginxList(2,"az1"));
  75 +
  76 + map.put("qcloudaz2List", getUpstreamTagNginxList(2,"az2"));
  77 + return new BaseResponse(map);
  78 + }catch (Exception e){
  79 + logger.error(" NginxSwitchCtrl - viewNginxConf - init - error" , e);
  80 + return new BaseResponse(500, e.getMessage());
77 } 81 }
78 - List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 2);  
79 - if (CollectionUtils.isEmpty(qcloudNginxHosts)) {  
80 - map.put("qcloudList", Lists.newArrayList()); 82 + }
  83 +
  84 + private List<String> getGatewayHostIPs(String tag){
  85 + List<String> hostIps=new ArrayList<String>();
  86 + List<HostInfo> serverHosts=hostInfoMapper.selectHostInfosByTagList(Arrays.asList(new String[]{HostTagConstant.tag_gateway,tag}));
  87 + if(CollectionUtils.isNotEmpty(serverHosts)){
  88 + hostIps = serverHosts.stream().map(p->p.getHostIp()).collect(Collectors.toList());
  89 + }
  90 + return hostIps;
  91 + }
  92 +
  93 + private List<Map<String, Object>> getUpstreamTagNginxList(int cloud,String azTag){
  94 + List<HostInfo> hosts = getUpstreamTagNginxHostList(cloud, azTag);
  95 + if (CollectionUtils.isEmpty(hosts)) {
  96 + return Lists.newArrayList();
81 } else { 97 } else {
82 - List<Map<String, Object>> qcloudList = CommodUtil.analyseConfigFile(exe(" -eview -n" + qcloudNginxHosts.get(0).getHostIp()));  
83 - map.put("qcloudList", qcloudList); 98 + return CommodUtil.analyseConfigFile(exe(" -eview -n" + hosts.get(0).getHostIp()));
  99 + }
84 } 100 }
85 101
86 - return new BaseResponse(map); 102 + private List<HostInfo> getUpstreamTagNginxHostList(int cloud,String azTag){
  103 + if(1==cloud){
  104 + return hostInfoMapper.selectHostInfosByTagAndCloudType(HostTagConstant.tag_upstreamSwitch, 1);
  105 + }else if(2==cloud){
  106 + List<HostInfo> qcloudNginxHosts= hostInfoMapper.selectHostInfosByTagAndCloudType(HostTagConstant.tag_upstreamSwitch, 2);
  107 + List<HostInfo> qcloudNginxHosts_az1=new ArrayList<>();
  108 + List<HostInfo> qcloudNginxHosts_az2=new ArrayList<>();
  109 + if (CollectionUtils.isNotEmpty(qcloudNginxHosts)) {
  110 + for(HostInfo hostInfo:qcloudNginxHosts){
  111 + if(hostInfo.getTags().contains("az1")){
  112 + qcloudNginxHosts_az1.add(hostInfo);
  113 + }else if(hostInfo.getTags().contains("az2")){
  114 + qcloudNginxHosts_az2.add(hostInfo);
  115 + }
  116 + }
  117 + }
  118 +
  119 + if(azTag.equals("az1")){
  120 + return qcloudNginxHosts_az1;
  121 + }else if(azTag.equals("az2")){
  122 + return qcloudNginxHosts_az2;
  123 + }
  124 + }
  125 + return Lists.newArrayList();
  126 + }
  127 +
  128 + /**
  129 + * 查看当前配置
  130 + *
  131 + * @param cloudName
  132 + * @return
  133 + */
  134 + @RequestMapping(value = "viewCurrentConf")
  135 + @ResponseBody
  136 + public BaseResponse viewCurrentConf(String cloudName) {
  137 + try{
  138 + int cloudType=2;
  139 + String azTag="";
  140 + if("aws".equals(cloudName)){
  141 + cloudType=1;
  142 + }else {
  143 + if(cloudName.contains("az2")){
  144 + azTag="az2";
  145 + }else{
  146 + azTag="az1";
  147 + }
  148 + }
  149 + List<HostInfo> hosts=getUpstreamTagNginxHostList(cloudType,azTag);
  150 + return new BaseResponse(200, "SUCCESS", exe(" -eview -n"+hosts.get(0).getHostIp()));
87 }catch (Exception e){ 151 }catch (Exception e){
88 - logger.error(" NginxSwitchCtrl - viewNginxConf - init - error" , e); 152 + logger.error(" NginxSwitchCtrl - viewCurrentConf - 1 - error" , e);
89 return new BaseResponse(500, e.getMessage()); 153 return new BaseResponse(500, e.getMessage());
90 } 154 }
91 } 155 }
@@ -101,7 +165,7 @@ public class NginxSwitchCtrl { @@ -101,7 +165,7 @@ public class NginxSwitchCtrl {
101 public BaseResponse viewToChangeNginxConf(String cloudName, String target, String ips, String onlineOrGray) { 165 public BaseResponse viewToChangeNginxConf(String cloudName, String target, String ips, String onlineOrGray) {
102 try{ 166 try{
103 //线上切换 167 //线上切换
104 - String tag = StringUtils.equals("gray", target) ? "灰度" : "gateway"; 168 + String tag = StringUtils.equals("gray", target) ? HostTagConstant.tag_huidu_cn : HostTagConstant.tag_gateway;
105 169
106 //需要查询的gateway机子 170 //需要查询的gateway机子
107 int gatewayCloudType = StringUtils.equals("aws", target) ? 1 : 2; 171 int gatewayCloudType = StringUtils.equals("aws", target) ? 1 : 2;
@@ -114,11 +178,11 @@ public class NginxSwitchCtrl { @@ -114,11 +178,11 @@ public class NginxSwitchCtrl {
114 Iterator<HostInfo> infoIterator = gatewayHosts.iterator(); 178 Iterator<HostInfo> infoIterator = gatewayHosts.iterator();
115 while (infoIterator.hasNext()) { 179 while (infoIterator.hasNext()) {
116 HostInfo hostInfo = infoIterator.next(); 180 HostInfo hostInfo = infoIterator.next();
117 - if ("gateway".equals(tag)) {  
118 - if (hostInfo.getTags().indexOf("灰度") != -1) { 181 + if (HostTagConstant.tag_gateway.equals(tag)) {
  182 + if (hostInfo.getTags().indexOf(HostTagConstant.tag_huidu_cn) != -1) {
119 infoIterator.remove(); 183 infoIterator.remove();
120 } 184 }
121 - } else if (hostInfo.getTags().indexOf("gateway") == -1) { 185 + } else if (hostInfo.getTags().indexOf(HostTagConstant.tag_gateway) == -1) {
122 infoIterator.remove(); 186 infoIterator.remove();
123 } 187 }
124 } 188 }
@@ -144,54 +208,6 @@ public class NginxSwitchCtrl { @@ -144,54 +208,6 @@ public class NginxSwitchCtrl {
144 logger.error(" NginxSwitchCtrl - viewToChangeNginxConf - 2 - error" , e); 208 logger.error(" NginxSwitchCtrl - viewToChangeNginxConf - 2 - error" , e);
145 return new BaseResponse(500, e.getMessage()); 209 return new BaseResponse(500, e.getMessage());
146 } 210 }
147 -// //切线上 灰度ips不能为空  
148 -// if(StringUtils.isBlank(ips) || "null".equals(ips)){  
149 -// List<HostInfo> hosts;  
150 -// if ("aws".equals(cloudName)) {  
151 -// hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 1);  
152 -// } else {  
153 -// hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 2);  
154 -// }  
155 -// List<Map<String, Object>> mapList = analyseConfigFile(exe("view", hosts.get(0).getHostIp(), null));  
156 -// JSONArray jsonArray = new JSONArray();  
157 -// for (Map<String, Object> apiMap : mapList) {  
158 -// if (StringUtils.equals("online", onlineOrGray)) {  
159 -// if (StringUtils.equals("grayapigateway", String.valueOf(apiMap.get("name")))) {  
160 -// List<String> serverList = (List<String>) apiMap.get("server");  
161 -// jsonArray.addAll(serverList.stream().map(server -> server.split(":")[0]).collect(Collectors.toList()));  
162 -// }  
163 -// }else {  
164 -// if (StringUtils.equals("apigateway", String.valueOf(apiMap.get("name")))) {  
165 -// List<String> serverList = (List<String>) apiMap.get("server");  
166 -// jsonArray.addAll(serverList.stream().map(server -> server.split(":")[0]).collect(Collectors.toList()));  
167 -// }  
168 -// }  
169 -// }  
170 -// ips = jsonArray.toJSONString();  
171 -//  
172 -// }  
173 -// //模板参数对象  
174 -// Map<String, Object> map = new HashMap<String, Object>();  
175 -// List<String> ipList = Lists.transform(gatewayHosts, new Function<HostInfo, String>() {  
176 -// @Override  
177 -// public String apply(HostInfo input) {  
178 -// return input.getHostIp();  
179 -// }  
180 -// });  
181 -// //切线上 灰度ips不变  
182 -// if (StringUtils.equals("online", onlineOrGray)) {  
183 -// map.put("grayips", JSON.parseArray(ips));  
184 -// map.put("ips", ipList);  
185 -// }  
186 -// //切灰度 线上ips不变  
187 -// else if (StringUtils.equals("gray", onlineOrGray)) {  
188 -// map.put("ips", JSON.parseArray(ips));  
189 -// map.put("grayips", ipList);  
190 -// }  
191 -//  
192 -// String path = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf";  
193 -// logger.info("nginx.conf create path is {}", path);  
194 -// String result = HandlebarsUtils.replace(map, "template/" + cloudName + "-nginx", path);  
195 } 211 }
196 212
197 /** 213 /**
@@ -225,29 +241,6 @@ public class NginxSwitchCtrl { @@ -225,29 +241,6 @@ public class NginxSwitchCtrl {
225 } 241 }
226 } 242 }
227 243
228 - /**  
229 - * 查看当前配置  
230 - *  
231 - * @param cloudName  
232 - * @return  
233 - */  
234 - @RequestMapping(value = "viewCurrentConf")  
235 - @ResponseBody  
236 - public BaseResponse viewCurrentConf(String cloudName) {  
237 - try{  
238 - List<HostInfo> hosts;  
239 - if ("aws".equals(cloudName)) {  
240 - hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 1);  
241 - } else {  
242 - hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 2);  
243 - }  
244 - return new BaseResponse(200, "SUCCESS", exe(" -eview -n"+hosts.get(0).getHostIp()));  
245 - }catch (Exception e){  
246 - logger.error(" NginxSwitchCtrl - viewCurrentConf - 1 - error" , e);  
247 - return new BaseResponse(500, e.getMessage());  
248 - }  
249 - }  
250 -  
251 244
252 /** 245 /**
253 * 执行命令 246 * 执行命令