Camel Spring Boot 入门


Red Hat Integration 2023.q1

Camel Spring Boot 入门

摘要

本指南介绍了 Camel Spring Boot,并解释了使用 Camel Spring Boot 创建和部署应用程序的各种方法。

前言

使开源包含更多

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

第 1 章 Camel Spring Boot 入门

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

1.1. Camel Spring Boot Starters

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

注意

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

首先,您必须将 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.18.3.redhat-00025</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- ... other BOMs or dependencies ... -->
    </dependencies>

</dependencyManagement>
Copy to Clipboard Toggle word wrap

camel-spring-boot-bom 是包含 Camel Spring Boot starter JAR 列表的基本 BOM。

接下来,添加 Camel Spring Boot 启动程序 以启动 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 应用程序所需的任何组件启动程序。下例演示了如何将 自动配置初学者 添加到 ActiveMQ 组件

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

curated camel-spring-boot-dependencies BOM (生成)包含 Spring Boot 和 Apache Camel 使用调整的 JAR,以避免任何冲突。此 BOM 用于测试 camel-spring-boot 本身。

Spring Boot 用户可以选择使用 Camel 依赖项,方法是使用 camel-spring-boot-bom,该依赖项只有 Camel 启动器 JAR 作为受管依赖项。但是,如果来自 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 路由

在 Spring 应用程序上下文中检测到 Camel 路由,例如,将带有 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.18.3.redhat-00025</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.18.3.redhat-00025</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 Auto-configuration

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

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-spring-boot-starter</artifactId>
  <version>3.18.3.redhat-00025</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,并负责该上下文的正确初始化和关闭。创建的 Camel 上下文也已在 Spring 应用程序上下文中注册(在 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 属性支持的属性占位符、OS 环境变量或系统属性)。它基本上意味着 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 上下文配置

如果要对 Camel 自动配置创建的 CamelContext bean 执行一些操作,请在 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 类型 converter API 类似。因为两个 API 都是类似的,Camel Spring Boot 会自动注册一个网桥转换器(SpringTypeConverter),它被委派给 Spring 转换 API。这意味着,开箱即用的 Camel 将像 Camel 一样对待 Spring Converters。使用这个方法,您可以使用 Camel TypeConverter 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-controller 激活到 true

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 路由放在 classpath 下,该目录的 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.6</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.18.3.redhat-00022</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 以正常 Spring 方式将它们注入到类。

您还可以使用 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 组件
组件工件描述

AMQP

camel-amqp-starter

使用 Apache QPid 客户端带有 AMQP 协议的消息传递.

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 Simple Notification System (SNS)

camel-aws2-sns-starter

使用 AWS SDK 版本 2.x 发送消息到 AWS Simple Notification Topic。

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 的消息。

bean

camel-bean-starter

调用存储在 Camel registry 中的 Java Bean 的方法。

Bean Validator

camel-bean-validator-starter

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

浏览

camel-browse-starter

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

Cassandra CQL

camel-cassandraql-starter

使用 CQL3 API (而不是 Thrift API)与 Cassandra 2.0 集成。基于 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

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

direct

camel-direct-starter

同步调用来自同一 Camel 上下文的另一个端点。

链接:https://access.redhat.com/documentation/zh-cn/red_hat_integration/2023.q1/html-single/camel_spring_boot_reference/index#csb-camel-elasticsearch-component-starter

camel-elasticsearch-starter

通过 Java 客户端 API 将请求发送到 ElasticSearch。

FHIR

camel-fhir-starter

使用 FHIR (Fast Healthcare 互操作性资源)标准在医疗域中交换信息。

File

camel-file-starter

读写文件。

FTP

camel-ftp-starter

从 FTP 服务器上传文件并将其下载到/从 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

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

mail

camel-mail-starter

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

Master

camel-master-starter

集群中只有一个消费者,消耗给定端点;如果 JVM 死机,则自动故障转移。

Minio

camel-minio-starter

使用 Minio SDK 从 Minio Storage Service 存储和检索对象。

MLLP

camel-mllp-starter

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

Mock

camel-mock-starter

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

MongoDB

camel-mongodb-starter

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

Netty

camel-netty-starter

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

Paho

camel-paho-starter

使用 Eclipse Paho MQTT Client 与 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 Registry 中按名称动态查找。

REST

camel-rest-starter

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

Salesforce

camel-salesforce-starter

使用 Java DTO 与 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 people)模型对象使用 HL7 MLLP codec。

JacksonXML

camel-jacksonxml-starter

将 XML 有效负载合并到 POJO,并使用 Jackson 的 XMLMapper 扩展。

