随着软件世界的发展,正在开发更复杂的系统,这些系统必须相互集成。它从 SOA 开始,然后继续使用微服务。
Camel 是我想到的第一个集成工具,因为现在 spring boot 和 camel 是一个非常强大的组合。
第一步是将 camel 依赖项包含到我们的 spring 项目中。
buildscript { ext { springBootVersion = '1.5.9.BUILD-SNAPSHOT' } repositories { mavenCentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' group = 'com.gkatzioura' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() maven { url "https://repo.spring.io/snapshot" } maven { url "https://repo.spring.io/milestone" } } dependencies { compile('org.apache.camel:camel-spring-boot-starter:2.20.0') testCompile('org.springframework.boot:spring-boot-starter-test') testCompile('org.apache.camel:camel-test-spring:2.20.0') }
为了从头开始更快地设置项目,您始终可以使用在线 spring initializer。
现在让我们添加一个简单的路由
package com.gkatzioura.springcamel.routes; import org.apache.camel.builder.RouteBuilder; import org.springframework.stereotype.Component; @Component public class TimerRoute extends RouteBuilder { public static final String ROUTE_NAME = "TIMER_ROUTE"; @Override public void configure() throws Exception { from("timer:initial//start?period=10000") .routeId(ROUTE_NAME) .to("log:executed"); } }
我们不必担心 camel 上下文配置,因为 Camel 自动配置会为您创建一个 SpringCamelContext 并负责正确初始化和关闭该上下文。
camel 自动配置还从 Spring 上下文中收集所有 RouteBuilder 实例,并自动将它们注入到提供的 CamelContext 中。因此我们不必将我们的路由注册到 CamelContext。
如您所见,我们的路由有一个周期为 10000 毫秒的定时器,它路由到一个日志端点。日志端点将每 10000 毫秒打印一次执行的字符串。
请记住,如果没有指定 routeId,camel 将自己分配一个名称,因此为我们的路由定义命名是一个很好的做法,以防我们想要检索根定义。
为了让骆驼熬夜,我们需要让我们的主线程保持阻塞状态。因此,我们将此配置添加到我们的 application.yml 文件中。
camel: springboot: main-run-controller: true
取而代之的是,我们可以包含 spring-boot-starter-web 依赖项,但我们的应用程序具有尽可能少的依赖项,我们需要保持这种方式。
然而,与其他系统集成中最困难的部分是测试。多年来,测试和我们使用的工具取得了快速进步。 Camel 还打包了一些很棒的工具来进行单元测试。
例如,我们将对先前指定的路由进行测试。
@RunWith(CamelSpringBootRunner.class) @SpringBootTest public class SpringCamelApplicationTests { @EndpointInject(uri = MOCK_RESULT) private MockEndpoint resultEndpoint; @Autowired private CamelContext camelContext; @EndpointInject(uri = MOCK_TIMER) private ProducerTemplate producer; private static final String MOCK_RESULT = "mock:result"; private static final String MOCK_TIMER = "direct:mock-timer"; @Before public void setup() throws Exception { camelContext.getRouteDefinition(TimerRoute.ROUTE_NAME) .autoStartup(true) .adviceWith(camelContext, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { replaceFromWith(MOCK_TIMER); interceptSendToEndpoint("log*") .skipSendToOriginalEndpoint() .to(MOCK_RESULT); } }); } @Test public void sendMessage() throws Exception { resultEndpoint.expectedMessageCount(1); producer.sendBody("A message"); resultEndpoint.assertIsSatisfied(); } }
让我们来看看测试的每个部分。
我们选择的 JUnit 运行程序是 CamelSpringBootRunner.class
@RunWith(CamelSpringBootRunner.class)
我们注入一个 ProducerTemplate。 ProducerTemplate 接口允许您以各种不同的方式将消息交换发送到端点,以便从 Java 代码轻松使用 Camel Endpoint 实例。
然后我们注入一个 MockEndpoint。 MockEndpoint 将通过替换原始端点来为我们服务。然后我们将设置要接收的消息的预期数量。处理完成后,我们断言接收到的消息数量已满足。
在我们的设置方法中,我们将用伪造的生产者模板端点替换我们的原始端点。因此,我们的路线将接收我们将从 ProducerTemplate 发出的事件。然后我们也将拦截日志端点并将消息定向到之前指定的 MockEndpoint。
所以我们最终得到了一个 camel 应用程序和一个指定路由的单元测试。您可以在 github 上找到源代码。
标签2: Java教程地址:https://www.cundage.com/article/jcg-spring-boot-apache-camel.html