From 4ea632d0f50adb71e4f32fdb163ffeac1a850440 Mon Sep 17 00:00:00 2001 From: fanqq Date: Thu, 21 May 2015 18:39:38 +0800 Subject: [PATCH] add rewrite --- .../Activate/impl/ActivateServiceImpl.java | 1 + .../zeus/service/build/conf/LocationConf.java | 29 +++++++++++++++++-- .../build/impl/NginxConfServiceImpl.java | 25 +++++++++++----- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/ctrip/zeus/service/Activate/impl/ActivateServiceImpl.java b/src/main/java/com/ctrip/zeus/service/Activate/impl/ActivateServiceImpl.java index 79fe4844..a4e62bce 100644 --- a/src/main/java/com/ctrip/zeus/service/Activate/impl/ActivateServiceImpl.java +++ b/src/main/java/com/ctrip/zeus/service/Activate/impl/ActivateServiceImpl.java @@ -82,6 +82,7 @@ public class ActivateServiceImpl implements ActivateService { { confAppSlbActiveDao.insert(new ConfAppSlbActiveDo().setAppName(name) .setSlbVirtualServerName(appSlb.getVirtualServer().getName()) + .setPriority(appSlb.getPriority()) .setSlbName(appSlb.getSlbName()).setLastModified(new Date())); } diff --git a/src/main/java/com/ctrip/zeus/service/build/conf/LocationConf.java b/src/main/java/com/ctrip/zeus/service/build/conf/LocationConf.java index 7772fa35..30cc6709 100644 --- a/src/main/java/com/ctrip/zeus/service/build/conf/LocationConf.java +++ b/src/main/java/com/ctrip/zeus/service/build/conf/LocationConf.java @@ -15,12 +15,10 @@ public class LocationConf { StringBuilder b = new StringBuilder(1024); b.append("location ").append(getPath(slb, vs, app)).append("{\n"); - - b.append("proxy_set_header Host $host").append(";\n"); b.append("set $upstream ").append(upstreamName).append(";\n"); + addRewriteCommand(b,slb,vs,app); b.append("proxy_pass http://$upstream ;\n"); -// b.append("proxy_pass http://").append(upstreamName).append(";\n"); b.append("proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n"); b.append("proxy_set_header X-Real-IP $remote_addr;"); @@ -40,4 +38,29 @@ public class LocationConf { AssertUtils.isNull(res,"Location path is null,Please check your configuration of SlbName:["+slb.getName()+"] VirtualServer :["+vs.getName()+"]"); return res; } + + private static String getRewrite(Slb slb, VirtualServer vs, App app) throws Exception{ + String res=null; + for (AppSlb appSlb : app.getAppSlbs()) { + if (slb.getName().equals(appSlb.getSlbName()) && vs.getName().equals(appSlb.getVirtualServer().getName())) { + res= appSlb.getRewrite(); + } + } + + return res; + } + + private static void addRewriteCommand(StringBuilder sb, Slb slb , VirtualServer vs , App app) throws Exception { + if (sb != null){ + String rewrite = getRewrite(slb,vs,app); + if (rewrite==null){ + return; + } + String[] rewrites = rewrite.split(";"); + for (int i = 0 ; i < rewrites.length ; i ++) + { + sb.append("rewrite ").append(rewrites[i]).append(" break;\n"); + } + } + } } diff --git a/src/main/java/com/ctrip/zeus/service/build/impl/NginxConfServiceImpl.java b/src/main/java/com/ctrip/zeus/service/build/impl/NginxConfServiceImpl.java index 8cf6e2fc..ad884568 100644 --- a/src/main/java/com/ctrip/zeus/service/build/impl/NginxConfServiceImpl.java +++ b/src/main/java/com/ctrip/zeus/service/build/impl/NginxConfServiceImpl.java @@ -202,7 +202,7 @@ public class NginxConfServiceImpl implements NginxConfService { @Override public void build(String slbName, int version) throws Exception { - Map> appNamesMap = new HashMap<>(); + Map> appNamesMap = new HashMap<>(); List appSlbActiveList = confAppSlbActiveDao.findBySlbName(slbName,ConfAppSlbActiveEntity.READSET_FULL); @@ -213,19 +213,22 @@ public class NginxConfServiceImpl implements NginxConfService { for (ConfAppSlbActiveDo appSlb : appSlbActiveList) { String vs = appSlb.getSlbVirtualServerName(); - Set apps = appNamesMap.get(vs); + Map apps = appNamesMap.get(vs); if (apps==null) { - apps = new HashSet<>(); + apps = new HashMap<>(); appNamesMap.put(vs,apps); } - apps.add(appSlb.getAppName()); + apps.put(appSlb.getAppName(),appSlb.getPriority()); } + Map> appsMap = new HashMap<>(); for (String vs : appNamesMap.keySet()) { - List l = activeConfService.getConfAppActiveContentByAppNames(appNamesMap.get(vs).toArray(new String[]{})); - appsMap.put(vs, Lists.transform(l, new Function() { + final Map appPriorityMap = appNamesMap.get(vs); + + List l = activeConfService.getConfAppActiveContentByAppNames(appPriorityMap.keySet().toArray(new String[]{})); + List appList = Lists.transform(l, new Function() { @Nullable @Override public App apply(@Nullable String content) { @@ -233,10 +236,16 @@ public class NginxConfServiceImpl implements NginxConfService { return DefaultSaxParser.parseEntity(App.class, content); } catch (Exception e) { e.printStackTrace(); - throw new RuntimeException("DefaultSaxParser fail! Class: App ,Content: ["+content+']',e); + throw new RuntimeException("DefaultSaxParser fail! Class: App ,Content: [" + content + ']', e); } } - })); + }); + Collections.sort(appList,new Comparator(){ + public int compare(App app0, App app1) { + return appPriorityMap.get(app0.getName())-appPriorityMap.get(app1.getName()); + } + }); + appsMap.put(vs, appList); } String slbContent =activeConfService.getConfSlbActiveContentBySlbNames(slbName);