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.18.3.redhat-00042</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.18.3.redhat-00042</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>3.18.3.redhat-00042</version> <!-- use the same version as your Camel core version -->
</dependency>
1.2.3. 自动配置的 Camel 上下文 复制链接链接已复制到粘贴板!
Camel 自动配置提供的最重要功能是 CamelContext 实例。Camel 自动配置为您创建一个 Spring CamelContext,并负责该上下文的正确初始化和关闭。创建的 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 注解类添加到您的 classpath 中一样简单:
@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 外部配置 (可能包含带有 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.2.6. 自定义 Camel 上下文配置 复制链接链接已复制到粘贴板!
如果要对 Camel 自动配置创建的 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 上下文启动前才会调用 ApplicationStart 的方法,因此传递到此回调的 CamelContext 实例完全自动配置。如果您将多个 CamelContextConfiguration 实例添加到 Spring 上下文中,则会执行每个实例。
1.2.7. 自动配置的消费者和制作者模板 复制链接链接已复制到粘贴板!
Camel auto-configuration 提供预配置的 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 auto-configuration 在 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 会自动注册到 Spring 转换 API 的桥接转换器(SpringTypeConverter)。这意味着开箱即用的 Camel 将像 Camel 一样对待 Spring Converters。使用这个方法,您可以使用通过 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>2.7.12</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.18.3.redhat-00034</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 注释自动-mock a 端点:
@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();
}
}