当前位置: 首页 > 知识库问答 >
问题:

屏障组件在spring集成中是如何工作的?

狄楷
2023-03-14

我试图理解以下示例:https://github.com/spring-projects/spring-integration-samples/tree/d8e71c687e86e7a7e35d515824832a92df9d4638/basic/barrier

并使用java DSL重写它。

首先,我想了解那个示例中发生了什么。当然,我已经阅读了github的解释,但我仍然不明白。我在这里问是因为我知道SI团队负责人(该示例的作者)回答了所有标记为Spring集成的问题。

服务器配置

<int-http:inbound-gateway request-channel="receiveChannel"
                            path="/postGateway"
                            error-channel="errorChannel"
                            supported-methods="POST"/>

    <int-http:inbound-gateway request-channel="createPayload"
                            path="/getGateway"
                            error-channel="errorChannel"
                            supported-methods="GET"/>

    <int:transformer input-channel="createPayload" output-channel="receiveChannel" expression="'A,B,C'" />

    <int:channel id="receiveChannel" />

    <int:header-enricher input-channel="receiveChannel" output-channel="processChannel">
        <int:header name="ackCorrelation" expression="headers['id']" />
    </int:header-enricher>

    <int:publish-subscribe-channel id="processChannel" />

    <int:chain input-channel="processChannel" order="1">
        <int:header-filter header-names="content-type, content-length" />
        <int:splitter delimiters="," />
        <int-amqp:outbound-channel-adapter amqp-template="rabbitTemplate"
            exchange-name="barrier.sample.exchange" routing-key="barrier.sample.key"
            confirm-ack-channel="confirmations"
            confirm-nack-channel="confirmations"
            return-channel="errorChannel"
            confirm-correlation-expression="#this"/>
    </int:chain>

    <!-- Suspend the HTTP thread until the publisher confirms are asynchronously received -->

    <int:barrier id="barrier" input-channel="processChannel" order="2"
        correlation-strategy-expression="headers['ackCorrelation']"
        output-channel="transform" timeout="10000" />

    <int:transformer input-channel="transform" expression="payload[1]" />

    <!-- Aggregate the publisher confirms and send the result to the barrier release channel -->

    <int:chain input-channel="confirmations" output-channel="release">
        <int:header-filter header-names="replyChannel, errorChannel" />
        <int:service-activator expression="payload" /> <!-- INT-3791; use s-a to retain ack header -->
        <int:aggregator>
            <bean class="org.springframework.integration.samples.barrier.AckAggregator" />
        </int:aggregator>
    </int:chain>

    <int:channel id="release" />

    <int:outbound-channel-adapter channel="release" ref="barrier.handler" method="trigger" />

    <!-- Consumer -> nullChannel -->

    <int-amqp:inbound-channel-adapter channel="nullChannel"
        queue-names="barrier.sample.queue"
        connection-factory="rabbitConnectionFactory" />

    <!-- Infrastructure -->

    <rabbit:queue name="barrier.sample.queue" auto-delete="true" />

    <rabbit:direct-exchange name="barrier.sample.exchange" auto-delete="true">
        <rabbit:bindings>
            <rabbit:binding queue="barrier.sample.queue" key="barrier.sample.key" />
        </rabbit:bindings>
    </rabbit:direct-exchange>

据我所知,有三方:

  • 客户
  • 服务器
  • rabbitMq

客户端将A、B、C发送到服务器

RequestGateway requestGateway = client.getBean("requestGateway", RequestGateway.class);
String request = "A,B,C";
System.out.println("\n\n++++++++++++ Sending: " + request + " ++++++++++++\n");
String reply = requestGateway.echo(request);

服务器接受该请求:

<int-http:inbound-gateway request-channel="receiveChannel"
                        path="/postGateway"
                        error-channel="errorChannel"
                        supported-methods="POST"

然后将一些值添加到消息标头中:

<int:header-enricher input-channel="receiveChannel" output-channel="processChannel">
        <int:header name="ackCorrelation" expression="headers['id']" />
    </int:header-enricher>

一些处理:

<int:chain input-channel="processChannel" order="1">
        <int:header-filter header-names="content-type, content-length" />
        <int:splitter delimiters="," />
        <int-amqp:outbound-channel-adapter amqp-template="rabbitTemplate"
            exchange-name="barrier.sample.exchange" routing-key="barrier.sample.key"
            confirm-ack-channel="confirmations"
            confirm-nack-channel="confirmations"
            return-channel="errorChannel"
            confirm-correlation-expression="#this"/>
    </int:chain>

如果我没有弄错的话,消息被分割,因此我们有3条消息AB和C,并发送到rabbitMq交换屏障。样品用钥匙交换。样品键

确实如此。接下来的步骤我还不清楚。你能澄清一下吗?

