Authored by zhouxiang

Merge branch 'switch'

@@ -124,6 +124,11 @@ @@ -124,6 +124,11 @@
124 <groupId>javax.activation</groupId> 124 <groupId>javax.activation</groupId>
125 <artifactId>activation</artifactId> 125 <artifactId>activation</artifactId>
126 </dependency> 126 </dependency>
  127 + <dependency>
  128 + <groupId>com.github.jknack</groupId>
  129 + <artifactId>handlebars</artifactId>
  130 + <version>2.0.0</version>
  131 + </dependency>
127 </dependencies> 132 </dependencies>
128 133
129 </project> 134 </project>
  1 +package com.monitor.common.util;
  2 +
  3 +import com.github.jknack.handlebars.Handlebars;
  4 +import com.github.jknack.handlebars.Template;
  5 +
  6 +import java.io.FileWriter;
  7 +import java.io.IOException;
  8 +import java.io.Writer;
  9 +import java.util.Map;
  10 +
  11 +/**
  12 + * Created by zhouxiang on 2016/10/20.
  13 + */
  14 +public class HandlebarsUtils {
  15 + private static Handlebars handlebars = new Handlebars();
  16 + static {
  17 + handlebars.setStartDelimiter("{@{");
  18 + handlebars.setEndDelimiter("}@}");
  19 + }
  20 +
  21 + public static String replace(Map<String, Object> obj,String tempfilePath,String createFileName) {
  22 + String result = "";
  23 + try {
  24 + Template template = handlebars.compile(tempfilePath);
  25 + Writer writer = new FileWriter(createFileName);
  26 + template.apply(obj,writer);
  27 + writer.flush();
  28 + writer.close();
  29 + return template.apply(obj);
  30 + } catch (IOException e) {
  31 + e.printStackTrace();
  32 + }
  33 + return result;
  34 + }
  35 +
  36 + public static void main(String[] args) {
  37 +
  38 + }
  39 +}
  1 +user www www;
  2 +
  3 +worker_processes 8;
  4 +worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
  5 +
  6 +pid logs/nginx.pid;
  7 +# [ debug | info | notice | warn | error | crit ]
  8 +#error_log /Data/logs/nginx/nginx_error.log info;
  9 +error_log /Data/logs/nginx/nginx_error.log info;
  10 +#Specifies the value for maximum file descriptors that can be opened by this process.
  11 +worker_rlimit_nofile 51200;
  12 +
  13 +events
  14 +{
  15 + use epoll;
  16 +
  17 + #maxclient = worker_processes * worker_connections / cpu_number
  18 + worker_connections 51200;
  19 +}
  20 +
  21 +http
  22 +{
  23 + include mime.types;
  24 + default_type application/octet-stream;
  25 + #charset gb2312,utf-8;
  26 + charset utf-8;
  27 +
  28 + log_format fenxi '$remote_addr|$http_x_forwarded_for|[$time_local]|$http_host|$request|'
  29 + '$status|$body_bytes_sent|$request_time|$upstream_response_time|$upstream_cache_status|$http_referer|'
  30 + '$http_user_agent|$upstream_addr';
  31 +
  32 +
  33 + #General Options
  34 + server_names_hash_bucket_size 128;
  35 + client_header_buffer_size 32k;
  36 + large_client_header_buffers 4 32k;
  37 + client_body_buffer_size 8m; #256k
  38 + #
  39 + #server_tokens off;
  40 + ignore_invalid_headers on;
  41 + recursive_error_pages on;
  42 + server_name_in_redirect off;
  43 +
  44 + sendfile on;
  45 +
  46 + #timeouts
  47 + keepalive_timeout 60;
  48 + #test
  49 + #client_body_timeout 3m;
  50 + #client_header_timeout 3m;
  51 + #send_timeout 3m;
  52 +
  53 +
  54 + #TCP Options
  55 + tcp_nopush on;
  56 + tcp_nodelay on;
  57 +
  58 + #fastcgi options
  59 + fastcgi_connect_timeout 300;
  60 + fastcgi_send_timeout 300;
  61 + fastcgi_read_timeout 300;
  62 + fastcgi_buffer_size 64k;
  63 + fastcgi_buffers 4 64k;
  64 + fastcgi_busy_buffers_size 128k;
  65 + fastcgi_temp_file_write_size 128k;
  66 +
  67 + #set_real_ip_from 10.159.191.0/24;
  68 + #real_ip_header X-Forwarded-For;
  69 +
  70 + #hiden php version
  71 + fastcgi_hide_header X-Powered-By;
  72 +
  73 + #size limits
  74 + client_max_body_size 50m;
  75 +
  76 + gzip on;
  77 + gzip_min_length 1k;
  78 + gzip_buffers 4 16k;
  79 + #gzip_http_version 1.0;
  80 + gzip_comp_level 2;
  81 + gzip_types text/plain application/x-javascript text/css application/xml application/json;
  82 + gzip_vary on;
  83 +
  84 +
  85 +
  86 + fastcgi_temp_path /dev/shm/fastcgi_temp;
  87 + client_body_temp_path /dev/shm/client_body_temp;
  88 +
  89 + # where the lua package exists
  90 + lua_package_path "/Data/local/openresty-1.9.15.1/nginx/conf/lua/?.lua;;";
  91 + init_by_lua_file "conf/lua/init_lua.lua";
  92 +
  93 +
  94 + #upstream web
  95 + upstream oldservice {
  96 + server service.api.yohobuy.com ;
  97 + }
  98 +
  99 + upstream oldapi {
  100 + server api.open.yohobuy.com ;
  101 + }
  102 +
  103 + #brower service
  104 + upstream brower {
  105 + server 172.31.30.165:8092;
  106 + server 172.31.57.222:8092;
  107 + server 172.31.57.221:8092;
  108 + }
  109 +
  110 + upstream social {
  111 + server 172.31.30.165:8095;
  112 + server 172.31.30.165:8095;
  113 + }
  114 +
  115 +
  116 + upstream apigateway {
  117 + {@{#each ips}@}server {@{this}@}:8080 max_fails=5 fail_timeout=3s;
  118 + {@{/each}@}
  119 + keepalive 32;
  120 + }
  121 +
  122 + upstream grayapigateway {
  123 + {@{#each grayips}@}server {@{this}@}:8080 max_fails=5 fail_timeout=3s;
  124 + {@{/each}@}
  125 + keepalive 32;
  126 + }
  127 +
  128 +
  129 + upstream pay.yohobuy.com {
  130 + server 172.31.55.207 max_fails=5 fail_timeout=3s weight=10;
  131 + server 172.31.56.135 max_fails=5 fail_timeout=3s weight=50;
  132 + }
  133 +
  134 + upstream www.yohobuy.com {
  135 + server www.yohobuy.com;
  136 + }
  137 +
  138 + upstream union {
  139 + server 172.31.16.99:8088;
  140 + }
  141 +
  142 +
  143 + upstream activityApi{
  144 + server 172.31.16.98:8090;
  145 + }
  146 +
  147 + upstream apiWechat {
  148 + server 172.31.31.13:8094 ;
  149 + }
  150 +
  151 +
  152 +
  153 +
  154 + #upstream
  155 + fastcgi_next_upstream error timeout invalid_header http_500;
  156 +
  157 + #limit_zone limit $binary_remote_addr 1m;
  158 +
  159 + #fastcgi cache
  160 + #fastcgi_cache_path /nginxcache levels=1:2 keys_zone=two:10m inactive=1d max_size=3000m;
  161 + #for example just for study! have fun!
  162 +
  163 +
  164 + include vhosts/api.yoho.cn.conf;
  165 + include vhosts/mapi.yohobuy.com.conf;
  166 + include vhosts/pay.yohobuy.com.conf;
  167 + include vhosts/service.yoho.cn.conf;
  168 + include vhosts/api_single.yoho.cn.conf;
  169 + include vhosts/unknown.conf;
  170 +
  171 + }
  1 +local resolver=require "resty.dns.resolver"
  2 +local cjson=require "cjson"
  3 +local aes=require "resty.aes"
  4 +
  5 +ngx.lookup_context={}
  6 +local context=ngx.lookup_context
  7 +context.dns_server={"114.114.114.114"}
  8 +
  9 +-- 1:aws ,2:qq ,3:aws+qq
  10 +context.cloud_flag={@{flag}@}
  11 +
  12 +--add 20160823 notLogin=aws/tencent
  13 +context.strategy_param={["model"]=1024,["tencentMax"]=128,["notLogin"]="aws"}
  14 +
  15 +
  16 +context.tencent_ips={
  17 + ["api.yoho.cn"]={{ip="123.206.1.98",ttl=60},{ip="123.206.2.80",ttl=60}},
  18 + ["service.yoho.cn"]={{ip="123.206.1.98",ttl=60},{ip="123.206.2.80",ttl=60}}
  19 + -- ["m.yohobuy.com"]={{ip="123.206.1.105",ttl=60}}
  20 +}
  21 +
  22 +context.tencent_ips_encrypt={
  23 +}
  24 +
  25 +-- aws domain slb domain
  26 +context.aws_domains={
  27 + ["api.yoho.cn"]="app-java-168863769.cn-north-1.elb.amazonaws.com.cn",
  28 + ["service.yoho.cn"]="service-yoho-579825100.cn-north-1.elb.amazonaws.com.cn"
  29 + -- ["m.yohobuy.com"]="pch5-1209971887.cn-north-1.elb.amazonaws.com.cn"
  30 +}
  31 +
  32 +--add 20160823 use for strategy
  33 +context.aws_ips={}
  34 +
  35 +context.aws_ips_encrypt={
  36 +}
  37 +
  38 +context.init_flag={
  39 +}
  40 +
  41 +context.encrypt_param={
  42 + secret_key="yoho9646YOHO9646",
  43 + iv="YOHO9646yoho9646"
  44 +}
  45 +
  46 +context.encrypt=function(msg)
  47 + if not msg then
  48 + return
  49 + end
  50 + local aes_128_cbc_pkcs7padding = assert(aes:new(context.encrypt_param.secret_key,nil,aes.cipher(128,"cbc"),{iv=context.encrypt_param.iv}))
  51 + local encrypt_msg=aes_128_cbc_pkcs7padding:encrypt(msg)
  52 + return ngx.encode_base64(encrypt_msg)
  53 +end
  54 +
  55 +context.encrypt_ips=function(ips)
  56 + local ips_json=cjson.encode(ips)
  57 + local md5=ngx.md5(ips_json)
  58 + local resp_content={content=ips_json,md5=md5}
  59 + local resp_json=cjson.encode(resp_content)
  60 + local ips_encrypt=context.encrypt(resp_json)
  61 + return ips_encrypt
  62 +end
  63 +
  64 +
  65 +local acquire_ip
  66 +acquire_ip=function(args)
  67 + -- define a function for acquire dns's ip return type is {ips={{ip="100.100.100.100",ttl=100},{ip="100.100.100.100",ttl=100}},delay_in_second=60}
  68 + context.resolve_domain_to_ip=function(domain)
  69 + if not domain then
  70 + return nil
  71 + end
  72 + local r,err=resolver:new{
  73 + nameservers=context.dns_server
  74 + }
  75 + if not r then
  76 + ngx.log(ngx.ERR,"resolve domain error:",err)
  77 + return nil
  78 + end
  79 + local answers,err=r:tcp_query(domain)
  80 + if (not answers) or (#answers <=0) then
  81 + ngx.log(ngx.ERR,"resolve domain tcp query error:",err)
  82 + return nil
  83 + end
  84 + local ips_and_delay_second={ips={}}
  85 + local delay_in_second=30
  86 + for i,ans in ipairs(answers) do
  87 + if ans.address and ans.ttl then
  88 + delay_in_second=30>ans.ttl and ans.ttl or 30
  89 + local ip={ip=ans.address,ttl=delay_in_second}
  90 + table.insert(ips_and_delay_second.ips,ip)
  91 + end
  92 + end
  93 + ips_and_delay_second.delay_in_second=delay_in_second
  94 + ngx.log(ngx.INFO,domain .. " ips:",cjson.encode(ips_and_delay_second))
  95 + return ips_and_delay_second
  96 + end
  97 +
  98 + local resolver_dns
  99 + resolver_dns=function(domain)
  100 + if not domain then
  101 + return
  102 + end
  103 + local org_domain=domain
  104 + local domain=context.aws_domains[domain]
  105 + if not domain then
  106 + return
  107 + end
  108 +
  109 + -- first time init the ip tables must success
  110 + local aws_ips_temp
  111 + while not context.init_flag[domain] do
  112 + aws_ips_temp=context.resolve_domain_to_ip(domain)
  113 + if aws_ips_temp and #(aws_ips_temp.ips)>0 then
  114 + -- add 20160823
  115 + context.aws_ips[org_domain]= aws_ips_temp.ips;
  116 + local encrypt_ips=context.encrypt_ips(aws_ips_temp.ips)
  117 + context.aws_ips_encrypt[org_domain]=encrypt_ips
  118 + context.init_flag[domain]=true
  119 + end
  120 + end
  121 +
  122 + local aws_ips_temp = context.resolve_domain_to_ip(domain)
  123 + if aws_ips_temp and #(aws_ips_temp.ips)>0 then
  124 + -- add 20160823
  125 + context.aws_ips[org_domain]= aws_ips_temp.ips;
  126 + local encrypt_ips=context.encrypt_ips(aws_ips_temp.ips)
  127 + ngx.log(ngx.INFO,org_domain .. " encrypt ips is " .. encrypt_ips)
  128 + context.aws_ips_encrypt[org_domain]=encrypt_ips
  129 + end
  130 +
  131 + end
  132 +
  133 + local timer_resolver_dns
  134 + timer_resolver_dns=function(premature,domain)
  135 + pcall(resolver_dns,domain)
  136 + ngx.timer.at(10,timer_resolver_dns,domain)
  137 + end
  138 +
  139 + -- init tencent ips encrypt list
  140 + for k,v in pairs(context.tencent_ips) do
  141 + context.tencent_ips_encrypt[k]=context.encrypt_ips(v)
  142 + end
  143 +
  144 + -- aws task execute
  145 + for k,v in pairs(context.aws_domains) do
  146 + timer_resolver_dns(false,k)
  147 + end
  148 +
  149 +end
  150 +-- first time execute right now
  151 +ngx.timer.at(0,acquire_ip)
  152 +
  1 +-- init redis twemproxy config
  2 +local redis_config1={host="127.0.0.1",port="6379",auth=nil,timeout=2000,max_idle_timeout=60000,pool_size=1000}
  3 +
  4 +local redis_util=require("redisutil")
  5 +
  6 +local redis1=redis_util:new(redis_config1)
  7 +
  8 +
  9 +lua_context={}
  10 +lua_context["redises"]={redis1}
  11 +
  12 +open_limit_flow={@{open_limit_flow}@}
  13 +api_default_max_per_sencond=1000
  14 +service_default_max_per_sencond=1000
  15 +default_err_code=9999991
  16 +default_err_msg="系统正忙,请稍后重试!"
  17 +--limit interface
  18 +limit_config={
  19 + {@{#each limit_config}@}{@{{this}}@}
  20 + {@{/each}@}
  21 +}
  22 +
  23 +limit_service_config={
  24 + {@{#each limit_service_config}@}{@{{this}}@}
  25 + {@{/each}@}
  26 +}
  1 +local cjson = require "cjson"
  2 +local aes = require "resty.aes"
  3 +local upt = require "url_process_tool"
  4 +local icutil = require "iconfig_util"
  5 +
  6 +
  7 +--5b2d8b4b242e067616c6cd98851b2306 close
  8 +--2a90dfa0f37b92aaebf369e9a4d38ba4 open
  9 +local open_dl = "{@{open_dl}@}"
  10 +local close_dl = "5b2d8b4b242e067616c6cd98851b2306"
  11 +
  12 +
  13 +local function common_lb ()
  14 + local response_table = {
  15 + code = 200,
  16 + ae = "e0323a9039add2978bf5b49550572c7c",
  17 + url = "http://m.yohobuy.com",
  18 + tl = "352f028bd6ecf28de1c285c573642659",
  19 + rp = "17d7a40f313560d4d9a1189a91e45ab9",
  20 + dl = open_dl,
  21 + dli = open_dl,
  22 + dlwi = open_dl
  23 + }
  24 + return response_table
  25 +end
  26 +
  27 +
  28 +function lb()
  29 +
  30 + ngx.log (ngx.INFO, "----- lb START ----- ")
  31 + -- args error --
  32 + local args = ngx.var.args
  33 + if not args then
  34 +
  35 + icutil.error_response ("40011","args not found")
  36 + ngx.log (ngx.ERROR, "- 40011 - args : ", args, "---")
  37 + return
  38 + end
  39 +
  40 +
  41 + local url_args = upt.url_args_decrypt(args)
  42 +
  43 + if not url_args then
  44 + icutil.error_response ("40012","args not found")
  45 + ngx.log (ngx.ERROR, "- 40012 - args : ", args, "---")
  46 + return
  47 + end
  48 +
  49 + -- args parse --
  50 + ngx.req.set_uri_args(url_args)
  51 + local uri_args = ngx.req.get_uri_args()
  52 +
  53 + --for k,v in pairs(uri_args) do ngx.log(ngx.INFO," k = ", k, " v = ", v) end
  54 +
  55 + local uid = uri_args["uid"]
  56 + local app_ver = uri_args["app_ver"]
  57 + local os = uri_args["os"]
  58 + local udid = uri_args["udid"]
  59 + local osversion = uri_args["osv"]
  60 +
  61 + ngx.log (ngx.INFO, "5、--- uid : ", uid, " app_ver :", app_ver, " os :", os, "---")
  62 +
  63 + local result
  64 +
  65 + -- ios
  66 + local ios_lb = {
  67 +
  68 + ["4.8.1"] = function()
  69 + return common_lb()
  70 + end,
  71 + ["4.9.2"] = function()
  72 + local response_table = common_lb()
  73 + response_table.dli = "2a90dfa0f37b92aaebf369e9a4d38ba4"
  74 + return response_table
  75 + end
  76 + ,
  77 + ["5.0.0"] = function()
  78 + local response_table = common_lb()
  79 + if osversion == "7" then
  80 + response_table.dli = close_dl
  81 + else
  82 + response_table.dli = open_dl
  83 + end
  84 + return response_table
  85 + end
  86 + }
  87 +
  88 + -- anopen_stringdroid
  89 + local android_lb = {
  90 + ["4.8.1"] = function()
  91 + return common_lb()
  92 + end,
  93 + ["5.0.0"] = function()
  94 + local response_table = common_lb()
  95 + local dla_value = { dlw = open_dl, dlp = close_dl }
  96 + response_table.dla = dla_value
  97 + return response_table
  98 + end
  99 + }
  100 + -- switch 0:ios 1:android
  101 + if os == "0" then
  102 + ngx.log (ngx.INFO, "----- 1. enter ios lb ----- ")
  103 + if ios_lb[app_ver] ~= nil and ios_lb[app_ver]() ~= nil then
  104 + result = ios_lb[app_ver]()
  105 + ngx.log (ngx.INFO, "----- 2. app_ver found ----- ")
  106 + else
  107 + result = common_lb()
  108 + ngx.log (ngx.INFO, "----- 2. app_ver not found ----- ")
  109 + end
  110 + elseif os == "1" then
  111 + ngx.log (ngx.INFO, "----- enter android lb ----- ")
  112 +
  113 + if android_lb[app_ver] ~= nil and android_lb[app_ver]() ~= nil then
  114 + result = android_lb[app_ver]()
  115 + ngx.log (ngx.INFO, "----- 2. app_ver found ----- ")
  116 + else
  117 + result = common_lb()
  118 + ngx.log (ngx.INFO, "----- 2. app_ver not found ----- ")
  119 + end
  120 + else
  121 + result = icutil.error_response ("40013","os error")
  122 + ngx.log (ngx.ERR, "- 40013 - enter error lb ----- ")
  123 + ngx.log (ngx.ERR, "- 40013 - uid : ", uid, " app_ver :", app_ver, " os :", os, "---")
  124 +
  125 + end
  126 +
  127 + --response
  128 + ngx.header["Content-type"]="text/plain;charset=utf-8"
  129 + local result_json = cjson.encode(result)
  130 + local resultstr = upt.url_args_encrypt (result_json)
  131 + ngx.log (ngx.INFO, string.format ("--- response %s ---",resultstr))
  132 + ngx.say(resultstr)
  133 +
  134 + ngx.log (ngx.INFO, "----- lb OVER ----- ")
  135 + end
  136 +
  137 + lb()
  1 +user www www;
  2 +
  3 +worker_processes 8;
  4 +worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
  5 +
  6 +pid logs/nginx.pid;
  7 +# [ debug | info | notice | warn | error | crit ]
  8 +#error_log /Data/logs/nginx/nginx_error.log info;
  9 +error_log /Data/logs/nginx/nginx_error.log info;
  10 +#Specifies the value for maximum file descriptors that can be opened by this process.
  11 +worker_rlimit_nofile 51200;
  12 +
  13 +events
  14 +{
  15 + use epoll;
  16 +
  17 + #maxclient = worker_processes * worker_connections / cpu_number
  18 + worker_connections 51200;
  19 +}
  20 +
  21 +http
  22 +{
  23 + include mime.types;
  24 + default_type application/octet-stream;
  25 + #charset gb2312,utf-8;
  26 + charset utf-8;
  27 +
  28 + log_format fenxi '$remote_addr|$http_x_forwarded_for|[$time_local]|$http_host|$request|'
  29 + '$status|$body_bytes_sent|$request_time|$upstream_response_time|$upstream_cache_status|$http_referer|'
  30 + '$http_user_agent|$upstream_addr';
  31 +
  32 +
  33 + #General Options
  34 + server_names_hash_bucket_size 128;
  35 + client_header_buffer_size 32k;
  36 + large_client_header_buffers 4 32k;
  37 + client_body_buffer_size 8m; #256k
  38 + #
  39 + #server_tokens off;
  40 + ignore_invalid_headers on;
  41 + recursive_error_pages on;
  42 + server_name_in_redirect off;
  43 +
  44 + sendfile on;
  45 +
  46 + #timeouts
  47 + keepalive_timeout 60;
  48 + #test
  49 + #client_body_timeout 3m;
  50 + #client_header_timeout 3m;
  51 + #send_timeout 3m;
  52 +
  53 +
  54 + #TCP Options
  55 + tcp_nopush on;
  56 + tcp_nodelay on;
  57 +
  58 + #fastcgi options
  59 + fastcgi_connect_timeout 300;
  60 + fastcgi_send_timeout 300;
  61 + fastcgi_read_timeout 300;
  62 + fastcgi_buffer_size 64k;
  63 + fastcgi_buffers 4 64k;
  64 + fastcgi_busy_buffers_size 128k;
  65 + fastcgi_temp_file_write_size 128k;
  66 +
  67 + #set_real_ip_from 10.159.191.0/24;
  68 + #real_ip_header X-Forwarded-For;
  69 +
  70 + #hiden php version
  71 + fastcgi_hide_header X-Powered-By;
  72 +
  73 + #size limits
  74 + client_max_body_size 50m;
  75 +
  76 + gzip on;
  77 + gzip_min_length 1k;
  78 + gzip_buffers 4 16k;
  79 + #gzip_http_version 1.0;
  80 + gzip_comp_level 2;
  81 + gzip_types text/plain application/x-javascript text/css application/xml application/json;
  82 + gzip_vary on;
  83 +
  84 +
  85 +
  86 + fastcgi_temp_path /dev/shm/fastcgi_temp;
  87 + client_body_temp_path /dev/shm/client_body_temp;
  88 +
  89 + # where the lua package exists
  90 + lua_package_path "/Data/local/openresty-1.9.15.1/nginx/conf/lua/?.lua;;";
  91 + init_by_lua_file "conf/lua/init_lua.lua";
  92 +
  93 +
  94 + #upstream web
  95 + upstream oldservice {
  96 + server service.api.yohobuy.com ;
  97 + }
  98 +
  99 + upstream oldapi {
  100 + server api.open.yohobuy.com ;
  101 + }
  102 +
  103 + #brower service
  104 + upstream brower {
  105 + server 10.66.0.6:8092;
  106 + }
  107 +
  108 + #social service
  109 + upstream social {
  110 + server 10.66.0.44:8095;
  111 + server 10.66.0.229:8095;
  112 + }
  113 +
  114 + upstream apigateway {
  115 + {@{#each ips}@}server {@{this}@}:8080 max_fails=5 fail_timeout=3s;
  116 + {@{/each}@}
  117 + keepalive 32;
  118 + }
  119 +
  120 + upstream grayapigateway{
  121 + # ip_hash;
  122 + {@{#each grayips}@}server {@{this}@}:8080 max_fails=5 fail_timeout=3s;
  123 + {@{/each}@}
  124 + keepalive 32;
  125 + }
  126 +
  127 + ## order picture check
  128 + upstream yoho-dashboard {
  129 + server 172.31.25.5:8089;
  130 + }
  131 +
  132 + upstream pay.yohobuy.com {
  133 + server 172.31.55.207 max_fails=5 fail_timeout=3s weight=10;
  134 + server 172.31.56.135 max_fails=5 fail_timeout=3s weight=50;
  135 + }
  136 +
  137 + upstream www.yohobuy.com {
  138 + server www.yohobuy.com;
  139 + }
  140 +
  141 + upstream union {
  142 + server 172.31.16.99:8088;
  143 + }
  144 +
  145 +
  146 + upstream activityApi{
  147 + server 10.66.0.16:8090;
  148 + }
  149 +
  150 + upstream apiWechat {
  151 + server 172.31.31.13:8094 ;
  152 + }
  153 +
  154 +
  155 +
  156 + #upstream
  157 + fastcgi_next_upstream error timeout invalid_header http_500;
  158 +
  159 + #limit_zone limit $binary_remote_addr 1m;
  160 +
  161 + #fastcgi cache
  162 + #fastcgi_cache_path /nginxcache levels=1:2 keys_zone=two:10m inactive=1d max_size=3000m;
  163 + #for example just for study! have fun!
  164 +
  165 +
  166 + include vhosts/api.yoho.cn.conf;
  167 + include vhosts/mapi.yohobuy.com.conf;
  168 + include vhosts/pay.yohobuy.com.conf;
  169 + include vhosts/service.yoho.cn.conf;
  170 + include vhosts/api_single.yoho.cn.conf;
  171 + include vhosts/unknown.conf;
  172 + }
@@ -25,6 +25,8 @@ public interface HostInfoMapper { @@ -25,6 +25,8 @@ public interface HostInfoMapper {
25 25
26 List<HostInfo> selectHostInfosByTag(@Param("tag") String tag); 26 List<HostInfo> selectHostInfosByTag(@Param("tag") String tag);
27 27
  28 + List<HostInfo> selectHostInfosByTagAndCloudType(@Param("tag") String tag,@Param("cloudType") int cloudType);
  29 +
28 HostInfo selectByHostIp(@Param("hostIp") String hostIp); 30 HostInfo selectByHostIp(@Param("hostIp") String hostIp);
29 31
30 } 32 }
@@ -182,4 +182,11 @@ @@ -182,4 +182,11 @@
182 where instr(tags, LOWER(#{tag})) &gt; 0 182 where instr(tags, LOWER(#{tag})) &gt; 0
183 </select> 183 </select>
184 184
  185 + <select id="selectHostInfosByTagAndCloudType" resultMap="BaseResultMap" >
  186 + select
  187 + <include refid="Base_Column_List" />
  188 + from host_info
  189 + where instr(tags, LOWER(#{tag})) &gt; 0
  190 + AND cloud_type = #{cloudType}
  191 + </select>
185 </mapper> 192 </mapper>
@@ -38,6 +38,10 @@ @@ -38,6 +38,10 @@
38 <artifactId>json-lib</artifactId> 38 <artifactId>json-lib</artifactId>
39 <classifier>jdk15</classifier> 39 <classifier>jdk15</classifier>
40 </dependency> 40 </dependency>
  41 + <dependency>
  42 + <groupId>javax.servlet</groupId>
  43 + <artifactId>servlet-api</artifactId>
  44 + </dependency>
41 45
42 </dependencies> 46 </dependencies>
43 47
@@ -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;
  1 +package com.monitor.switchs;
  2 +
  3 +import com.google.common.collect.Maps;
  4 +import com.model.HostInfo;
  5 +import com.monitor.common.util.HandlebarsUtils;
  6 +import com.monitor.model.response.BaseResponse;
  7 +import com.monitor.mysql.mapper.HostInfoMapper;
  8 +import org.apache.commons.collections.CollectionUtils;
  9 +import org.apache.commons.lang.StringUtils;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
  12 +import org.springframework.beans.factory.annotation.Autowired;
  13 +import org.springframework.stereotype.Controller;
  14 +import org.springframework.web.bind.annotation.RequestMapping;
  15 +import org.springframework.web.bind.annotation.ResponseBody;
  16 +
  17 +import javax.servlet.http.HttpServletRequest;
  18 +import java.util.ArrayList;
  19 +import java.util.HashMap;
  20 +import java.util.List;
  21 +import java.util.Map;
  22 +
  23 +/**
  24 + * Created by zhouxiang on 2016/10/25.
  25 + */
  26 +@Controller
  27 +@RequestMapping("lbswitch")
  28 +public class LbSwitchCtrl {
  29 +
  30 + private Logger logger = LoggerFactory.getLogger(getClass());
  31 +
  32 + @Autowired
  33 + HostInfoMapper hostInfoMapper;
  34 +
  35 + private static final String OPENDL = "2a90dfa0f37b92aaebf369e9a4d38ba4";
  36 + private static final String CLOSEDL = "5b2d8b4b242e067616c6cd98851b2306";
  37 +
  38 + /**
  39 + * 查看直连配置
  40 + * @return 2016年5月12日下午1:49:48
  41 + */
  42 + @RequestMapping(value = "viewConf")
  43 + @ResponseBody
  44 + public BaseResponse viewConf() throws Exception {
  45 + //直连的配置放在腾讯云nginx上
  46 + List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("lb-switch");
  47 + if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
  48 + return new BaseResponse(500,"fail","没有找到直连nginx服务器");
  49 + }
  50 + return new BaseResponse(200,"success",analyseConfigFile(exe("view", qcloudNginxHosts.get(0).getHostIp(),null)));
  51 + }
  52 +
  53 + /**
  54 + * 查看模板生成lb.lua脚本
  55 + *
  56 + * @return 2016年5月12日下午1:49:48
  57 + */
  58 + @RequestMapping(value = "viewToChangeLbConf")
  59 + @ResponseBody
  60 + public BaseResponse viewToChangeLuaConf(String status, HttpServletRequest request) throws Exception {
  61 + logger.info("will change status {}", status);
  62 + if(!StringUtils.equals("1", status) && !StringUtils.equals("0", status)){
  63 + throw new Exception("切换状态参数不正确!");
  64 + }
  65 + Map<String, Object> map = new HashMap();
  66 + map.put("open_dl", StringUtils.equals("1", status) ? OPENDL : CLOSEDL);
  67 + String path = request.getSession().getServletContext().getRealPath("/") + "lua/lb.lua";
  68 + String result = HandlebarsUtils.replace(map, "template/lb", path);
  69 + return new BaseResponse(200, "success", result);
  70 + }
  71 +
  72 + /**
  73 + * 修改配置
  74 + * @return 2016年5月12日下午1:49:48
  75 + */
  76 + @RequestMapping(value = "switchConf")
  77 + @ResponseBody
  78 + public BaseResponse switchConf(HttpServletRequest request) {
  79 + String path = request.getSession().getServletContext().getRealPath("/") + "lua/lb.lua";
  80 + logger.info("lb.lua template path {}", path);
  81 + //lua dns 只放在腾讯云nginx服务器上
  82 + List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag("lb-switch");
  83 + if (CollectionUtils.isEmpty(nginxHosts)) {
  84 + return new BaseResponse(500, "在cmdb中未找到直连对应的nginx服务器!");
  85 + }
  86 + List<String> ips = new ArrayList<String>();
  87 + for(HostInfo hostInfo : nginxHosts){
  88 + ips.add(hostInfo.getHostIp());
  89 + }
  90 + return new BaseResponse(200,"success", exe("switch", StringUtils.join(ips,","),path));
  91 + }
  92 +
  93 + /**
  94 + * 执行命令
  95 + *
  96 + * @param commod
  97 + * @param host
  98 + * @param templatePath
  99 + * @return 2016年5月17日下午3:26:29
  100 + */
  101 + public String exe(String commod, String host, String templatePath) {
  102 + StringBuilder commond = new StringBuilder();
  103 + commond.append(" " + commod).append(" " + host).append(" " + templatePath);
  104 + return CommodUtil.exe(CommodUtil.LB_SWITCH_SH, commond.toString());
  105 + }
  106 +
  107 + private Map<String,String> analyseConfigFile(String rlt) {
  108 + String[] str = rlt.split("\r\n");
  109 + StringBuilder sb = new StringBuilder();
  110 + Map<String,String> result = Maps.newHashMap();
  111 + for (int i = 0; i < str.length; i++) {
  112 + if (str[i].indexOf("open_dl") != -1) {
  113 + if(i > 1 && StringUtils.isNotEmpty(str[i - 2]) && str[i - 2].contains("--")){
  114 + sb.append(str[i - 2]).append("</br>");
  115 + }
  116 + if(i > 0 && StringUtils.isNotEmpty(str[i - 1]) && str[i - 1].contains("--")){
  117 + sb.append(str[i - 1]).append("</br>");
  118 + }
  119 + sb.append(str[i]).append("</br>");
  120 + result.put("conf",sb.toString());
  121 + String dl = str[i].substring(str[i].indexOf("\"")).replaceAll("\"","").trim();
  122 + if(StringUtils.equals(OPENDL,dl)){
  123 + result.put("status","1");
  124 + return result;
  125 + }
  126 + if(StringUtils.equals(CLOSEDL,dl)){
  127 + result.put("status","0");
  128 + return result;
  129 + }
  130 + }
  131 + }
  132 + return result;
  133 + }
  134 +
  135 + public static void main(String[] args) {
  136 + String s = "local open_dl = \"2a90dfa0f37b92aaebf369e9a4d38ba4\" ";
  137 + String b = s.substring(s.indexOf("\"")).replaceAll("\"","");
  138 + System.out.println(b);
  139 + }
  140 +
  141 +}
1 package com.monitor.switchs; 1 package com.monitor.switchs;
2 2
  3 +import com.model.HostInfo;
3 import com.monitor.common.service.AlarmMsgService; 4 import com.monitor.common.service.AlarmMsgService;
  5 +import com.monitor.common.util.HandlebarsUtils;
4 import com.monitor.model.domain.SmsTicket; 6 import com.monitor.model.domain.SmsTicket;
5 import com.monitor.model.response.BaseResponse; 7 import com.monitor.model.response.BaseResponse;
  8 +import com.monitor.mysql.mapper.HostInfoMapper;
  9 +import org.apache.commons.codec.binary.StringUtils;
  10 +import org.apache.commons.collections.CollectionUtils;
6 import org.slf4j.Logger; 11 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory; 12 import org.slf4j.LoggerFactory;
8 import org.springframework.beans.factory.annotation.Autowired; 13 import org.springframework.beans.factory.annotation.Autowired;
@@ -11,6 +16,7 @@ import org.springframework.stereotype.Controller; @@ -11,6 +16,7 @@ import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestMapping; 16 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.ResponseBody; 17 import org.springframework.web.bind.annotation.ResponseBody;
13 18
  19 +import javax.servlet.http.HttpServletRequest;
14 import java.io.BufferedReader; 20 import java.io.BufferedReader;
15 import java.io.File; 21 import java.io.File;
16 import java.io.FileReader; 22 import java.io.FileReader;
@@ -32,6 +38,9 @@ public class LuaSwitchCtrl { @@ -32,6 +38,9 @@ public class LuaSwitchCtrl {
32 38
33 private Logger logger = LoggerFactory.getLogger(getClass()); 39 private Logger logger = LoggerFactory.getLogger(getClass());
34 40
  41 + @Autowired
  42 + HostInfoMapper hostInfoMapper;
  43 +
35 /** 44 /**
36 * 查看nginx配置 45 * 查看nginx配置
37 * 46 *
@@ -39,16 +48,28 @@ public class LuaSwitchCtrl { @@ -39,16 +48,28 @@ public class LuaSwitchCtrl {
39 */ 48 */
40 @RequestMapping(value = "viewConf") 49 @RequestMapping(value = "viewConf")
41 @ResponseBody 50 @ResponseBody
42 - public String viewConf() {  
43 -// return "if not uid then" +  
44 -// "</br>return ngx.lookup_context.aws_ips_encrypt[domain]</br>" +  
45 -// "end</br>" +  
46 -// "if uid%1024>128 then</br>" +  
47 -// "return ngx.lookup_context.aws_ips_encrypt[domain]</br>" +  
48 -// "else</br>" +  
49 -// "return ngx.lookup_context.tencent_ips_encrypt[domain]</br>" +  
50 -// "end</br>";  
51 - return analyseConfigFile(exe("aws", "view")); 51 + public String viewConf() throws Exception {
  52 + List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("lua-switch");
  53 + if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
  54 + return "没有找到lua切换nginx服务器";
  55 + }
  56 + return analyseConfigFile(exe("view", qcloudNginxHosts.get(0).getHostIp(),null));
  57 + }
  58 +
  59 + /**
  60 + * 查看nginx配置
  61 + *
  62 + * @return 2016年5月12日下午1:49:48
  63 + */
  64 + @RequestMapping(value = "viewToChangeLuaConf")
  65 + @ResponseBody
  66 + public BaseResponse viewToChangeLuaConf(String cloudName, HttpServletRequest request) {
  67 + logger.info("cloudName {}", cloudName);
  68 + Map<String, Object> map = new HashMap();
  69 + map.put("flag", StringUtils.equals("aws", cloudName) ? 1 : (StringUtils.equals("qcloud", cloudName) ? 2 : 3));
  70 + String path = request.getSession().getServletContext().getRealPath("/") + "lua/dns_resolver_worker.lua";
  71 + String result = HandlebarsUtils.replace(map, "template/dns_resolver_worker", path);
  72 + return new BaseResponse(200, "success", result);
52 } 73 }
53 74
54 /** 75 /**
@@ -58,25 +79,33 @@ public class LuaSwitchCtrl { @@ -58,25 +79,33 @@ public class LuaSwitchCtrl {
58 */ 79 */
59 @RequestMapping(value = "switchConf") 80 @RequestMapping(value = "switchConf")
60 @ResponseBody 81 @ResponseBody
61 - public BaseResponse switchConf(String cloudName) {  
62 - String result = exe(cloudName, "switch");  
63 - BaseResponse<String> baseResponse = new BaseResponse<>();  
64 - baseResponse.setCode(200);  
65 - baseResponse.setData(result);  
66 - return baseResponse; 82 + public BaseResponse switchConf(HttpServletRequest request) {
  83 + String path = request.getSession().getServletContext().getRealPath("/") + "lua/dns_resolver_worker.lua";
  84 + logger.info("path {}", path);
  85 + //lua dns 只放在腾讯云nginx服务器上
  86 + List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag("lua-switch");
  87 + if (CollectionUtils.isEmpty(nginxHosts)) {
  88 + return new BaseResponse(500, "腾讯云在cmdb中未找到对应的nginx服务器!");
  89 + }
  90 + List<String> ips = new ArrayList<String>();
  91 + for(HostInfo hostInfo : nginxHosts){
  92 + ips.add(hostInfo.getHostIp());
  93 + }
  94 +
  95 + return new BaseResponse(200,"success", exe("switch", org.apache.commons.lang.StringUtils.join(ips,","),path));
67 } 96 }
68 97
69 /** 98 /**
70 * 执行命令 99 * 执行命令
71 * 100 *
72 - * @param cloudName  
73 * @param commod 101 * @param commod
  102 + * @param host
  103 + * @param templatePath
74 * @return 2016年5月17日下午3:26:29 104 * @return 2016年5月17日下午3:26:29
75 */ 105 */
76 - public String exe(String cloudName, String commod) { 106 + public String exe(String commod, String host, String templatePath) {
77 StringBuilder commond = new StringBuilder(); 107 StringBuilder commond = new StringBuilder();
78 - commond.append(" -e " + commod);  
79 - commond.append(" -s " + cloudName); 108 + commond.append(" " + commod).append(" " + host).append(" " + templatePath);
80 return CommodUtil.exe(CommodUtil.LUA_SWITCH_SH, commond.toString()); 109 return CommodUtil.exe(CommodUtil.LUA_SWITCH_SH, commond.toString());
81 } 110 }
82 111
@@ -85,7 +114,7 @@ public class LuaSwitchCtrl { @@ -85,7 +114,7 @@ public class LuaSwitchCtrl {
85 StringBuilder sb = new StringBuilder(); 114 StringBuilder sb = new StringBuilder();
86 for (int i = 0; i < str.length; i++) { 115 for (int i = 0; i < str.length; i++) {
87 if (str[i].indexOf("cloud_flag") != -1) { 116 if (str[i].indexOf("cloud_flag") != -1) {
88 - sb.append(str[i-1]).append("</br>"); 117 + sb.append(str[i - 1]).append("</br>");
89 sb.append(str[i]).append("</br>"); 118 sb.append(str[i]).append("</br>");
90 } 119 }
91 } 120 }
  1 +package com.monitor.switchs;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.google.common.collect.Lists;
  5 +import com.google.common.collect.Maps;
  6 +import com.model.HostInfo;
  7 +import com.monitor.common.util.HandlebarsUtils;
  8 +import com.monitor.model.response.BaseResponse;
  9 +import com.monitor.mysql.mapper.HostInfoMapper;
  10 +import org.apache.commons.collections.CollectionUtils;
  11 +import org.apache.commons.lang.StringUtils;
  12 +import org.slf4j.Logger;
  13 +import org.slf4j.LoggerFactory;
  14 +import org.springframework.beans.factory.annotation.Autowired;
  15 +import org.springframework.stereotype.Controller;
  16 +import org.springframework.web.bind.annotation.RequestBody;
  17 +import org.springframework.web.bind.annotation.RequestMapping;
  18 +import org.springframework.web.bind.annotation.ResponseBody;
  19 +
  20 +import javax.servlet.http.HttpServletRequest;
  21 +import java.io.*;
  22 +import java.util.ArrayList;
  23 +import java.util.HashMap;
  24 +import java.util.List;
  25 +import java.util.Map;
  26 +
  27 +/**
  28 + * Created by zhouxiang on 2016/10/25.
  29 + */
  30 +@Controller
  31 +@RequestMapping("limitswitch")
  32 +public class NginxLimit {
  33 +
  34 + private Logger logger = LoggerFactory.getLogger(getClass());
  35 +
  36 + @Autowired
  37 + HostInfoMapper hostInfoMapper;
  38 +
  39 + /**
  40 + * 查看nginx配置
  41 + *
  42 + * @return 2016年5月12日下午1:49:48
  43 + */
  44 + @RequestMapping(value = "viewConf")
  45 + @ResponseBody
  46 + public BaseResponse viewConf() throws Exception {
  47 + List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTag("limit-switch");
  48 + if (CollectionUtils.isEmpty(qcloudNginxHosts)) {
  49 + return new BaseResponse(500,"fail","没有找到限流的nginx服务器");
  50 + }
  51 + return new BaseResponse(200,"success",analyseConfigFile(exe("view", qcloudNginxHosts.get(0).getHostIp(),null)));
  52 + }
  53 +
  54 + /**
  55 + * 查看nginx配置
  56 + *
  57 + * @return 2016年5月12日下午1:49:48
  58 + */
  59 + @RequestMapping(value = "viewToChangeLimitConf")
  60 + @ResponseBody
  61 + public BaseResponse viewToChangeLuaConf(@RequestBody Map<String,String> params, HttpServletRequest request) {
  62 + logger.info("params {}", params);
  63 + Map<String, Object> map = new HashMap();
  64 + map.put("open_limit_flow", params.get("limitFlow"));
  65 + List apiConfigList = JSON.parseArray(params.get("apiConfs"),String.class);
  66 + int apiConfigSize = apiConfigList.size();
  67 + for(int i = 0;i< apiConfigSize;i++){
  68 + if(i == (apiConfigSize -1)){
  69 + break;
  70 + }
  71 + apiConfigList.set(i,apiConfigList.get(i)+",");
  72 + }
  73 + map.put("limit_config", apiConfigList);
  74 + List serviceConfList = JSON.parseArray(params.get("serviceConfs"),String.class);
  75 + int serviceConfSize = serviceConfList.size();
  76 + for(int i = 0;i< serviceConfSize;i++){
  77 + if(i == (serviceConfSize -1)){
  78 + break;
  79 + }
  80 + serviceConfList.set(i,serviceConfList.get(i)+",");
  81 + }
  82 + map.put("limit_service_config", serviceConfList);
  83 + String path = request.getSession().getServletContext().getRealPath("/") + "lua/init_lua.lua";
  84 + String result = HandlebarsUtils.replace(map, "template/init_lua", path);
  85 + return new BaseResponse(200, "success", result);
  86 + }
  87 +
  88 + /**
  89 + * 修改配置
  90 + * type:环境类型 1:线上 2:灰度
  91 + *
  92 + * @return 2016年5月12日下午1:49:48
  93 + */
  94 + @RequestMapping(value = "switchConf")
  95 + @ResponseBody
  96 + public BaseResponse switchNginxConf(HttpServletRequest httpRequest) {
  97 + //获取对应cloud的机器 aws 1 qcloud 2
  98 + List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTag("limit-switch");
  99 + if (CollectionUtils.isEmpty(nginxHosts)) {
  100 + return new BaseResponse(500, "在cmdb中未找到限流对应的nginx服务器!");
  101 + }
  102 + List<String> ips = new ArrayList<String>();
  103 + for(HostInfo hostInfo : nginxHosts){
  104 + ips.add(hostInfo.getHostIp());
  105 + }
  106 + String tempPath = httpRequest.getSession().getServletContext().getRealPath("/") + "lua/init_lua.lua";
  107 + return new BaseResponse(200,"success", exe("switch", StringUtils.join(ips,","),tempPath));
  108 + }
  109 +
  110 + /**
  111 + * 执行命令
  112 + *
  113 + * @param commod
  114 + * @param host
  115 + * @param templatePath
  116 + * @return 2016年5月17日下午3:26:29
  117 + */
  118 + public String exe(String commod, String host, String templatePath) {
  119 + StringBuilder commond = new StringBuilder();
  120 + commond.append(" " + commod).append(" " + host).append(" " + templatePath);
  121 + return CommodUtil.exe(CommodUtil.LIMIT_SWITCH_SH, commond.toString());
  122 + }
  123 +
  124 + private Map<String,Object> analyseConfigFile(String str) throws IOException {
  125 + String[] strs = str.split("\r\n");
  126 + Map<String,Object> map = Maps.newHashMap();
  127 + for(int i = 0; i< strs.length ; i++){
  128 + if(map.keySet().containsAll(Lists.newArrayList("limit_flow","limit_config","limit_service_config"))){
  129 + break;
  130 + }
  131 + if(strs[i].contains("open_limit_flow")){
  132 + map.put("open_limit_flow",strs[i].trim().split("=")[1]);
  133 + continue;
  134 + }
  135 + if(strs[i].contains("limit_config") || strs[i].contains("limit_service_config")){
  136 + List<String> limitConf = Lists.newArrayList();
  137 + String key = strs[i].contains("limit_config") ? "limit_config" : "limit_service_config";
  138 + while (true){
  139 + i++;
  140 + if(!strs[i].trim().startsWith("[") && strs[i].trim().endsWith("}")){
  141 + break;
  142 + }
  143 + if(StringUtils.isEmpty(strs[i].trim()) || strs[i].trim().startsWith("--")){
  144 + continue;
  145 + }
  146 + limitConf.add(strs[i].trim().replace("},","}"));
  147 + }
  148 + map.put(key, limitConf);
  149 + continue;
  150 + }
  151 + }
  152 + return map;
  153 + }
  154 +
  155 +}
1 package com.monitor.switchs; 1 package com.monitor.switchs;
2 2
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.alibaba.fastjson.JSONArray;
  5 +import com.google.common.base.Function;
  6 +import com.google.common.collect.Lists;
  7 +import com.model.HostInfo;
3 import com.monitor.common.service.AlarmMsgService; 8 import com.monitor.common.service.AlarmMsgService;
  9 +import com.monitor.common.util.HandlebarsUtils;
4 import com.monitor.common.util.RandomUtil; 10 import com.monitor.common.util.RandomUtil;
5 import com.monitor.model.domain.SmsTicket; 11 import com.monitor.model.domain.SmsTicket;
6 import com.monitor.model.response.BaseResponse; 12 import com.monitor.model.response.BaseResponse;
  13 +import com.monitor.mysql.mapper.HostInfoMapper;
  14 +import org.apache.commons.collections.CollectionUtils;
  15 +import org.apache.commons.collections.map.HashedMap;
  16 +import org.apache.commons.lang.StringUtils;
  17 +import org.apache.http.HttpRequest;
7 import org.slf4j.Logger; 18 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory; 19 import org.slf4j.LoggerFactory;
9 import org.springframework.beans.factory.annotation.Autowired; 20 import org.springframework.beans.factory.annotation.Autowired;
@@ -12,6 +23,7 @@ import org.springframework.stereotype.Controller; @@ -12,6 +23,7 @@ import org.springframework.stereotype.Controller;
12 import org.springframework.web.bind.annotation.RequestMapping; 23 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.ResponseBody; 24 import org.springframework.web.bind.annotation.ResponseBody;
14 25
  26 +import javax.servlet.http.HttpServletRequest;
15 import java.io.*; 27 import java.io.*;
16 import java.util.ArrayList; 28 import java.util.ArrayList;
17 import java.util.HashMap; 29 import java.util.HashMap;
@@ -33,6 +45,9 @@ public class NginxSwitchCtrl { @@ -33,6 +45,9 @@ public class NginxSwitchCtrl {
33 @Value("${system.envi}") 45 @Value("${system.envi}")
34 private String env; 46 private String env;
35 47
  48 + @Autowired
  49 + HostInfoMapper hostInfoMapper;
  50 +
36 /** 51 /**
37 * 查看nginx配置 52 * 查看nginx配置
38 * 53 *
@@ -41,49 +56,125 @@ public class NginxSwitchCtrl { @@ -41,49 +56,125 @@ public class NginxSwitchCtrl {
41 @RequestMapping(value = "viewNginxConf") 56 @RequestMapping(value = "viewNginxConf")
42 @ResponseBody 57 @ResponseBody
43 public BaseResponse viewNginxConf() { 58 public BaseResponse viewNginxConf() {
44 - Map<String,List<Map<String, Object>>> map = new HashMap<>();  
45 - if ("test".equals(env)) { 59 + Map<String, List<Map<String, Object>>> map = new HashMap<>();
  60 + /*if ("test".equals(env)) {
46 List<Map<String, Object>> awsList = localTest("aws"); 61 List<Map<String, Object>> awsList = localTest("aws");
47 List<Map<String, Object>> qcloudList = localTest("qq"); 62 List<Map<String, Object>> qcloudList = localTest("qq");
48 map.put("awsList", awsList); 63 map.put("awsList", awsList);
49 map.put("qcloudList", qcloudList); 64 map.put("qcloudList", qcloudList);
50 return new BaseResponse(map); 65 return new BaseResponse(map);
51 - } 66 + }*/
52 67
53 - List<Map<String, Object>> awsList = analyseConfigFile(exe("aws", "view", null));  
54 - List<Map<String, Object>> qcloudList = analyseConfigFile(exe("qcloud", "view", null)); 68 + List<HostInfo> awsNginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 1);
  69 + if(CollectionUtils.isEmpty(awsNginxHosts)){
  70 + map.put("awsList", Lists.newArrayList());
  71 + }else {
  72 + List<Map<String, Object>> awsList = analyseConfigFile(exe("view", awsNginxHosts.get(0).getHostIp(), null));
55 map.put("awsList", awsList); 73 map.put("awsList", awsList);
  74 + }
  75 + List<HostInfo> qcloudNginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", 2);
  76 + if(CollectionUtils.isEmpty(qcloudNginxHosts)){
  77 + map.put("qcloudList", Lists.newArrayList());
  78 + }else {
  79 + List<Map<String, Object>> qcloudList = analyseConfigFile(exe("view", qcloudNginxHosts.get(0).getHostIp(), null));
56 map.put("qcloudList", qcloudList); 80 map.put("qcloudList", qcloudList);
  81 + }
  82 +
57 return new BaseResponse(map); 83 return new BaseResponse(map);
58 } 84 }
59 85
60 /** 86 /**
61 * 修改配置 87 * 修改配置
  88 + * type:环境类型 1:线上 2:灰度
  89 + *
  90 + * @return 2016年5月12日下午1:49:48
  91 + */
  92 + @RequestMapping(value = "viewToChangeNginxConf")
  93 + @ResponseBody
  94 + public BaseResponse viewToChangeNginxConf(String cloudName, String target, String ips, String onlineOrGray, HttpServletRequest httpRequest) {
  95 +
  96 + //切线上 灰度ips不能为空
  97 + if (StringUtils.isEmpty(ips)) {
  98 + return new BaseResponse(500, "线上或灰度服务器入参不能为空!");
  99 + }
  100 +
  101 + //线上切换
  102 + String tag = StringUtils.equals("gray", target) ? "灰度" : "gateway";
  103 +
  104 + //需要查询的gateway机子
  105 + int gatewayCloudType = StringUtils.equals("aws", target) ? 1 : (StringUtils.equals("qcloud", target) ? 2 : (StringUtils.equals("aws", cloudName) ? 1 : 2));
  106 +
  107 + //获取gateway或graygateway服务器
  108 + List<HostInfo> gatewayHosts = hostInfoMapper.selectHostInfosByTagAndCloudType(tag, gatewayCloudType);
  109 + if (CollectionUtils.isEmpty(gatewayHosts)) {
  110 + return new BaseResponse(500, cloudName + "在cmdb中未找到对应" + tag + "的服务器!");
  111 + }
  112 +
  113 + //模板参数对象
  114 + Map<String, Object> map = new HashMap<String, Object>();
  115 + List<String> ipList = Lists.transform(gatewayHosts, new Function<HostInfo, String>() {
  116 + @Override
  117 + public String apply(HostInfo input) {
  118 + return input.getHostIp();
  119 + }
  120 + });
  121 + //切线上 灰度ips不变
  122 + if (StringUtils.equals("online", onlineOrGray)) {
  123 + map.put("grayips", JSON.parseArray(ips));
  124 + map.put("ips", ipList);
  125 + }
  126 + //切灰度 线上ips不变
  127 + else if (StringUtils.equals("gray", onlineOrGray)) {
  128 + map.put("ips", JSON.parseArray(ips));
  129 + map.put("grayips", ipList);
  130 + }
  131 +
  132 + String path = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf";
  133 + logger.info("nginx.conf create path is {}",path);
  134 + String result = HandlebarsUtils.replace(map, "template/" + cloudName + "-nginx", path);
  135 + Map<String,Object> resultMap = new HashMap();
  136 + resultMap.put("cloudName",cloudName);
  137 + resultMap.put("result",result);
  138 + return new BaseResponse(200, "success",resultMap);
  139 + }
  140 +
  141 + /**
  142 + * 修改配置
  143 + * type:环境类型 1:线上 2:灰度
62 * 144 *
63 * @return 2016年5月12日下午1:49:48 145 * @return 2016年5月12日下午1:49:48
64 */ 146 */
65 @RequestMapping(value = "switchNginxConf") 147 @RequestMapping(value = "switchNginxConf")
66 @ResponseBody 148 @ResponseBody
67 - public BaseResponse switchNginxConf(String cloudName, String target) {  
68 - String result = exe(cloudName, "switch", target);  
69 - BaseResponse<String> baseResponse = new BaseResponse<>();  
70 - baseResponse.setCode(200);  
71 - baseResponse.setData(result);  
72 - return baseResponse; 149 + public BaseResponse switchNginxConf(String cloudName, HttpServletRequest httpRequest) {
  150 + int nginxCloudType = StringUtils.equals("aws", cloudName) ? 1 : (StringUtils.equals("qcloud", cloudName) ? 2 : 0);
  151 + if (0 == nginxCloudType) {
  152 + return new BaseResponse(500, "没有匹配到要切换的云");
  153 + }
  154 + //获取对应cloud的机器 aws 1 qcloud 2
  155 + List<HostInfo> nginxHosts = hostInfoMapper.selectHostInfosByTagAndCloudType("upstream-switch", nginxCloudType);
  156 + if (CollectionUtils.isEmpty(nginxHosts)) {
  157 + return new BaseResponse(500, cloudName + "在cmdb中未找到对应的nginx服务器!");
  158 + }
  159 + List<String> ips = new ArrayList<String>();
  160 + for(HostInfo hostInfo : nginxHosts){
  161 + ips.add(hostInfo.getHostIp());
  162 + }
  163 + String tempPath = httpRequest.getSession().getServletContext().getRealPath("/") + "java-nginx/" + cloudName + "/nginx.conf";
  164 + return new BaseResponse(200,"success", exe("switch",StringUtils.join(ips,","),tempPath));
73 } 165 }
74 166
75 /** 167 /**
76 * 执行命令 168 * 执行命令
77 * 169 *
78 - * @param cloudName  
79 * @param commod 170 * @param commod
  171 + * @param host
  172 + * @param templatePath
80 * @return 2016年5月17日下午3:26:29 173 * @return 2016年5月17日下午3:26:29
81 */ 174 */
82 - public String exe(String cloudName, String commod, String target) { 175 + public String exe(String commod, String host, String templatePath) {
83 StringBuilder commond = new StringBuilder(); 176 StringBuilder commond = new StringBuilder();
84 - commond.append(" -s " + cloudName);  
85 - commond.append(" -e " + commod);  
86 - commond.append(" -d " + target); 177 + commond.append(" " + commod).append(" " + host).append(" " + templatePath);
87 return CommodUtil.exe(CommodUtil.NGINX_SWITCH_SH, commond.toString()); 178 return CommodUtil.exe(CommodUtil.NGINX_SWITCH_SH, commond.toString());
88 } 179 }
89 180