Springboot整合rabbitMQ

  • Post author:
  • Post category:其他


一、导入依赖

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

二、配置yml文件连接rabbitMQ

  #  rabbitmq
  rabbitmq:
    #rabbitmq服务器地址
    host: 127.0.0.1
    #rabbitmq端口号
    port: 5672
    #rabbitmq连接那台虚拟机
    virtual-host: /
    #rabbitmq密码
    password: guest
    #rabbitmq账号
    username: guest

三、开启rabbitMQ服务

1.在启动器添加@EnableRabbit注解

@SpringBootApplication(scanBasePackages = {"com.javaweb.*"})
@MapperScan("com.javaweb.**.mapper")
@EnableTransactionManagement
// 开启定时任务支持
@EnableScheduling
//开启rabbitMQ服务
@EnableRabbit
public class ApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
        System.out.println("I启动成功");
    }
}


四、创建交换机

 @RequestMapping("/exchange")
    public JsonResult exchange(){
        DirectExchange directExchange=new DirectExchange("hello-java-exchange",true,true);
        amqpAdmin.declareExchange(directExchange);
        log.info("创建{}交换机成功","hello-java-exchange");
        return  JsonResult.success("创建交换机成功");
    }

五、创建队列

@RequestMapping("/queue")
    public JsonResult queue(){
        Queue queue=new Queue("hello-java-queue",true,false,false);
        amqpAdmin.declareQueue(queue);
        log.info("创建{}交换机成功","hello-java-queue");
        return  JsonResult.success("创建队列成功");
    }

六、创建绑定

@RequestMapping("/binding")
    public JsonResult binding()
    {
        Binding binding=new Binding("hello-java-queue",
                                    Binding.DestinationType.QUEUE,
                                    "hello-java-exchange",
                                    "hello.java",
                                    null);
        amqpAdmin.declareBinding(binding);
        log.info("创建{}binding成功","hello-java");
        return  JsonResult.success("创建绑定成功");
    }

七、给队列发消息

  @RequestMapping("/sendMessage")
    public JsonResult sendMessage()
    {
        rabbitTemplate.convertAndSend("hello-java-exchange","hello.java","哈哈哈哈");
       log.info("发送消息成功");
        return JsonResult.success("发送消息成功");
    }

八、如果需要发送JSON对象的需要写配置文件

@Configuration
public class MyRabbitConfig {
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
  @RequestMapping("/sendMessage")
    public JsonResult sendMessage()
    {
        Member member= memberService.getById(116);
        rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",member);
       log.info("发送消息成功");
        return JsonResult.success("发送消息成功");
    }

九、用@RabbitListener和@RabbitHandler接受消息

1.@RabbitListener 可用在类上+方法上

//    声明收到消息的队列
    @RabbitListener(queues = {"hello-java-queue"})
//    可以用Object接受信息
//    也可以用实体类member接受消息
    public void rabbitListener(Object object,Member member)
    {
//        获取消息
        System.out.println("object接受的消息是:"+object+"member实体类接受的消息是:"+member);
    }

2.@RabbitHandler 只能用在方法上

@Service
@RabbitListener(queues = {"hello-java-queue"})
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Autowired
    private MemberMapper memberMapper;

//    声明收到消息的队列
//    也可以用实体类member接受消息
    @RabbitHandler
    public void rabbitListener(Member member)
    {
//        获取消息
        System.out.println("member实体类接受的消息是:"+member);
    }



版权声明:本文为m0_48861211原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。