SpringMvc项目配置RabbitMq

前言:只有消费者部分,没有记录生产者部分

结构图

配置类

可以xml配置,也可以配置类,二者可以相互转化。两种bean注入的方式。

java 复制代码
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DbDataQueueConsumeConfig {

    @Value("${rabbitmq.host}")
    private String host;

    @Value("${rabbitmq.port:5672}")
//    @Value("#{new Integer('${rabbitmq.port}')}")
    private Integer port;

    @Value("${rabbitmq.username}")
    private String username;

    @Value("${rabbitmq.password}")
    private String password;

    @Value("${rabbitmq.virtual-host}")
    private String virtualHost;

    @Value("${rabbitmq.queue}")
    private String queueName;

    /**
     * 配置连接工厂
     */
    @Bean(name = "rabbitConnectionFactory")
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    /**
     * 配置 RabbitTemplate,用于发送消息
     */
    @Bean
    public RabbitTemplate rabbitTemplate(@Qualifier("rabbitConnectionFactory") ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate;
    }


    @Bean
    public Jackson2JsonMessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 配置消息监听容器
     */
    @Bean
    public MessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory, BdDataQueueListener bdDataQueueListener) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(new MessageListenerAdapter(bdDataQueueListener));
        return container;
    }


}

配置文件中配置好rabbitmq服务的地址

properties 复制代码
rabbitmq.host=xxx.xxx.xxx.xxx
rabbitmq.port=3011
rabbitmq.username=username
rabbitmq.password=password
rabbitmq.virtual-host=/xxx
rabbitmq.queue=queuename

消费者

java 复制代码
@Component
public class BdDataQueueListener implements MessageListener {


    @Override
    public void onMessage(Message message) {

        try {
           // 这里写业务逻辑
        } catch (Exception e) {

        }
    }

}

重要部分都在上面。其余的不在放出来。

参考文章

文章1

文章2

相关推荐
kyle~33 分钟前
DDS分布式实时系统---自省机制
开发语言·分布式·机器人·c#·接口·ros2
q21030633721 小时前
kafka启动几秒后挂了,重启多次无果
分布式·kafka
凯源智能2 小时前
工商业分布式光伏箱变智能监控落地实战
分布式·箱变测控·光伏箱变测控装置·箱变监控系统·箱式变测控装置
沂水弦音2 小时前
软控 EI 系列模块优势与竞品对比分析:面向 EtherCAT 分布式 I/O 的工程选型视角
分布式·制造·工业自动化·ethercat·io模块
木心术13 小时前
在NVIDIA DGX Spark上部署NemoClaw的实际操作方案以及实际应用便利性。
大数据·分布式·spark
kuokay3 小时前
MLOps 与 AIOps 的核心概
人工智能·分布式·大模型·agent·llama
openFuyao4 小时前
openFuyao InferNex:云原生分布式 LLM 推理加速套件——从生产痛点到算力的极致释放
分布式·云原生·ai原生·openfuyao·多样化算力
咖啡星人k21 小时前
MonkeyCode 开源协作指南:如何让分布式团队高效使用AI编程
分布式·开源·ai编程·monkeycode
阿坤带你走近大数据21 小时前
如何保证kafka中的数据一致性
分布式·kafka
凯源智能21 小时前
高寒地区分布式光伏箱变测控系统落地实战
分布式·箱变测控·光伏箱变测控装置·箱变监控系统