我正在寻找可以帮助我将弹簧REST Web服务与消息总线(RabbitMQ)集成在一起的spring模块。 REST Web服务充当来自客户端的AMQP消息的使用者。无论何时通过总线发送消息都是AMQP消息,并且使其与REST一起工作,必须将其转换为REST调用。有没有人知道现有的解决方案,使其工作?spring REST消息总线通信
0
A
回答
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的管理客户端,显示消息的
截图抵达
截图实际消息的
注意:这是一个非常基本的例子,只有一个RabbitMQ交换/队列,并且不包含安全性或任何您可能需要的其他基本内容!
相关问题
- 1. 消息总线中的总线发现
- 2. 通过REST发送消息到服务总线队列并通过TCP接收消息?
- 3. Azure服务总线消息
- 4. Liferay的消息总线
- 5. NService总线消息大小
- 6. 消息总线与组播
- 7. Azure服务总线 - 消息通信异常通道开放超时
- 8. Spring集成中的REST端点使消息传递通道多线程
- 9. 如何从REST通信到消息队列
- 10. Windows服务总线消息消失
- 11. Quickbooks Desktop通过REST API发送信息
- 12. 线程在Spring AMQP中的实现消息消息
- 13. 向线控通道添加额外信息以登录Spring
- 14. 互操作性Azure服务总线消息队列消息
- 15. 消息队列和服务总线的消息粒度
- 16. 托管的消息总线像推者,但大消息
- 17. 如何将消息总线消息类型设置为JSON?
- 18. 用Azure服务总线中继消息推送消息
- 19. NService总线消息延迟问题
- 20. OSGi服务的消息总线
- 21. 消息总线的最佳容器
- 22. Azure服务总线消息泵
- 23. 归档Windows Server服务总线消息
- 24. 服务总线的消息需要
- 25. Azure服务总线消息例外
- 26. 解析服务总线Queue消息
- 27. GStreamer总线发送无消息
- 28. 未收到Azure服务总线消息
- 29. 消息到期后的服务总线
- 30. Azure服务总线和消息会话
您是否试图使用由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
我没有使用RabbitMQ提供的REST服务。试图创建我自己的服务,希望保持REST接口与我是否使用消息总线无关。 – Milind 2015-02-12 13:59:44