Camel Spring Boot 入门


Red Hat build of Apache Camel for Spring Boot 3.14

Camel Spring Boot 入门

摘要

本指南介绍了红帽为 Spring Boot 构建的 Apache Camel 构建,并说明了使用红帽构建用于 Spring Boot 的 Apache Camel 创建和部署应用程序的各种方式。

前言

使开源包含更多

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。我们从这四个术语开始:master、slave、黑名单和白名单。由于此项工作十分艰巨,这些更改将在即将推出的几个发行版本中逐步实施。有关更多详情,请参阅我们的首席技术官 Chris Wright 提供的消息

第 1 章 Camel Spring Boot 入门

本指南介绍 Camel Spring Boot,并演示如何开始使用 Camel Spring Boot 构建应用程序:

1.1. Camel Spring Boot starters

Camel 支持 Spring Boot,为许多 Camel 组件提供 Camel 和 starters 的 自动配置。Camel 上下文自动检测可在 Spring 上下文中可用的 Camel 路由的看法,并将密钥 Camel 实用程序(如制作者模板、消费者模板和类型转换器)注册为 Bean。

注意

有关使用 Maven archtype 为 Spring Boot 应用程序生成 Camel 的详情,请参阅使用 Maven 为 Spring Boot 应用程序生成 Camel

首先,您必须将 Camel Spring Boot BOM 添加到 Maven pom.xml 文件中。

<dependencyManagement>

    <dependencies>
        <!-- Camel BOM -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot-bom</artifactId>
            <version>3.14.5.redhat-00012</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- ... other BOMs or dependencies ... -->
    </dependencies>

</dependencyManagement>
Copy to Clipboard Toggle word wrap

camel-spring-boot-bom 是一个基本的 BOM,包含 Camel Spring Boot starter JARs 列表。

接下来,添加 Camel Spring Boot starter 以启动 Camel 上下文

    <dependencies>
        <!-- Camel Starter -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
        </dependency>
        <!-- ... other dependencies ... -->
    </dependencies>
Copy to Clipboard Toggle word wrap

您还需要添加 Spring Boot 应用程序所需的任何组件启动器。???以下示例演示了如何将 auto-configuration starter 添加到 ActiveMQ 组件

    <dependencies>
        <!-- ... other dependencies ... -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-activemq-starter</artifactId>
        </dependency>
    </dependencies>
Copy to Clipboard Toggle word wrap

策展的 camel-spring-boot-dependencies BOM (生成的 BOM)包含已调整的 JAR,用于避免出现任何冲突。此 BOM 用于测试 camel-spring-boot 本身。

Spring Boot 用户可以选择使用 camel-spring-boot-bom,其中只有 Camel starter JARs 作为受管依赖项。但是,如果 Spring Boot 的第三方 JAR 不与特定 Camel 组件不兼容,则可能会导致类路径冲突。

1.1.2. Spring Boot 配置支持

每个 初学者 都会列出您可以在标准 application.propertiesapplication.yml 文件中配置的配置参数。这些参数具有 camel.component 的形式。[component-name].[parameter]。例如,您可以配置 ActiveMQ 代理的 URL:

camel.component.activemq.broker-url=tcp://localhost:61616
Copy to Clipboard Toggle word wrap

1.1.3. 添加 Camel 路由

Camel 路由 在 Spring 应用程序上下文中检测到,例如,会加载一个标有 org.springframework.stereotype.Component 的路由,并添加到 Camel 上下文并运行。

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

@Component
public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("...")
            .to("...");
    }

}
Copy to Clipboard Toggle word wrap

1.2. Spring Boot

Spring Boot 会自动为您配置 Camel。Camel 上下文自动检测可在 Spring 上下文中可用的 Camel 路由的看法,并将密钥 Camel 实用程序(如制作者模板、消费者模板和类型转换器)注册为 bean。

Maven 用户需要将以下依赖关系添加到其 pom.xml 中,才能使用此组件:

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot</artifactId>
    <version>3.14.5.redhat-00012</version> <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

Camel -spring-boot jar 与 spring.factories 文件一同推出,因此当将该依赖关系添加到类路径后,Spring Boot 会自动为您自动配置 Camel。

1.2.1. Camel Spring Boot Starter

Apache Camel 附带了一个 Spring Boot Starter 模块,允许您使用初学者开发 Spring Boot 应用程序。源代码中也有一个 示例应用

