2015-02-12 87 views
0

我正在尋找可以幫助我將彈簧REST Web服務與消息總線(RabbitMQ)集成在一起的spring模塊。 REST Web服務充當來自客戶端的AMQP消息的使用者。無論何時通過總線發送消息都是AMQP消息,並且使其與REST一起工作,必須將其轉換爲REST調用。有沒有人知道現有的解決方案,使其工作?spring REST消息總線通信

+1

您是否試圖使用由Rabbit Mq提供的REST-api來獲取消息。 (http://hg.rabbitmq.com/rabbitmq-management/raw-file/rabbitmq_v3_3_4/priv/www/api/index.html)。或者嘗試創建自己的客戶端,以接收消息並將其作爲REST調用暴露給其他客戶端。 – Vishnu 2015-02-12 06:53:29

+0

我沒有使用RabbitMQ提供的REST服務。試圖創建我自己的服務,希望保持REST接口與我是否使用消息總線無關。 – Milind 2015-02-12 13:59:44

回答

2

我個人沒有看到這個值,即使用同步REST接口消耗一些同步AMQP消息,因爲您有點失去像RabbitMQ這樣的同步消息系統的目的/優點。

關於AMQP的好處是它是一個有線協議,並且不依賴於一種語言(例如,JMS與Java非常緊密相關)。這意味着你可以使用Java/Spring/AMQP庫,Node.JS/AMQP庫,C#/ AMQP庫等。這篇文章解釋了比我更好的優勢http://www.wmrichards.com/amqp.pdf我的觀點是,如果你正在尋找REST來建立一個橋樑另一種語言/系統等到RabbitMQ然後我首先會調查其他語言/系統是否支持AMQP庫。然而,如果你必須有一個REST'ful接口,你可以使用SpringMVC創建一個簡單的控制器,並用一些方法注入一個private AmqpTemplate amqpTemplate;。這有效地創建了您的REST到AMQP網橋/代理。 Spring配置/ Java的控制器如下(請注意,這已經測試和工程): -

/spring/restAmqpContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:rabbit="http://www.springframework.org/schema/rabbit" 
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
        http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
        http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> 

<!-- Location of "config.properties" to override RabbitMQ connection details if required. --> 
<context:property-placeholder ignore-resource-not-found="true" 
           location="classpath:/config.properties, 
             ${DATA_HOME:}/config.properties" /> 

<bean class="com.bobmarks.controller.RestAmqpController"> 
    <property name="amqpTemplate" ref="amqpTemplate"/> 
</bean> 

<mvc:default-servlet-handler /> 
<mvc:annotation-driven/> 

<rabbit:connection-factory id="amqpConnectionFactory" 
          host="${rabbitmq.host:localhost}" 
          port="${rabbitmq.port:5672}" 
          username="${rabbitmq.username:guest}" 
          password="${rabbitmq.password:guest}" 
          publisher-confirms="${rabbitmq.publisher.confirms:true}" 
          publisher-returns="${rabbitmq.publisher.returns:true}" /> 

<rabbit:template id="amqpTemplate" connection-factory="amqpConnectionFactory" mandatory="true" /> 

<rabbit:admin id="rabbitAdmin" connection-factory="amqpConnectionFactory" /> 

<rabbit:queue name="my_queue" /> 
<rabbit:direct-exchange name="my_exchange"> 
    <rabbit:bindings><rabbit:binding queue="my_queue" key="my_binding" /></rabbit:bindings> 
</rabbit:direct-exchange> 

RestAmqpController.java

package com.bobmarks.controller; 

import java.util.Date; 

import org.springframework.amqp.AmqpException; 
import org.springframework.amqp.core.AmqpTemplate; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 

/** 
* Simple Rest To AMQP Controller. 
*/ 
@Controller 
@RequestMapping(value = "/rest2amqp") 
public class RestAmqpController { 

    private AmqpTemplate amqpTemplate; 

    public RestAmqpController() {} 

    public void setAmqpTemplate(AmqpTemplate amqpTemplate) { 
     this.amqpTemplate = amqpTemplate; 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    public ResponseEntity<String> message(@RequestParam(value = "message") String message) { 

     try { 
      amqpTemplate.convertAndSend("my_exchange", "my_binding", message); 
      return new ResponseEntity<String>("Message sent to AMQP queue at: " + new Date(), HttpStatus.OK); 
     } 
     catch (AmqpException amqpEx) { 
      return new ResponseEntity<String>(amqpEx.getMessage(), HttpStatus.BAD_REQUEST); 
     } 
    } 
} 

這些都是以通常的方式打包(Tomcat/Spri NG啓動/等),例如,如果該項目被稱爲數據的REST端點發送消息將如下所示: -

http://localhost/data/rest2amqp?message=Hello_World

這可能是更好的截圖所示。

截圖提交消息(火狐只是用)的RabbitMQ的管理客戶端,顯示消息的 REST Endpoint to add message

截圖抵達 RabbitMQ showing queued message

截圖實際消息的 RabbitMQ message details

注意:這是一個非常基本的例子,只有一個RabbitMQ交換/隊列,並且不包含安全性或任何您可能需要的其他基本內容!