add rewrite

This commit is contained in:
fanqq 2015-05-21 18:39:38 +08:00 committed by Mengyi Zhou
parent fe91ef61b3
commit 4ea632d0f5
3 changed files with 44 additions and 11 deletions

View file

@ -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()));
}

View file

@ -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");
}
}
}
}

View file

@ -202,7 +202,7 @@ public class NginxConfServiceImpl implements NginxConfService {
@Override
public void build(String slbName, int version) throws Exception {
Map<String, Set<String>> appNamesMap = new HashMap<>();
Map<String, Map<String,Integer>> appNamesMap = new HashMap<>();
List<ConfAppSlbActiveDo> 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<String> apps = appNamesMap.get(vs);
Map<String,Integer> 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<String, List<App>> appsMap = new HashMap<>();
for (String vs : appNamesMap.keySet()) {
List<String> l = activeConfService.getConfAppActiveContentByAppNames(appNamesMap.get(vs).toArray(new String[]{}));
appsMap.put(vs, Lists.transform(l, new Function<String, App>() {
final Map<String,Integer> appPriorityMap = appNamesMap.get(vs);
List<String> l = activeConfService.getConfAppActiveContentByAppNames(appPriorityMap.keySet().toArray(new String[]{}));
List<App> appList = Lists.transform(l, new Function<String, App>() {
@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<App>(){
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);