要使用初学者,请在 spring boot pom.xml 文件中添加以下内容:

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot-bom</artifactId>
    <version>3.14.5.redhat-00012</version> <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

然后,您只需使用 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");
    }
}
Copy to Clipboard Toggle word wrap

然后会自动启动这些路由。

您可以在 application.propertiesapplication.yml 文件中自定义 Camel 应用程序。

1.2.2. Spring Boot 自动配置

在 Spring Boot 中使用 spring-boot 时,请确保使用以下 Maven 依赖项支持自动配置:

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-spring-boot-starter</artifactId>
  <version>3.14.5.redhat-00012</version> <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

1.2.3. 自动配置的 Camel 上下文

Camel 自动配置提供的最重要功能是 CamelContext 实例。Camel 自动配置为您创建 SpringCamelContext,并处理该上下文的正确初始化和关闭。在 Spring 应用程序上下文中也注册了创建的 Camel 上下文(在 camelContext bean 名称下),因此您可以像任何其他 Spring bean 一样访问它。

@Configuration
public class MyAppConfig {

  @Autowired
  CamelContext camelContext;

  @Bean
  MyService myService() {
    return new DefaultMyService(camelContext);
  }

}
Copy to Clipboard Toggle word wrap

1.2.4. 自动探测 Camel 路由

Camel 自动配置从 Spring 上下文收集所有 RouteBuilder 实例,并将它们注入到提供的 CamelContext 中。这意味着,使用 Spring Boot starter 创建新的 Camel 路由就像将 @Component 注释类添加到类路径一样简单:

@Component
public class MyRouter extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    from("jms:invoices").to("file:/invoices");
  }

}
Copy to Clipboard Toggle word wrap

或在您的 @Configuration 类中创建新的路由 RouteBuilder bean:

@Configuration
public class MyRouterConfiguration {

  @Bean
  RoutesBuilder myRouter() {
    return new RouteBuilder() {

      @Override
      public void configure() throws Exception {
        from("jms:invoices").to("file:/invoices");
      }

    };
  }

}
Copy to Clipboard Toggle word wrap

1.2.5. Camel 属性

Spring Boot 自动配置会自动连接到 Spring Boot 外部配置 (可能包含属性占位符、操作系统环境变量或系统属性),且支持 Camel 属性。它基本上意味着 application.properties 文件中定义的任何属性:

route.from = jms:invoices
Copy to Clipboard Toggle word wrap

或者通过系统属性设置:

java -Droute.to=jms:processed.invoices -jar mySpringApp.jar
Copy to Clipboard Toggle word wrap

可用作 Camel 路由中的占位符:

@Component
public class MyRouter extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    from("{{route.from}}").to("{{route.to}}");
  }

}
Copy to Clipboard Toggle word wrap

1.2.6. 自定义 Camel 上下文配置

如果要在 CamelContext bean 上由 Camel 自动配置创建的一些操作,请在 Spring 上下文中注册 CamelContextConfiguration 实例:

@Configuration
public class MyAppConfig {

  @Bean
  CamelContextConfiguration contextConfiguration() {
    return new CamelContextConfiguration() {
      @Override
      void beforeApplicationStart(CamelContext context) {
        // your custom configuration goes here
      }
    };
  }

}
Copy to Clipboard Toggle word wrap

在启动 Spring 上下文 之前,ApplicationStart 的方法才会被调用,因此传递给这个回调的 CamelContext 实例完全自动配置。如果在 Spring 上下文中添加多个 CamelContextConfiguration 实例,则执行每个实例。

1.2.7. 自动配置的使用者和制作者模板

Camel 自动配置提供预先配置的 ConsumerTemplateProducerTemplate 实例。您只需将它们注入到 Spring 管理的 Bean 中:

@Component
public class InvoiceProcessor {

  @Autowired
  private ProducerTemplate producerTemplate;

  @Autowired
  private ConsumerTemplate consumerTemplate;

  public void processNextInvoice() {
    Invoice invoice = consumerTemplate.receiveBody("jms:invoices", Invoice.class);
    ...
    producerTemplate.sendBody("netty-http:http://invoicing.com/received/" + invoice.id());
  }

}
Copy to Clipboard Toggle word wrap

默认情况下,使用者模板和制作者模板以端点缓存大小设置为 1000。您可以通过修改以下 Spring 属性来更改这些值:

camel.springboot.consumer-template-cache-size = 100
camel.springboot.producer-template-cache-size = 200
Copy to Clipboard Toggle word wrap

