Springboot整合Rabbitmq

101 阅读1分钟
  <!-- springboot整合rabbitmq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

配置

spring.application.name=springboot_rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.virtual-host=/
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.port=5672

配置队列 交换机类型以及绑定等


package com.test.springboot_rabbitmq.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {


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


    @Bean
    public Exchange myExchange() {

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


    @Bean
    public Binding myBining() {

        // 绑定的目的地,绑定的类型:到交换器还是到队列,交换器名称,路由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);

    }

}

发送消息



@RestController
@RequestMapping("test")
public class RabbitmqTestController {


    @Autowired
    private AmqpTemplate rabbitTemplate;

    @RequestMapping("/send/{message}")
    public String sendMessage(@PathVariable String message) throws UnsupportedEncodingException {

        //消息属性
        MessageProperties messageProperties = MessagePropertiesBuilder.newInstance()
                .setContentEncoding(MessageProperties.CONTENT_TYPE_JSON) //类型
                .setHeader("key", "123").build();

        //消息编码
        Message build = MessageBuilder.withBody(message.getBytes("utf-8")).andProperties(messageProperties).build();
        rabbitTemplate.convertAndSend("myex", "direct.biz.ex", build);
        return "ok";
    }

}

监听消息



@Component
public class HelloConsumer {
//
//    @RabbitListener(queues = "myqueue")
//    public void service(String message) {
//        System.out.println("消息队列推送来的消息:" + message);
//    }


    /**
     * 监听到的消息 以及设置的消息属性
     * @param message
     * @param value
     */
    @RabbitListener(queues = "myqueue")
    public void service(@Payload String message, @Header(name = "key")String value) {
        System.out.println("消息队列推送来的消息:" + message  + " value = " + value);
    }
}