第 1 章 红帽构建的 Apache Camel for Spring Boot 入门
本指南介绍了红帽构建的 Apache Camel for Spring Boot,并演示如何使用红帽构建的 Apache Camel for Spring Boot 构建应用程序:
Camel 对 Spring Boot 的支持为许多 Camel 组件提供 Camel 和启动程序的 自动配置。在 Spring 上下文中提供的 Camel 上下文自动探测 Camel 路由的建议自动配置,并将密钥 Camel 工具(如制作者模板、消费者模板和类型转换器)注册为 beans。
有关使用 Maven archtype 为 Spring Boot 应用程序生成 Camel 的详情,请参考使用 Maven 为 Spring Boot 应用程序生成 Camel。
要开始,您必须将 Camel Spring Boot BOM 添加到 Maven pom.xml 文件中。
<dependencyManagement>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>com.redhat.camel.springboot.platform</groupId>
<artifactId>camel-spring-boot-bom</artifactId>
<version>4.0.0.redhat-00039</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- ... other BOMs or dependencies ... -->
</dependencies>
</dependencyManagement>
camel-spring-boot-bom 是一个基本的 BOM,其中包含 Camel Spring Boot starter JAR 列表。
接下来,添加 Camel Spring Boot 初学者 以启动 Camel 上下文。
<dependencies>
<!-- Camel Starter -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<!-- ... other dependencies ... -->
</dependencies>
您还必须添加 Spring Boot 应用程序 所需的组件 启动程序。以下示例演示了如何将自动配置入门添加到 MQTT5 组件。https://access.redhat.com/documentation/zh-cn/red_hat_build_of_apache_camel/4.0/html-single/red_hat_build_of_apache_camel_for_spring_boot_reference/index#spring_boot_auto_configuration_69
<dependencies>
<!-- ... other dependencies ... -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-paho-mqtt5</artifactId>
</dependency>
</dependencies>
生成策展的 camel-spring-boot-dependencies BOM,其中包含 Spring Boot 和 Apache Camel 使用的经过调整的 JAR,以避免出现任何冲突。此 BOM 用于测试 camel-spring-boot 本身。
Spring Boot 用户可以选择使用 纯 Camel 依赖项,方法是使用只有 Camel starter JAR 作为受管依赖项的 camel-spring-boot-bom。但是,如果 Spring Boot 中的第三方 JAR 与特定的 Camel 组件不兼容,这可能会导致类路径冲突。
1.1.2. Spring Boot 配置支持 复制链接链接已复制到粘贴板!
每个 入门 都列出了您可以在标准 application.properties 或 application.yml 文件中配置的配置参数。这些参数的格式为 camel.component.[component-name].[parameter]。例如,要配置 MQTT5 代理的 URL,您可以设置:
camel.component.paho-mqtt5.broker-url=tcp://localhost:61616
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("...");
}
}