Camel Spring Boot 入门


Red Hat Integration 2022.Q3

Camel Spring Boot 入门

摘要

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

前言

使开源包含更多

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

第 1 章 Camel Spring Boot 入门

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

1.1. Camel Spring Boot starters

对 Spring Boot 的 Camel 支持为 Camel 提供自动配置,并为许多 Camel 组件 提供入门。Spring 上下文的 Camel 上下文自动探测 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.2.redhat-00054</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- ... other BOMs or dependencies ... -->
    </dependencies>

</dependencyManagement>

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>

您还需要添加 Spring Boot 应用程序所需的任何组件 开始者。以下示例演示了如何将 自动配置入门 程序添加到 ActiveMQ 组件中

    <dependencies>
        <!-- ... other dependencies ... -->
        <dependency>
            <groupId>org.apache.camel.springboot</groupId>
            <artifactId>camel-activemq-starter</artifactId>
        </dependency>
    </dependencies>

生成的 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

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("...");
    }

}

1.2. Spring Boot

Spring Boot 会自动配置 Camel。Spring 上下文中提供的 Camel 上下文自动探测 Camel 路由的评论性自动配置,并将主要 Camel 实用程序(如制作者模板、消费者模板和类型转换器)注册为 Bean。

Maven 用户需要在其 pom.xml 中添加以下依赖项才能使用这个组件:

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot</artifactId>
    <version>3.14.2.redhat-00054</version> <!-- use the same version as your Camel core version -->
</dependency>

Camel -spring-boot jar 附带了 spring.factories 文件,因此当您将依赖项添加到类路径时,Spring Boot 会自动为您配置 Camel。

1.2.1. Camel Spring Boot Starter

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

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

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot-bom</artifactId>
    <version>3.14.2.redhat-00054</version> <!-- use the same version as your Camel core 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");
    }
}

这些路由将自动启动。

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

1.3. Spring Boot 自动配置

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

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-spring-boot-starter</artifactId>
  <version>3.14.2.redhat-00054</version> <!-- use the same version as your Camel core version -->
</dependency>

1.3.1. 自动配置 Camel 上下文

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

@Configuration
public class MyAppConfig {

  @Autowired
  CamelContext camelContext;

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

}

1.3.2. 自动探测 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");
  }

}

或者在 @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");
      }

    };
  }

}

1.3.3. Camel 属性

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

route.from = jms:invoices

或者通过系统属性设置:

java -Droute.to=jms:processed.invoices -jar mySpringApp.jar

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

@Component
public class MyRouter extends RouteBuilder {

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

}

1.3.4. 自定义 Camel 上下文配置

如果要在 CamelContextan 中对 CamelContext an 执行一些操作,请在 Spring 上下文中注册 CamelContextConfiguration 实例:

@Configuration
public class MyAppConfig {

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

}

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

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

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());
  }

}

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

camel.springboot.consumer-template-cache-size = 100
camel.springboot.producer-template-cache-size = 200

1.3.6. 自动配置 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);
  }

}
1.3.6.1. Spring 类型转换 API 网桥

Spring 附带了强大的 类型转换 API。Spring API 与 Camel 类型转换器 API 类似。因为两个 API 与此类似,Camel Spring Boot 会自动注册到 Spring conversion API 的桥接转换程序(SpringTypeConverter)。这意味着,开箱即用的 Camel 将对待诸如 Camel 的 Spring Converters。使用这个方法,您可以使用 Camel TypeConverter API 访问的 Camel 和 Spring converters:

@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());
  }

}

在 hood Camel Spring Boot 下,它将转换至应用程序上下文中可用的 Spring ConversionService 实例。如果没有可用的 ConversionService 实例,Camel Spring Boot auto-configuration 将为您创建一个。

1.3.7. 使应用程序保持活跃

使用此功能的 Camel 应用程序在启动时启用一个新的线程,以满足防止 JVM 终止而让应用程序保持活动的唯一目的。这意味着,在使用 Spring Boot 启动 Camel 应用程序后,您的应用程序会等待 Ctrl+C 信号,且不会立即退出。

可以使用 camel.springboot.main-run-controller 激活控制器线程到 true

camel.springboot.main-run-controller = true

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

1.3.8. 添加 XML 路由

默认情况下,您可以将 Camel XML 路由放在目录 camel 下的类路径中,该路径 camel-spring-boot 将自动检测并包括:您可以使用配置选项配置目录名称或关闭此名称:

