第 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.properties 或 application.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("...");
}
}