Springboot集成RabbitMq初级

RabbitMq集成到SpringBoot中

引入依赖

<!--rabbitmq 的依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置rabbit

spring:
  # 配置rabbitmq
  rabbitmq:
    host: localhost
    port: 5672
    username: ems
    password: 123456
    virtual-host: /ems

写测试类

springboot的测试,没有具体了解。 注入rabbitTemplate,测试各个工作模式

//启动类的class文件
@SpringBootTest(classes = RabbitMqApplication.class)
@RunWith(SpringRunner.class)
public class MqTest {

    //注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //hello
    @Test
    public void testHello() {
        rabbitTemplate.convertAndSend("hello", "hello world!");
        System.out.println("testHello 执行了");
    }

    //work
    @Test
    public void testWork() {
        for (int i = 1; i <= 10; i++) {
            rabbitTemplate.convertAndSend("work", "work模型~"+i);
        }
    }

    //fanout 广播
    @Test
    public void testFanout(){
        rabbitTemplate.convertAndSend("logs","","fanout模型发送的消息");
    }

    //route 路由
    @Test
    public void testRoute(){
        rabbitTemplate.convertAndSend("directs","info","发送info的key路由信息");
    }

    //topic 动态路由 订阅模式
    @Test
    public void testTopic(){
        rabbitTemplate.convertAndSend("topic","user.save","发送user.save路由信息");
    }
}

各个模型的消费者

1、helloworld模型 点对点

@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloConsumer {

    @RabbitHandler
    public void receive1(String message){
        System.out.println("message: "+message);
    }
}

2、work模型

@Component
public class WorkConsumer {

    //第一个消费之
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1(String message){
        System.out.println("message1: "+message);
    }

    //第二个消费之
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(String message){
        System.out.println("message2: "+message);
    }
}

3、fanout 广播模型

@Component
public class FanoutConsumer {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, //创建临时队列
                    exchange = @Exchange(value = "logs",type = "fanout") //绑定交换机
            )
    })
    public void receive1(String message){
        System.out.println("message1: "+message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, //创建临时队列
                    exchange = @Exchange(value = "logs",type = "fanout") //绑定交换机
            )
    })
    public void receive2(String message){
        System.out.println("message2: "+message);
    }
}

4、route 路由模型

@Component
public class RouteConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, //创建临时队列
                    exchange = @Exchange(value = "directs",type = "direct"), //绑定交换机
                    key={"info","error","warn"}
            )
    })
    public void receive1(String message){
        System.out.println("message1: "+message);
    }


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, //创建临时队列
                    exchange = @Exchange(value = "directs",type = "direct"), //绑定交换机
                    key={"error"}
            )
    })
    public void receive2(String message){
        System.out.println("message2: "+message);
    }
}

5、topic 动态路由 订阅模型

@Component
public class TopicConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, //创建临时队列
                    exchange = @Exchange(value = "topic",type = "topic"), //绑定交换机
                    key={"user.save","user.add"}
            )
    })
    public void receive1(String message){
        System.out.println("message1: "+message);
    }


    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, //创建临时队列
                    exchange = @Exchange(value = "topic",type = "topic"), //绑定交换机
                    key={"user.*"}
            )
    })
    public void receive2(String message){
        System.out.println("message2: "+message);
    }
}

修改rabbitmq的序列化方式

@Configuration
public class SpringBeanConfig {

    //替换RabbitMq的序列化处理用jackson
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

直接使用对象

生成者:

//将添加的新闻放入消息队列
rabbitTemplate.convertAndSend("news", "news.addNews", news);

消费者:

/**
* 添加新闻文档
* @param news
*/
@RabbitListener(bindings = {
    @QueueBinding(
        value = @Queue, //创建临时队列
        exchange = @Exchange(value = "news", type = "topic"), //绑定交换机
        key = {"news.addNews"}
    )
})
public void addNews(News news) {
    Integer id = news.getId();
    String createTime = news.getCreateTime();

    String yearMonth = DateUtil.getYearMonth(createTime);
    String day = DateUtil.getDay(createTime);

    news.setYearMonth(yearMonth);
    news.setDay(day);
    try {
        String newsJson = objectMapper.writeValueAsString(news);
        clientService.addDoc(INDEX_NAME, id.toString(), newsJson);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值