# turn off
camel.springboot.routes-include-pattern = false
# scan only in the com/foo/routes classpath
camel.springboot.routes-include-pattern = classpath:com/foo/routes/*.xml

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>

1.3.9. 测试 JUnit 5 方法

要测试,Maven 用户需要在 pom.xml 中添加以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.7.1</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.2.redhat-00067</version> <!-- use the same version as your Camel core version -->
    <scope>test</scope>
</dependency>

要测试 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();
    }

}

1.3.10. 测试 JUnit 4 方法(已弃用)

要测试,Maven 用户需要在 pom.xml 中添加以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.7.1</version> <!-- Use the same version as your Spring Boot version -->
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-test-spring</artifactId>
    <version>3.14.2.redhat-00067</version> <!-- use the same version as your Camel core version -->
    <scope>test</scope>
</dependency>

要测试 Camel Spring Boot 应用程序,请使用 @RunWith(CamelSpringBootRunner.class) 标注测试类。这会将 Camel 的 Spring Test 支持引入到应用程序中,以便您可以使用 Spring Boot 测试规则编写测试

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

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

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints("direct:end")
public class MyApplicationTest {

    @Autowired
    private ProducerTemplate template;

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

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

}

1.4. 组件开始

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 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 注册表中的 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 语法指定事件的通用接口。

数据格式

camel-dataformat-starter

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

Dataset

camel-dataset-starter

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

Direct

camel-direct-starter

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

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

使用MAP、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

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

Paho

camel-paho-starter

使用 Eclipse Paho [...] 客户端与 advertise 错误消息代理通信。

Paho MQTT 5

camel-paho-mqtt5-starter

使用 Eclipse Paho [...] v5 客户端与 advertise 错误消息代理通信。

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 上下文的另一个端点。

Slack

camel-slack-starter

从 Slack 发送和接收消息。

SQL

camel-sql-starter

使用 Spring JDBC 执行 SQL 查询。

Stub

camel-stub-starter

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

Telegram

camel-telegram-starter

发送和接收充当 Telegram Bot API 的消息。

timer

camel-timer-starter

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

Validator

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 CURRENTs to Avro and back using Jackson.

Bindy

camel-bindy-starter

marshal 和 unmarshal,使用 Camel Bindy(Trans)和键值对(KVP)格式

HL7

camel-hl7-starter

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

JacksonXML

camel-jacksonxml-starter

解包 XML 载荷,以使用 Jackson 的 XMLMapper 扩展来回放。

JAXB

camel-jaxb-starter

unmarshal XML 有效负载到 POJO,并使用 JAXB2 XML marshalling 标准进行后退。

JSON Gson

camel-gson-starter

marshal CURRENTs to JSON 并使用 Gson

protobuf Jackson

camel-jackson-protobuf-starter

marshal POJOs to Protobuf and back using Jackson.

SOAP

camel-soap-starter

结合 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.5. 初学者配置

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

1.5.1. 使用外部配置

在内部,每个 初学者 通过 Spring Boot 的配置 Properties 配置。各个配置参数均可通过 不同的方式应用程序。[properties|json|json] 文件、命令行参数、环境变量等)进行设置。参数的格式是 camel。[component|language|dataformat].[name].[parameter]

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

camel.component.activemq.broker-url=tcp://localhost:61616

或者将 CSV 数据格式的淘汰配置为分号:

camel.dataformat.csv.delimiter=;

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

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

camel.component.jms.transactionManager=#bean:myjtaTransactionManager

Bean 通常将以 Java 创建:

@Bean("myjtaTransactionManager")
public JmsTransactionManager myjtaTransactionManager(PooledConnectionFactory pool) {
    JmsTransactionManager manager = new JmsTransactionManager(pool);
    manager.setDefaultTimeout(45);
    return manager;
}

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

1.5.2. 使用 Beans

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

@Bean("kafka")
public KafkaComponent kafka(KafkaConfiguration kafkaconfiguration){
    return ComponentsBuilderFactory.kafka()
                        .brokers("{{kafka.host}}:{{kafka.port}}")
                        .build();
}

Bean 名称必须等于您要配置的组件、数据格式或语言。如果无法在注解中指定 Bean 名称,它将设置为方法名称。

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

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

您可以使用 Maven archetype org.apache.camel.archetypes:camel-archetype-spring-boot:3.14.2 生成 Camel Spring Boot 应用程序。

流程

  1. 运行以下命令:

    mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes \
      -DarchetypeArtifactId=camel-archetype-spring-boot \
      -DarchetypeVersion=3.14.2 \
      -DgroupId=com.redhat \
      -DartifactId=csb-app \
      -Dversion=1.0-SNAPSHOT \
      -DinteractiveMode=false \
      -Dspring-boot-version=2.7.1
  2. 编辑 pom.xml 文件,以更改 org.apache.camel.springboot:camel-spring-boot-dependencies 中的 org.apache.camel.springboot-dependencies 版本

    <version>3.14.2</version>

    <version>3.14.2.redhat-00054</version>
  3. 构建应用程序:

    mvn package -f csb-app/pom.xml
  4. 运行应用程序:

    java -jar csb-app/target/csb-app-1.0-SNAPSHOT.jar
  5. 检查应用生成的 Hello World 输出的控制台日志,以验证应用正在运行。

    com.redhat.MySpringBootApplication       : Started MySpringBootApplication in 3.514 seconds (JVM running for 4.006)
    Hello World
    Hello World
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat
返回顶部