SpringBoot整合RabbitMQ

1 添加starter依赖

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

2 application.properties中添加连接信息

spring.application.name=springboot_rabbitmq
spring.rabbitmq.host=node111
spring.rabbitmq.virtual-host=/
spring.rabbitmq.username=root
spring.rabbitmq.password=112233
spring.rabbitmq.port=8088

3 主入口类

package com.clair.rabbitmq.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication 
pub1ic class RabbitqmDemo {

    public static void main(String[] args) {
        SpringApplication.run(RabbitqmDemo.class,args);
    }

}

4 RabbitConfig类

package com.clair.rabbitmq.demo.config;

import org.springframework.amqp.core.Binding; 
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    @Bean
    pub1ic Queue myQueue() {
        return new Queue ("myqueue");
    }

    @Bean
    pub1ic Exchange myExchange() {
//    new Exchange ()
//    return new TopicExchange("topic.biz.ex",false,false,nu11);
//    return new DirectExchange("direct.biz.ex",false,false,nu11);
//    return new FanoutExchange("fanout.biz.ex",false,false,nu11);
//    return new HeadersExchange("header.biz.ex",false,false,nu11);
//    交换器名称,交换器类型(),是否是持久化的,是否自动删除,交换器属性Map集合
//    return new CustomExchange("custom.biz.ex",ExchangeTypes.DIRECT,false,false,nu11);
        return new DirectExchange("myex",false,false,nu11);
    }

    @Bean 
    pub1ic Binding myBinding() {
//    绑定的目的地,绑定的类型:到交换器还是到队列,交换器名称,路由key,绑定的属性
//    new Binding("",Binding.DestinationType.EXCHANGE,"","",null);
//    绑定的目的地,绑定的类型:到交换器还是到队列,交换器名称,路由key,绑定的属性
//    new Binding("",Binding.DestinationType.QUEUE,null);
//    绑定了交换器direct.biz.ex到队列myqueue,路由key是direct.biz.ex
        return new Binding(
                "myqueue"
                Binding.DestinationType.QUEUE,
                "myex",
                "direct.biz.ex",
                null);
    }
}

5 使用RestController发送消息

package com.clair.rabbitmq.demo;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.Pathvariab1e;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestControl1er;

@RestContro1ler
pub1ic class Hel1oController {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    @RequestMapping("/send/{message}")
    public String sendMessage(@PathVariable String message) {
        rabbitTemplate.convertAndsend("myex","direct.biz.ex",message);
        return "ok";
    }

}

6 使用监听器,用于推消息

package com.clair.rabbitmq.demo;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
    pub1ic class HelloConsumer {

        @Rabbitistener(queues = "myqueue")
        public void service(String message) {
            System.out.print1n("消息队列推送来的消息:”+ message);
        }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值