TopoSwitchCtrl.java 7.43 KB
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);
    }
}