敞开生长之旅!这是我参加「日新计划 12 月更文挑战」的第18天,点击查看活动概况

Springboot整合RabbitMQ(出产端)

过程如下

1.创立出产者SpringBoot项目

Springboot整合RabbitMQ生产端和消费端

2.引进依靠


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!--RabbitMQ依靠-->
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <!--单元测验-->
 <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3.编写yml装备基本信息

 #装备rabbitmq基本信息 ip port  username password...
spring:
    rabbitmq:
        #ip 默许localhost 长途172.16.98.133
  host: localhost
        #暗码  默许guest 游客
  password: guest
        #端口
  port: 5672
        #用户名 默许guest 游客
  username: guest
        #虚拟机 默许/
  virtual-host: /

4.界说交换机,行列以及绑定联系的装备类

package com.wyh.rabbitmq.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 /**
 *  @program:  SpringBoot_RabbitMQ_Procuder
 *  @description:  RabbitMQ装备类
 *  @author:  魏一鹤
 *  @createDate:  2022-03-31 23:49
 **/
 //用于界说装备类,可替换xml装备文件
@Configuration
public class RabbitMQConfig {
    //界说常量
  public static final String EXCHANGE_NAME = "boot_topic_exchange" ;
    public static final String QUEUE_NAME = "boot_queue" ;
    //1 交换机 exchange
 //界说Bean
 @Bean( "bootExchange" )
    public Exchange bootExchange(){
        //构建并且回来通配符规矩的交换机               durable耐久化
  return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }
    //2 行列 queue
 //界说Bean
 @Bean( "bootQueue" )
    public Queue bootQueue() {
        //耐久化行列
  return QueueBuilder.durable(QUEUE_NAME).build();
    }
    //3 行列和行列的绑定联系 binding
 /**
 * 1 知道哪个交换机
 * 2 知道哪个行列
 * 3 设置routingkey 路由键
 **/
 @Bean
    //把交换机和行列进行注入作为参数 @Qualifier精准注入某一个bean
  public Binding bindQueueExchange(@Qualifier( "bootQueue" )Queue queue,@Qualifier( "bootExchange" ) Exchange exchange){
       //指定交换机和行列的绑定 并且指定路由key #0个或许多个单词  noargs不指定参数
  return BindingBuilder.bind(queue).to(exchange).with( "boot.#" ).noargs();
    }
}

5.注入RabbitTemplate,调用办法,完结音讯发送

package com.wyh.test;
import com.wyh.rabbitmq.config.RabbitMQConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import javax.annotation.security.RunAs;
 /**
 *  @program:  SpringBoot_RabbitMQ_Procuder
 *  @description:  测验rabbitmq出产者
 *  @author:  魏一鹤
 *  @createDate:  2022-04-02 23:26
 **/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
    //1 注入RabbitTemplate类
 @Resource
    private RabbitTemplate rabbitTemplate;
    //测验
 @Test
    public void send() {
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "boot.weiyihe" , "boot mq hello!!!" );
    }
}

6测验

登录guest(装备的那个账户).查看对应的虚拟机(装备的那个虚拟机),查看行列以及发送的音讯

Springboot整合RabbitMQ生产端和消费端

Springboot整合RabbitMQ生产端和消费端

17 Springboot整合RabbitMQ(消费端)

过程如下

1 创立出产者SpringBoot项目

Springboot整合RabbitMQ生产端和消费端

2 引进依靠


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!--RabbitMQ依靠-->
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <!--单元测验-->
 <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3 编写yml装备基本信息

 #装备rabbitmq基本信息 ip port  username password...
spring:
  rabbitmq:
    #ip 默许localhost 长途172.16.98.133
  host: localhost
    #暗码  默许guest 游客
  password: weiyihe
    #端口
  port: 5672
    #用户名 默许guest 游客
  username: weiyihe
    #虚拟机 默许/
  virtual-host: /itcast_wyh

4 界说监听器,运用@RabbitListener注解完结行列监听

package com.wyh.listener;
import com.sun.org.apache.xpath.internal.operations.String;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
 /**
 *  @program:  SpringBoot_RabbitMQ_Procuder
 *  @description:  RabbitMQ消费者监听器
 *  @author:  魏一鹤
 *  @createDate:  2022-04-03 22:39
 **/
 //表明这是一个装备文件
@Component
public class RabbitMQListener {
    //监听器注解 里边是监听的行列的称号
 @RabbitListener(queues = "boot_queue" )
    public void Listener(Message message) {
        //由于音讯以及被消费了 想要得到纤细只能重新发送
 System.out.println(message); //(Body:'boot mq hello!!!' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=true, receivedExchange=boot_topic_exchange, receivedRoutingKey=boot.weiyihe, deliveryTag=1, consumerTag=amq.ctag-S-FylSWagEzp7TP8BkTvpA, consumerQueue=boot_queue])
 }
}

5 测验

Springboot整合RabbitMQ生产端和消费端

SpingBoot整合RabbitMQ总结

1 SpringBoot提供了快速整合RabbitMQ的方法.需求引进AMQP依靠

2 基本信息在YML中装备,行列交换机以及绑定联系在装备类中运用Bean方法装备

3 出产端直接注入RabbitTemplate完结音讯发送

4 消费端直接引用@RabbitListener完结音讯接收