Rabbit

Author Avatar
kevin
发表:2023-08-18 15:49:00
修改:2024-10-09 15:49:21

Rabbit常用命令和搭建

消息队列(中间件)MQ

队列的形式是:先进先出

主要的作用

  1. 异步处理
    1. 不是同步进行,不影响后续组件接入
  2. 解耦服务
    1. 应用程序通过队列来通信
    2. 不像之前直接调用别的程序
  3. 流量削峰
    1. 将大量请求慢慢抽离
    2. 一步步的慢慢消化

spring+mq

添加依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>2.1.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.7.RELEASE</version>
        </dependency>
    </dependencies>
    
     <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

rabbitmq.properties

rabbitmq.host=localhost
rabbitmq.port=5672
rabbitmq.username=guest
rabbitmq.password=guest
rabbitmq.virtual-host=/

boot

  <!--
        1. 父工程依赖
    -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
</parent>

<dependencies>
    <!--2. rabbitmq-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>

application.yml

spring:
  rabbitmq:
    host: localhost # ip
    port: 5672
    username: guest
    password: guest
    virtual-host: /

创建提供者

配置类

 //交换机名字
    public static final String EXCHANGE_NAME = "boot_exchange";
    //路由规则
    public static final String QUEUE_NAME = "boot_queue";

  //构建交换机
    @Bean
    public Exchange bootExchange(){
        /**
         * durable表示是否持久化(重启还在)
         * topicExchange表示通配交换机
         * 也可以选择其他的
         */
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

 //构建队列
    @Bean
    public Queue bootQueue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }


    //交换机绑定队列
    @Bean
    public Binding bindQueueExchange(Queue queue,Exchange exchange){
        /**
         * 先绑定队列,在绑定交换机,由于上面的交换机是通配交换机
         * 所以还需要绑定路由规则,选择无参
         */
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }

创建入口类

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

创建测试类发送信息

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestRabbit {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void testMessage(){
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME,
                "boot.abc","我是一条信息");
    }
}

消费者工程

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>
    <dependencies>
        <!--RabbitMQ 启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>

配置文件和上面一样,不需要测试类

消息监听器,进行了封装,所以不需要测试类

@Component
public class MqListener {

    @RabbitListener(queues = "boot_queue")
    public void listenerQueue(Message message){
        System.out.println(new String(message.getBody()));
    }
}

创建入口类

和上面一样,直接运行即可

评论