Spring集成 - 通道适配器与网关(AMQP)

时间:2018-05-16 05:55:34

标签: spring rabbitmq spring-integration amqp spring-amqp

我对弹簧集成中的通道适配器和网关的区别感到困惑。如https://stackoverflow.com/a/29992267/467944中所述,@ gary-russell通道适配器是非双向的,而网关是双向的。如果是这种情况,为什么有amqp入站网关以及amqp出站网关?

我最终要完成的工作如下:

  1. 在控制器内收到Http请求
  2. 在amqp队列上发送消息
  3. 使用者消费消息并将结果放入结果队列
  4. 结果到达控制器
  5. 所以我猜我需要一个带有接口的网关,该接口从控制器调用并将有效负载放入amqp队列(配置为其请求通道),同时在其回复通道上侦听其答案。 但是,使用该配置,我总是以

    结束
     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <AutoCompleteTextView
            android:id="@+id/countries_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/_20sdp"
            android:dropDownHeight="match_parent"
            android:ems="10">
        </AutoCompleteTextView>
    
        <RelativeLayout
            android:id="@+id/revealLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="?android:attr/actionBarSize"
            android:animateLayoutChanges="true"
            android:background="#00FFA9"
            android:visibility="gone">
    
            <TextView
                android:id="@+id/textReveal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Hello Reveal!"
                android:textColor="#000000"
                android:textSize="@dimen/_24ssp"/>
        </RelativeLayout>
    </RelativeLayout>
    

    其中fromRabibbit是我的回复队列。

2 个答案:

答案 0 :(得分:2)

在提出这样的问题时,您应该始终显示配置。

Dispatcher没有订阅者......

这是一个配置错误,您没有正确连接集成流程。

Spring Integration中有两种类型的网关 -

与外部系统接口的网关

这些提供与外部系统的请求/回复语义。

入站网关用于服务器端请求/回复,其中服务器接收请求,执行一些处理并返回回复。

出站网关是客户端等效的,客户端发送请求并等待回复。使用AMQP,我们有一个异步版本,其中回复在不同的线程上。

消息传递网关

这些基于接口的网关通过Java代码(而不是某些外部系统)提供网关(通常是请求/回复,但是带有无效结果的方法是单向的)。

这使旧版Java代码能够使用基于集成的流程。

所以你可能......

controller -> gateway -> transformer(optional) -> amqp-outbound-gateway

......这是一种常见的模式并且效果很好。

服务器端可能是

amqp-inbound-gateway -> service-activator

显示您的代码/配置,有人可以帮助您调试配置问题。

答案 1 :(得分:0)

感谢@ gary-russell的见解。我想我现在有正确的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!-- 'CLIENT' SIDE CONFIG -->
    <int:channel id="clientToRabbit" />
    <int:channel id="clientFromRabbit" />
    <int:gateway id="uppercaseGateway" service-interface="com.example.queuing.UpperCaseService" default-request-channel="clientToRabbit" />
    <int-amqp:outbound-gateway request-channel="clientToRabbit"  amqp-template="amqpTemplate" exchange-name="si.test.exchange" routing-key="si.test.binding" />


    <!-- 'SERVER' SIDE CONFIG -->
    <int:channel id="serverFromRabbit" />
    <int:channel id="serverToRabbit" />
    <int-amqp:inbound-gateway request-channel="serverFromRabbit" reply-channel="serverToRabbit" queue-names="si.test.queue" amqp-template="amqpTemplate" connection-factory="connectionFactory" />
    <int:service-activator input-channel="serverFromRabbit" output-channel="serverToRabbit" ref="upperCaseService" method="toUpperCase" />


    <!-- Infrastructure -->
    <rabbit:connection-factory id="connectionFactory" host="localhost" />

    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />

    <rabbit:admin connection-factory="connectionFactory" />

    <rabbit:queue name="si.test.queue" />
    <rabbit:direct-exchange name="si.test.exchange">
        <rabbit:bindings>
            <rabbit:binding queue="si.test.queue" key="si.test.binding" />
        </rabbit:bindings>
    </rabbit:direct-exchange>
</beans>
相关问题