Authored by zhouxiang

直连 限流

@@ -9,7 +9,7 @@ local redis1=redis_util:new(redis_config1) @@ -9,7 +9,7 @@ local redis1=redis_util:new(redis_config1)
9 lua_context={} 9 lua_context={}
10 lua_context["redises"]={redis1} 10 lua_context["redises"]={redis1}
11 11
12 -open_limit_flow=false 12 +open_limit_flow={@{open_limit_flow}@}
13 api_default_max_per_sencond=1000 13 api_default_max_per_sencond=1000
14 service_default_max_per_sencond=1000 14 service_default_max_per_sencond=1000
15 default_err_code=9999991 15 default_err_code=9999991
@@ -6,7 +6,7 @@ local icutil = require "iconfig_util" @@ -6,7 +6,7 @@ local icutil = require "iconfig_util"
6 6
7 --5b2d8b4b242e067616c6cd98851b2306 close 7 --5b2d8b4b242e067616c6cd98851b2306 close
8 --2a90dfa0f37b92aaebf369e9a4d38ba4 open 8 --2a90dfa0f37b92aaebf369e9a4d38ba4 open
9 -local open_dl = {@{open_dl}@} 9 +local open_dl = "{@{open_dl}@}"
10 local close_dl = "5b2d8b4b242e067616c6cd98851b2306" 10 local close_dl = "5b2d8b4b242e067616c6cd98851b2306"
11 11
12 12
@@ -39,6 +39,16 @@ public class CommodUtil { @@ -39,6 +39,16 @@ public class CommodUtil {
39 */ 39 */
40 public static final String LUA_SWITCH_SH = "switch_lua.sh"; 40 public static final String LUA_SWITCH_SH = "switch_lua.sh";
41 41
  42 + /**
  43 + * 直连lua
  44 + */
  45 + public static final String LB_SWITCH_SH = "switch_lb.sh";
  46 +
  47 + /**
  48 + * 限流
  49 + */
  50 + public static final String LIMIT_SWITCH_SH = "nginx_limit.sh";
  51 +
42 public static String exe(String commond, String param) { 52 public static String exe(String commond, String param) {
43 logger.info("start to exe commond:{},param:{}",commond,param); 53 logger.info("start to exe commond:{},param:{}",commond,param);
44 Process process = null; 54 Process process = null;
@@ -41,13 +41,13 @@ public class LbSwitchCtrl { @@ -41,13 +41,13 @@ public class LbSwitchCtrl {
41 */ 41 */
42 @RequestMapping(value = "viewConf") 42 @RequestMapping(value = "viewConf")
43 @ResponseBody 43 @ResponseBody
44 - public Map<String,String> viewConf() throws Exception { 44 + public BaseResponse viewConf() throws Exception {
45 //直连的配置放在腾讯云nginx上 45 //直连的配置放在腾讯云nginx上
46 List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("直连"); 46 List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("直连");
47 if (CollectionUtils.isEmpty(qcloudNginxHosts)) { 47 if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
48 throw new Exception("没有找到直连nginx服务器"); 48 throw new Exception("没有找到直连nginx服务器");
49 } 49 }
50 - return analyseConfigFile(exe("view", qcloudNginxHosts.get(0).getHostIp(),null)); 50 + return new BaseResponse(200,"success",analyseConfigFile(exe("view", qcloudNginxHosts.get(0).getHostIp(),null)));
51 } 51 }
52 52
53 /** 53 /**
@@ -101,7 +101,7 @@ public class LbSwitchCtrl { @@ -101,7 +101,7 @@ public class LbSwitchCtrl {
101 public String exe(String commod, String host, String templatePath) { 101 public String exe(String commod, String host, String templatePath) {
102 StringBuilder commond = new StringBuilder(); 102 StringBuilder commond = new StringBuilder();
103 commond.append(" " + commod).append(" " + host).append(" " + templatePath); 103 commond.append(" " + commod).append(" " + host).append(" " + templatePath);
104 - return CommodUtil.exe(CommodUtil.LUA_SWITCH_SH, commond.toString()); 104 + return CommodUtil.exe(CommodUtil.LB_SWITCH_SH, commond.toString());
105 } 105 }
106 106
107 private Map<String,String> analyseConfigFile(String rlt) { 107 private Map<String,String> analyseConfigFile(String rlt) {
@@ -13,6 +13,7 @@ import org.slf4j.Logger; @@ -13,6 +13,7 @@ import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory; 13 import org.slf4j.LoggerFactory;
14 import org.springframework.beans.factory.annotation.Autowired; 14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Controller; 15 import org.springframework.stereotype.Controller;
  16 +import org.springframework.web.bind.annotation.RequestBody;
16 import org.springframework.web.bind.annotation.RequestMapping; 17 import org.springframework.web.bind.annotation.RequestMapping;
17 import org.springframework.web.bind.annotation.ResponseBody; 18 import org.springframework.web.bind.annotation.ResponseBody;
18 19
@@ -42,12 +43,12 @@ public class NginxLimit { @@ -42,12 +43,12 @@ public class NginxLimit {
42 */ 43 */
43 @RequestMapping(value = "viewConf") 44 @RequestMapping(value = "viewConf")
44 @ResponseBody 45 @ResponseBody
45 - public Map<String,Object> viewConf() throws Exception { 46 + public BaseResponse viewConf() throws Exception {
46 List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("限流"); 47 List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("限流");
47 if (CollectionUtils.isEmpty(qcloudNginxHosts)) { 48 if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
48 throw new Exception("没有找到限流nginx服务器"); 49 throw new Exception("没有找到限流nginx服务器");
49 } 50 }
50 - return analyseConfigFile(exe("view", qcloudNginxHosts.get(0).getHostIp(),null)); 51 + return new BaseResponse(200,"success",analyseConfigFile(exe("view", qcloudNginxHosts.get(0).getHostIp(),null)));
51 } 52 }
52 53
53 /** 54 /**
@@ -57,11 +58,11 @@ public class NginxLimit { @@ -57,11 +58,11 @@ public class NginxLimit {
57 */ 58 */
58 @RequestMapping(value = "viewToChangeLimitConf") 59 @RequestMapping(value = "viewToChangeLimitConf")
59 @ResponseBody 60 @ResponseBody
60 - public BaseResponse viewToChangeLuaConf(String limitFlow, String apiConfs, String serviceConfs, HttpServletRequest request) {  
61 - logger.info("limitFlow {} apiConfig {} serviceCong {}", limitFlow,apiConfs,serviceConfs); 61 + public BaseResponse viewToChangeLuaConf(@RequestBody Map<String,String> params, HttpServletRequest request) {
  62 + logger.info("params {}", params);
62 Map<String, Object> map = new HashMap(); 63 Map<String, Object> map = new HashMap();
63 - map.put("open_limit_flow", limitFlow);  
64 - List apiConfigList = JSON.parseArray(apiConfs,String.class); 64 + map.put("open_limit_flow", params.get("limitFlow"));
  65 + List apiConfigList = JSON.parseArray(params.get("apiConfs"),String.class);
65 int apiConfigSize = apiConfigList.size(); 66 int apiConfigSize = apiConfigList.size();
66 for(int i = 0;i< apiConfigSize;i++){ 67 for(int i = 0;i< apiConfigSize;i++){
67 if(i == (apiConfigSize -1)){ 68 if(i == (apiConfigSize -1)){
@@ -70,7 +71,7 @@ public class NginxLimit { @@ -70,7 +71,7 @@ public class NginxLimit {
70 apiConfigList.set(i,apiConfigList.get(i)+","); 71 apiConfigList.set(i,apiConfigList.get(i)+",");
71 } 72 }
72 map.put("limit_config", apiConfigList); 73 map.put("limit_config", apiConfigList);
73 - List serviceConfList = JSON.parseArray(serviceConfs,String.class); 74 + List serviceConfList = JSON.parseArray(params.get("serviceConfs"),String.class);
74 int serviceConfSize = serviceConfList.size(); 75 int serviceConfSize = serviceConfList.size();
75 for(int i = 0;i< serviceConfSize;i++){ 76 for(int i = 0;i< serviceConfSize;i++){
76 if(i == (serviceConfSize -1)){ 77 if(i == (serviceConfSize -1)){
@@ -117,7 +118,7 @@ public class NginxLimit { @@ -117,7 +118,7 @@ public class NginxLimit {
117 public String exe(String commod, String host, String templatePath) { 118 public String exe(String commod, String host, String templatePath) {
118 StringBuilder commond = new StringBuilder(); 119 StringBuilder commond = new StringBuilder();
119 commond.append(" " + commod).append(" " + host).append(" " + templatePath); 120 commond.append(" " + commod).append(" " + host).append(" " + templatePath);
120 - return CommodUtil.exe(CommodUtil.NGINX_SWITCH_SH, commond.toString()); 121 + return CommodUtil.exe(CommodUtil.LIMIT_SWITCH_SH, commond.toString());
121 } 122 }
122 123
123 private Map<String,Object> analyseConfigFile(String str) throws IOException { 124 private Map<String,Object> analyseConfigFile(String str) throws IOException {
@@ -128,7 +129,7 @@ public class NginxLimit { @@ -128,7 +129,7 @@ public class NginxLimit {
128 break; 129 break;
129 } 130 }
130 if(strs[i].contains("open_limit_flow")){ 131 if(strs[i].contains("open_limit_flow")){
131 - map.put("limit_flow",strs[i].trim()); 132 + map.put("open_limit_flow",strs[i].trim().split("=")[1]);
132 continue; 133 continue;
133 } 134 }
134 if(strs[i].contains("limit_config") || strs[i].contains("limit_service_config")){ 135 if(strs[i].contains("limit_config") || strs[i].contains("limit_service_config")){
@@ -139,7 +140,7 @@ public class NginxLimit { @@ -139,7 +140,7 @@ public class NginxLimit {
139 if(!strs[i].trim().startsWith("[") && strs[i].trim().endsWith("}")){ 140 if(!strs[i].trim().startsWith("[") && strs[i].trim().endsWith("}")){
140 break; 141 break;
141 } 142 }
142 - if(strs[i].trim().startsWith("--")){ 143 + if(StringUtils.isEmpty(strs[i].trim()) || strs[i].trim().startsWith("--")){
143 continue; 144 continue;
144 } 145 }
145 limitConf.add(strs[i].trim().replace(",","")); 146 limitConf.add(strs[i].trim().replace(",",""));