diff --git a/src/main/java/com/ctrip/zeus/restful/resource/ActivateResource.java b/src/main/java/com/ctrip/zeus/restful/resource/ActivateResource.java index 9b80b07f..1eccb9f8 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/ActivateResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/ActivateResource.java @@ -20,6 +20,7 @@ import com.ctrip.zeus.task.entity.TaskResult; import com.ctrip.zeus.task.entity.TaskResultList; import com.ctrip.zeus.util.AssertUtils; import com.google.common.base.Joiner; +import com.ctrip.zeus.util.MessageUtil; import com.netflix.config.DynamicLongProperty; import com.netflix.config.DynamicPropertyFactory; import org.springframework.stereotype.Component; @@ -114,11 +115,15 @@ public class ActivateResource { propertyBox.set("status", "activated", "slb", _slbIds.toArray(new Long[_slbIds.size()])); } catch (Exception ex) { } + + String slbMessageData = MessageUtil.getMessageData(request, null, null, + slbModelStatusMapping.getOfflineMapping().values().toArray(new Slb[]{}), null, true); + for (Long slbId : _slbIds) { if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), slbId, null); + messageQueueService.produceMessage(request.getRequestURI(), slbId, slbMessageData); } else { - messageQueueService.produceMessage(MessageType.ActivateSlb, slbId, null); + messageQueueService.produceMessage(MessageType.ActivateSlb, slbId, slbMessageData); } } return responseHandler.handle(resultList, hh.getMediaType()); @@ -241,11 +246,13 @@ public class ActivateResource { } catch (Exception ex) { } + String slbMessageData = MessageUtil.getMessageData(request, + mapping.getOfflineMapping().values().toArray(new Group[mapping.getOfflineMapping().size()]), null, null, null, true); for (Long id : _groupIds) { if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), id, null); + messageQueueService.produceMessage(request.getRequestURI(), id, slbMessageData); } else { - messageQueueService.produceMessage(MessageType.ActivateGroup, id, null); + messageQueueService.produceMessage(MessageType.ActivateGroup, id, slbMessageData); } } @@ -323,10 +330,13 @@ public class ActivateResource { propertyBox.set("status", "activated", "vs", vsId); } catch (Exception ex) { } + + String slbMessageData = MessageUtil.getMessageData(request, null, + new VirtualServer[]{vsMaping.getOfflineMapping().get(vsId)}, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), vsId, null); + messageQueueService.produceMessage(request.getRequestURI(), vsId, slbMessageData); } else { - messageQueueService.produceMessage(MessageType.ActivateVs, vsId, null); + messageQueueService.produceMessage(MessageType.ActivateVs, vsId, slbMessageData); } return responseHandler.handle(resultList, hh.getMediaType()); diff --git a/src/main/java/com/ctrip/zeus/restful/resource/GroupMemberResource.java b/src/main/java/com/ctrip/zeus/restful/resource/GroupMemberResource.java index 935b1259..aeb9cab1 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/GroupMemberResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/GroupMemberResource.java @@ -23,6 +23,7 @@ import com.ctrip.zeus.status.entity.GroupStatus; import com.ctrip.zeus.support.ObjectJsonParser; import com.ctrip.zeus.tag.PropertyBox; import com.ctrip.zeus.task.entity.OpsTask; +import com.ctrip.zeus.util.MessageUtil; import com.google.common.base.Joiner; import com.netflix.config.DynamicLongProperty; import com.netflix.config.DynamicPropertyFactory; @@ -158,10 +159,11 @@ public class GroupMemberResource { } finally { lock.unlock(); } + String slbMessageData = MessageUtil.getMessageData(request, new Group[]{group}, null,null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), group.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), group.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateGroup, group.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateGroup, group.getId(), slbMessageData); } return responseHandler.handle("Successfully added group servers to group with id " + gsl.getGroupId() + ".", hh.getMediaType()); } @@ -213,10 +215,12 @@ public class GroupMemberResource { } finally { lock.unlock(); } + + String slbMessageData = MessageUtil.getMessageData(request, new Group[]{group}, null,null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), group.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), group.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateGroup, group.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateGroup, group.getId(), slbMessageData); } return responseHandler.handle("Successfully updated group servers to group with id " + gsl.getGroupId() + ".", hh.getMediaType()); } @@ -266,10 +270,12 @@ public class GroupMemberResource { } finally { lock.unlock(); } + + String slbMessageData = MessageUtil.getMessageData(request, new Group[]{group}, null,null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), group.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), group.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateGroup, group.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateGroup, group.getId(), slbMessageData); } return responseHandler.handle("Successfully removed " + Joiner.on(",").join(ips) + " from group with id " + groupId + ".", hh.getMediaType()); } 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 188e5d72..fa43478b 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/GroupResource.java @@ -24,6 +24,7 @@ import com.ctrip.zeus.tag.PropertyBox; import com.ctrip.zeus.tag.entity.Property; import com.ctrip.zeus.tag.TagBox; +import com.ctrip.zeus.util.MessageUtil; import com.google.common.base.Joiner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -243,10 +244,11 @@ public class GroupResource { } addHealthProperty(g.getId()); + String slbMessageData = MessageUtil.getMessageData(request, new Group[]{g}, null, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), g.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), g.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.NewGroup, g.getId(), null); + messageQueueService.produceMessage(MessageType.NewGroup, g.getId(), slbMessageData); } return responseHandler.handle(new ExtendedView.ExtendedGroup(g), hh.getMediaType()); @@ -323,10 +325,12 @@ public class GroupResource { } addHealthProperty(g.getId()); + + String slbMessageData = MessageUtil.getMessageData(request, new Group[]{g}, null, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), g.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), g.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateGroup, g.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateGroup, g.getId(), slbMessageData); } return responseHandler.handle(new ExtendedView.ExtendedGroup(g), hh.getMediaType()); @@ -386,10 +390,11 @@ public class GroupResource { } finally { lock.unlock(); } + String slbMessageData = MessageUtil.getMessageData(request, new Group[]{g}, null, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), g.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), g.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateGroup, g.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateGroup, g.getId(), slbMessageData); } return responseHandler.handle(new ExtendedView.ExtendedGroup(g), hh.getMediaType()); } @@ -457,10 +462,12 @@ public class GroupResource { } finally { lock.unlock(); } + + String slbMessageData = MessageUtil.getMessageData(request, new Group[]{target}, null, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), target.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), target.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateGroup, target.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateGroup, target.getId(), slbMessageData); } return responseHandler.handle(new ExtendedView.ExtendedGroup(target), hh.getMediaType()); @@ -509,10 +516,11 @@ public class GroupResource { lock.unlock(); } + String slbMessageData = MessageUtil.getMessageData(request, new Group[]{target}, null, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), target.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), target.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateGroup, target.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateGroup, target.getId(), slbMessageData); } return responseHandler.handle(new ExtendedView.ExtendedGroup(target), hh.getMediaType()); @@ -553,10 +561,12 @@ public class GroupResource { } catch (Exception ex) { } + + String slbMessageData = MessageUtil.getMessageData(request, null, null, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), groupId, null); + messageQueueService.produceMessage(request.getRequestURI(), groupId, slbMessageData); } else { - messageQueueService.produceMessage(MessageType.DeleteGroup, groupId, null); + messageQueueService.produceMessage(MessageType.DeleteGroup, groupId, slbMessageData); } return responseHandler.handle("Group is deleted.", hh.getMediaType()); diff --git a/src/main/java/com/ctrip/zeus/restful/resource/OperationResource.java b/src/main/java/com/ctrip/zeus/restful/resource/OperationResource.java index 0a58b2ed..19f952a1 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/OperationResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/OperationResource.java @@ -26,6 +26,7 @@ import com.ctrip.zeus.status.entity.ServerStatus; import com.ctrip.zeus.tag.PropertyBox; import com.ctrip.zeus.task.entity.OpsTask; import com.ctrip.zeus.task.entity.TaskResult; +import com.ctrip.zeus.util.MessageUtil; import com.google.common.base.Joiner; import com.netflix.config.DynamicBooleanProperty; import com.netflix.config.DynamicLongProperty; @@ -704,17 +705,16 @@ public class OperationResource { GroupStatus groupStatus = groupStatusService.getOfflineGroupStatus(groupId); addHealthyProperty(groupStatus); + String slbMessageData = MessageUtil.getMessageData(request, new Group[]{offlineGroup}, null, null, ips.toArray(new String[ips.size()]), true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), groupId, null); + messageQueueService.produceMessage(request.getRequestURI(), groupId, slbMessageData); } else { if (type.equals(TaskOpsType.HEALTHY_OPS)) { - messageQueueService.produceMessage(MessageType.OpsHealthy, groupId, null); - } - if (type.equals(TaskOpsType.PULL_MEMBER_OPS)) { - messageQueueService.produceMessage(MessageType.OpsPull, groupId, null); - } - if (type.equals(TaskOpsType.MEMBER_OPS)) { - messageQueueService.produceMessage(MessageType.OpsMember, groupId, null); + messageQueueService.produceMessage(MessageType.OpsHealthy, groupId, slbMessageData); + } else if (type.equals(TaskOpsType.PULL_MEMBER_OPS)) { + messageQueueService.produceMessage(MessageType.OpsPull, groupId, slbMessageData); + } else if (type.equals(TaskOpsType.MEMBER_OPS)) { + messageQueueService.produceMessage(MessageType.OpsMember, groupId, slbMessageData); } } diff --git a/src/main/java/com/ctrip/zeus/restful/resource/VirtualServerResource.java b/src/main/java/com/ctrip/zeus/restful/resource/VirtualServerResource.java index f8641fcf..251d9a14 100644 --- a/src/main/java/com/ctrip/zeus/restful/resource/VirtualServerResource.java +++ b/src/main/java/com/ctrip/zeus/restful/resource/VirtualServerResource.java @@ -25,6 +25,7 @@ import com.ctrip.zeus.service.model.IdVersion; import com.ctrip.zeus.service.query.VirtualServerCriteriaQuery; import com.ctrip.zeus.support.ObjectJsonParser; import com.ctrip.zeus.support.ObjectJsonWriter; +import com.ctrip.zeus.util.MessageUtil; import com.ctrip.zeus.tag.PropertyBox; import com.ctrip.zeus.tag.TagBox; import com.ctrip.zeus.tag.entity.Property; @@ -179,10 +180,11 @@ public class VirtualServerResource { if (extendedView.getTags() != null) { addTag(vs.getId(), extendedView.getTags()); } + String slbMessageData = MessageUtil.getMessageData(request, null, new VirtualServer[]{vs}, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), vs.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), vs.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.NewVs, vs.getId(), null); + messageQueueService.produceMessage(MessageType.NewVs, vs.getId(), slbMessageData); } return responseHandler.handle(new ExtendedView.ExtendedVs(vs), hh.getMediaType()); @@ -225,10 +227,11 @@ public class VirtualServerResource { addTag(vs.getId(), extendedView.getTags()); } + String slbMessageData = MessageUtil.getMessageData(request, null, new VirtualServer[]{vs}, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), vs.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), vs.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateVs, vs.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateVs, vs.getId(), slbMessageData); } return responseHandler.handle(new ExtendedView.ExtendedVs(vs), hh.getMediaType()); @@ -264,10 +267,11 @@ public class VirtualServerResource { } catch (Exception ex) { } + String slbMessageData = MessageUtil.getMessageData(request, null, new VirtualServer[]{vs}, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), vs.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), vs.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateVs, vs.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateVs, vs.getId(), slbMessageData); } return responseHandler.handle(new ExtendedView.ExtendedVs(vs), hh.getMediaType()); @@ -310,10 +314,12 @@ public class VirtualServerResource { } } catch (Exception ex) { } + + String slbMessageData = MessageUtil.getMessageData(request, null, new VirtualServer[]{vs}, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), vs.getId(), null); + messageQueueService.produceMessage(request.getRequestURI(), vs.getId(), slbMessageData); } else { - messageQueueService.produceMessage(MessageType.UpdateVs, vs.getId(), null); + messageQueueService.produceMessage(MessageType.UpdateVs, vs.getId(), slbMessageData); } return responseHandler.handle(new ExtendedView.ExtendedVs(vs), hh.getMediaType()); } @@ -345,10 +351,12 @@ public class VirtualServerResource { tagBox.clear("vs", vsId); } catch (Exception ex) { } + + String slbMessageData = MessageUtil.getMessageData(request, null, null, null, null, true); if (configHandler.getEnable("use.new,message.queue.producer", false)) { - messageQueueService.produceMessage(request.getRequestURI(), vsId, null); + messageQueueService.produceMessage(request.getRequestURI(), vsId, slbMessageData); } else { - messageQueueService.produceMessage(MessageType.DeleteVs, vsId, null); + messageQueueService.produceMessage(MessageType.DeleteVs, vsId, slbMessageData); } return responseHandler.handle("Successfully deleted virtual server with id " + vsId + ".", hh.getMediaType()); } diff --git a/src/main/java/com/ctrip/zeus/util/MessageUtil.java b/src/main/java/com/ctrip/zeus/util/MessageUtil.java new file mode 100644 index 00000000..24140aba --- /dev/null +++ b/src/main/java/com/ctrip/zeus/util/MessageUtil.java @@ -0,0 +1,82 @@ +package com.ctrip.zeus.util; + +import com.ctrip.zeus.model.entity.Group; +import com.ctrip.zeus.model.entity.Slb; +import com.ctrip.zeus.model.entity.VirtualServer; +import com.ctrip.zeus.queue.entity.GroupData; +import com.ctrip.zeus.queue.entity.SlbData; +import com.ctrip.zeus.queue.entity.SlbMessageData; +import com.ctrip.zeus.queue.entity.VsData; +import com.ctrip.zeus.queue.transform.DefaultJsonParser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +/** + * Created by fanqq on 2016/10/13. + */ +public class MessageUtil { + static Logger logger = LoggerFactory.getLogger(MessageUtil.class); + + public static String getMessageData(HttpServletRequest request, Group[] groups, VirtualServer[] vses, Slb[] slbs, String[] ips, boolean success) { + SlbMessageData res = new SlbMessageData(); + res.setQuery(request.getQueryString()) + .setUri(request.getRequestURI()) + .setSuccess(success) + .setUser(UserUtils.getUserName(request)) + .setClientIp(getClientIP(request)); + if (groups != null && groups.length > 0) { + for (Group group : groups) { + res.addGroupData(new GroupData().setId(group.getId()).setName(group.getName()).setVersion(group.getVersion())); + } + } + if (slbs != null && slbs.length > 0) { + for (Slb slb : slbs) { + res.addSlbData(new SlbData().setId(slb.getId()).setName(slb.getName()).setVersion(slb.getVersion())); + } + } + if (vses != null && vses.length > 0) { + for (VirtualServer vs : vses) { + res.addVsData(new VsData().setId(vs.getId()).setName(vs.getName()).setVersion(vs.getVersion())); + } + } + if (ips != null && ips.length > 0) { + for (String ip : ips) { + res.addIp(ip); + } + } + return String.format(SlbMessageData.JSON, res); + } + + public static SlbMessageData parserSlbMessageData(String res) { + try { + if (res == null) return null; + return DefaultJsonParser.parse(SlbMessageData.class, res); + } catch (IOException e) { + logger.warn("Parser Slb Message Data Failed. Message:" + res, e); + return null; + } + } + + private static String getClientIP(HttpServletRequest request) { + String ip = request.getHeader("X-Forwarded-For"); + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_CLIENT_IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_X_FORWARDED_FOR"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getRemoteAddr(); + } + return ip; + } +} diff --git a/src/main/resources/META-INF/dal/model/queue-codegen.xml b/src/main/resources/META-INF/dal/model/queue-codegen.xml index 2ab58454..71060f94 100644 --- a/src/main/resources/META-INF/dal/model/queue-codegen.xml +++ b/src/main/resources/META-INF/dal/model/queue-codegen.xml @@ -2,6 +2,7 @@ + @@ -11,5 +12,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/com/ctrip/zeus/model/queue.xml b/src/test/resources/com/ctrip/zeus/model/queue.xml index 1cdd3974..0a5d7476 100644 --- a/src/test/resources/com/ctrip/zeus/model/queue.xml +++ b/src/test/resources/com/ctrip/zeus/model/queue.xml @@ -7,4 +7,31 @@ Unsupported content type: 2013-05-20 13:34:00 + + /api/ + abc=abc + ff + 10.2.2.2 + true + + 123456789809 + name + 12345678 + + + + 123456789809 + name + 12345678 + + + + 123456789809 + name + 12345678 + + + ip + + \ No newline at end of file