Camel 和rabbitmq 集成处理

本文详细介绍了Camel作为Java中的编程范式,主要用于企业集成,它使用DSL语言配置路由规则。核心思想是从源头获取数据,通过处理器处理,发送到目标。主要讨论了Camel的Java DSL,RouteBuilder类,以及CamelContext和Endpoint的概念。此外,还探讨了Camel在Clojure微服务中的应用,包括如何使用Camel与RabbitMQ进行集成,创建微服务间的通信路由。
摘要由CSDN通过智能技术生成

camel可以看作是java中的一个编程范式,通过DSL语言来编写路由规则。

camel 核心

camel是一个基于规则路由和处理的引擎,提供企业集成模式的Java对象的实现,通过应用程序接口或称为陈述式的Java领域特定语言(DSL)来配置路由和处理的规则。
其核心的思想就是从一个from源头得到数据,通过processor处理,再发到一个to目的的.

发散:像是网络中超强的路由器规则,是否可以看做是软件定义的交换机

这里的from和to可以是我们在项目集成中经常碰到的类型:一个FTP文件夹中的文件,一个MQ的queue,一个HTTP request/response,一个webservice等等。

现在的问题是 什么是java领域特定语言(DSL)?

Camel uses a Java Domain Specific Language or DSL for creating Enterprise Integration Patterns or Routes in a variety of domain-specific languages (DSL) as listed below.
Java DSL - A Java based DSL using the fluent builder style.
Spring XML - A XML based DSL in Spring XML files
Blueprint XML - A XML based DSL in OSGi Blueprint XML files
Rest DSL - A DSL to define REST services using a REST style in either Java or XML.
Groovy DSL - A Groovy based DSL using Groovy programming language
Scala DSL - A Scala based DSL using Scala programming language
Annotation DSL - Use annotations in Java beans.
Kotlin DSL - Work in progress - Currently developed outside ASF, but will we included later in Camel when Kotlin and the DSL is ready.
The main entry points for the DSL are
CamelContext for creating a Camel routing rulebase
RouteBuilder for creating a collection of routes using the routing DSL—http://camel.apache.org/dsl.html
也就是说camel可以使用多种方式配置,包括Java xml等。

这里主要关注java DSL,java DSL的语法主要是什么呢?
RouteBuilder类就是Java的smart DSL。

public abstract class RouteBuilder
extends BuilderSupport
implements RoutesBuilder
A Java DSL which is used to build DefaultRoute instances in a CamelContext for smart routing.

主要的方法有:

public abstract void configure()
throws Exception
Called on initialization to build the routes using the fluent builder syntax.
This is a central method for RouteBuilder implementations to implement the routes using the Java fluent builder syntax.

configure()就是主要的实现路由语法的函数。
主要的路由配置的方法我整理成了一张表格:

语法 作用 语法
from 路由的起点 from(String uri),返回the builder
to 路由的终点 参数String uri,返回the builder
choice(when,otherwise) 选择某个内容 以choice开头,when表示某个条件
fromF/toF 使用String formatting创建URI 和from/to类似
filter 选择某个内容 过滤想要的内容
process 对内容进行一定的处理 需要编写继承自Processor接口的类,实现process方法,完成对Exchanage(交换内容)处理的逻辑

Processer接口用于处理消息转换或者消息交换。

来自网络的一个简单例子:
这里写图片描述
这里实现了一个简单的路由,这里的起点是DefaultCamelConetxt,那么DefaultCamelConetxt这个类是怎样的?

public class DefaultCamelContext
extends ServiceSupport
implements ModelCamelContext, SuspendableService
Represents the context used to configure routes and the policies to use.

也就是说DefaultCamelContext类代表了配置camel路由的上下文,和一些必要的策略,DefaultCamelContext继承自Service,可以使用start,stop等基本的管理生命周期的方法。
让camel引擎开始工作使用start方法,对应的停止工作的方法是stop。使用addRoutes方法给camelContext添加对应的路由。
下图表示了一个继承自Processor类的基本的实现,
这里写图片描述
主要的逻辑是对Exchange进行操作,来达到想要的目的。上图的逻辑是将a文件所有的行连成一行,以空格分隔组成一个新的文件给to对应的终点uri。
前面讲了一个简单的这里,到这个时候,还需要补充一些基本的概念。

camel一些定义

补充的概念有Uris,EndPoint,Exchange,Message等

关于uris的官方解释: Camel makes extensive use of URIs to allow you to refer to endpoints which are lazily created by a Component if you refer to them within Routes.

意思是,uris的作用就是用来指向组件的endpoint的一种表示方法。

那么endpoint呢?

官方解释: public interface Endpoint extends IsSingleton, Service
An endpoint implements the Message Endpoint pattern and represents an endpoint that can send and receive message exchanges

endpoint就是一个可以收发消息的组件,组件component就是一个一个的camel组件的术语代表。

那么,message呢?

public interface Exchange An Exchange is the message container holdingthe information during the entire routing of a Message received by a Consumer.

public interface Message Implements the Message pattern and represents an inbound or outbound message as part of an Exchange.

Exchange是message容器,message有属性inbound和outbound,是Exchange的一部分。比如上面的处理器使用exchange的getIn()方法可以得到message本身。

目前我写的一系列文章都是讲的clojure,比如语法和数据处理,同样我本文的目的依旧是讲camel在clojure中的应用,讲的更具体一点的话,其实是谈论camel在clojure 微服务中的使用方法。

camel在clojure 微服务中的使用

首先什么是微服务,这里有一篇很好的介绍微服务的文章,http://chuansong.me/n/350466751870(属于Chris Richardson 微服务系列7 篇中的第二篇)。

比如说一个微服务架构中,使用jsonrpc作为网关,rabbitmq作为amqp队列,一系列微服务通过amqp和网关(给外部提供web服务)通信。那么在这个架构中,camel的定义是怎样的?

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值