1.2. Spring Boot
Spring Boot 自动配置 Camel。在 Spring 上下文中提供了 Camel 上下文自动探测 Camel 路由的建议自动配置,并将密钥 Camel 实用程序(如制作者模板、消费者模板和类型转换器)注册为 beans。
Maven 用户需要将以下依赖项添加到其 pom.xml 中,才能使用此组件:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>4.4.0.redhat-00039</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>4.4.0.redhat-00039</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.properties 或 application.yml 文件中自定义 Camel 应用程序。
1.2.2. Spring Boot 自动配置 复制链接链接已复制到粘贴板!
当在 Spring Boot 中使用 spring-boot 时,请确保使用以下 Maven 依赖项来支持自动配置:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>4.4.0.redhat-00039</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 一样访问它。
@Configuration
public class MyAppConfig {
@Autowired
CamelContext camelContext;
@Bean
MyService myService() {
return new DefaultMyService(camelContext);
}
}
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");
}
}
或者在您的 @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.2.5. Camel 属性 复制链接链接已复制到粘贴板!
Spring Boot 自动配置会自动连接到 Spring Boot 外部配置 (可能包含属性占位符、OS 环境变量或系统属性)支持 Camel 属性。它基本上意味着 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.2.6. 自定义 Camel 上下文配置 复制链接链接已复制到粘贴板!
如果要在 Camel auto-configuration 创建的 CamelContext bean 上执行一些操作,请在 Spring 上下文中注册 CamelContextConfiguration 实例:
@Configuration
public class MyAppConfig {
@Bean
CamelContextConfiguration contextConfiguration() {
return new CamelContextConfiguration() {
@Override
void beforeApplicationStart(CamelContext context) {
// your custom configuration goes here
}
};
}
}
只有在 Spring 上下文启动前,才会调用方法 beforeApplicationStart,因此传递给此回调的 CamelContext 实例被完全自动配置。如果您将多个 CamelContextConfiguration 实例添加到 Spring 上下文中,则执行每个实例。
1.2.7. 自动配置的消费者和制作者模板 复制链接链接已复制到粘贴板!
Camel 自动配置提供预配置的 ConsumerTemplate 和 ProducerTemplate 实例。只需将它们注入到 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.2.8. 自动配置的 TypeConverter 复制链接链接已复制到粘贴板!
Camel 自动配置在 Spring 上下文中注册一个名为 typeConverter 的 TypeConverter 实例。
@Component
public class InvoiceProcessor {
@Autowired
private TypeConverter typeConverter;
public long parseInvoiceValue(Invoice invoice) {
String invoiceValue = invoice.grossValue();
return typeConverter.convertTo(Long.class, invoiceValue);
}
}
1.2.8.1. Spring 类型转换 API 网桥 复制链接链接已复制到粘贴板!
Spring 附带强大的 类型转换 API。Spring API 与 Camel 类型转换器 API 类似。因为两个 API 相似,Camel Spring Boot 会自动注册桥接转换器(SpringTypeConverter),以委派给 Spring 转换 API。这意味着开箱即用的 Camel 将对待 Spring Converters,如 Camel。使用这个方法,您可以使用 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());
}
}
在 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
使用 Web 模块的应用程序(例如,导入 org.springframework.boot:spring-boot-web-starter 模块的应用程序通常不需要使用此功能),因为应用程序存在其他非守护进程线程。
1.2.10. 添加 XML 路由 复制链接链接已复制到粘贴板!
默认情况下,您可以将 Camel XML 路由放在 camel 目录下的 classpath 中,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>),例如:
<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.2.11. 测试 JUnit 5 方法 复制链接链接已复制到粘贴板!
为了进行测试,Maven 用户需要将以下依赖项添加到其 pom.xml 中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.2.11</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>4.4.0.redhat-00046</version> <!-- use the same version as your Camel core version -->
<scope>test</scope>
</dependency>
要测试 Camel Spring Boot 应用程序,请使用 @CamelSpringBootTest 注解您的测试类。这会为应用程序提供 Camel 的 Spring Test 支持,以便您可以使用 Spring Boot 测试惯例编写测试。
要获取 CamelContext 或 ProducerTemplate,您可以使用 @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();
}
}