camel web service Component 动态代理功能
随着企业信息化建设的发展,企业应用系统越来越多(某省电信软件系统有38套),而各个系统之间不是相互独立,彼此之间是有数据交互,参与建设的系统中,经常会与开通、crm等系统进行交互。就目前企业应用,主要使用web service进行接口交互。为了管理监控这些繁复多样的接口,服务集成的应用开始在企业中得到重视,在电信行业中,上至集团,下至省公司,都在加紧建设系统之间的服务集成。
在建设的系统中采用的ibm来建设这套系统、采购ibm esb成本也很高。而在实际使用过程中esb平台被弱化为一个web service代理功能,使用的很简单。中间流量,带宽、访问次数、异常等监控还是需要自己做,esb提供的其它Component使用不到,有点大材效用。为了脱离商业软件,技术选型使用camel来实现web service代理,camel良好的技术架构和扩展点,方便我们实现接口的管理,监控。
camel webservice 代理配置
<cxf:cxfEndpoint id="helloWorld" address="/helloworld" endpointName="s:HelloWorldPort" serviceName="s:HelloWorldService" wsdlURL="helloWorld.wsdl" xmlns:s="http://demo.cxf.starit.com/"/> <!-- a bean to enrich the input --> <bean id="enrichBean" value="true" /></bean><bean id="traceFormatter" value="true" /><property name="showOutBody" value="true" /> <property name="showOutBodyType" value="true" /></bean> <!-- this is the camel route which proxy the web service and forward it to the real web service --> <camelContext id="camel" trace="false" xmlns="http://camel.apache.org/schema/spring"> <interceptFrom> <to uri="log:received"/> </interceptFrom> <route id="routetest"> <!-- cxf consumer using MESSAGE format --> <from uri="cxf:bean:helloWorld?dataFormat=MESSAGE"/> <!-- enrich the input by ensure the incidentId parameter is set --> <to uri="bean:enrichBean"/> <!-- 负载均衡配置 --> <loadBalance> <weighted distributionRatio="2 1" roundRobin="true"/> <!-- <failover roundRobin="true"> <exception>java.io.IOException</exception> </failover> --> <to uri="http://localhost:9000/helloWorld"/> <to uri="http://localhost:9001/helloWorld"/> </loadBalance> <!-- send proxied request to real web service <dynamicRouter> <method ref="mySlip" method="slip"/> </dynamicRouter> --> </route> </camelContext> <bean name="code"> @Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse response)throws ServletException, IOException {ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());DefaultListableBeanFactory acf = (DefaultListableBeanFactory) ctx.getAutowireCapableBeanFactory();CamelContext context = (CamelContext)ctx.getBean("camel");if(ctx.containsBean("helloWorld")) {acf.removeBeanDefinition("helloWorld");try {context.stopRoute("routetest1");context.removeRoute("routetest1");} catch (Exception e) {e.printStackTrace();}response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("contain helloword bean");} else {String xml = "<?xml version="1.0" encoding="UTF-8"?>"+"<beans xmlns="http://www.springframework.org/schema/beans""+" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance""+" xmlns:cxf="http://camel.apache.org/schema/cxf""+" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"+" http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf-2.5.0.xsd">"+"<cxf:cxfEndpoint id="helloWorld" address="/helloworld" endpointName="s:HelloWorldPort" serviceName="s:HelloWorldService""+" wsdlURL="helloWorld.wsdl" xmlns:s="http://demo.cxf.starit.com/"/>"+"</beans>";XmlBeanFactory factory = new XmlBeanFactory(new ByteArrayResource(xml.getBytes()));acf.registerBeanDefinition("helloWorld", factory.getMergedBeanDefinition("helloWorld"));//添加路由规则 try {context.addRoutes(new RouteBuilder() {@Overridepublic void configure() throws Exception {from("cxf:bean:helloWorld?dataFormat=MESSAGE").id("routetest1").to("bean:enrichBean").to("http://localhost:9000/helloWorld?throwExceptionOnFailure=false");}});context.startRoute("routetest1");} catch (Exception e) {e.printStackTrace();}response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("create new helloword bean ");}}