设为首页 收藏本站
查看: 627|回复: 0

[经验分享] Apache ServiceMix应用之深入Apache Camel

[复制链接]

尚未签到

发表于 2016-12-29 09:40:44 | 显示全部楼层 |阅读模式
转自:http://blog.csdn.net/lubiaopan/article/details/16371387
阅读本文章以前建议先阅读《Apache ServiceMix 初探》
预备知识:
BluePrint
OSGI
Maven
Java DSL
Apache Camel
Apache ActiveMQ

ESB最核心的功能便是应用集成和服务路由,Apache ServiceMix完成这两大核心功能的尖兵利器便是Apache Camel。Apache Camel是一个开源的、功能丰富的应用集成框架,它支持常见的EIP模式,是一个强大的基于规则的路由引擎,可以轻松的实现消息路由和消息转换,ServiceMix对Camel进行了深度集成来支持各种复杂的ESB功能。
关于Apache Camel的介绍:
    http://camel.apache.org/(主页)
    http://marshal.easymorse.com/archives/1431
    http://blog.csdn.net/njchenyi/article/details/5174261
关于EAI模式的介绍:
    http://www.eaipatterns.com/toc.html

发布路由的4种方式
在ServiceMix上发布Camel Routes有两大类4种方式,所谓的两大类是指:其一,直接通过简单的xml配置文件发布;其二,通过自定义bundle的方式发布。而每类下面既可以通过Spring容器发布又可以通过BluePrint容器发布,所以说有4种方式。下面对这4种方式进行介绍,然后比较他们的优缺点。
1、基于blueprint容器,通过简单的Blueprint XML文档发布路由,发布方式及实验见《Apache ServiceMix 初探》

[html] view plaincopy 



  • <?xml version="1.0" encoding="utf-8"?>  
  • <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"  
  •       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  •       xsi:schemaLocation="  
  •       http://www.osgi.org/xmlns/blueprint/v1.0.0  
  •       http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">  
  •   <camelContext xmlns="http://camel.apache.org/schema/blueprint">  
  •     <route>  
  •       <from uri="file:camel/blueprin-plain-input"/>  
  •       <log message="Copying ${file:name} to the output directory"/>  
  •       <to uri="file:camel/blueprint-plain-output"/>  
  •     </route>  
  •   </camelContext>  
  • </blueprint>  

2、基于spring容器,通过简单的Spring XML文档发布路由(与blueprint相比,配置上没有太大的区别,只不过是换了一种容器而已),发布方式及实验见《Apache ServiceMix 初探》
[html] view plaincopy 



  • <?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:camel="http://camel.apache.org/schema/spring"  
  •        xsi:schemaLocation="  
  •           http://www.springframework.org/schema/beans  
  •           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  •           http://camel.apache.org/schema/spring  
  •           http://camel.apache.org/schema/spring/camel-spring-2.10.3.xsd">  
  •   <camelContext xmlns="http://camel.apache.org/schema/spring">  
  •     <route>  
  •       <from uri="file:camel/spring-plain-input"/>  
  •       <log message="Copying ${file:name} to the output directory"/>  
  •       <to uri="file:camel/spring-plain-output"/>  
  •     </route>  
  •   </camelContext>  
  • </beans>  

3、基于Spring容器,自定义bundle发布路由(以apache-servicemix-4.5.3附带的例子进行演示,例子所在目录:\apache-servicemix-4.5.3\examples\camel\camel-osgi)
第一步:创建一个MyTransform类(这里对示例中的代码进行了一些调整)
[java] view plaincopy 



  • package org.apache.servicemix.examples.camel;  
  •   
  • import org.slf4j.Logger;  
  • import org.slf4j.LoggerFactory;  
  •   
  • import java.util.Date;  
  •   
  • public class MyTransform  {  
  •   
  •     private static final transient Logger LOG = LoggerFactory.getLogger(MyTransform.class);  
  •   
  •     private boolean verbose = true;//是否打印冗长字符串">>>>"  
  •     private String prefix = "MyTransform";//日志的消息前缀  
  •   
  •     public Object transform(Object body) {  
  •         String answer = prefix + " set body:  " + new Date();//拼接日志字符串  
  •         if (verbose) {  
  •             System.out.println(">>>> " + answer);  
  •             LOG.info(">>>> " + answer);  
  •         }else  
  •         {  
  •             System.out.println(answer);  
  •             LOG.info(answer);  
  •   
  •   
  •         }  
  •           
  •         return answer;  
  •     }  
  •   
  •     public boolean isVerbose() {  
  •         return verbose;  
  •     }  
  •   
  •     public void setVerbose(boolean verbose) {  
  •         this.verbose = verbose;  
  •     }  
  •   
  •     public String getPrefix() {  
  •         return prefix;  
  •     }  
  •   
  •     public void setPrefix(String prefix) {  
  •         this.prefix = prefix;  
  •     }  
  • }  


第二步:创建路由类(这里对示例中的代码进行了一些调整)

[java] view plaincopy 



  • package org.apache.servicemix.examples.camel;  
  •   
  • import org.apache.camel.builder.RouteBuilder;  
  • import org.apache.camel.spring.Main;  
  •   
  • public class MyRouteBuilder extends RouteBuilder {  
  •     public static void main(String[] args) throws Exception{  
  •         new Main().run(args);  
  •     }  
  •   
  •     public void configure() {  
  •         MyTransform transform = new MyTransform();//创建MyTransform的一个实例  
  •         transform.setPrefix("JavaDSL");//设置日志前缀为"JavaDSL"  
  •         transform.setVerbose("false");//不打印冗长串">>>>"  
  •           
  •         from("timer://javaTimer?fixedRate=true&period=2000")//从定时器取数据  
  •             .bean(transform, "transform")//执行MyTransform的transform方法              
  •            .to("log:ExampleRouter");//将定时器数据路由到日志模块          
  •     }      
  • }  