附笔。

我知道我们希望等待来自Rabbit的所有ACK消息,以确保我们传递给Rabbit的消息,当我们从Rabbit获得所有ACK时,我们希望回答客户端。但我不明白导致该结果的SI组件定义。您能否提供任何模式/图片/解释它是如何发生的?

更具体的问题:

为什么我们需要充实标题以及我们是如何做到的?

receiveChannel的目标是什么-愚蠢的问题

2个超文本传输协议服务-/postGateway/getGateway在哪里。/postGateway用于从客户端获取msg、拆分成部分、发送到兔子、获取Acks然后回复给客户端。但是/getGateway的目的是什么?

目前,我的流定义如下所示:

    @Bean
public IntegrationFlow integrationFlow() {
    return IntegrationFlows.from(Http.inboundGateway("/spring_integration_post")
            .requestMapping(m -> m.methods(HttpMethod.POST))
            .requestPayloadType(String.class))
            .enrich(enricherSpec -> {
                enricherSpec.header("correlationId", 1); //or ackCorrelationId ?
            })
            .split(s -> s.applySequence(false).get().getT2().setDelimiters(","))
            .log()
            //.barrier(1000L) is it correct place for barrier?
            .log()
            .handle(Amqp.outboundAdapter(amqpTemplate())
                    .exchangeName("barrierExchange")
                    .routingKey("barrierKey"))
            .get();
}

我不知道怎么使用这里的屏障。

从服务器端,一切都在部分工作-我看到队列中的消息(在rabbitMq web界面上),但从客户端,我开始看到以下堆栈跟踪:

2019-08-28 22:38:43.432 ERROR 12936 --- [ask-scheduler-8] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://localhost:8080/spring_integration_post]; nested exception is org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 null, failedMessage=GenericMessage [payload=6, headers={id=36781a7f-3d4f-e17d-60e6-33450c9307e4, timestamp=1567021122424}]
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.exchange(HttpRequestExecutingMessageHandler.java:171)
    at org.springframework.integration.http.outbound.AbstractHttpRequestExecutingMessageHandler.handleRequestMessage(AbstractHttpRequestExecutingMessageHandler.java:289)
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:123)
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:169)
    at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:115)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:132)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:105)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:73)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:453)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:401)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:187)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:166)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
    at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:109)
    at org.springframework.integration.endpoint.SourcePollingChannelAdapter.handleMessage(SourcePollingChannelAdapter.java:234)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:390)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.pollForMessage(AbstractPollingEndpoint.java:329)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.lambda$null$1(AbstractPollingEndpoint.java:277)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.lambda$execute$0(ErrorHandlingTaskExecutor.java:57)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:55)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.lambda$createPoller$2(AbstractPollingEndpoint.java:274)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 null
    at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:79)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:124)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:598)
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.exchange(HttpRequestExecutingMessageHandler.java:165)
    ... 30 more

共有1个答案

颜志业
2023-03-14

这很简单;不清楚你不明白什么。

  • 我们收到超文本传输协议的请求
  • 我们为消息添加一个相关标头(标头增强器)
  • 我们向Rabbitmq发送3条消息
  • 我们需要等待确认-所以HTTP线程挂起在屏障中
  • 3个确认异步返回
  • 我们将响应聚合到一条消息中(使用相关标头)
  • 我们将该消息发送到释放HTTP线程的屏障

接收器通道是标头增强器的输入通道。

您需要添加流来接收确认、聚合和触发屏障。

 类似资料:
  • 我有一个Makefile的目标与几个作业运行是并行的选项。 完成这些工作可能需要不同的时间。我是否有办法确保在进入构建过程的下一阶段之前完成所有这些工作?

  • 我试图使用Spring/AspectJ集成,但运气不好。Spring版本是3.2.17(是的,我知道有点旧)。 以下是我的相关配置: pom.xml: 应用程序上下文。xml: spect.java(相关类): 我倒了很多在线教程,运气不好。有人能指出我做错了什么吗? 杰森

  • 我需要在登录时屏蔽敏感信息。我们使用集成框架提供的wire-tap进行日志记录,我们已经设计了许多接口,这些接口使用wire-tap进行日志记录。我们目前正在使用spring boot 2.1和spring集成。

  • null 如何在transform()步骤中添加Jaxb2Marshaller?

  • <罢工> 错误: 没有类型为'org.springframework.test.web.servlet.mockMVC'的合格bean可用:至少需要1个符合autowire候选的bean。依赖项注释:{@org.springframework.beans.factory.annotation.autowire(required=true)}位于org.springframework.beans.f

  • 尽管我的测试方法被@WithMockUser注释,但我仍然被拒绝访问。为什么这在集成测试中不起作用?使用@webappconfiguration和mockmvc进行测试一切都很好。