RabbitMQ:使用Spring将消息映射到课堂

发布于 2025-02-11 19:38:02 字数 2757 浏览 1 评论 0 原文

我有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());
    }


}

I have 2 different microservices that communicate through rabbit. The first microservice has the logic of the publisher and I was requested to create the consumer to consume the messages ih the queue, all this in a new microservice.

What I've done so far is the service class of the consumer like this:

@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);
    }

But I don't know how to map what I get in the payload in a proper class.

My config class is as follows:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2025-02-18 19:38:02

AMQP消息包含字节[] 的身体。因此,使其转换为 country ,您必须提供相应的转换器。请参阅 MessageConverter @rabbitlistener 的属性。或提供全局BEAN,Spring Boot的自动配置将其设置为 RabbitListenerContainerFactory

在文档中查看有关转换的更多信息:

The AMQP message contains a byte[] for its body. So make it converted to that Country, you must provide a respective converter. See messageConverter attribute of the @RabbitListener. Or provide a global bean and Spring Boot's auto-configuration will set it into the RabbitListenerContainerFactory.

See more in docs about conversions: https://docs.spring.io/spring-amqp/docs/current/reference/html/#message-converters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文