第三步:在META-INF/spring文件夹下创建beans.xml
[html] view plaincopy 



  • <?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:ctx="http://www.springframework.org/schema/context"  
  •        xmlns:camel="http://camel.apache.org/schema/spring"  
  •        xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"  
  •        xsi:schemaLocation="  
  •        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  •        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
  •        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd  
  •        http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">  
  •   
  •   <camel:camelContext xmlns="http://camel.apache.org/schema/spring">  
  •     <!-- install the Java DSL route builder -->  
  •     <package>org.apache.servicemix.examples.camel</package>  
  •     <!-- install the route which is defined with xml tags -->  
  •     <route>  
  •       <from uri="timer://springTimer?fixedRate=true&period=2000"/>  
  •       <bean ref="myTransform" method="transform"/>  
  •       <to uri="log:ExampleRouter"/>  
  •     </route>  
  •   </camel:camelContext>  
  •   
  •   <bean id="myTransform" class="org.apache.servicemix.examples.camel.MyTransform">  
  •     <property name="prefix" value="${prefix}"/>  
  •   </bean>  
  •      
  •     <osgix:cm-properties id="preProps" persistent-id="org.apache.servicemix.examples">  
  •         <prop key="prefix">MyTransform</prop><!--前缀名成为MyTransform-->  
  •     </osgix:cm-properties>  
  •   
  •     <ctx:property-placeholder properties-ref="preProps" />  
  •   
  • </beans>  

看配置文件,<package>org.apache.servicemix.examples.camel</package>通过引用MyRouteBuilder类来定义路由,而紧接着下面的<route>通过配置文件来定义路由。前者直接在MyRouteBuilder中使用MyTransform,后者通过注入的方式使用MyTransform。如果想要完成配置文件无法提供的更强大的、自定义的功能则用前者,否则的话建议用后者。
第四步:创建pom文件(这里只把文件贴出来,不再赘述)

[html] view plaincopy 



  • <?xml version="1.0" encoding="UTF-8"?>  
  • <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 <a target="_blank" href="http://maven.apache.org/maven-v4_0_0.xsd'>">http://maven.apache.org/maven-v4_0_0.xsd">  
  • </a>    <modelVersion>4.0.0</modelVersion>  
  •   
  •     <parent>  
  •         <groupId>org.apache.servicemix.examples</groupId>  
  •         <artifactId>camel-examples</artifactId>  
  •         <version>4.5.3</version>  
  •     </parent>  
  •   
  •     <artifactId>camel-osgi</artifactId>  
  •     <packaging>bundle</packaging>  
  •     <name>Apache ServiceMix :: Features :: Examples :: Camel OSGi</name>  
  •     <description>Camel example using OSGi instead of JBI</description>  
  •   
  •     <dependencies>  
  •         <dependency>  
  •             <groupId>org.slf4j</groupId>  
  •             <artifactId>slf4j-api</artifactId>  
  •             <scope>provided</scope>  
  •         </dependency>  
  •         <dependency>  
  •             <groupId>org.apache.camel</groupId>  
  •             <artifactId>camel-spring</artifactId>  
  •         </dependency>  
  •     </dependencies>  
  •   
  •     <build>  
  •         <plugins>  
  •             <plugin>  
  •                 <groupId>org.apache.felix</groupId>  
  •                 <artifactId>maven-bundle-plugin</artifactId>  
  •                 <configuration>  
  •                     <instructions>  
  •                         <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>  
  •                         <Bundle-Description>${project.description}</Bundle-Description>  
  •                         <Import-Package>*</Import-Package>  
  •                         <Private-Package>org.apache.servicemix.examples.camel</Private-Package>  
  •                     </instructions>  
  •                 </configuration>  
  •             </plugin>  
  •         </plugins>  
  •     </build>  
  •   
  • </project>  

第五步:编译、安装、运行
转到examples/camel/camel-osgi文件夹下,运行mvn install,耐心等待,编译成功之后会在examples/camel/camel-osgi文件夹下出现一个target文件夹,里面便是我们需要的bundle。
在ServicMix控制台上运行features:install  examples-camel-osgi命令安装编译好的bundle,安装完成后观察控制台,我的截图如下:
DSC0000.jpg
由图可见:通过XML配置的路由信息和通过MyRouteBuilder实现的路由信息都打印在了控制台上。前者带">>>>"符号,前缀为“MyTransfor”,后者不带">>>>"符号,前缀为“JavaDSL”。控制台上打印的内容是System.out.println()语句运行的结果,而路由的终结点是<to uri="log:ExampleRouter"/>,通过log:display命令查看打印的日志。
卸载该bundle的命令为:features:uninstall examples-camel-osgi
4、通过Blueprint发布osgi bundle的方式发布路由
此处不再详细描述,流程和3大致相同,ServiceMix自带的readme.txt描述的也很清楚了
4种方式的对比

Spring和BluePrint的对比
如果想得到基于OSGI框架的、更优的集成和服务注册方案,则优先考虑使用Blueprint,因为Blueprint有更强的针对性,OSGI联盟对blueprint进行了很多有针对性的、特殊的开发;当然,使用spring也完全没有问题,如果对spring已经有了丰富的实践经验,it is ok to use spring。
简单配置文件和自定义bundle对比
这两者没有孰优孰劣之分,大部分情况下直接通过xml一配就ok了;如果需要自定义一些功能,需要用到自己的辅助类的时候就该考虑用bundle了。两者可用的情况下就看个人习惯了。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-320910-1-1.html 上篇帖子: apache相关知识 下篇帖子: Apache Log4j配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表