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" class="com.starit.EnrichBean"/>

<bean id="mySlip" class="com.starit.DynamicRouterTest"/>

<bean id="camelTracer" class="org.apache.camel.processor.interceptor.Tracer">
<property name="traceOutExchanges" value="true" />
</bean>

<bean id="traceFormatter" class="org.apache.camel.processor.interceptor.DefaultTraceFormatter">
<property name="showOutHeaders" 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 class="com.starit.MyInterceptor"/>

使用spring来配置web service endpoint和camel route。是静态配置在xml文件中。不利于业务快速变化需求,需要有一个技术方案来满足endpoint和route动态的增删改。想到只要spring能够动态装配bean和camel动态控制route就可以满足需求,经过测试方案可行。
下面是一段测试代码

@Override
protected 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() {
@Override
public 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 ");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值