ESB提供比传统中间件产品更为廉价的解决方案,同时它还能够消除不一样应用之间的技术差别,让不一样的应用服务器协调运做,实现了不一样服务之间的通讯与整合。从功能上看,ESB提供了事件驱动和文档导向的处理模式,以及分布式的运行管理机制,它支持基于内容的路由和过滤,具有了复杂数据的传输能力,并能够提供一系列的标准接口,可是ESB并无http请求效率高。 废话就很少说了,先看看怎么实现的吧。 首先肯定请求报文的格式,以我在项目中的报文为例:
<MbfService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <input1> <MbfHeader> <ServiceCode>CMercPerformanceMgmt</ServiceCode> <Operation>queryStoreScores</Operation> <AppCode>SFS</AppCode> <UId>5136fa413f96493391d6075f654d4d83bcfbbc57bd8e47e5</UId> </MbfHeader> <MbfBody> <cmfUserId>0224136722</cmfUserId> <currentPage>1</currentPage> <pageSize>5</pageSize> <supportCStore>0</supportCStore> </MbfBody> </input1></MbfService>java
这就是一个接口的请求报文,通常报文格式是固定的,须要改动的只是报文的节点,被调用方接口也是根据节点筛选出所须要的请求参数,另外,报文是用POST方式提交,参数是封装在body中的。ajax
下面是被请求接口:为了各位看的明白些,我狠添注释,这样代码和注释一块儿看比较方便
//RequestMapping,这个是spring中截获请求的url做用 @RequestMapping("/my_product_review") public String getPendingOrderItems(HttpServletRequest request, HttpServletResponse response) throws IOException { //这里是从request中取到报文,getMbfRequestInput方法会在下面贴出代码 MbfRequestInput mbfRequestInput = getMbfRequestInput(request); //根据报文中参数建立请求对象,这里只要PendingOrderItemDTO 中的属性跟请求报文的节点一致便可(也就是报文中 <MbfBody>中的节点名称) PendingOrderItemDTO requestDTO = (PendingOrderItemDTO) mbfRequestInput .getMbfBody(PendingOrderItemDTO.class); //封装返回对像的报文 MbfResponseWrapper mbfResponseWrapper = new MbfResponseWrapper( mbfRequestInput.getMbfHeader());spring
// 处理请求参数,orderVo为最终返回报文的类 MobilePhonePendingOrderVO orderVO = null; try { //这句代码能够忽略,就是根据请求参数作处理,封装类,做为返回报文数据 orderVO = mobilePhoneService.getPendingOrderItems(requestDTO.getCmfUserId(), requestDTO.getCurrentPage(), requestDTO.getPageSize(), requestDTO.getSupportCStore()); //将返回类按报文格式处理,就是跟请求报文同样,好比orderVo有一个属性name,返回报文<MbfBody>中就会有<name>XXX</name> mbfResponseWrapper.setMbfBody(orderVO); //返回这次请求成功标识 mbfResponseWrapper.setStatus(Constants.COMPLETE); } catch (Exception e) { mbfResponseWrapper.setMbfBody(new DummyResponseDTO()); //返回这次请求失败标识 mbfResponseWrapper.setStatus(Constants.FAIL); mbfResponseWrapper.setCode(""); mbfResponseWrapper.setDesc(e.getMessage()); //下面是异常处理 EventMessage eventMessage = new EventMessage(); eventMessage.setFunctionName(ExceptionUtil.getCurrentMethodName()); logger.logException(eventMessage, e); } //按报文格式返回数据 return ajaxXmlCache(response, mbfResponseWrapper.getMbfResponse().toString(), ReviewProperties.getMaxAge()); }
上面这些代码对使用esb处理请求,返回报文等就够了,由于解析报文是有专门的封装类的,只须要引入,就可使用里面的方法。为了各位方便学习,我多写点吧服务器
/** * 从HttpServletRequest中获取ESB请求报文 */ public MbfRequestInput getMbfRequestInput(HttpServletRequest request) throws IOException { InputStream inputStream = request.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } return MbfRequest.getMbfRequest(buffer.toString()).getInput(); } 其中MbfRequest.getMbfRequest()方法app
/** * 功能描述:resquestXml转换成MbfResquest对象 */ public static MbfRequest getMbfRequest(String xml){ MbfRequest mbfRequest = (MbfRequest) XStreamUtil.xmlToObject(xml, MbfRequest.class); mbfRequest.getInput().setMbfBodyXml(getMbfBodyXml(xml)); return mbfRequest; }分布式
至于好比getInput()等方法就是本身封装的很基础的java类了,贴出来也无妨 /** *ide
<MbfService xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">学习
<input1>
<MbfHeader>
<!--ServiceCode: 服务代码-->
<ServiceCode>OrderManage</ServiceCode>
<!--Operation: 操做编码-->
<Operation>CreateOrder</Operation>
<!--AppCode: 请求方系统代码-->
<AppCode>b2c</AppCode>
<!--UId: 服务流水号-->
<UId>414d5120514d5f6c6f63616c202020203baa474c20012802</UId>
<!--AuthId: 认证ID -->
<AuthId/>
</MbfHeader>
<MbfBody>
<!--请求服务系统BODY -->
</MbfBody>
</input1>
</MbfService>this
*/ @XStreamAlias("MbfService") public class MbfRequest {编码
private static final String rootStart = "<MbfService xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"; private static final String rootEnd = "</MbfService>"; private static final String bodyStart = "<MbfBody>"; private static final String bodyEnd = "</MbfBody>"; @XStreamAlias("input1") private MbfRequestInput input; public MbfRequestInput getInput() { return input; } public void setInput(MbfRequestInput input) { this.input = input; } /** * 功能描述:resquestXml转换成MbfResquest对象 */ public static MbfRequest getMbfRequest(String xml){ MbfRequest mbfRequest = (MbfRequest) XStreamUtil.xmlToObject(xml, MbfRequest.class); mbfRequest.getInput().setMbfBodyXml(getMbfBodyXml(xml)); return mbfRequest; } private static String getMbfBodyXml(String xml){ int beginIndex = xml.indexOf(bodyStart); int endIndex = xml.indexOf(bodyEnd); String bodyXml = ""; if(beginIndex != -1 && endIndex != -1){ bodyXml = xml.substring(beginIndex + bodyStart.length(), endIndex); } return bodyXml; } @Override public String toString() { return toString(false); } public String toString(boolean replaceBlank) { String inputStr = null; if(replaceBlank){ inputStr = input.toString(true); } else { inputStr = input.toString(false); } String result = rootStart + inputStr + rootEnd; return result; }
} 虽然上面主要贴了的是解析请求报文,可是相信你们看到这里也应该知道是怎么回事了,若是你用现有的解析报文的jar包,很方便用,可是若是你就想本身写jar包玩,根据上面的慢慢琢磨是能够的,可是仍是有些底层jar须要引用,好比:com.thoughtworks.xstream.annotations.XStreamAlias,不过就是一层一层的解析xml文件,封装成请求对象,而后在把返回对象解析成xml文件发送给调用方罢了。
最后,我以为有必要说下主方法中的return后面的内容:ajaxXmlCache(response, mbfResponseWrapper.getMbfResponse().toString(), ReviewProperties.getMaxAge());
这个地方是比较容易出现问题的,很少说 了,直接贴代码吧:
/** * 根据字符串输出xml,返回null * * @param xml * @return */ public String ajaxXmlCache(HttpServletResponse response, String xmlString, String cacheTime) { return ajaxCache(response, xmlString, "text/xml", cacheTime); }
//下面就是最原始的写内容了 /** * AJAX输出,返回null * * @param content * @param type * @return */ public String ajaxCache(HttpServletResponse response, String content, String type, String cacheTime) { try { response.setContentType(type + ";charset=UTF-8"); setCache(response, cacheTime); response.getWriter().write(content); response.getWriter().flush(); } catch (IOException e) { EventMessage eventMessage = new EventMessage(); eventMessage.setFunctionName(ExceptionUtil.getCurrentMethodName()); logger.logException(eventMessage, e); } return null; }
其中针对解析报文,封装报文,以及对内容的封装代码也是有不少的,以为没有必要所有贴出来,就写这些吧,刚开始写博客,算做是记录一下本身的成长经历吧