zoukankan      html  css  js  c++  java
  • rabbitmq学习记录3

    rabbitmq学习记录

    1.topic 主题模式

    rounting key满足如下规则
    单词.单词
    可以代表一个任意单词 #可以代表多个任意单词
    当一个队列的绑定键为#就变成fanout
    当一个队列没有#和
    绑定类型就是direct

    public class EmitLogTopic {
        public static final String EXCHANGE_NAME = "topic_logs";
    
        public static void main(String[] args) throws  Exception{
            Channel channel = RabbitMqUtils.getChannel();
            /**
             *
             */
            Map<String,String> bindingKeyMap= new HashMap<String,String>();
            bindingKeyMap.put("quick.orange.rabbit","被队列Q1Q2接收到了");
            bindingKeyMap.put("lazy.orange.elephant","被队列Q1Q2接收到");
            bindingKeyMap.put("quick.orange.fox","被队列Q1接收到");
            bindingKeyMap.put("lazy.brown.fox","被队列Q2接收到");
            bindingKeyMap.put("lazy.pink.rabbit","虽然满足两个绑定但只被队列Q2接受一次");
            bindingKeyMap.put("quick.brown.fox","不匹配任何绑定不会被任何队列接受到,会被丢弃");
            bindingKeyMap.put("quick.orange.nale.rabbit","是四个单词,不匹配任何绑定,会被丢弃");
            bindingKeyMap.put("lazy.orange.male.rabbit","是四个单词但匹配Q2");
            //高级循环
            for (Map.Entry<String, String> bindkingKeyEntry : bindingKeyMap.entrySet()) {
                String routingKey = bindkingKeyEntry.getKey();
                String message = bindkingKeyEntry.getValue();
                channel.basicPublish(EXCHANGE_NAME,routingKey,null,message.getBytes(StandardCharsets.UTF_8));
                System.out.println("生产者发送消息");
            }
        }
    }
    
    public class ReceiveLogsTopic02 {
        //交换机名称
        public static final String EXCHANGE_NAME = "topic_logs";
    
        public static void main(String[] args) throws Exception{
            Channel channel = RabbitMqUtils.getChannel();
            //声明交换机
            channel.exchangeDeclare(EXCHANGE_NAME,"topic");
            //声明队列
            String queueName = "Q2";
            /**
             * 1.名称
             * 2.持久化
             * 3.共享
             * 4.自动删除
             * 5。参数
             */
            channel.queueDeclare(queueName,false,false,false,null);
            channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit");
            channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#");
            System.out.println("等待接收消息");
            //接收消息
            DeliverCallback deliverCallback = (consumerTag, message) -> {
                System.out.println("控制台1打印接收到的消息" + new String(message.getBody()));
                System.out.println("接收队列:" + queueName + "绑定键" + message.getEnvelope().getRoutingKey());
            };
            channel.basicConsume(queueName,true,deliverCallback,consumerTag ->{});
        }
    
    }
    

    Springboot与rabbitmq整合

    添加依赖

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.amqp</groupId>
                <artifactId>spring-rabbit-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    
    server:
      port: 8021
    spring:
      #应用的名字,在微服务中比较重要
      application:
        name: demo2
      #rabbitmq的信息
      rabbitmq:
        host: 202.200.231.14
        port: 15672
        username: 
        password: 
    
    

    Springboot中提供母版对象,rabbittemplate,使用时直接在项目中注入即可使用。

    rabbitmq整合springboot

    引入依赖

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

    配置文件

    server:
      port: 8021
    spring:
      #给项目来个名字
      application:
        name: rabbitmq-provider
      #配置rabbitMq 服务器
      rabbitmq:
        host: 
        port: 
        username: 
        password:
        virtual-host: /ems
    
  • 相关阅读:
    [转帖]活用Quartus II内置模板,快速输入HDL代码、TimeQuset束缚及tcl语句等
    [笔记] FPGA的发展
    [转帖]状态机的编码
    [笔记]Altera中DDR3设计
    [笔记]Test Plan的编写 及 程序开头注释
    [HDOJ2457]DNA repair
    [HDOJ2355]The Sidewinder Sleeps Tonite
    [HDOJ2825]Wireless Password
    [HDOJ2222]Keywords Search
    [HDOJ2454]Degree Sequence of Graph G
  • 原文地址:https://www.cnblogs.com/wenwenjiejie/p/15161941.html
Copyright © 2011-2022 走看看