2015-02-12 137 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交换/队列,并且不包含安全性或任何您可能需要的其他基本内容!