Apache Camel 框架入门记录(一)

4 阅读2分钟

一、关于Apache Camel

Apache Camel 是一个开源集成框架,旨在简化系统集成,提供基于规则的路由和中介引擎。 因此通过camel框架可以轻松处理各系统之间的数据传递。

二、Camel中的概念

  • Endpoint(端点):数据的来源和去向。
  • Route(路由):定义了消息从源到目的地的流动路径。
  • Exchange(交换对象):Camel 内部的数据载体。
  • Processor(处理器):用于处理消息逻辑。

三、SpringBoot集成Camel框架

首先,将 Camel Core 和 Spring Boot Starter 模块添加到 Mavenpom.xml文件中:

    <!-- Spring Boot Starter模块 -->
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
        <version>${camel.version}</version> <!-- use the same version as your Camel core version -->
    </dependency>
    <!-- Camel Core -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>3.14.0</version>
    </dependency>

然后就可以在 Camel 路由中添加类,例如:

package com.example;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("timer:foo").to("log:bar");
    }
}

如果需要Camel支持其他中间件(如kafka等)消息传递,可以引入Camel组件,引入方法和URI配置方法参考Camel组件文档,例如需要Camel支持Kafka消息就需要引入Kafka组件:

<dependency> 
    <groupId>org.apache.camel</groupId> 
    <artifactId>camel-kafka</artifactId> 
    <version>x.x.x</version> 
    <!-- use the same version as your Camel core version --> 
</dependency>

四、Spring boot 中的Camel路由配置

1、路由

Apache Camel 提供了一种基于 Java 的领域特定语言 (DSL),可以通过继承RouteBuilder并实现configure方法来创建路由。以下是Camel官方给出的路由示例:

@Override
public void configure() { 
    // here is a sample which processes the input files 
    // (leaving them in place - see the 'noop' flag) 
    // then performs content based routing on the message using XPath
    from("file:src/data?noop=true") 
        .choice() 
            .when(xpath("/person/city = 'London'")) 
                .to("file:target/messages/uk") 
            .otherwise() 
                .to("file:target/messages/others"); 
}
  • from("file:src/data?noop=true")表示了路由的起点,在上面的例子中,我们只有一个路线,用于收集文件。
  • .choice用于标明后续会根据内容选择不同分支
    • .when类似于if,填写条件
    • .otherwise类似于else
  • .to表示路由的终点。

Camel 支持Gregor Hohpe 和 Bobby Woolf 在其优秀著作中介绍的大部分企业集成模式,针对不同消息传递模式,路由配置也不相同,详细参考Camel组件中的Enterprise Integration Patterns

2、URI

Camel 使用 URI标明端点信息,以kafka为例:

kafka:cheese?brokers=127.0.0.1:9092&clientId=foo
  • cheese是kafka使用的主题
  • brokers是要连接的远程 Kafka 代理,它是 Kafka 组件的一个配置项
  • clientId客户端 ID,它也是 Kafka 组件的一个配置项

实际上kafka URI的可配置参数很多,其他配置参数和其他中间件的配置方法可参考Camel组件文档

如果端点URI非常长,可以通过Java 文本块来声明它们:

from(""" 
        debezium-postgres:customerEvents 
        ?databasePassword={{myPassword}} 
        &databaseDbname=myDB 
        &databaseHostname=myHost 
        &pollIntervalMs=2000 
        &queryFetchSize=100 
    """) 
    .to("kafka:cheese");

或者自行编写URI创建函数:

public String generateUrl(String topic,
                          String brokers,
                          String groupId,
                          String autoOffsetReset,
                          String keyDeserializer,
                          String valueDeserializer
    StringBuilder sb = new StringBuilder();
    sb.append("kafka:").append(topic).append("?");
    sb.append("brokers=").append(brokers).append("&");
    sb.append("groupId=").append(groupId).append("&");
    sb.append("autoOffsetReset=").append(autoOffsetReset).append("&");
    sb.append("keyDeserializer=").append(keyDeserializer).append("&");
    sb.append("valueDeserializer=").append(valueDeserializer).append("&");
    return sb.toString();
}