SpringBoot使用RabbitMQ

在SpringBoot下使用RabbitMQ

1.环境搭建:

在Spring初始化器里导入Message模块里面的Spring for RabbitMQ的starter和web模块即可

在配置文件application.yml中配置

spring:
  rabbitmq:
    host: www.luckycurve.cn
    username: **********
    password: *********

检验环境搭建是否成功

@RestController
public class RabbitMQController {

    @Autowired
    AmqpAdmin amqpAdmin;

    @GetMapping("/")
    public String hello(){
        amqpAdmin.declareExchange(new TopicExchange("myExchange2"));
        return "创建成功";
    }
}

SpringBoot操纵RabbitMQ的思路:

尽量不使用15672端口去管理Exchange和Binding

使用AmqpAdmin(自动注入即可)去操作固件【建议改成@Configuration注解方式】,使用rabbitTemplate去操纵收发消息

AmpqAdmin部分(创建结构):

    @GetMapping("/")
    public String hello(){
//        创建Exchange
        amqpAdmin.declareExchange(new TopicExchange("myFirstExchange"));
//        创建Queue
        amqpAdmin.declareQueue(new Queue("myFirstQueue",true));
//        建立Binding
        amqpAdmin.declareBinding(new Binding("myFirstQueue", Binding.DestinationType.QUEUE,"myFirstExchange","hello.*",null));
        return "创建成功";
    }

如果已经存在了就不会创建,不存在就会创建(可以在消息队列里面放置一条消息,然后在运行上面的代码段,过后发现消息依旧在里面,即可证明)

建议使用@configuration注解代替AmpqAdmin部分:

能保证每次启动SpringBoot程序的时候即可自动去RabbitMQ里创建Exchange和Binding

@Configuration
public class TopicRabbitConfig {

    final static String message = "topic.message";
    final static String messages = "topic.messages";

    @Bean
    public Queue queueMessage() {
        return new Queue(TopicRabbitConfig.message);
    }

    @Bean
    public Queue queueMessages() {
        return new Queue(TopicRabbitConfig.messages);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }

    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
    }

    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
    }
}

rabbitTemplate部分(收发消息,不过收消息使用了Listener来做了):

    @GetMapping("/send")
    public String hello2(){
        GirlFriend girlFriend = new GirlFriend();
        girlFriend.setName("Lucy");
        girlFriend.setHeight(160);
        girlFriend.setLevel(5);
        rabbitTemplate.convertAndSend("myFirstExchange","hello.lucy",girlFriend);
        return "发送成功";
    }

监听,要加上@EnableRabbit注解开启监听【只有当队列有变化的时候,Listener才会触发】

一般加在Service上

@Service
public class RabbitMQService {

    @RabbitListener(queues = {"myFirstQueue"})
    public void receive(GirlFriend girlFriend){
        System.out.println("收到的GirlFriend为:"+girlFriend);
    }
}

项目完成

附上实体类GirlFriend:

public class GirlFriend implements Serializable {
    private String name;
    private Integer height;
    private Integer level;		
}

建议其中的level用枚举类型哈(弱鸡还在研究当中,先不用了)

此时如果去掉监听器,去15672,发现GirlFriend类默认使用JDK序列化机制成乱码

可以使用如下代码取代JDK序列化,即可解决

@Configuration
public class MyAMQPConfig {
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

项目的运行思路:

访问localhost:8080,会创建消息队列,转换器,绑定规则

访问localhost:8080/send,会发送消息

此时监听器接收到消息,打印出收到的消息(消息消费),流程完成。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值