Authored by qinchao

nginx切换

... ... @@ -191,8 +191,8 @@ public class HostInfoCtrl {
List<Host> qcloudHosts = qcloudFetcher.getAllInstancesSet();
message += hostInfoService.compareHostInfo(qcloudHosts,2);
List<Host> awsHosts = awsFetcher.getAllEc2Details();
message += hostInfoService.compareHostInfo(awsHosts, 1);
/*List<Host> awsHosts = awsFetcher.getAllEc2Details();
message += hostInfoService.compareHostInfo(awsHosts, 1);*/
if(StringUtils.isNotBlank(message)){
return new BaseResponse(message);
}
... ...
... ... @@ -714,15 +714,20 @@ public class QcloudSdkUtil {
}
public static void main(String args[]){
/*Calendar calendar = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, -60);
String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
List<Double> dd=getMonitorData("outtraffic",startTime);
System.out.println(dd);
System.out.println("");
List<Double> dd2=getMonitorData("intraffic",startTime);
System.out.println(dd2);*/
System.out.println(dd2);
//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"};
/*List<Host> ls= getAllInstancesSet();
System.out.println(ls.size());
System.out.println(ls);*/
}
}
\ No newline at end of file
... ...
package com.monitor.common;
public class HostTagConstant {
public static final String tag_upstreamSwitch="upstream-switch";
public static final String tag_gateway="gateway";
public static final String tag_az1="az1";
public static final String tag_az2="az2";
public static final String tag_huidu_cn="灰度";
}
... ...
... ... @@ -2,6 +2,7 @@ package com.monitor.switchs;
import com.google.common.collect.Lists;
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;
... ... @@ -22,6 +23,7 @@ import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* @author zhengyouwei 2016年5月12日 下午1:49:55
... ... @@ -49,43 +51,105 @@ public class NginxSwitchCtrl {
try{
Map<String, List<Map<String, Object>>> map = new HashMap<>();
List<HostInfo> grayServerHosts=hostInfoMapper.selectHostInfosByTagList(Arrays.asList(new String[]{"灰度","gateway"}));
List<String> grayHostIps=new ArrayList<String>();
for(HostInfo host:grayServerHosts){
grayHostIps.add(host.getHostIp());
}
//获取灰度机器的server。ip
List<Map<String, Object>> grayHosts=new ArrayList<>();
Map<String, Object> tmpmap=new HashMap<String, Object>();
tmpmap.put("grayServerHosts", grayHostIps);
grayHosts.add(tmpmap);
map.put("grayServerHosts", grayHosts);
tmpmap.put("grayServerHosts", getGatewayHostIPs(HostTagConstant.tag_huidu_cn));
tmpmap.put("az1ServerHosts", getGatewayHostIPs(HostTagConstant.tag_az1));
tmpmap.put("az2ServerHosts", getGatewayHostIPs(HostTagConstant.tag_az2));
List<Map<String, Object>> gatewayHosts=new ArrayList<>();
gatewayHosts.add(tmpmap);
map.put("gatewayServerHosts", gatewayHosts);
if ("test".equals(env)) {
List<Map<String, Object>> awsList = localTest("aws");
List<Map<String, Object>> qcloudList = localTest("qq");
map.put("awsList", awsList);
map.put("qcloudList", qcloudList);
map.put("qcloudaz2List", Lists.newArrayList());
return new BaseResponse(map);
}
List<HostInfo> awsNginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 1);
if (CollectionUtils.isEmpty(awsNginxHosts)) {
map.put("awsList", Lists.newArrayList());
} else {
List<Map<String, Object>> awsList = CommodUtil.analyseConfigFile(exe(" -eview -n" + awsNginxHosts.get(0).getHostIp()));
map.put("awsList", awsList);
map.put("awsList", getUpstreamTagNginxList(1,null));
map.put("qcloudList", getUpstreamTagNginxList(2,"az1"));
map.put("qcloudaz2List", getUpstreamTagNginxList(2,"az2"));
return new BaseResponse(map);
}catch (Exception e){
logger.error(" NginxSwitchCtrl - viewNginxConf - init - error" , e);
return new BaseResponse(500, e.getMessage());
}
List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 2);
if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
map.put("qcloudList", Lists.newArrayList());
}
private List<String> getGatewayHostIPs(String tag){
List<String> hostIps=new ArrayList<String>();
List<HostInfo> serverHosts=hostInfoMapper.selectHostInfosByTagList(Arrays.asList(new String[]{HostTagConstant.tag_gateway,tag}));
if(CollectionUtils.isNotEmpty(serverHosts)){
hostIps = serverHosts.stream().map(p->p.getHostIp()).collect(Collectors.toList());
}
return hostIps;
}
private List<Map<String, Object>> getUpstreamTagNginxList(int cloud,String azTag){
List<HostInfo> hosts = getUpstreamTagNginxHostList(cloud, azTag);
if (CollectionUtils.isEmpty(hosts)) {
return Lists.newArrayList();
} else {
List<Map<String, Object>> qcloudList = CommodUtil.analyseConfigFile(exe(" -eview -n" + qcloudNginxHosts.get(0).getHostIp()));
map.put("qcloudList", qcloudList);
return CommodUtil.analyseConfigFile(exe(" -eview -n" + hosts.get(0).getHostIp()));
}
}
return new BaseResponse(map);
private List<HostInfo> getUpstreamTagNginxHostList(int cloud,String azTag){
if(1==cloud){
return hostInfoMapper.selectHostInfosByTagAndCloudType(HostTagConstant.tag_upstreamSwitch, 1);
}else if(2==cloud){
List<HostInfo> qcloudNginxHosts= hostInfoMapper.selectHostInfosByTagAndCloudType(HostTagConstant.tag_upstreamSwitch, 2);
List<HostInfo> qcloudNginxHosts_az1=new ArrayList<>();
List<HostInfo> qcloudNginxHosts_az2=new ArrayList<>();
if (CollectionUtils.isNotEmpty(qcloudNginxHosts)) {
for(HostInfo hostInfo:qcloudNginxHosts){
if(hostInfo.getTags().contains("az1")){
qcloudNginxHosts_az1.add(hostInfo);
}else if(hostInfo.getTags().contains("az2")){
qcloudNginxHosts_az2.add(hostInfo);
}
}
}
if(azTag.equals("az1")){
return qcloudNginxHosts_az1;
}else if(azTag.equals("az2")){
return qcloudNginxHosts_az2;
}
}
return Lists.newArrayList();
}
/**
* 查看当前配置
*
* @param cloudName
* @return
*/
@RequestMapping(value = "viewCurrentConf")
@ResponseBody
public BaseResponse viewCurrentConf(String cloudName) {
try{
int cloudType=2;
String azTag="";
if("aws".equals(cloudName)){
cloudType=1;
}else {
if(cloudName.contains("az2")){
azTag="az2";
}else{
azTag="az1";
}
}
List<HostInfo> hosts=getUpstreamTagNginxHostList(cloudType,azTag);
return new BaseResponse(200, "SUCCESS", exe(" -eview -n"+hosts.get(0).getHostIp()));
}catch (Exception e){
logger.error(" NginxSwitchCtrl - viewNginxConf - init - error" , e);
logger.error(" NginxSwitchCtrl - viewCurrentConf - 1 - error" , e);
return new BaseResponse(500, e.getMessage());
}
}
... ... @@ -101,7 +165,7 @@ public class NginxSwitchCtrl {
public BaseResponse viewToChangeNginxConf(String cloudName, String target, String ips, String onlineOrGray) {
try{
//线上切换
String tag = StringUtils.equals("gray", target) ? "灰度" : "gateway";
String tag = StringUtils.equals("gray", target) ? HostTagConstant.tag_huidu_cn : HostTagConstant.tag_gateway;
//需要查询的gateway机子
int gatewayCloudType = StringUtils.equals("aws", target) ? 1 : 2;
... ... @@ -114,11 +178,11 @@ public class NginxSwitchCtrl {
Iterator<HostInfo> infoIterator = gatewayHosts.iterator();
while (infoIterator.hasNext()) {
HostInfo hostInfo = infoIterator.next();
if ("gateway".equals(tag)) {
if (hostInfo.getTags().indexOf("灰度") != -1) {
if (HostTagConstant.tag_gateway.equals(tag)) {
if (hostInfo.getTags().indexOf(HostTagConstant.tag_huidu_cn) != -1) {
infoIterator.remove();
}
} else if (hostInfo.getTags().indexOf("gateway") == -1) {
} else if (hostInfo.getTags().indexOf(HostTagConstant.tag_gateway) == -1) {
infoIterator.remove();
}
}
... ... @@ -144,54 +208,6 @@ public class NginxSwitchCtrl {
logger.error(" NginxSwitchCtrl - viewToChangeNginxConf - 2 - error" , e);
return new BaseResponse(500, e.getMessage());
}
// //切线上 灰度ips不能为空
// if(StringUtils.isBlank(ips) || "null".equals(ips)){
// List<HostInfo> hosts;
// if ("aws".equals(cloudName)) {
// hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 1);
// } else {
// hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 2);
// }
// List<Map<String, Object>> mapList = analyseConfigFile(exe("view", hosts.get(0).getHostIp(), null));
// JSONArray jsonArray = new JSONArray();
// for (Map<String, Object> apiMap : mapList) {
// if (StringUtils.equals("online", onlineOrGray)) {
// if (StringUtils.equals("grayapigateway", String.valueOf(apiMap.get("name")))) {
// List<String> serverList = (List<String>) apiMap.get("server");
// jsonArray.addAll(serverList.stream().map(server -> server.split(":")[0]).collect(Collectors.toList()));
// }
// }else {
// if (StringUtils.equals("apigateway", String.valueOf(apiMap.get("name")))) {
// List<String> serverList = (List<String>) apiMap.get("server");
// jsonArray.addAll(serverList.stream().map(server -> server.split(":")[0]).collect(Collectors.toList()));
// }
// }
// }
// ips = jsonArray.toJSONString();
//
// }
// //模板参数对象
// Map<String, Object> map = new HashMap<String, Object>();
// List<String> ipList = Lists.transform(gatewayHosts, new Function<HostInfo, String>() {
// @Override
// public String apply(HostInfo input) {
// return input.getHostIp();
// }
// });
// //切线上 灰度ips不变
// if (StringUtils.equals("online", onlineOrGray)) {
// map.put("grayips", JSON.parseArray(ips));
// map.put("ips", ipList);
// }
// //切灰度 线上ips不变
// else if (StringUtils.equals("gray", onlineOrGray)) {
// map.put("ips", JSON.parseArray(ips));
// map.put("grayips", ipList);
// }
//
// String path = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf";
// logger.info("nginx.conf create path is {}", path);
// String result = HandlebarsUtils.replace(map, "template/" + cloudName + "-nginx", path);
}
/**
... ... @@ -225,29 +241,6 @@ public class NginxSwitchCtrl {
}
}
/**
* 查看当前配置
*
* @param cloudName
* @return
*/
@RequestMapping(value = "viewCurrentConf")
@ResponseBody
public BaseResponse viewCurrentConf(String cloudName) {
try{
List<HostInfo> hosts;
if ("aws".equals(cloudName)) {
hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 1);
} else {
hosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 2);
}
return new BaseResponse(200, "SUCCESS", exe(" -eview -n"+hosts.get(0).getHostIp()));
}catch (Exception e){
logger.error(" NginxSwitchCtrl - viewCurrentConf - 1 - error" , e);
return new BaseResponse(500, e.getMessage());
}
}
/**
* 执行命令
... ...