1.2.8. Auto-configured TypeConverter

Camel 自动配置注册了 Spring 上下文中名为 typeConverterTypeConverter 实例。

@Component
public class InvoiceProcessor {

  @Autowired
  private TypeConverter typeConverter;

  public long parseInvoiceValue(Invoice invoice) {
    String invoiceValue = invoice.grossValue();
    return typeConverter.convertTo(Long.class, invoiceValue);
  }

}
Copy to Clipboard Toggle word wrap
1.2.8.1. Spring 类型转换 API 网桥

Spring 附带功能强大的 类型转换 API。Spring API 与 Camel 类型转换器 API 类似。因为这两个 API 都类似,Camel Spring Boot 会自动注册代表到 Spring 转换 API 的网桥转换器(SpringTypeConverter)。这意味着,开箱即用的 Camel 将处理 Spring Converters,如 Camel Converters。通过此方法,您可以使用 Camel 类型Converter API 访问的 Camel 和 Spring 转换器:

@Component
public class InvoiceProcessor {

  @Autowired
  private TypeConverter typeConverter;

  public UUID parseInvoiceId(Invoice invoice) {
    // Using Spring's StringToUUIDConverter
    UUID id = invoice.typeConverter.convertTo(UUID.class, invoice.getId());
  }

}
Copy to Clipboard Toggle word wrap

在 hood Camel Spring Boot 下,将转换委派给应用程序上下文中可用的 Spring 的 ConversionService 实例。如果没有 ConversionService 实例,Camel Spring Boot 自动配置将为您创建一个。

1.2.9. 保持应用程序处于活动状态

启用了这个功能的 Camel 应用程序在启动时启动新线程,以便完全通过防止 JVM 终止使应用程序保持处于活动状态。这意味着,在启动带有 Spring Boot 的 Camel 应用程序后,应用程序会等待 Ctrl+C 信号,且不会立即退出。

可以使用 camel.springboot.main-run-controllertrue 来激活控制器线程。

camel.springboot.main-run-controller = true
Copy to Clipboard Toggle word wrap

使用 web 模块的应用程序(例如,导入 org.springframework.boot:spring-boot-web-starter 模块的应用程序),通常不需要使用这个功能,因为应用程序会由存在其他非守护进程线程而保留。

1.2.10. 添加 XML 路由

默认情况下,您可以将 Camel XML 路由放在类路径下,它位于 camel-spring-boot 目录下,其中包括:您可以使用配置选项配置目录名称或关闭此名称:

