zoukankan      html  css  js  c++  java
  • 基于spring-boot集成rabbit项目搭建。

    1. 创建生产者工程,pom依赖引入。
      <!--
      1. 父工程依赖
      -->
      <parent>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-parent</artifactId>
      	<version>2.2.2.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>
      
    2. application.yml配置整合
      spring:
        rabbitmq:
      	host: 192.168.137.130
      	port: 5672
      	username: admin
      	password: 123456
      	virtual-host: /
      
    3. 创建配置类
      @Configuration
      public class RabbitMQConfig {
      	public static final String EXCHANGE_NAME = "boot_topic_exchange";
      	public static final String QUEUE_NAME = "boot_queue";
      
      	// 1 交换机
      	@Bean("bootExchange")
      	public Exchange bootExchange(){
      		//代码连编   ctrl+alt+v:补全返回值格式。类似于.var和.castvar
      		return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
      	}
      	//2.Queue 队列
      	@Bean("bootQueue")
      	public Queue bootQueue(){
      		return QueueBuilder.durable(QUEUE_NAME).build();
      	}
      	//3. 队列和交互机绑定关系 Binding
      	/*
      		1. 知道哪个队列
      		2. 知道哪个交换机
      		3. routing key
      		noargs():表示不指定参数
      	 */
      	@Bean
      	public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue,
      									 @Qualifier("bootExchange") Exchange exchange){
      		return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
      	}
      }
      
    4. 创建入口类
      @SpringBootApplication
      public class ProducerApplication {
      	public static void main(String[] args) {
      		SpringApplication.run(ProducerApplication.class);
      	}
      }
      
    5. 发送消息
      @SpringBootTest
      @RunWith(SpringRunner.class)
      public class ProducerTest {
      
      	@Autowired
      	private RabbitTemplate rabbitTemplate;
      
      	/**
      	 * 第一个参数:交换机名字
      	 * 第二个参数:routingKey
      	 * 第三个参数:发送的消息
      	 */
      	@Test
      	public void testSend(){
      		rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","mq hello");
      	}
      }
      
    6. 搭建消费者工程,pom依赖引入
      <parent>
      	 <groupId>org.springframework.boot</groupId>
      	 <artifactId>spring-boot-starter-parent</artifactId>
      	 <version>2.2.2.RELEASE</version>
      </parent>
      <dependencies>
      	<!--RabbitMQ 启动依赖-->
      	<dependency>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-amqp</artifactId>
      	</dependency>
      </dependencies>
      
    7. application.yml配置整合
      spring:
        rabbitmq:
          host: 192.168.137.130
          port: 5672
      	username: admin
      	password: 123456
      	virtual-host: /
      
    8. 消息监听器
      @Component
      public class RabbimtMQListener {
      	@RabbitListener(queues = "boot_queue")
      	public void listenerQueue(Message message){
      		System.out.println(new String(message.getBody()));
      	}
      }
      
    9. 创建入口类
      @SpringBootApplication
      public class ConsumerSpringbootApplication {
      	public static void main(String[] args) {
      		SpringApplication.run(ConsumerSpringbootApplication.class, args);
      	}
      }
      
    10. 小结:
      • SpringBoot提供了快速整合RabbitMQ的方式
      • 基本信息在yml中配置,队列交互机以及绑定关系在配置类中使用Bean的方式配置
      • 生产端直接注入RabbitTemplate完成消息发送
      • 消费端直接使用@RabbitListener完成消息接收
  • 相关阅读:
    数据库事务隔离级别
    impala jdbc4的group by语句的bug,加上limit没错
    火狐不支持innerText属性,只支持innerHTML属性
    struts2.x + Tiles2.x读取多个xml 配置文件
    ids for this class must be manually assigned before calling save():Xxx
    整合ssh model $$_javassist_13 cannot be cast to javassist.util.proxy.Proxy
    火狐点击链接请求两次的问题
    C++——类和动态内存分配
    C++——使用类
    C++——对象和类
  • 原文地址:https://www.cnblogs.com/rbwbear/p/15557819.html
Copyright © 2011-2022 走看看