SpringBoot整合Rabbitmq

4 篇文章 0 订阅
1 篇文章 0 订阅

 

安装Rabbitmq

本人是在Linux上安装的docker

启动docker,然后执行命令:docker pull rabbitmq:management 安装Rabbitmq容器

启动Rabbitmq:docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq 30e33de9be86  最后一个是id(使用docker ps -a查看)

输入端口+15672进入Management页面

 

 

创建工程

导入依赖

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

配置

application.properties

spring.rabbitmq.host=192.168.32.128
spring.rabbitmq.username=guest
spring.activemq.password=guest
spring.freemarker.view-names=/

配置类 序列化转json格式

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

Bean

public class Student implements Serializable {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

简单Test类

@SpringBootTest
class RabbitmqDemoApplicationTests {

    @Test
    void contextLoads() {
    }

    @Autowired
    RabbitTemplate rabbitTemplate;

    //点对点direct  需要具体 routingKey
    @Test
    public void ptp(){
        rabbitTemplate.convertAndSend("qyc.direct","py","987654");
    }
    @Test
    public void receive1(){
        String message = (String) rabbitTemplate.receiveAndConvert("py");
        System.out.println(message);
    }


    //广播fanout  不需要路由 发送转换器下全体queue
    @Test
    public void guangbo(){
        rabbitTemplate.convertAndSend("qyc.fanout","","123456");

    }
    @Test
    public void receive2(){
        String s = (String) rabbitTemplate.receiveAndConvert("py");
        String s1 = (String) rabbitTemplate.receiveAndConvert("py.news");
        String s2 = (String) rabbitTemplate.receiveAndConvert("py.emps");
        System.out.println(s+" "+s1+" "+s2);
    }
    //订阅 topic
    @Test
    public void dinyue(){
        LocalDateTime localDateTime = LocalDateTime.now();
        Student student = new Student("强月城",22 );
        rabbitTemplate.convertAndSend("qyc.topic","py.*",student);
    }
    @Test
    public void receive3(){
        Student student = (Student) rabbitTemplate.receiveAndConvert("py.emps");
        System.out.println(student);
    }
}

 

注解

开启注解

@SpringBootApplication
@EnableRabbit
public class RabbitmqDemoApplication {

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

}

Service

@Service
public class SericeDemo1 {
    @RabbitListener(queues = "py.news")
    public void getMessage(Student student){
        System.out.println(student);
    }
    //获得消息
    @RabbitListener(queues = "py.emps")
    public void getMessage2(Message message){
        System.out.println("py.emp Body:"+message.getBody());
        System.out.println("py.emp Properties:"+message.getMessageProperties());
    }

}

创建 Exchange queue Binding

    //创建Exchange(转换器)  queue(队) Binding(绑定)
    @Autowired
    AmqpAdmin amqpAdmin;
    @Test
    public void createExchange(){
        amqpAdmin.declareExchange(new DirectExchange("directExchange",true,false));
        System.out.println("创建完成");
    }
    @Test
    public void createFanout(){
        amqpAdmin.declareExchange(new FanoutExchange("fanoutExchange",true,false));
        System.out.println("创建完成");
    }
    @Test
    public void createTopic(){
        amqpAdmin.declareExchange(new TopicExchange("topicExchange",true,false));
        System.out.println("创建完成");
    }

    //创建队列
    @Test void createQueue(){
        //drable 持久化 默认不自动删除
        amqpAdmin.declareQueue(new Queue("py.emps.qyc",true));
    }
    //创建绑定  topic
    @Test void createBinding(){
        amqpAdmin.declareBinding(new Binding("py.news", Binding.DestinationType.QUEUE,
                "topicExchange","py.*",null));
        amqpAdmin.declareBinding(new Binding("py.emps", Binding.DestinationType.QUEUE,
                "topicExchange","py.*",null));
        amqpAdmin.declareBinding(new Binding("py.emps.qyc", Binding.DestinationType.QUEUE,
                "topicExchange","py.emps.*",null));
        amqpAdmin.declareBinding(new Binding("py.emps.qyc", Binding.DestinationType.QUEUE,
                "topicExchange","py.#",null));
    }

路由

个人理解:路由相当于一个链接,一对一模式和订阅模式需要路由,因为在发送消息的时候(参数:交换器,路由,消息),Exchange交换器要定位导具体的queue,

订阅模式支持 *  #  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值