TopoSwitchCtrl.java
7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.ui.ctrl;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.project.ProjectOnline;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.*;
/**
* 限流调整
*
* @author bblu 2016-10-26
*/
@Controller
@RequestMapping("topoSwitch")
public class TopoSwitchCtrl {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
HttpRestClient httpRestClient;
@Autowired
TopoSwitchUtil topoSwitchUtil;
@RequestMapping(value = "toTopoSwitch")
public ModelAndView toTopoSwitch() {
ModelAndView mdv = new ModelAndView("switch/switch_topology");
return mdv;
}
@RequestMapping(value = "toAppTopoSwitch")
public ModelAndView toAppTopoSwitch() {
ModelAndView mdv = new ModelAndView("switch/switch_topology_app");
return mdv;
}
/**
* 展示当前配置
*
* @return ModelAndView
*/
@RequestMapping(value = "getNgixnStatus")
@ResponseBody
public BaseResponse getNgixnStatus() {
Map<String, Object> resultMap = new HashMap<>();
try{
//nginx
BaseResponse<Map<String, List<Map<String, Object>>>> response = httpRestClient.exchangeForget(HttpUriContants.VIEW_NGINX_CONF, new ParameterizedTypeReference<BaseResponse<Map<String, List<Map<String, Object>>>>>() {
}, null);
if(response==null||response.getCode()!=200){
return new BaseResponse(201,response==null?"获取后台数据为空":response.getMessage());
}
Map<String, List<Map<String, Object>>> map = response.getData();
//List<Map<String, Object>> qcloudList = map.get("qcloudList");
//List<Map<String, Object>> qcloudaz2List = map.get("qcloudaz2List");
List<String> az1HostIps=(List<String>)(map.get("gatewayServerHosts").get(0).get("az1ServerHosts"));
List<String> az2HostIps=(List<String>)(map.get("gatewayServerHosts").get(0).get("az2ServerHosts"));
List<String> az3HostIps=(List<String>)(map.get("gatewayServerHosts").get(0).get("az3ServerHosts"));
List<String> grayHostIps=(List<String>)(map.get("gatewayServerHosts").get(0).get("grayServerHosts"));
List<String> az1NginxIps=(List<String>)(map.get("nginxServerHosts").get(0).get("az1NginxHosts"));
List<String> az2NginxIps=(List<String>)(map.get("nginxServerHosts").get(0).get("az2NginxHosts"));
List<String> az3NginxIps=(List<String>)(map.get("nginxServerHosts").get(0).get("az3NginxHosts"));
//lb信息
Map<String, Object> lbMap=map.get("lbMap").get(0);
Map<String,List<String>> forward_nginx_Map=new HashMap<>();
for(String key:lbMap.keySet()){
String[] keys=key.split(",");
resultMap.put("elb_"+keys[0]+"_"+keys[1], keys[2]);//指定elb的ip
//计算elb指向的nginx
Map<String,Integer> ipInWeightMap=(HashMap<String,Integer>)lbMap.get(key);
if(ipInWeightMap!=null&&ipInWeightMap.size()>0){
String forward_nginx=keys[0]+"_"+keys[1];
List<String> ipInWeight=Lists.newArrayList(ipInWeightMap.keySet());
if(forward_nginx_Map.get(forward_nginx)==null){
forward_nginx_Map.put(forward_nginx,new ArrayList<String>());
}
if(CollectionUtils.retainAll(az1NginxIps,ipInWeight).size()>0){
forward_nginx_Map.get(forward_nginx).add("az1"+":"+ipInWeightMap.get(az1NginxIps.get(0)));
}
if(CollectionUtils.retainAll(az2NginxIps,ipInWeight).size()>0){
forward_nginx_Map.get(forward_nginx).add("az2"+":"+ipInWeightMap.get(az2NginxIps.get(0)));
}
if(CollectionUtils.retainAll(az3NginxIps,ipInWeight).size()>0){
forward_nginx_Map.get(forward_nginx).add("az3"+":"+ipInWeightMap.get(az3NginxIps.get(0)));
}
}
}
Map<String,String> forward_nginx_str_Map=new HashMap<>();
for(String key:forward_nginx_Map.keySet()){
forward_nginx_str_Map.put(key, StringUtils.join(forward_nginx_Map.get(key),",") );
}
resultMap.put("lb_forward_nginx",forward_nginx_str_Map);
resultMap.put("lbMap", lbMap);
//List<String> ls=topoSwitchUtil.getNginxLocationType(az1HostIps,az2HostIps,grayHostIps,qcloudList);
//String qqApiNginx = ls.get(0);
//String qqGrayNginx = ls.get(1);
//ls=topoSwitchUtil.getNginxLocationType(az1HostIps,az2HostIps,grayHostIps,qcloudaz2List);
//String az2ApiNginx = ls.get(0);
//String az2GrayNginx = ls.get(1);
/*resultMap.put("qqApiNginx", qqApiNginx);
resultMap.put("qqGrayNginx", qqGrayNginx);
resultMap.put("az2ApiNginx", az2ApiNginx);
resultMap.put("az2GrayNginx", az2GrayNginx);*/
//resultMap.put("defaultDns", httpRestClient.defaultGet(HttpUriContants.CENTERSWITCH_DEFAULT_DNS,String.class));
//resultMap.put("luaType", topoSwitchUtil.getLuaType());
//
resultMap.put("az1NginxIps", az1NginxIps);
resultMap.put("az2NginxIps", az2NginxIps);
resultMap.put("az3NginxIps", az3NginxIps);
resultMap.put("az1GatewayIps", az1HostIps);
resultMap.put("az2GatewayIps", az2HostIps);
resultMap.put("az3GatewayIps", az3HostIps);
resultMap.put("grayGatewayIps", grayHostIps);
}catch (Exception e){
logger.error(" TopoSwitchCtrl - getNgixnStatus - err", e);
return new BaseResponse(201,e.getMessage());
}
return new BaseResponse(resultMap);
}
/**
* 切换lb权重,nginx指向变化
* @return
*/
@RequestMapping(value = "switchLbWeight", method = RequestMethod.POST)
@ResponseBody
public BaseResponse switchLbWeight(String cloudName,String target) {
if(StringUtils.isBlank(cloudName)||StringUtils.isBlank(target)){
return new BaseResponse(201,"switchLbWeight传入参数为空");
}
Map<String,String> map=new HashMap<>();
map.put("cloudName",cloudName);
map.put("target",target);
return httpRestClient.defaultGet(HttpUriContants.NGINX_SWTICH_LB_WEIGHT,BaseResponse.class,map);
}
/**
* 切换lb权重结果查询
* @return
*/
@RequestMapping(value = "getLbSwitchResult")
@ResponseBody
public BaseResponse getLbSwitchResult(String rtnID) {
Map<String,String> map=new HashMap<>();
map.put("rtnID",rtnID);
return httpRestClient.defaultGet(HttpUriContants.NGINX_SWTICH_LB_RESULT,BaseResponse.class,map);
}
}