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-00032</version> <!-- use the same version as your Camel core version --> </dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>3.14.5.redhat-00032</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 应用程序。源代码中也有一个 示例应用程序。
要使用启动程序,将以下内容添加到 spring boot pom.xml 文件中:
<dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-spring-boot-bom</artifactId> <version>3.14.5.redhat-00032</version> <!-- use the same version as your Camel core version --> </dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-bom</artifactId>
<version>3.14.5.redhat-00032</version> <!-- use the same version as your Camel core version -->
</dependency>
然后,您只能使用 Camel 路由添加类,例如:
然后,这些路由将自动启动。
您可以在 application.properties
或 application.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.14.5.redhat-00032</version> <!-- use the same version as your Camel core version --> </dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.14.5.redhat-00032</version> <!-- use the same version as your Camel core version -->
</dependency>
1.2.3. 自动配置的 Camel 上下文 复制链接链接已复制到粘贴板!
Camel 自动配置提供的最重要功能是 CamelContext
实例。Camel 自动配置会为您创建一个 SpringCamelContext
,并负责该上下文的正确初始化和关闭。创建的 Camel 上下文也已在 Spring 应用程序上下文中注册(在 camelContext
bean 名称下),因此您可以像任何其他 Spring bean 一样访问它。
1.2.4. 自动探测 Camel 路由 复制链接链接已复制到粘贴板!
Camel 自动配置从 Spring 上下文收集所有 RouteBuilder
实例,并将它们自动注入到提供的 CamelContext
中。这意味着,使用 Spring Boot starter 创建新的 Camel 路由非常简单,就像将 @Component
注解类添加到您的类路径中一样简单:
或在 @Configuration
类中创建新的路由 RouteBuilder
bean:
1.2.5. Camel 属性 复制链接链接已复制到粘贴板!
Spring Boot 自动配置会自动连接到 Spring Boot 外部配置 (可能包含 Camel 属性支持的属性占位符、OS 环境变量或系统属性)。它基本上意味着 application.properties
文件中定义的任何属性:
route.from = jms:invoices
route.from = jms:invoices
或者通过系统属性设置:
java -Droute.to=jms:processed.invoices -jar mySpringApp.jar
java -Droute.to=jms:processed.invoices -jar mySpringApp.jar
可用作 Camel 路由中的占位符:
1.2.6. 自定义 Camel 上下文配置 复制链接链接已复制到粘贴板!
如果要对 Camel 自动配置创建的 CamelContext
bean 执行一些操作,请在 Spring 上下文中注册 CamelContextConfiguration
实例:
在启动 Spring 上下文 前,才会调用ApplicationStart
的方法,因此传递给此回调的 CamelContext
实例会被完全自动配置。如果您在 Spring 上下文中添加多个 CamelContextConfiguration
实例,则会执行每个实例。
1.2.7. 自动配置的消费者和制作者模板 复制链接链接已复制到粘贴板!
Camel 自动配置提供预配置的 ConsumerTemplate
和 ProducerTemplate
实例。您只需将它们注入到 Spring 管理的 Bean 中:
默认情况下,使用者模板和制作者模板带有端点缓存大小设置为 1000。您可以通过修改以下 Spring 属性来更改这些值:
camel.springboot.consumer-template-cache-size = 100 camel.springboot.producer-template-cache-size = 200
camel.springboot.consumer-template-cache-size = 100
camel.springboot.producer-template-cache-size = 200
1.2.8. Auto-configured TypeConverter 复制链接链接已复制到粘贴板!
Camel 自动配置在 Spring 上下文中注册名为 typeConverter
的 TypeConverter
实例。
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 转换器:
在 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
camel.springboot.main-run-controller = true
使用 Web 模块(例如,导入 org.springframework.boot:spring-boot-web-starter
模块的应用程序)的应用程序通常不需要使用此功能,因为应用程序会因存在其他非守护进程线程而保持有效。
1.2.10. 添加 XML 路由 复制链接链接已复制到粘贴板!
默认情况下,您可以将 Camel XML 路由放在 classpath 下,该目录的 camel-spring-boot 将会自动探测并包含。您可以使用配置选项配置目录名称或关闭它:
turn off
# turn off
camel.springboot.routes-include-pattern = false
scan only in the com/foo/routes classpath
# scan only in the com/foo/routes classpath
camel.springboot.routes-include-pattern = classpath:com/foo/routes/*.xml
XML 文件应该是 Camel XML 路由(而不是 < CamelContext>
; ),例如:
1.2.11. 测试 JUnit 5 复制链接链接已复制到粘贴板!
要进行测试,Maven 用户需要将以下依赖项添加到其 pom.xml
中:
要测试 Camel Spring Boot 应用,请使用 @CamelSpringBootTest
注解您的测试类。这会为您的应用程序带来 Camel 的 Spring Test 支持,以便您可以使用 Spring Boot 测试惯例 编写测试。
要获得 CamelContext
或 ProducerTemplate
,您可以使用 @Autowired
以正常 Spring 方式将它们注入到类。
您还可以使用 camel-test-spring-junit5 来声明性配置测试。本例使用 @MockEndpoints
注释来自动对端点进行自动处理: