Springboot+RabbitMQ消息队列

第一步:安装对应的RabbitMQ安装和配置

RabbitMQ是基于Erlang语言开发的。所以安装RabbitMQ之前需要先下载安装配置Erlang,下载地址:http://www.erlang.org/downloads 
并将安装后的......\erl9.0\bin的bin目录配置到path环境变量中。然后下载安装RabbitMQ。下载地址:http://www.rabbitmq.com/download.html 
安装完成之后在开始菜单中找到RabbitMQ Command Promt,打开控制台,输入命令:

rabbitmq-plugins enable rabbitmq_management

第二步:springboot对应的配置:

pom.xml中添加

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

application.properties文件添加配置

#队列
spring.rabbitmq.host=localhost      //队列地址
spring.rabbitmq.port=5672           //端口号
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

第三步:配置队列

@Configuration
public class RabbitMQconfig {
    public static final String QUEUE = "direct_queue";
    @Bean
    public Queue directQueue(){
        return new Queue(QUEUE,true);
    }

}

第四步:发送端

@SpringBootApplication
@MapperScan("com.csv.dao")
public class CsvApplication implements CommandLineRunner {
    @Autowired
    private AmqpTemplate amqpTemplate;
    public static void main(String[] args) {
        SpringApplication.run( CsvApplication.class, args );
    }

    @Override
    public void run(String... args) {
        String massage="哈喽,帅哥";
        amqpTemplate.convertSendAndReceive( RabbitMQconfig.QUEUE,massage);
        System.out.println("ok");
    }
}

第五步:接收端

public class Receiver {
    @RabbitListener(queues = RabbitMQconfig.QUEUE)
    public  void  ReceiveMesaage(String  message){
        System.out.println("接受到"+message);
    }
}

接收到的效果

这个是自己学习的一个简单的练习,如有不对,请指教。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值