From a7935d1427a6eabfa3c5e4905a333f9977f98e7e Mon Sep 17 00:00:00 2001 From: fanqq Date: Mon, 23 Nov 2015 16:29:56 +0800 Subject: [PATCH 1/9] add $proxy_host ; add keepalive settings --- .../zeus/service/build/conf/LocationConf.java | 19 ++++++++++++++ .../zeus/service/build/conf/NginxConf.java | 2 +- .../service/build/conf/UpstreamsConf.java | 25 ++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) 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 dd009122..14b14841 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 @@ -28,6 +28,7 @@ public class LocationConf { private static DynamicStringProperty errorPage_500 = DynamicPropertyFactory.getInstance().getStringProperty("errorPage.500.url", null);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); private static DynamicBooleanProperty errorPageEnable = DynamicPropertyFactory.getInstance().getBooleanProperty("errorPage.enable", false);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); private static DynamicBooleanProperty errorPageEnableAll = DynamicPropertyFactory.getInstance().getBooleanProperty("errorPage.enable-all", false);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); + private static DynamicStringProperty upstreamKeepAlive = DynamicPropertyFactory.getInstance().getStringProperty("upstream.keep-alive", null);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); public static String generate(Slb slb, VirtualServer vs, Group group, String upstreamName)throws Exception { StringBuilder b = new StringBuilder(1024); @@ -53,6 +54,7 @@ public class LocationConf { b.append("proxy_set_header Host $host").append(";\n"); b.append("proxy_set_header X-Real-IP $remote_addr;\n"); + addKeepAliveSettings(b,group.getId()); addProxyReadTimeout(group.getId(),b); @@ -101,6 +103,23 @@ public class LocationConf { return b.toString(); } + private static void addKeepAliveSettings(StringBuilder b,Long groupId) { + String tmp = upstreamKeepAlive.get(); + if (tmp==null||tmp.trim().equals("")){ + return; + }else if (tmp.equals("All")){ + b.append("proxy_set_header Connection \"\";\n"); + }else { + String[] pairs = tmp.split(";"); + for (String pair : pairs){ + String[] t = pair.split("="); + if (t.length==2&&t[0].trim().equals(String.valueOf(groupId))){ + b.append("proxy_set_header Connection \"\";\n"); + } + } + } + } + private static void addProxyReadTimeout(Long gid , StringBuilder sb) { String defaultConfig = proxyTimeoutDefault.get(); String config = proxyTimeoutList.get(); diff --git a/src/main/java/com/ctrip/zeus/service/build/conf/NginxConf.java b/src/main/java/com/ctrip/zeus/service/build/conf/NginxConf.java index 505996c0..f392c139 100644 --- a/src/main/java/com/ctrip/zeus/service/build/conf/NginxConf.java +++ b/src/main/java/com/ctrip/zeus/service/build/conf/NginxConf.java @@ -24,7 +24,7 @@ public class NginxConf { "'\"$query_string\" $server_port $remote_user $remote_addr $http_x_forwarded_for '\n" + "'$server_protocol \"$http_user_agent\" \"$cookie_COOKIE\" \"$http_referer\" '\n" + "'$host $status $body_bytes_sent $request_time $upstream_response_time '\n" + - "'$upstream_addr $upstream_status';\n" + "'$upstream_addr $upstream_status $proxy_host';\n" ); diff --git a/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java b/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java index 0a91f236..11c7ce00 100644 --- a/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java +++ b/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java @@ -6,6 +6,9 @@ import com.ctrip.zeus.model.entity.Slb; import com.ctrip.zeus.model.entity.VirtualServer; import com.ctrip.zeus.util.AssertUtils; import com.ctrip.zeus.util.StringFormat; +import com.netflix.config.DynamicIntProperty; +import com.netflix.config.DynamicPropertyFactory; +import com.netflix.config.DynamicStringProperty; import java.util.ArrayList; import java.util.List; @@ -16,6 +19,9 @@ import java.util.Set; * @date: 3/10/2015. */ public class UpstreamsConf { + private static DynamicStringProperty upstreamKeepAlive = DynamicPropertyFactory.getInstance().getStringProperty("upstream.keep-alive", null);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); + private static DynamicIntProperty upstreamKeepAliveDefault = DynamicPropertyFactory.getInstance().getIntProperty("upstream.keep-alive.default", 100);//"http://slberrorpages.ctripcorp.com/slberrorpages/500.htm"); + public static String generate(Slb slb, VirtualServer vs, List groups, Set allDownServers, Set allUpGroupServers)throws Exception { StringBuilder b = new StringBuilder(10240); @@ -79,11 +85,28 @@ public class UpstreamsConf { .append(isDown?" down":"") .append(";\n"); } - + addKeepAliveSetting(b,group.getId()); //HealthCheck b.append(HealthCheckConf.generate(slb, vs, group)); return b.toString(); } + private static void addKeepAliveSetting(StringBuilder b, Long gid) { + String tmp = upstreamKeepAlive.get(); + if (tmp==null||tmp.trim().equals("")){ + return; + }else if (tmp.equals("All")){ + b.append("keepalive ").append(upstreamKeepAliveDefault.get()).append(";\n"); + }else { + String[] pairs = tmp.split(";"); + for (String pair : pairs){ + String[] t = pair.split("="); + if (t.length==2&&t[0].trim().equals(String.valueOf(gid))){ + b.append("keepalive ").append(t[1]).append(";\n"); + } + } + } + } + } From ce5db58e8f67ca674a1a8f3fe58ab3d7c8112eee Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Mon, 23 Nov 2015 11:08:07 +0800 Subject: [PATCH 2/9] add root path order --- src/main/java/com/ctrip/zeus/service/model/AutoFiller.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java index 3117bf1a..4986eb39 100644 --- a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java +++ b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java @@ -12,6 +12,7 @@ import javax.annotation.Resource; public class AutoFiller { @Resource private VirtualServerRepository virtualServerRepository; + private static final String RegexRootPath = " /"; public void autofill(Group group) throws Exception { for (GroupVirtualServer gvs : group.getGroupVirtualServers()) { @@ -23,7 +24,10 @@ public class AutoFiller { tvs.getDomains().add(domain); } if (gvs.getPriority() == null) { - gvs.setPriority(gvs.getRewrite() == null ? 1000 : -1000); + if (gvs.getPath().endsWith(RegexRootPath)) + gvs.setPriority(Integer.MIN_VALUE); + else + gvs.setPriority(gvs.getRewrite() == null ? 1000 : -1000); } } HealthCheck hc = group.getHealthCheck(); From f81189fcff5c391395bc28fa02ca18dea38a769b Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Mon, 23 Nov 2015 12:11:25 +0800 Subject: [PATCH 3/9] add virtual group model field and db table --- .../META-INF/dal/jdbc/core-codegen.xml | 39 ++++++++++++++++++- .../resources/META-INF/dal/jdbc/rel-group.xml | 14 ++++++- .../META-INF/dal/model/model-codegen.xml | 1 + .../resources/META-INF/plexus/components.xml | 18 +++++++++ .../resources/META-INF/wizard/jdbc/wizard.xml | 1 + src/main/resources/dao-beans.xml | 4 ++ .../resources/com/ctrip/zeus/model/model.xml | 1 + 7 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/main/resources/META-INF/dal/jdbc/core-codegen.xml b/src/main/resources/META-INF/dal/jdbc/core-codegen.xml index 84a72ea7..96ebd42e 100644 --- a/src/main/resources/META-INF/dal/jdbc/core-codegen.xml +++ b/src/main/resources/META-INF/dal/jdbc/core-codegen.xml @@ -1227,7 +1227,44 @@ - + + + + + + + + + + + + + + + + + + FROM + WHERE = ${key-id}]]> + + + () + VALUES()]]> + + + + + SET + WHERE = ${key-id}]]> + + + + + WHERE = ${key-id}]]> + + + + diff --git a/src/main/resources/META-INF/dal/jdbc/rel-group.xml b/src/main/resources/META-INF/dal/jdbc/rel-group.xml index 49b74f45..3bc8679f 100644 --- a/src/main/resources/META-INF/dal/jdbc/rel-group.xml +++ b/src/main/resources/META-INF/dal/jdbc/rel-group.xml @@ -1,6 +1,18 @@ - + + + + + + FROM
+ ]]> + + + + + diff --git a/src/main/resources/META-INF/dal/model/model-codegen.xml b/src/main/resources/META-INF/dal/model/model-codegen.xml index 4bf2fcf7..94f9a692 100644 --- a/src/main/resources/META-INF/dal/model/model-codegen.xml +++ b/src/main/resources/META-INF/dal/model/model-codegen.xml @@ -62,6 +62,7 @@ + diff --git a/src/main/resources/META-INF/plexus/components.xml b/src/main/resources/META-INF/plexus/components.xml index 59fbd243..ab6a59cb 100644 --- a/src/main/resources/META-INF/plexus/components.xml +++ b/src/main/resources/META-INF/plexus/components.xml @@ -278,6 +278,15 @@ zeus + + org.unidal.dal.jdbc.mapping.TableProvider + r-group-vg + org.unidal.dal.jdbc.mapping.SimpleTableProvider + + r_group_vg + zeus + + org.unidal.dal.jdbc.mapping.TableProvider r-group-vs @@ -701,6 +710,15 @@ + + com.ctrip.zeus.dal.core.RGroupVgDao + com.ctrip.zeus.dal.core.RGroupVgDao + + + org.unidal.dal.jdbc.QueryEngine + + + com.ctrip.zeus.dal.core.RGroupVsDao com.ctrip.zeus.dal.core.RGroupVsDao diff --git a/src/main/resources/META-INF/wizard/jdbc/wizard.xml b/src/main/resources/META-INF/wizard/jdbc/wizard.xml index dc28f41f..5b37cf50 100644 --- a/src/main/resources/META-INF/wizard/jdbc/wizard.xml +++ b/src/main/resources/META-INF/wizard/jdbc/wizard.xml @@ -56,6 +56,7 @@
+
diff --git a/src/main/resources/dao-beans.xml b/src/main/resources/dao-beans.xml index 2ecaeaca..ed44f24e 100644 --- a/src/main/resources/dao-beans.xml +++ b/src/main/resources/dao-beans.xml @@ -132,6 +132,10 @@ + + + + diff --git a/src/test/resources/com/ctrip/zeus/model/model.xml b/src/test/resources/com/ctrip/zeus/model/model.xml index 841ebdea..dcd909f4 100644 --- a/src/test/resources/com/ctrip/zeus/model/model.xml +++ b/src/test/resources/com/ctrip/zeus/model/model.xml @@ -41,6 +41,7 @@ /hotel ^/regex + /fullurl From 47869cb5a92f0c188ae2ad4a8e075fa1d48f34f5 Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Mon, 23 Nov 2015 12:12:20 +0800 Subject: [PATCH 4/9] impl virtual group logics --- .../zeus/restful/resource/GroupResource.java | 102 ++++++++++++++++++ .../zeus/service/model/GroupRepository.java | 6 ++ .../zeus/service/model/handler/GroupSync.java | 2 + .../handler/impl/GroupEntityManager.java | 14 +++ .../model/handler/impl/GroupSyncImpl.java | 5 + .../model/impl/GroupRepositoryImpl.java | 45 +++++--- .../service/query/GroupCriteriaQuery.java | 2 + .../query/impl/DefaultGroupCriteriaQuery.java | 11 ++ 8 files changed, 174 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java b/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java index 63d5f4d0..f8f7b14f 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java @@ -131,6 +131,72 @@ public class GroupResource { return input; } }) + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + input.removeAll(groupCriteriaQuery.queryAllVGroups()); + return input; + } + }) + .build(); + for (Group group : groupRepository.list(executer.run())) { + groupList.addGroup(getGroupByType(group, type)); + } + groupList.setTotal(groupList.getGroups().size()); + return responseHandler.handle(groupList, hh.getMediaType()); + } + + @GET + @Path("/vgroups") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + @Authorize(name = "getAllGroups") + public Response listVGroups(@Context HttpHeaders hh, + @Context HttpServletRequest request, + @QueryParam("slbId") final Long slbId, + @TrimmedQueryParam("domain") final String domain, + @TrimmedQueryParam("type") String type, + @TrimmedQueryParam("tag") final String tag, + @TrimmedQueryParam("pname") final String pname, + @TrimmedQueryParam("pvalue") final String pvalue) throws Exception { + GroupList groupList = new GroupList(); + QueryExecuter executer = new QueryExecuter.Builder() + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + return groupCriteriaQuery.queryAllVGroups(); + } + }) + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + if (tag != null) { + input.retainAll(tagService.query(tag, "group")); + } + return input; + } + }) + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + if (pname != null) { + if (pvalue != null) + input.retainAll(propertyService.query(pname, pvalue, "group")); + else + input.retainAll(propertyService.query(pname, "group")); + } + return input; + } + }) + .addFilterId(new FilterSet() { + @Override + public Set filter(Set input) throws Exception { + if (domain != null) { + Set vsIds = virtualServerCriteriaQuery.queryByDomain(domain); + input.retainAll(groupCriteriaQuery.queryByVsIds(vsIds.toArray(new Long[vsIds.size()]))); + } + return input; + } + }) .build(); for (Group group : groupRepository.list(executer.run())) { groupList.addGroup(getGroupByType(group, type)); @@ -172,6 +238,15 @@ public class GroupResource { return responseHandler.handle(g, hh.getMediaType()); } + @POST + @Path("/vgroup/new") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "*/*"}) + @Authorize(name = "addGroup") + public Response addVGroup(@Context HttpHeaders hh, @Context HttpServletRequest request, String group) throws Exception { + Group g = groupRepository.addVGroup(parseGroup(hh.getMediaType(), group)); + return responseHandler.handle(g, hh.getMediaType()); + } + @POST @Path("/group/update") @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "*/*"}) @@ -188,6 +263,22 @@ public class GroupResource { return responseHandler.handle(g, hh.getMediaType()); } + @POST + @Path("/vgroup/update") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, "*/*"}) + @Authorize(name = "updateGroup") + public Response updateVGroup(@Context HttpHeaders hh, @Context HttpServletRequest request, String group) throws Exception { + Group g = parseGroup(hh.getMediaType(), group); + DistLock lock = dbLockFactory.newLock(g.getName() + "_updateGroup"); + try { + lock.lock(); + g = groupRepository.updateVGroup(g); + } finally { + lock.unlock(); + } + return responseHandler.handle(g, hh.getMediaType()); + } + @GET @Path("/group/delete") @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @@ -199,6 +290,17 @@ public class GroupResource { return Response.ok().build(); } + @GET + @Path("/vgroup/delete") + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + @Authorize(name = "deleteGroup") + public Response deleteVGroup(@Context HttpHeaders hh, @Context HttpServletRequest request, @QueryParam("groupId") Long groupId) throws Exception { + if (groupId == null) + throw new Exception("Missing parameter."); + groupRepository.deleteVGroup(groupId); + return Response.ok().build(); + } + @GET @Path("/group/upgradeAll") @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) diff --git a/src/main/java/com/ctrip/zeus/service/model/GroupRepository.java b/src/main/java/com/ctrip/zeus/service/model/GroupRepository.java index 6a717bf3..44c0b195 100644 --- a/src/main/java/com/ctrip/zeus/service/model/GroupRepository.java +++ b/src/main/java/com/ctrip/zeus/service/model/GroupRepository.java @@ -17,12 +17,18 @@ public interface GroupRepository extends Repository { Group add(Group group) throws Exception; + Group addVGroup(Group group) throws Exception; + Group update(Group group) throws Exception; + Group updateVGroup(Group group) throws Exception; + List updateVersion(Long[] groupIds) throws Exception; int delete(Long groupId) throws Exception; + int deleteVGroup(Long groupId) throws Exception; + @Deprecated List listGroupsByGroupServer(String groupServerIp) throws Exception; diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/GroupSync.java b/src/main/java/com/ctrip/zeus/service/model/handler/GroupSync.java index 74497a4e..34d31d0b 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/GroupSync.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/GroupSync.java @@ -12,6 +12,8 @@ public interface GroupSync { void add(Group group) throws Exception; + void add(Group group, boolean isVirtual) throws Exception; + void update(Group group) throws Exception; void updateVersion(Long[] groupIds) throws Exception; diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java index 0c8792d7..26b8d1c8 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java @@ -29,6 +29,8 @@ public class GroupEntityManager implements GroupSync { private RGroupVsDao rGroupVsDao; @Resource private RGroupGsDao rGroupGsDao; + @Resource + private RGroupVgDao rGroupVgDao; @Override public void add(Group group) throws Exception { @@ -41,6 +43,13 @@ public class GroupEntityManager implements GroupSync { relSyncGs(group, true); } + @Override + public void add(Group group, boolean isVirtual) throws Exception { + add(group); + if (isVirtual) + relSyncVg(group); + } + @Override public void update(Group group) throws Exception { GroupDo check = groupDao.findById(group.getId(), GroupEntity.READSET_FULL); @@ -64,6 +73,7 @@ public class GroupEntityManager implements GroupSync { public int delete(Long groupId) throws Exception { rGroupVsDao.deleteAllByGroup(new RelGroupVsDo().setGroupId(groupId)); rGroupGsDao.deleteAllByGroup(new RelGroupGsDo().setGroupId(groupId)); + rGroupVgDao.deleteByPK(new RelGroupVgDo().setGroupId(groupId)); int count = groupDao.deleteById(new GroupDo().setId(groupId)); archiveGroupDao.deleteByGroup(new ArchiveGroupDo().setGroupId(groupId)); return count; @@ -129,6 +139,10 @@ public class GroupEntityManager implements GroupSync { rGroupGsDao.insert(dos); } + private void relSyncVg(Group group) throws DalException { + rGroupVgDao.insert(new RelGroupVgDo().setGroupId(group.getId())); + } + private void relSyncVs(Group group, boolean isnew) throws DalException { if (isnew) { for (GroupVirtualServer groupVirtualServer : group.getGroupVirtualServers()) { diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupSyncImpl.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupSyncImpl.java index 524bf562..8bec5ff7 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupSyncImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupSyncImpl.java @@ -38,6 +38,11 @@ public class GroupSyncImpl implements GroupSync { cascadeSync(group); } + @Override + public void add(Group group, boolean isVirtual) throws Exception { + add(group); + } + @Override public void update(Group group) throws Exception { GroupDo check = groupDao.findById(group.getId(), GroupEntity.READSET_FULL); diff --git a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java index 0ee95a33..7dfba355 100644 --- a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java @@ -28,12 +28,6 @@ public class GroupRepositoryImpl implements GroupRepository { @Resource private GroupValidator groupModelValidator; - @Override - public List list(Long slbId) throws Exception { - Set groupIds = groupCriteriaQuery.queryBySlbId(slbId); - return list(groupIds.toArray(new Long[groupIds.size()])); - } - @Override public List list(Long[] ids) throws Exception { List result = archiveService.getLatestGroups(ids); @@ -54,16 +48,13 @@ public class GroupRepositoryImpl implements GroupRepository { } @Override - public Group get(String groupName) throws Exception { - return getById(groupCriteriaQuery.queryByName(groupName)); + public Group add(Group group) throws Exception { + return add(group, false); } @Override - public Group add(Group group) throws Exception { - groupModelValidator.validate(group); - autoFiller.autofill(group); - groupEntityManager.add(group); - return group; + public Group addVGroup(Group group) throws Exception { + return add(group, true); } @Override @@ -76,6 +67,11 @@ public class GroupRepositoryImpl implements GroupRepository { return group; } + @Override + public Group updateVGroup(Group group) throws Exception { + return update(group); + } + // this would be called iff virtual servers are modified @Override public List updateVersion(Long[] groupIds) throws Exception { @@ -95,6 +91,11 @@ public class GroupRepositoryImpl implements GroupRepository { return groupEntityManager.delete(groupId); } + @Override + public int deleteVGroup(Long groupId) throws Exception { + return delete(groupId); + } + @Override public List listGroupsByGroupServer(String groupServerIp) throws Exception { Set groupIds = groupCriteriaQuery.queryByGroupServerIp(groupServerIp); @@ -114,4 +115,22 @@ public class GroupRepositoryImpl implements GroupRepository { Group group = getById(groupId); groupEntityManager.port(group); } + + @Override + public Group get(String groupName) throws Exception { + return getById(groupCriteriaQuery.queryByName(groupName)); + } + + @Override + public List list(Long slbId) throws Exception { + Set groupIds = groupCriteriaQuery.queryBySlbId(slbId); + return list(groupIds.toArray(new Long[groupIds.size()])); + } + + private Group add(Group group, boolean isVirtual) throws Exception { + groupModelValidator.validate(group); + autoFiller.autofill(group); + groupEntityManager.add(group, isVirtual); + return group; + } } diff --git a/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java index e5f73ef3..9dd8dac3 100644 --- a/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java @@ -20,4 +20,6 @@ public interface GroupCriteriaQuery { Set queryByVsIds(Long[] vsIds) throws Exception; Set queryByGroupServerIp(String ip) throws Exception; + + Set queryAllVGroups() throws Exception; } diff --git a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java index 6d0f635a..6237fe39 100644 --- a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java @@ -23,6 +23,8 @@ public class DefaultGroupCriteriaQuery implements GroupCriteriaQuery { private RGroupGsDao rGroupGsDao; @Resource private RVsSlbDao rVsSlbDao; + @Resource + private RGroupVgDao rGroupVgDao; @Override public Long queryByName(String name) throws Exception { @@ -83,4 +85,13 @@ public class DefaultGroupCriteriaQuery implements GroupCriteriaQuery { } return groupIds; } + + @Override + public Set queryAllVGroups() throws Exception { + Set groupIds = new HashSet<>(); + for (RelGroupVgDo relGroupVgDo : rGroupVgDao.findAll(RGroupVgEntity.READSET_FULL)) { + groupIds.add(relGroupVgDo.getGroupId()); + } + return groupIds; + } } From 700c2f9563b827c902537b494ff7a2dc262e77de Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Mon, 23 Nov 2015 16:22:08 +0800 Subject: [PATCH 5/9] autofill vgroup --- .../zeus/restful/resource/GroupResource.java | 31 +++++++++++-------- .../ctrip/zeus/service/model/AutoFiller.java | 21 +++++++++++++ .../model/impl/GroupRepositoryImpl.java | 24 ++++++++------ 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java b/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java index f8f7b14f..ea2c49bc 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java @@ -151,13 +151,13 @@ public class GroupResource { @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @Authorize(name = "getAllGroups") public Response listVGroups(@Context HttpHeaders hh, - @Context HttpServletRequest request, - @QueryParam("slbId") final Long slbId, - @TrimmedQueryParam("domain") final String domain, - @TrimmedQueryParam("type") String type, - @TrimmedQueryParam("tag") final String tag, - @TrimmedQueryParam("pname") final String pname, - @TrimmedQueryParam("pvalue") final String pvalue) throws Exception { + @Context HttpServletRequest request, + @QueryParam("slbId") final Long slbId, + @TrimmedQueryParam("domain") final String domain, + @TrimmedQueryParam("type") String type, + @TrimmedQueryParam("tag") final String tag, + @TrimmedQueryParam("pname") final String pname, + @TrimmedQueryParam("pvalue") final String pvalue) throws Exception { GroupList groupList = new GroupList(); QueryExecuter executer = new QueryExecuter.Builder() .addFilterId(new FilterSet() { @@ -336,18 +336,23 @@ public class GroupResource { throw new Exception("Group cannot be parsed."); } } - g.setAppId(g.getAppId().trim()); - g.setName(g.getName().trim()); + g.setAppId(trimIfNotNull(g.getAppId())); + g.setName(trimIfNotNull(g.getName())); if (g.getHealthCheck() != null) - g.getHealthCheck().setUri(g.getHealthCheck().getUri().trim()); + g.getHealthCheck().setUri(trimIfNotNull(g.getHealthCheck().getUri())); for (GroupServer groupServer : g.getGroupServers()) { - groupServer.setIp(groupServer.getIp().trim()); - groupServer.setHostName(groupServer.getHostName().trim()); + groupServer.setIp(trimIfNotNull(groupServer.getIp())); + groupServer.setHostName(trimIfNotNull(groupServer.getHostName())); } - g.getLoadBalancingMethod().setValue(g.getLoadBalancingMethod().getValue()); + if (g.getLoadBalancingMethod() != null) + g.getLoadBalancingMethod().setValue(trimIfNotNull(g.getLoadBalancingMethod().getValue())); return g; } + private String trimIfNotNull(String value) { + return value != null ? value.trim() : value; + } + private Group getGroupByType(Group group, String type) { if ("INFO".equalsIgnoreCase(type)) { return new Group().setId(group.getId()) diff --git a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java index 4986eb39..7ad38bc8 100644 --- a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java +++ b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java @@ -48,6 +48,27 @@ public class AutoFiller { } } + public void autofillVGroup(Group group) throws Exception { + for (GroupVirtualServer gvs : group.getGroupVirtualServers()) { + VirtualServer tvs = gvs.getVirtualServer(); + VirtualServer vs = virtualServerRepository.getById(gvs.getVirtualServer().getId()); + tvs.setName(vs.getName()).setSlbId(vs.getSlbId()).setPort(vs.getPort()).setSsl(vs.getSsl()); + tvs.getDomains().clear(); + for (Domain domain : vs.getDomains()) { + tvs.getDomains().add(domain); + } + if (gvs.getPriority() == null) { + if (gvs.getPath().endsWith(RegexRootPath)) + gvs.setPriority(Integer.MIN_VALUE); + else + gvs.setPriority(gvs.getRewrite() == null ? 1000 : -1000); + } + } + group.setHealthCheck(null); + group.setLoadBalancingMethod(null); + group.getGroupServers().clear(); + } + public void autofill(Slb slb) throws Exception { slb.setNginxBin("/opt/app/nginx/sbin").setNginxConf("/opt/app/nginx/conf").setNginxWorkerProcesses(9) .setStatus(slb.getStatus() == null ? "Default" : slb.getStatus()); diff --git a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java index 7dfba355..72fb5c7c 100644 --- a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java @@ -49,12 +49,18 @@ public class GroupRepositoryImpl implements GroupRepository { @Override public Group add(Group group) throws Exception { - return add(group, false); + groupModelValidator.validate(group); + autoFiller.autofill(group); + groupEntityManager.add(group, false); + return group; } @Override public Group addVGroup(Group group) throws Exception { - return add(group, true); + groupModelValidator.validate(group); + autoFiller.autofillVGroup(group); + groupEntityManager.add(group, false); + return group; } @Override @@ -69,7 +75,12 @@ public class GroupRepositoryImpl implements GroupRepository { @Override public Group updateVGroup(Group group) throws Exception { - return update(group); + if (!groupModelValidator.exists(group.getId())) + throw new ValidationException("Group with id " + group.getId() + "does not exist."); + groupModelValidator.validate(group); + autoFiller.autofillVGroup(group); + groupEntityManager.update(group); + return group; } // this would be called iff virtual servers are modified @@ -126,11 +137,4 @@ public class GroupRepositoryImpl implements GroupRepository { Set groupIds = groupCriteriaQuery.queryBySlbId(slbId); return list(groupIds.toArray(new Long[groupIds.size()])); } - - private Group add(Group group, boolean isVirtual) throws Exception { - groupModelValidator.validate(group); - autoFiller.autofill(group); - groupEntityManager.add(group, isVirtual); - return group; - } } From 2d4aebb71e078cb83ab852cd697a8ac79ed157cb Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Mon, 23 Nov 2015 17:29:13 +0800 Subject: [PATCH 6/9] add vgroupvalidator --- .../model/handler/VGroupValidator.java | 9 +++ .../handler/impl/DefaultGroupValidator.java | 5 +- .../handler/impl/DefaultVGroupValidator.java | 55 +++++++++++++++++++ .../handler/impl/GroupEntityManager.java | 5 +- .../model/impl/GroupRepositoryImpl.java | 16 ++++-- .../resources/META-INF/dal/jdbc/rel-group.xml | 14 ++++- 6 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/ctrip/zeus/service/model/handler/VGroupValidator.java create mode 100644 src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/VGroupValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/VGroupValidator.java new file mode 100644 index 00000000..a4f3cff1 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/model/handler/VGroupValidator.java @@ -0,0 +1,9 @@ +package com.ctrip.zeus.service.model.handler; + +import com.ctrip.zeus.model.entity.Group; + +/** + * Created by zhoumy on 2015/11/23. + */ +public interface VGroupValidator extends ModelValidator { +} diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java index 8f8e7e76..999532d1 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java @@ -32,11 +32,14 @@ public class DefaultGroupValidator implements GroupValidator { @Resource private RGroupVsDao rGroupVsDao; @Resource + private RGroupVgDao rGroupVgDao; + @Resource private GroupDao groupDao; @Override public boolean exists(Long targetId) throws Exception { - return groupDao.findById(targetId, GroupEntity.READSET_FULL) != null; + return groupDao.findById(targetId, GroupEntity.READSET_FULL) != null + && rGroupVgDao.findByGroup(targetId, RGroupVgEntity.READSET_FULL) == null; } @Override diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java new file mode 100644 index 00000000..44eb1522 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultVGroupValidator.java @@ -0,0 +1,55 @@ +package com.ctrip.zeus.service.model.handler.impl; + +import com.ctrip.zeus.dal.core.GroupDao; +import com.ctrip.zeus.dal.core.GroupEntity; +import com.ctrip.zeus.dal.core.RGroupVgDao; +import com.ctrip.zeus.dal.core.RGroupVgEntity; +import com.ctrip.zeus.exceptions.ValidationException; +import com.ctrip.zeus.model.entity.Group; +import com.ctrip.zeus.model.entity.GroupVirtualServer; +import com.ctrip.zeus.service.model.handler.GroupValidator; +import com.ctrip.zeus.service.model.handler.VGroupValidator; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * Created by zhoumy on 2015/11/23. + */ +@Component("vGroupValidator") +public class DefaultVGroupValidator implements VGroupValidator { + @Resource + private RGroupVgDao rGroupVgDao; + @Resource + private GroupDao groupDao; + @Resource + private GroupValidator groupModelValidator; + + @Override + public boolean exists(Long targetId) throws Exception { + return groupDao.findById(targetId, GroupEntity.READSET_FULL) != null + && rGroupVgDao.findByGroup(targetId, RGroupVgEntity.READSET_FULL) != null; + } + + @Override + public void validate(Group target) throws Exception { + if (target.getName() == null || target.getName().isEmpty()) { + throw new ValidationException("Group name is required."); + } + groupModelValidator.validateGroupVirtualServers(target.getId(), target.getGroupVirtualServers()); + for (GroupVirtualServer groupVirtualServer : target.getGroupVirtualServers()) { + if (groupVirtualServer.getRedirect() == null) + throw new ValidationException("Redirect value is required."); + } + } + + @Override + public void checkVersion(Group target) throws Exception { + groupModelValidator.checkVersion(target); + } + + @Override + public void removable(Long targetId) throws Exception { + groupModelValidator.removable(targetId); + } +} diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java index 26b8d1c8..e46f7789 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java @@ -35,7 +35,8 @@ public class GroupEntityManager implements GroupSync { @Override public void add(Group group) throws Exception { group.setVersion(1); - GroupDo d = C.toGroupDo(0L, group); + // App id cannot be null + GroupDo d = C.toGroupDo(0L, group).setAppId("VirtualGroup"); groupDao.insert(d); group.setId(d.getId()); archiveGroupDao.insert(new ArchiveGroupDo().setGroupId(group.getId()).setVersion(group.getVersion()).setContent(ContentWriters.writeGroupContent(group))); @@ -57,7 +58,7 @@ public class GroupEntityManager implements GroupSync { throw new ValidationException("Newer Group version is detected."); group.setVersion(group.getVersion() + 1); - GroupDo d = C.toGroupDo(group.getId(), group); + GroupDo d = C.toGroupDo(group.getId(), group).setAppId("VirtualGroup"); groupDao.updateById(d, GroupEntity.UPDATESET_FULL); archiveGroupDao.insert(new ArchiveGroupDo().setGroupId(group.getId()).setVersion(group.getVersion()).setContent(ContentWriters.writeGroupContent(group))); relSyncVs(group, false); diff --git a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java index 72fb5c7c..73a015d8 100644 --- a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java @@ -5,6 +5,7 @@ import com.ctrip.zeus.model.entity.*; import com.ctrip.zeus.service.model.*; import com.ctrip.zeus.service.model.handler.GroupSync; import com.ctrip.zeus.service.model.handler.GroupValidator; +import com.ctrip.zeus.service.model.handler.VGroupValidator; import com.ctrip.zeus.service.query.GroupCriteriaQuery; import org.springframework.stereotype.Repository; @@ -27,6 +28,8 @@ public class GroupRepositoryImpl implements GroupRepository { private ArchiveService archiveService; @Resource private GroupValidator groupModelValidator; + @Resource + private VGroupValidator vGroupValidator; @Override public List list(Long[] ids) throws Exception { @@ -57,16 +60,16 @@ public class GroupRepositoryImpl implements GroupRepository { @Override public Group addVGroup(Group group) throws Exception { - groupModelValidator.validate(group); + vGroupValidator.validate(group); autoFiller.autofillVGroup(group); - groupEntityManager.add(group, false); + groupEntityManager.add(group, true); return group; } @Override public Group update(Group group) throws Exception { if (!groupModelValidator.exists(group.getId())) - throw new ValidationException("Group with id " + group.getId() + "does not exist."); + throw new ValidationException("Group with id " + group.getId() + " does not exist."); groupModelValidator.validate(group); autoFiller.autofill(group); groupEntityManager.update(group); @@ -75,9 +78,9 @@ public class GroupRepositoryImpl implements GroupRepository { @Override public Group updateVGroup(Group group) throws Exception { - if (!groupModelValidator.exists(group.getId())) - throw new ValidationException("Group with id " + group.getId() + "does not exist."); - groupModelValidator.validate(group); + if (!vGroupValidator.exists(group.getId())) + throw new ValidationException("Group with id " + group.getId() + " does not exist."); + vGroupValidator.validate(group); autoFiller.autofillVGroup(group); groupEntityManager.update(group); return group; @@ -104,6 +107,7 @@ public class GroupRepositoryImpl implements GroupRepository { @Override public int deleteVGroup(Long groupId) throws Exception { + vGroupValidator.removable(groupId); return delete(groupId); } diff --git a/src/main/resources/META-INF/dal/jdbc/rel-group.xml b/src/main/resources/META-INF/dal/jdbc/rel-group.xml index 3bc8679f..607b10f9 100644 --- a/src/main/resources/META-INF/dal/jdbc/rel-group.xml +++ b/src/main/resources/META-INF/dal/jdbc/rel-group.xml @@ -5,8 +5,18 @@ - FROM
+ SELECT + FROM
+ ]]> + + + + + + + FROM
+ WHERE = ${group-id} ]]> From 341cea2b2bd0b63869d0ba0f33a97fb69a25c7ec Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Mon, 23 Nov 2015 18:03:27 +0800 Subject: [PATCH 7/9] bug fix --- .../model/handler/impl/GroupEntityManager.java | 6 ++++-- src/main/resources/META-INF/dal/jdbc/rel-group.xml | 2 +- src/main/resources/sql/create-tables.sql | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java index e46f7789..3239de1b 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/GroupEntityManager.java @@ -35,8 +35,10 @@ public class GroupEntityManager implements GroupSync { @Override public void add(Group group) throws Exception { group.setVersion(1); - // App id cannot be null - GroupDo d = C.toGroupDo(0L, group).setAppId("VirtualGroup"); + GroupDo d = C.toGroupDo(0L, group); + if (d.getAppId() == null) + // if app id is null, it must be virtual group + d.setAppId("VirtualGroup"); groupDao.insert(d); group.setId(d.getId()); archiveGroupDao.insert(new ArchiveGroupDo().setGroupId(group.getId()).setVersion(group.getVersion()).setContent(ContentWriters.writeGroupContent(group))); diff --git a/src/main/resources/META-INF/dal/jdbc/rel-group.xml b/src/main/resources/META-INF/dal/jdbc/rel-group.xml index 607b10f9..4682f9b9 100644 --- a/src/main/resources/META-INF/dal/jdbc/rel-group.xml +++ b/src/main/resources/META-INF/dal/jdbc/rel-group.xml @@ -10,7 +10,7 @@ ]]> - + Date: Tue, 24 Nov 2015 14:45:58 +0800 Subject: [PATCH 8/9] add virtual value for conf gen --- .../service/model/impl/GroupRepositoryImpl.java | 15 ++++++++++++++- .../META-INF/dal/model/model-codegen.xml | 1 + src/test/resources/com/ctrip/zeus/model/model.xml | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java index 73a015d8..3c6c43a4 100644 --- a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java @@ -36,15 +36,17 @@ public class GroupRepositoryImpl implements GroupRepository { List result = archiveService.getLatestGroups(ids); for (Group group : result) { autoFiller.autofill(group); + hideVirtualValue(group); } return result; } @Override public Group getById(Long id) throws Exception { - if (groupModelValidator.exists(id)) { + if (groupModelValidator.exists(id) || vGroupValidator.exists(id)) { Group result = archiveService.getLatestGroup(id); autoFiller.autofill(result); + hideVirtualValue(result); return result; } return null; @@ -54,6 +56,7 @@ public class GroupRepositoryImpl implements GroupRepository { public Group add(Group group) throws Exception { groupModelValidator.validate(group); autoFiller.autofill(group); + hideVirtualValue(group); groupEntityManager.add(group, false); return group; } @@ -62,7 +65,9 @@ public class GroupRepositoryImpl implements GroupRepository { public Group addVGroup(Group group) throws Exception { vGroupValidator.validate(group); autoFiller.autofillVGroup(group); + group.setVirtual(true); groupEntityManager.add(group, true); + hideVirtualValue(group); return group; } @@ -72,6 +77,7 @@ public class GroupRepositoryImpl implements GroupRepository { throw new ValidationException("Group with id " + group.getId() + " does not exist."); groupModelValidator.validate(group); autoFiller.autofill(group); + hideVirtualValue(group); groupEntityManager.update(group); return group; } @@ -82,7 +88,9 @@ public class GroupRepositoryImpl implements GroupRepository { throw new ValidationException("Group with id " + group.getId() + " does not exist."); vGroupValidator.validate(group); autoFiller.autofillVGroup(group); + group.setVirtual(true); groupEntityManager.update(group); + hideVirtualValue(group); return group; } @@ -93,6 +101,7 @@ public class GroupRepositoryImpl implements GroupRepository { for (Long groupId : groupIds) { Group g = getById(groupId); autoFiller.autofill(g); + hideVirtualValue(g); groupEntityManager.update(g); result.add(g); } @@ -141,4 +150,8 @@ public class GroupRepositoryImpl implements GroupRepository { Set groupIds = groupCriteriaQuery.queryBySlbId(slbId); return list(groupIds.toArray(new Long[groupIds.size()])); } + + private void hideVirtualValue(Group group) { + group.setVirtual(null); + } } diff --git a/src/main/resources/META-INF/dal/model/model-codegen.xml b/src/main/resources/META-INF/dal/model/model-codegen.xml index 94f9a692..253a0f85 100644 --- a/src/main/resources/META-INF/dal/model/model-codegen.xml +++ b/src/main/resources/META-INF/dal/model/model-codegen.xml @@ -83,6 +83,7 @@ + diff --git a/src/test/resources/com/ctrip/zeus/model/model.xml b/src/test/resources/com/ctrip/zeus/model/model.xml index dcd909f4..a6768ddb 100644 --- a/src/test/resources/com/ctrip/zeus/model/model.xml +++ b/src/test/resources/com/ctrip/zeus/model/model.xml @@ -55,7 +55,7 @@ - + From 65f936ce051c28f572333919bedea8eb2c09860b Mon Sep 17 00:00:00 2001 From: fanqq Date: Tue, 24 Nov 2015 18:17:57 +0800 Subject: [PATCH 9/9] add vg build --- .../zeus/service/build/conf/LocationConf.java | 14 +++++++++++++- .../zeus/service/build/conf/UpstreamsConf.java | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) 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 14b14841..335286a2 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 @@ -1,5 +1,6 @@ package com.ctrip.zeus.service.build.conf; +import com.ctrip.zeus.exceptions.ValidationException; import com.ctrip.zeus.model.entity.Group; import com.ctrip.zeus.model.entity.GroupVirtualServer; import com.ctrip.zeus.model.entity.Slb; @@ -32,8 +33,19 @@ public class LocationConf { public static String generate(Slb slb, VirtualServer vs, Group group, String upstreamName)throws Exception { StringBuilder b = new StringBuilder(1024); - + if (group.isVirtual()){ + b.append("location ").append(getPath(slb, vs, group)).append(" {\n"); + if (group.getGroupVirtualServers().size()==1) + { + b.append("rewrite ").append(group.getGroupVirtualServers().get(0).getRedirect()).append(" redirect;\n"); + }else { + throw new ValidationException("Virtual Group has Multiple Group VirtualServers Redirect"); + } + b.append("}\n"); + return b.toString(); + } b.append("location ").append(getPath(slb, vs, group)).append(" {\n"); + if (clientMaxSizeList.get() !=null) { String []sizeList = clientMaxSizeList.get().split(";"); diff --git a/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java b/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java index 11c7ce00..a7ba1eed 100644 --- a/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java +++ b/src/main/java/com/ctrip/zeus/service/build/conf/UpstreamsConf.java @@ -40,6 +40,9 @@ public class UpstreamsConf { } public static String buildUpstreamConf(Slb slb, VirtualServer vs, Group group, String upstreamName, Set allDownServers, Set allUpGroupServers) throws Exception { + if (group.isVirtual()){ + return ""; + } StringBuilder b = new StringBuilder(1024); String body = buildUpstreamConfBody(slb,vs,group,allDownServers,allUpGroupServers); if (null == body){