# turn off
camel.springboot.routes-include-pattern = false
Copy to Clipboard Toggle word wrap
# scan only in the com/foo/routes classpath
camel.springboot.routes-include-pattern = classpath:com/foo/routes/*.xml
Copy to Clipboard Toggle word wrap

XML 文件应为 Camel XML 路由(而不是 < CamelContext&gt;),例如:

<routes xmlns="http://camel.apache.org/schema/spring">
    <route id="test">
        <from uri="timer://trigger"/>
        <transform>
            <simple>ref:myBean</simple>
        </transform>
        <to uri="log:out"/>
    </route>
</routes>
Copy to Clipboard Toggle word wrap

1.2.11. 测试 JUnit 5 方法

要进行测试,Maven 用户需要将以下依赖项添加到其 pom.xml 中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.7.4</version> <!-- Use the same version as your Spring Boot version -->
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-test-spring-junit5</artifactId>
    <version>3.14.5.redhat-00008</version> <!-- use the same version as your Camel core version -->
    <scope>test</scope>
</dependency>
Copy to Clipboard Toggle word wrap

要测试 Camel Spring Boot 应用,请使用 @CamelSpringBootTest 注解您的测试类。这会将 Camel 的 Spring Test 支持引入到应用程序,以便您可以使用 Spring Boot 测试规则编写测试

要获得 CamelContextProducerTemplate,您可以使用 @Autowired 将它们注入到类中。

您还可以使用 camel-test-spring-junit5 来配置测试声明性。本例使用 @MockEndpoints 注释来自动模拟端点:

@CamelSpringBootTest
@SpringBootApplication
@MockEndpoints("direct:end")
public class MyApplicationTest {

    @Autowired
    private ProducerTemplate template;

    @EndpointInject("mock:direct:end")
    private MockEndpoint mock;

    @Test
    public void testReceive() throws Exception {
        mock.expectedBodiesReceived("Hello");
        template.sendBody("direct:start", "Hello");
        mock.assertIsSatisfied();
    }

}
Copy to Clipboard Toggle word wrap

1.3. 组件发起器

Camel Spring Boot 支持以下 Camel 工件作为 Spring Boot Starters:

注意

以下列出的某些工件还没有参考文档。本文档将在可用后马上发布。

Expand
表 1.1. Camel 组件
组件工件描述

AWS Cloudwatch

camel-aws2-cw-starter

使用 AWS SDK 版本 2.x 将指标发送到 AWS CloudWatch。

AWS DynamoDB

camel-aws2-ddb-starter

使用 AWS SDK 版本 2.x 从 AWS DynamoDB 服务存储和检索数据。

AWS Kinesis

camel-aws2-kinesis-starter

使用 AWS SDK 版本 2.x 从 和 生成记录到 AWS Kinesis Streams。

AWS Lambda

camel-aws2-lambda-starter

使用 AWS SDK 版本 2.x 管理并调用 AWS Lambda 功能。

AWS S3 Storage Service

camel-aws2-s3-starter

使用 AWS SDK 版本 2.x 从 AWS S3 Storage Service 存储和检索对象。

AWS 简单通知系统(SNS)

camel-aws2-sns-starter

使用 AWS SDK 版本 2.x 向 AWS 简单通知主题发送消息。

AWS Simple Queue Service (SQS)

camel-aws2-sqs-starter

使用 AWS SDK 版本 2.x 从 AWS SQS 服务发送和接收信息。

Azure Storage Blob Service

camel-azure-storage-blob-starter

使用 SDK v12 从 Azure Storage Blob Service 存储和检索 Blob。

Azure Storage Queue Service

camel-azure-storage-queue-starter

azure-storage-queue 组件使用 Azure SDK v12 存储和检索从 Azure Storage Queue 到/from Azure Storage Queue 的信息。

bean

camel-bean-starter

调用存储在 Camel 注册表中的 Java Bean 方法。

bean Validator

camel-bean-validator-starter

使用 Java Bean Validation API 验证消息正文。

浏览

camel-browse-starter

检查支持 BrowsableEndpoint 的端点上收到的消息。

Cassandra CQL

camel-cassandraql-starter

使用 CQL3 API 与 Cassandra 2.0 集成(而不是 Thrift API)。基于 DataStax 提供的 Cassandra Java 驱动程序。

控制总线

camel-controlbus-starter

管理和监控 Camel 路由.

cron

camel-cron-starter

通过 Unix cron 语法触发事件的通用接口。

CXF

camel-cxf-soap-starter

使用 Apache CXF 公开 SOAP WebServices 或使用 CXF WS 客户端连接到外部 Web 服务。

数据格式

camel-dataformat-starter

使用 Camel 数据格式作为常规 Camel 组件。

dataset

camel-dataset-starter

提供用于加载和 soak 测试 Camel 应用程序的数据。

直接

camel-direct-starter

同步从同一 Camel 上下文调用另一个端点。

FHIR

camel-fhir-starter

使用 FHIR (Fast healthcare Interoperability Resources)标准交换医疗域中的信息。

File

camel-file-starter

读写文件。

FTP

camel-ftp-starter

从 FTP 服务器上传文件并下载文件。

HTTP

camel-http-starter

使用 Apache HTTP 客户端 4.x 将请求发送到外部 HTTP 服务器。

Infinispan

camel-infinispan-starter

从/到 Infinispan 分布式键/值存储和数据网格进行读取和写入。

JIRA

camel-jira-starter

与 JIRA 问题跟踪程序交互。

JMS

camel-jms-starter

发送并接收来自 JMS Queue 或 Topic 的消息。

Kafka

camel-kafka-starter

从 Apache Kafka 代理发送和接收消息。

Kamelet

camel-kamelet-starter

打 Kamelets

语言

camel-language-starter

使用 Camel 支持的任何语言执行脚本。

Log

camel-log-starter

日志消息记录到底层日志机制。

邮件

camel-mail-starter

使用 imap、pop3 和 smtp 协议发送和接收电子邮件。

Master

camel-master-starter

集群中只有一个消费者使用给定端点,如果 JVM 中断,则具有自动故障转移。

MLLP

camel-mllp-starter

使用 MLLP 协议与外部系统通信。

Mock

camel-mock-starter

使用模拟测试路由和调解规则。

MongoDB

camel-mongodb-starter

对 MongoDB 文档和集合执行操作。

Netty

camel-netty-starter

使用带有 Netty 4.x 的 TCP 或 UDP 的套接字级别网络。

Paho

camel-paho-starter

使用 Eclipse Paho MQTT 客户端与 MQTT 消息代理进行通信。

Paho MQTT 5

camel-paho-mqtt5-starter

使用 Eclipse Paho MQTT v5 客户端与 MQTT 消息代理进行通信。

Quartz

camel-quartz-starter

使用 Quartz 2.x 调度程序来调度消息发送。

Ref

camel-ref-starter

将消息路由到端点,具体由 Camel 注册表中的名称动态查找。

REST

camel-rest-starter

公开 REST 服务或调用外部 REST 服务。

Salesforce

camel-salesforce-starter

使用 Java DTOs 与 Salesforce 通信。

scheduler

camel-scheduler-starter

使用 java.util.concurrent.ScheduledExecutorService 指定的时间间隔生成消息。

SEDA

camel-seda-starter

从同一 JVM 中的任何 Camel 上下文异步调用另一端点。

servlet

camel-servlet-starter

为 Servlet 提供 HTTP 请求。

Slack

camel-slack-starter

从 Slack 发送和接收信息。

SQL

camel-sql-starter

使用 Spring JDBC 执行 SQL 查询。

Stub

camel-stub-starter

在开发或测试时存出任何物理端点。

Telegram

camel-telegram-starter

发送并接收作为 Telegram Bot Telegram Bot API 的消息。

timer

camel-timer-starter

使用 java.util.Timer 指定的时间间隔生成消息。

验证器

camel-validator-starter

使用 XML Schema 和 JAXP Validation 验证载荷。

Webhook

camel-webhook-starter

公开 webhook 端点以接收其他 Camel 组件的推送通知。

XSLT

camel-xslt-starter

使用 XSLT 模板转换 XML 有效负载。

Expand
表 1.2. Camel 数据格式
组件工件描述

Avro

camel-avro-starter

使用 Apache Avro 二进制数据格式对消息进行序列化和反序列化。

Avro Jackson

camel-jackson-avro-starter

marshal POJO 到 Avro,并使用 Jackson 返回。

Bindy

camel-bindy-starter

使用 Camel Bindy 在 POJO 和键值对(KVP)格式之间marshal 和 unmarshal

HL7

camel-hl7-starter

marshal 和 unmarshal HL7 (Health Care)使用 HL7 MLLP codec 进行对象建模。

JacksonXML

camel-jacksonxml-starter

向 POJO 联合 XML 有效负载,并使用 Jackson 的 XMLMapper 扩展进行后退。

JAXB

camel-jaxb-starter

将 XML 有效负载联合到 POJO,并使用 JAXB2 XML 总结标准.

JSON Gson

camel-gson-starter

marshal POJO 使用 Gson 到 JSON 并返回

JSON Jackson

camel-jackson-starter

marshal POJO 到 JSON 和使用 Jackson 返回

protobuf Jackson

camel-jackson-protobuf-starter

marshal POJO 到 Protobuf,使用 Jackson 返回。

SOAP

camel-soap-starter

marshal Java 对象到 SOAP 消息并返回。

zip 文件

camel-zipfile-starter

使用 java.util.zip.ZipStream 进行压缩和解压缩流.

Expand
表 1.3. Camel 语言
语言工件描述

常数

camel-core-starter

一个固定的值只在路由启动期间设置一次。

CSimple

camel-core-starter

评估编译的简单表达式。

ExchangeProperty

camel-core-starter

从 Exchange 获取属性。

File

camel-core-starter

简单语言的文件相关功能。

标头

camel-core-starter

从 Exchange 获取标头。

JSONPath

camel-jsonpath-starter

针对 JSON 消息正文评估 JSONPath 表达式。

Ref

camel-core-starter

使用 registry 中的现有表达式。

Simple(简单)

camel-core-starter

评估 Camel 简单表达式。

令牌化

camel-core-starter

使用分隔符模式映射文本有效负载。

XML 令牌

camel-xml-jaxp-starter

令牌化 XML 载荷。

XPath

camel-xpath-starter

根据 XML 有效负载评估 XPath 表达式。

XQuery

camel-saxon-starter

使用 XQuery 和 Saxon 查询和/或转换 XML 有效负载。

Expand
表 1.4. 其它扩展
扩展工件描述

Openapi Java

camel-openapi-java-starter

使用 openapi doc 的 rest-dsl 支持

1.4. 初学程序配置

清除且可访问的配置是任何应用程序的关键部分。Camel 启动者完全支持 Spring Boot 的外部配置机制。您还可以通过 Spring Beans 为更复杂的用例配置它们。

1.4.1. 使用外部配置

在内部,每个 起始 者都通过 Spring Boot 的 ConfigurationProperties 配置。每个配置参数可以以不同的方式设置(application.[properties|json|yaml] 文件、命令行参数、环境变量等)。https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config参数格式为 camel。[component|language|dataformat].[name].[parameter]

例如,您可以配置 ActiveMQ 代理的 URL:

camel.component.activemq.broker-url=tcp://localhost:61616
Copy to Clipboard Toggle word wrap

或将 CSV 数据格式的精简配置为可以设置的分号(;):

camel.dataformat.csv.delimiter=;
Copy to Clipboard Toggle word wrap

在将属性设置为所需类型时,Camel 将使用 Type Converter 机制。

您可以使用 #bean:name 来引用 registry 中的 Bean:

camel.component.jms.transactionManager=#bean:myjtaTransactionManager
Copy to Clipboard Toggle word wrap

Bean 通常会在 Java 中创建:

@Bean("myjtaTransactionManager")
public JmsTransactionManager myjtaTransactionManager(PooledConnectionFactory pool) {
    JmsTransactionManager manager = new JmsTransactionManager(pool);
    manager.setDefaultTimeout(45);
    return manager;
}
Copy to Clipboard Toggle word wrap

也可以 在配置文件中 创建 Bean,但不建议用于复杂的用例。

1.4.2. 使用 Beans

也可以通过 Spring Beans 创建和配置者。在创建初学者之前,Camel 首先会在注册表中查找它(如果名称已存在)。例如,配置 Kafka 组件:

@Bean("kafka")
public KafkaComponent kafka(KafkaConfiguration kafkaconfiguration){
    return ComponentsBuilderFactory.kafka()
                        .brokers("{{kafka.host}}:{{kafka.port}}")
                        .build();
}
Copy to Clipboard Toggle word wrap

Bean 名称必须与您配置的组件、数据格式或语言相同。如果在注解中没有指定 Bean 名称,它将被设置为方法名称。

典型的 Camel Spring Boot 项目将使用外部配置和 Bean 的组合来配置应用程序。有关如何配置 Camel Spring Boot 项目的更多示例,请参阅示例 存储库

1.5. 使用 Maven 为 Spring Boot 应用程序生成 Camel

您可以使用 Maven archetype org.apache.camel.archetypes:camel-archetype-spring-boot:3.14.5.redhat-00012 生成 Camel Spring Boot 应用。

流程

  1. 运行以下命令:

    mvn archetype:generate \
     -DarchetypeGroupId=org.apache.camel.archetypes \
     -DarchetypeArtifactId=camel-archetype-spring-boot \
     -DarchetypeVersion=3.14.5.redhat-00012 \
     -DgroupId=com.redhat \
     -DartifactId=csb-app \
     -Dversion=1.0-SNAPSHOT \
     -DinteractiveMode=false
    Copy to Clipboard Toggle word wrap
  2. 构建应用程序:

    mvn package -f csb-app/pom.xml
    Copy to Clipboard Toggle word wrap
  3. 运行应用程序:

    java -jar csb-app/target/csb-app-1.0-SNAPSHOT.jar
    Copy to Clipboard Toggle word wrap
  4. 通过检查由应用生成的 Hello World 输出的 console 日志来验证应用是否正在运行。

    com.redhat.MySpringBootApplication       : Started MySpringBootApplication in 3.514 seconds (JVM running for 4.006)
    Hello World
    Hello World
    Copy to Clipboard Toggle word wrap

第 2 章 迁移到 Camel Spring Boot

本指南提供有关在 Spring Boot 上从 Red Hat Fuse 7 迁移到 Camel 3 的信息。

2.1. Java 版本

Camel 3 支持 Java 11,但不支持 Java 8。在 Java 11 中,JAXB 模块已从 JDK 中删除,因此您将需要将它们添加为 Maven 依赖项(如果您使用 XML DSL 或 camel-jaxb 组件时):

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0.1</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.2</version>
</dependency>
Copy to Clipboard Toggle word wrap

2.2. The Modularization of camel-core

在 Camel 3.x 中,Carl-core 被分成多个 JARs,如下所示:

  • camel-api
  • Camel-base
  • camel-caffeine-lrucache
  • camel-cloud
  • camel-core
  • camel-jaxp
  • camel-main
  • camel-management-api
  • camel-management
  • camel-support
  • camel-util
  • camel-util-json

Apache Camel 的 Maven 用户可使用依赖项的 camel-core,其对所有模块的依赖关系都有一定的依赖关系,除了 camel-main 除外,因此不需要迁移。

2.3. 组件模块化

在 Camel 3.x 中,一些 camel-core 组件被移到独立的组件中。

  • camel-attachments
  • camel-bean
  • Camel-browse
  • camel-controlbus
  • camel-dataformat
  • camel-dataset
  • camel-direct
  • camel-directvm
  • scamel-file
  • Camel-language
  • camel-log
  • camel-mock
  • Camel-ref
  • camel-rest
  • camel-saga
  • camel-scheduler
  • camel-seda
  • camel-stub
  • amel-timer
  • camel-validator
  • camel-vm
  • camel-xpath
  • camel-xslt
  • camel-xslt-saxon
  • camel-zip-deflater

2.4. Spring Boot starters 的更改

Spring Boot starters 的 Maven groupIdorg.apache.camel 改为 org.apache.camel.springboot

Example

使用:

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-component-starter</artifactId>
</dependency>
Copy to Clipboard Toggle word wrap

而不是

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-component-starter</artifactId>
</dependency>
Copy to Clipboard Toggle word wrap

2.5. 不支持每个应用程序有多个 CamelContexts

删除了对多个 CamelContext 的支持,并且每个部署只能有一个 CamelContext。因此,各种 Camel 注释上的 context 属性(如 @EndpointInject@Produce@Consume 等)已被删除。

2.6. 弃用的 API 和组件

在 Camel 2.x 中删除了所有已弃用的 API 和组件。

2.6.1. 删除的组件

Camel 2.x 中的所有已弃用的组件均在 Camel 3.x 中删除,包括旧的 camel-httpcamel-hdfscamel-minacamel-mongodbcamel-netty -httpcamel-quartzcamel-restletcamel-rx 组件。

  • 删除了 camel-jibx 组件。
  • 删除了 camel-boon 数据格式。
  • 删除了 camel-linkedin 组件,因为 Linkedin API 1.0 不再被支持CAMEL-13813 可跟踪对新 2.0 API 的支持。
  • camel-zookeeper 将其路由策略功能被删除,而是使用 ZooKeeperClusterServicecamel-zookeeper-master 组件。
  • camel-jetty 组件不再支持制作者(已被移除),而是使用 camel-http 组件。
  • 二十个 流流 组件已被删除,因为它依赖于已弃用的 Twitter Streaming API,它无法正常工作。

2.6.2. 重命名的组件

在 Camel 3.x 中重命名了以下组件。

  • 测试 组件已重命名为 dataset-test,并将其从 camel-core 移到 camel-dataset JAR。
  • http4 组件已重命名为 http,它对应的组件软件包来自于 org.apache.camel.component.http4org.apache.camel.component.http。支持的方案现在只有 httphttps
  • hdfs2 组件已重命名为 hdfs,它对应的组件软件包来自于 org.apache.camel.component.hdfs2org.apache.camel.component.hdfs。现在支持的方案是 hdfs
  • mina2 组件已重命名为 mina,它对应的组件软件包来自于 org.apache.camel.component.mina2org.apache.camel.component.mina。现在,支持的方案为 mina
  • mongodb3 组件已重命名为 mongodb,它对应的组件软件包来自于 org.apache.camel.component.mongodb3org.apache.camel.mongodb.mongodb。现在,支持的方案是 mongodb
  • netty4-http 组件已重命名为 netty-http,它对应的组件软件包来自于 org.apache.camel.component.netty4.httporg.apache.camel.component.netty.http。现在,支持的方案是 netty-http
  • netty4 组件已重命名为 netty,它对应的组件软件包来自于 org.apache.camel.component.netty4org.apache.camel.component.netty。现在,支持的方案是 netty
  • quartz2 组件已重命名为 quartz,它对应的组件软件包来自于 org.apache.camel.component.quartz2org.apache.camel.component.quartz。现在,支持的方案是 quartz
  • rxjava2 组件已重命名为 rxjava,它对应的组件软件包来自于 org.apache.camel.component.rxjava2org.apache.camel.component.rxjava
  • 重命名为 camel-jetty9camel-jetty。现在支持的方案是 jetty

2.6.3. 模拟组件

模拟 组件已移出 camel-core。由于其断ion子 构建器 上的许多方法已被删除。

2.6.4. ActiveMQ

如果您使用 activemq-camel 组件,您应该迁移到 camel-activemq 组件,其中组件名称已从 org.apache.activemq.camel.component.ActiveMQComponent 改为 org.apache.camel.component.activemq.ActiveMQComponent

2.6.5. AWS

组件 camel-aws 已被分成多个组件:

  • camel-aws-cw
  • Camel-aws-ddb (包含 ddb 和 ddbstreams 组件)
  • camel-aws-ec2
  • camel-aws-iam
  • Camel-aws-kinesis (包含 kinesis 和 kinesis-firehose 组件)
  • camel-aws-kms
  • camel-aws-lambda
  • camel-aws-mq
  • camel-aws-s3
  • camel-aws-sdb
  • camel-aws-ses
  • camel-aws-sns
  • camel-aws-sqs
  • camel-aws-swf
注意

建议您为这些组件添加 specifc 依赖项。

2.6.6. FHIR

camel-fhir 组件会将其 hapi-fhir 依赖项升级到 4.1.0。默认的 FHIR 版本已改为 R4。因此,如果需要明确设置 DSTU3。

2.6.7. Kafka

camel-kafka 组件删除了 options bridgeEndpointcircularTopicDetection,因为组件充当 Camel 2.x。换句话说,camel-kafka 将向端点 uri 中的主题发送信息。要覆盖此项,请使用带有新主题的 KafkaConstants.OVERRIDE_TOPIC 标头。请参阅 camel-kafka 组件文档中的更多详情。

2.6.8. Telegram

camel-telegram 组件已将授权令牌从 uri-path 移到查询参数,如迁移。

telegram:bots/myTokenHere
Copy to Clipboard Toggle word wrap

telegram:bots?authorizationToken=myTokenHere
Copy to Clipboard Toggle word wrap

2.6.9. JMX

如果您使用 camel-core 作为依赖项运行 Camel 独立,您希望 JMX 开箱即用,那么您需要添加 camel-management 作为依赖项。

对于使用 ManagedCamelContext,您现在可以从 CamelContext 获取此扩展,如下所示:

ManagedCamelContext managed = camelContext.getExtension(ManagedCamelContext.class);
Copy to Clipboard Toggle word wrap

2.6.10. XSLT

XSLT 组件已从 camel-core 移到 camel-xsltcamel-xslt-saxon 中。组件被分隔,因此 camel-xslt 是使用 JDK XSTL 引擎(Xalan),而 camel-xslt-saxon 是使用 Saxon 时。这意味着,您应该使用 xsltxslt-saxon 作为 Camel 端点 URI 中的组件名称。如果您使用 XSLT 聚合策略,则使用 org.apache.camel.component.xslt.saxon.XsltSaxonAggregationStrategy 作为 Saxon 支持。如果使用 xslt builder,则使用 org.apache.camel.component.xslt.XsltSaxonBuilder 支持。另请注意,allowStax 只支持 camel-xslt-saxon,因为 JDK XSLT 不支持它。

2.6.11. XML DSL 迁移

XML DSL 稍有变化。

自定义负载均衡器 EIP 从 < custom> 改为 &lt; customLoadBalancer>

XMLSecurity 数据格式已将属性 keyOrTrustStoreParametersId 重命名为 < secureXML > 标签中的 keyOrTrustStoreParametersRef

& lt;zipFile& gt; 数据格式已重命名为 < zipfile>

2.7. 迁移 Camel Maven 插件

camel-maven-plugin 已被分成两个 maven 插件:

camel-maven-plugin
Camel-maven-plugin 具有 run 目标,主要用于快速运行 Camel 应用程序。如需更多信息,请参阅 https://camel.apache.org/manual/camel-maven-plugin.html
camel-report-maven-plugin
camel-report-maven-plugin 具有 验证和 路由分类 目标,用于生成 Camel 项目的报告,如验证 Camel 端点 URI 和路由覆盖报告等。如需更多信息,请参阅 https://camel.apache.org/manual/camel-report-maven-plugin.html
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat