我有几个使用Spring Boot和RPC通过RabbitMQ的教程。但是,只要我尝试添加Jackson JSON消息转换器,它就会崩溃。
服务器已成功接收远程调用,因此我非常有信心这不是客户端配置。
Exchange DATAFLOW_EXCHANGE
Routing Key dataflowRunner
Redelivered ○
Properties
reply_to: amq.rabbitmq.reply-to.g2dkABZyYWJiaXRAdXNoeWRnbmFkaXBhbHZ4AAAr0wAAAAAB.MmIZ6Htejtc1qB11G7BBQw==
priority: 0
delivery_mode: 2
headers:
__TypeId__: org.springframework.remoting.support.RemoteInvocation
content_encoding: UTF-8
content_type: application/json
Payload
675 bytes
Encoding: string
{"methodName":"run","parameterTypes":["dw.dataflow.Dataflow"],"arguments":[{ Valid Dataflow JSON Removed for Brevity } ]}
但是,输出以下异常:
Caused by: org.springframework.messaging.converter.MessageConversionException:
No converter found to convert to class dw.dataflow.Dataflow, message=GenericMessage
[payload=RemoteInvocation: method name 'run'; parameter types [dw.dataflow.Dataflow], headers={amqp_receivedExchange=DATAFLOW_EXCHANGE, amqp_deliveryTag=1, amqp_replyTo=amq.rabbitmq.reply-to.g2dkABZyYWJiaXRAdXNoeWRnbmFkaXBhbHZ4AAArRAAAAAQC.PA/bJ6lcUfaP3csAP5v5NA==, amqp_consumerQueue=DATAFLOW_QUEUE, amqp_redelivered=false, amqp_receivedRoutingKey=dataflowRunner, amqp_contentEncoding=UTF-8, amqp_deliveryMode=PERSISTENT, id=adb37c77-c0da-16bd-8df4-b739cfddf89f, amqp_consumerTag=amq.ctag-N_tFCc_Hp9UtQkiXl7FZ8g, contentType=application/json, __TypeId__=org.springframework.remoting.support.RemoteInvocation, timestamp=1462560945203}]
at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:118)
at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:98)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:138)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:107)
at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48)
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:112)
... 12 common frames omitted
所以,在交付时,它知道它应该是一个dw.dataflow.Dataflow对象,它只是找不到转换器。但是,我的转换器定义在任何地方。
服务器配置
@Configuration
@EnableRabbit
public class RabbitListenerConfiguration {
@Autowired
ConnectionFactory connectionFactory;
@Autowired
ObjectMapper jacksonObjectMapper;
@Bean
public TopicExchange exchange() {
return new TopicExchange("DATAFLOW_EXCHANGE", true, false);
}
@Bean
public Queue queue() {
return new Queue("DATAFLOW_QUEUE", true);
}
@Bean
public AmqpInvokerServiceExporter amqpInvokerServiceExporter() {
AmqpInvokerServiceExporter exporter = new AmqpInvokerServiceExporter() ;
exporter.setAmqpTemplate(rabbitTemplate());
exporter.setMessageConverter(jackson2JsonMessageConverter());
exporter.setServiceInterface(DataflowRunner.class);
exporter.setService(dataflowRunner());
return exporter ;
}
@Bean
public DataflowRunner dataflowRunner() {
return new DataflowRunnerServerImpl();
}
@Bean
public MessageConverter jackson2JsonMessageConverter() {
Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
converter.setJsonObjectMapper(jacksonObjectMapper);
return converter;
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(jackson2JsonMessageConverter());
return template;
}
@Bean(name="rabbitListenerContainerFactory")
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(jackson2JsonMessageConverter());
factory.setDefaultRequeueRejected(false);
return factory;
}
这是服务界面:
public interface DataflowRunner {
String run(Dataflow dataflow) throws Exception;
}
具体实施:
public class DataflowRunnerServerImpl implements DataflowRunner {
@RabbitListener(containerFactory = "rabbitListenerContainerFactory", queues="DATAFLOW_QUEUE")
public String run(Dataflow dataflow) throws Exception {
// SNIP
}
对于grins和giggles,我还尝试使用以下注释配置服务器实现类,但它有相同的错误:
@RabbitHandler
@RabbitListener(
bindings = @QueueBinding(key = "dataflowRunner",
value = @Queue(value = "DATAFLOW_QUEUE", durable = "true", autoDelete = "false", exclusive = "false"),
exchange = @Exchange(value = "DATAFLOW_EXCHANGE", durable = "true", autoDelete = "false", type = "topic")) )
public String run(Dataflow dataflow) throws Exception {
客户端配置
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitHost, rabbitPort);
connectionFactory.setUsername(rabbitUser);
connectionFactory.setPassword(rabbitPassword);
connectionFactory.setAddresses(rabbitAddresses);
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
template.setMessageConverter(jackson2MessageConverter());
return template;
}
是否有任何错误配置?我错过了什么?我在服务导出器和侦听器容器工厂上设置了转换器。
感谢任何帮助和/或想法。
答案 0 :(得分:5)
@RabbitListener
不打算与服务导出器一起使用 - 只是一个普通的Java类。
对于基于RPC的Spring Remoting,服务导出器是MessageListener
的{{1}}。
使用SimpleMessageListenerContainer
,有一个包含pojo方法的特殊侦听器适配器。
所以你似乎在混合两种不同的范例。
@RabbitListener
(Spring远程处理)预计将与客户端的ServiceExporter
配对,服务导出器作为服务器端的侦听器。
对于简单的POJO RPC(比通过RabbitMQ使用Spring Remoting要新得多),请在客户端使用AmqpProxyFactoryBean
和@RabbitListener
。摆脱PFB和SE。
如果我们需要在文档中添加一些说明,你能否解释是什么导致你走这条道路。
修改强>
如果你做想要使用Spring Remoting(在客户端注入一个接口并让它“神奇地”调用服务器端的服务),你需要摆脱所有的容器工厂的东西,只需连接RabbitTemplate.convertSendAndReceive()
并将服务导出器注入SimpleMessageListenerContainer
。
参考手册有an XML example,但您可以将SMLC连接为MessageListener
。
<强> EDIT2 强>
我已经运行了一些测试并且Spring Remoting over AMQP无法使用JSON,因为顶级对象是@Bean
- 而消息转换器可以重新创建该对象,它没有关于实际参数因此将其保留为链接哈希映射。
目前,如果您必须使用JSON,那么将RemoteInvocation
模板与convertSendAndReceive
结合使用即可。我将打开一个JIRA问题,看看我们是否可以使用带有JSON的Spring Remoting RPC解决,但它确实是为Java序列化而设计的。
答案 1 :(得分:0)
我花了几分钟时间在这上面,我设法通过一个似乎有效的可怕黑客解决问题。
我基本上扩展了双方调用中涉及的类,以确保内部参数和值转换为JSON字符串/从JSON字符串转换。
如果有更多的爱,可以使用其他转换器与其他数据类型一起使用,但我没有时间。如果有足够的勇气尝试一下,我会留给你: - )
在服务器端
首先,我将AmqpInvokerServiceExporter
子类化,以便能够添加对JSON对象的转换支持。第一步是将方法参数从JSON转换为相应的类型。第二步是将返回的值从对象转换为相应的JSON字符串以将其发回。
public class JSONAmqpInvokerServiceExporter extends AmqpInvokerServiceExporter {
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void onMessage(Message message) {
Address replyToAddress = message.getMessageProperties().getReplyToAddress();
if (replyToAddress == null) {
throw new AmqpRejectAndDontRequeueException("No replyToAddress in inbound AMQP Message");
}
Object invocationRaw = getMessageConverter().fromMessage(message);
RemoteInvocationResult remoteInvocationResult;
if (invocationRaw == null || !(invocationRaw instanceof RemoteInvocation)) {
remoteInvocationResult = new RemoteInvocationResult(
new IllegalArgumentException("The message does not contain a RemoteInvocation payload"));
}
else {
RemoteInvocation invocation = (RemoteInvocation) invocationRaw;
int argCount = invocation.getArguments().length;
if (argCount > 0) {
Object[] arguments = invocation.getArguments();
Class<?>[] parameterTypes = invocation.getParameterTypes();
for (int i = 0; i < argCount; i++) {
try {
//convert arguments from JSON strings to objects
arguments[i] = objectMapper.readValue(arguments[i].toString(), parameterTypes[i]);
}
catch (IOException cause) {
throw new MessageConversionException(
"Failed to convert JSON to value: " + arguments[i] + " of type" + parameterTypes[i], cause);
}
}
}
remoteInvocationResult = invokeAndCreateResult(invocation, getService());
}
send(remoteInvocationResult, replyToAddress);
}
private void send(RemoteInvocationResult result, Address replyToAddress) {
Object value = result.getValue();
if (value != null) {
try {
//convert the returning value from a model to a JSON string
//before we send it back
Object json = objectMapper.writeValueAsString(value);
result.setValue(json);
}
catch (JsonProcessingException cause) {
throw new MessageConversionException("Failed to convert value to JSON: " + value, cause);
}
}
Message message = getMessageConverter().toMessage(result, new MessageProperties());
getAmqpTemplate().send(replyToAddress.getExchangeName(), replyToAddress.getRoutingKey(), message);
}
}
现在,定义了这个类后,我将服务侦听器的定义更改为:
<bean id="toteServiceListener" class="amqphack.FFDAmqpInvokerServiceExporter">
<property name="serviceInterface" value="ampqphack.ToteService"/>
<property name="service" ref="defaultToteService"/>
<property name="amqpTemplate" ref="rabbitTemplate"/>
</bean>
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener ref="toteServiceListener" queue-names="tote-service"/>
</rabbit:listener-container>
在这种情况下我使用了常规AmqTemplate
,因为我知道ResultInvocationValue总是会转换为JSON字符串,所以我不介意使用传统的Java序列化序列化InvocationResult。 / p>
在客户端
在客户端我不得不改变一切。首先,我需要在调用之前将我们发送到调用的任何参数转换为JSON字符串,但我们仍然保留它们的参数类型。幸运的是,现有的AmqpProxyFactoryBean
接受remoteInvocationFactory
参数,我们可以拦截调用并更改它。所以我首先定义了新的RemoteInvocationFactory
:
public class JSONRemoteInvocationFactory implements RemoteInvocationFactory {
private final ObjectMapper mapper = new ObjectMapper();
@Override
public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
RemoteInvocation invocation = new RemoteInvocation(methodInvocation);
if (invocation.getParameterTypes() != null) {
int paramCount = invocation.getParameterTypes().length;
Object[] arguments = new Object[paramCount];
try {
for (int i = 0; i < paramCount; i++) {
arguments[i] = mapper.writeValueAsString(invocation.getArguments()[i]);
}
invocation.setArguments(arguments);
}
catch (JsonProcessingException cause) {
throw new RuntimeException(
"Failed converting arguments to json: " + Arrays.toString(invocation.getArguments()), cause);
}
}
return invocation;
}
}
但这还不够。当我们得到结果时,我们需要将其结果再次转换回Java对象。为此我们可以使用服务接口预期的返回类型。为此我扩展了存在AmqpProxyFactoryBean
以简单地将其结果(我知道将永远是String)转换为Java模型。
public class JSONAmqpProxyFactoryBean extends AmqpProxyFactoryBean {
private final ObjectMapper mapper = DefaultObjectMapper.createDefaultObjectMapper();
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object ret = super.invoke(invocation);
return mapper.readValue(ret.toString(), invocation.getMethod().getReturnType());
}
}
有了这个,我能够像我这样定义我的客户端:
<bean id="toteService" class="amqphack.JSONAmqpProxyFactoryBean">
<property name="amqpTemplate" ref="rabbitTemplate"/>
<property name="serviceInterface" value="amqphack.ToteService"/>
<property name="routingKey" value="tote-service"/>
<property name="remoteInvocationFactory" ref="remoteInvocationFactory"/>
</bean>
在此之后,这一切都像一个魅力:
ToteService toteService = context.getBean("toteService", ToteService.class);
ToteModel tote = toteService.findTote("18251", "ABCD");
由于我没有更改传统的转换器,这意味着InvocationResult
中的异常仍然正确序列化。
答案 2 :(得分:0)
不知道是否还需要它,但这就是我解决了使用AmqpProxyFactoryBean
/ AmqpInvokerServiceExporter
使用JSON的问题。在客户端,我使用Jackson2JsonMessageConverter
转换器,在服务器端使用RemoteInvocationAwareMessageConverterAdapter
包装Jackson2JsonMessageConverter
转换器。
ClientConfig.java
:
import com.stayfriends.commons.services.interfaces.GameService;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.remoting.client.AmqpProxyFactoryBean;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ClientConfig {
@Bean
public RabbitTemplate gameServiceTemplate(ConnectionFactory connectionFactory,
Jackson2JsonMessageConverter messageConverter) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setExchange("rpc");
template.setMessageConverter(messageConverter);
return template;
}
@Bean
public ServiceAmqpProxyFactoryBean gameServiceProxy2(@Qualifier("gameServiceTemplate") RabbitTemplate template) {
return new ServiceAmqpProxyFactoryBean(template);
}
public static class ServiceAmqpProxyFactoryBean implements FactoryBean<Service>, InitializingBean {
private final AmqpProxyFactoryBean proxy;
ServiceAmqpProxyFactoryBean(RabbitTemplate template) {
proxy = new AmqpProxyFactoryBean();
proxy.setAmqpTemplate(template);
proxy.setServiceInterface(GameService.class);
proxy.setRoutingKey(GameService.class.getSimpleName());
}
@Override
public void afterPropertiesSet() {
proxy.afterPropertiesSet();
}
@Override
public Service getObject() throws Exception {
return (Service) proxy.getObject();
}
@Override
public Class<?> getObjectType() {
return Service.class;
}
@Override
public boolean isSingleton() {
return proxy.isSingleton();
}
}
}
ServerConfig.java
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.remoting.service.AmqpInvokerServiceExporter;
import org.springframework.amqp.support.converter.RemoteInvocationAwareMessageConverterAdapter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServerConfig {
@Bean
public DirectExchange serviceExchange() {
return new DirectExchange("rpc");
}
@Bean
public Queue serviceQueue() {
return new Queue(Service.class.getSimpleName());
}
@Bean
public Binding binding(@Qualifier("serviceQueue") Queue queue, @Qualifier("serviceExchange") Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(Service.class.getSimpleName()).noargs();
}
@Bean("remoteInvocationAwareMessageConverter")
@Primary
public RemoteInvocationAwareMessageConverterAdapter remoteInvocationAwareMessageConverterAdapter(
Jackson2JsonMessageConverter jsonMessageConverter) {
return new RemoteInvocationAwareMessageConverterAdapter(jsonMessageConverter);
}
@Bean
public AmqpInvokerServiceExporter exporter(RabbitTemplate template, ServiceImpl service,
RemoteInvocationAwareMessageConverterAdapter messageConverter) {
AmqpInvokerServiceExporter exporter = new AmqpInvokerServiceExporter();
exporter.setAmqpTemplate(template);
exporter.setService(service);
exporter.setServiceInterface(Service.class);
exporter.setMessageConverter(messageConverter);
return exporter;
}
@Bean
public MessageListenerContainer container(ConnectionFactory connectionFactory,
@Qualifier("serviceQueue") Queue queue,
AmqpInvokerServiceExporter exporter) {
DirectMessageListenerContainer container = new DirectMessageListenerContainer(connectionFactory);
container.setQueues(queue);
container.setMessageListener(exporter);
container.setConsumersPerQueue(5);
return container;
}
}