RabbitMQ:使用Spring将消息映射到课堂
我有2种通过兔子通信的不同微服务。第一个微服务具有发布者的逻辑,我被要求创建消费者以在新的微服务中消费邮件中的所有消息。
到目前为止,我所做的就是这样的消费者服务类:
@Service
public class RabbitMQService {
private final Logger logger = LoggerFactory.getLogger(RabbitMQService.class);
@RabbitListener(queues = "${peopleevents.queue}")
public void receivedMessage(@Payload Country country) {
logger.info("Received message is.. " + country.toString());
System.out.println(country);
}
但是我不知道如何在适当的课程中映射我在有效载荷中获得的内容。
我的配置类如下:
@EnableRabbit
@Configuration
public class MQConfig {
@Value("${peopleevents.queue}")
public String queue;
@Value("${peopleevents.exchange}")
public String exchange;
@Value("${peopleevents.routingkey}")
public String routingKey;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
@Value("${spring.rabbitmq.addresses}")
private String address;
@Value("${spring.rabbitmq.vhost}")
private String vHost;
@Bean
public Queue queue() {
return new Queue(queue, true, false, false);
}
@Bean
Exchange myExchange() {
return ExchangeBuilder.topicExchange(exchange).durable(true).build();
}
@Bean
public Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder
.bind(queue)
.to(exchange)
.with(routingKey);
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public AmqpTemplate template(ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMandatory(true);
return rabbitTemplate;
}
@Bean
public ConnectionFactory connectionFactory() throws IOException {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses(address);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(vHost);
connectionFactory.setPublisherReturns(true);
return connectionFactory;
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
@Bean
public AmqpAdmin amqpAdmin() throws IOException {
return new RabbitAdmin(connectionFactory());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AMQP消息包含
字节[]
的身体。因此,使其转换为country
,您必须提供相应的转换器。请参阅MessageConverter
@rabbitlistener
的属性。或提供全局BEAN,Spring Boot的自动配置将其设置为RabbitListenerContainerFactory
。在文档中查看有关转换的更多信息:
The AMQP message contains a
byte[]
for its body. So make it converted to thatCountry
, you must provide a respective converter. SeemessageConverter
attribute of the@RabbitListener
. Or provide a global bean and Spring Boot's auto-configuration will set it into theRabbitListenerContainerFactory
.See more in docs about conversions: https://docs.spring.io/spring-amqp/docs/current/reference/html/#message-converters