JAXB

camel-jaxb-starter

unmarshal XML 有效负载到 POJO,使用 JAXB2 XML marshalling 标准。

JSON Gson

camel-gson-starter

marshal POJO,使用 Gson

JSON Jackson

camel-jackson-starter

marshal POJOs 到 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 简单表达式.

tokenize

camel-core-starter

使用分隔符模式编码文本有效负载。

XML Tokenize

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

rest-dsl 支持使用 openapi doc

1.4. 初学者配置

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

1.4.1. 使用外部配置

在内部,每个 初学者 都通过 Spring Boot 的 ConfigurationProperties 配置。每个配置参数都可以 以各种方式 设置(application.[properties|json|yaml] 文件、命令行参数、环境变量等)。参数的格式为 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:

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.18.3.redhat-00025 生成 Camel Spring Boot 应用程序。

流程

  1. 运行以下命令:

    mvn archetype:generate \
     -DarchetypeGroupId=org.apache.camel.archetypes \
     -DarchetypeArtifactId=camel-archetype-spring-boot \
     -DarchetypeVersion=3.18.3.redhat-00025 \
     -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 输出的控制台日志来验证应用是否正在运行。

    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 组件时,请使用 JAXB (如使用 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. camel-core 的模块化

在 Camel 3.x 中,camel-core 已分成多个 JAR,如下所示:

  • 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
  • Camel-file
  • Camel-language
  • camel-log
  • camel-mock
  • Camel-ref
  • camel-rest
  • camel-saga
  • camel-scheduler
  • camel-seda
  • camel-stub
  • Camel-timer
  • camel-validator
  • camel-vm
  • camel-xpath
  • camel-xslt
  • camel-xslt-saxon
  • camel-zip-deflater

2.4. 对 Spring Boot 启动者的更改

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

示例

使用:

<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

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

2.6. 弃用的 API 和组件

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

2.6.1. 删除的组件

Camel 2.x 中的所有已弃用的组件均在 Camel 3.x 中删除,包括旧的 camel-http,camel-hdfs,camel-mina,camel-mongodb,camel-netty,camel-netty-http,camel-quartz,camel-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 组件已被删除,因为它依赖于已弃用的 Twitter Streaming API,且无法正常工作。

2.6.2. 重命名组件

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

  • 测试 组件已重命名为 dataset-test,并移出 camel-corecamel-dataset JAR。
  • http4 组件已重命名为 http,它是对应的组件软件包从 org.apache.camel.component.http4 重命名为 org.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 2 的软件包。支持的方案现在是 mina
  • mongodb3 组件已重命名为 mongodb,它是从 org.apache.camel.component.mongodb3org.apache.camel.component.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.netty4 重命名为 org.apache.camel.component.netty。支持的方案现在为 netty
  • quartz2 组件已重命名为 quartz,它是从 org.apache.camel.component.quartz2org.apache.camel.component.quartz 的对应组件软件包。支持的方案现在是 quartz
  • rxjava2 组件已重命名为 rxjava,它对应的组件软件包从 org.apache.camel.component.rxjava2 重命名为 org.apache.camel.component.rxjava
  • camel-jetty9 重命名为 camel-jetty。支持的方案现在是 jetty

2.6.3. 模拟组件

模拟 组件已从 camel-core 移出。由于其 assertion 子句构建器 上的这一方法已被删除。

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 组件删除了 bridgeEndpointcircularTopicDetection 选项,因为组件不再需要,因为组件在 Camel 2.x 上正常工作。换句话说 camel-kafka 将从 endpoint uri 中发送消息到主题。要覆盖它,请使用 KafkaConstants.OVERRIDE_TOPIC 标头的新主题。请参阅 camel-kafka 组件文档以了解更多信息。

2.6.8. Telegram

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

telegram:bots/myTokenHere
Copy to Clipboard Toggle word wrap

to

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 时。这意味着,您应该在 Camel 端点 URI 中使用 xsltxslt-saxon 作为组件名称。如果您使用 XSLT 聚合策略,则对 Saxon 支持使用 org.apache.camel.component.xslt.saxon.XsltSaxonAggregationStrategy。如果使用 xslt builder,则使用 org.apache.camel.component.xslt.saxon.XsltSaxonBuilder 进行 Saxon 支持。另请注意,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 具有 validateroute-coverage 目标,用于生成 Camel 项目报告,如验证 Camel 端点 URI 和路由覆盖报告等。如需更多信息,请参阅 https://camel.apache.org/manual/camel-report-maven-plugin.html

法律通告

Copyright © 2023 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat