当前位置:   article > 正文

SpringBoot 集成RabbitMQ集群及简单操作_springboot rabbitmq集群

springboot rabbitmq集群

RabbitMQ集群搭建参考:https://blog.csdn.net/weixin_42465125/article/details/88368784

在Spring官网的生成器,生成两个Maven工程:

rabbit-producer-demo:

Pom文件生成者和消费者一致

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>cn.cuit.rabbit</groupId>
  12. <artifactId>rabbit-producer-demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>rabbit-producer-demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-amqp</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <plugins>
  36. <plugin>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-maven-plugin</artifactId>
  39. </plugin>
  40. </plugins>
  41. </build>
  42. </project>

properties文件也是一致的:

  1. spring.application.name=spirng-boot-rabbitmq-producer
  2. # 配置集群的VIP # 192.168.174.150:5672
  3. spring.rabbitmq.addresses=192.168.174.150:5672
  4. # 配置真实IP也可以
  5. #spring.rabbitmq.addresses=192.168.174.140:5672,192.168.174.141:5672
  6. spring.rabbitmq.username=cuit
  7. spring.rabbitmq.password=cuit
  8. spring.rabbitmq.connection-timeout=15000
  9. # confirm模式
  10. spring.rabbitmq.publisher-confirms=true
  11. # return机制
  12. spring.rabbitmq.publisher-returns=true
  13. # 与return机制结合配置次属性
  14. spring.rabbitmq.template.mandatory=true

配置一个队列: 

  1. package cn.cuit.rabbit.one2one.producer;
  2. import org.springframework.amqp.core.Queue;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. public class RabbitConfig {
  7. @Bean
  8. public Queue queue() {
  9. return new Queue("q_hello");
  10. }
  11. }

消息生成者:

  1. package cn.cuit.rabbit.one2one.producer;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import org.springframework.amqp.core.AmqpTemplate;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * 简单队列
  9. *
  10. * 更多例子参考
  11. * https://blog.csdn.net/hellozpc/article/details/81436980#8SpringbootRabbitMQ_1267
  12. * https://blog.csdn.net/aa1215018028/article/details/81325082
  13. */
  14. @Component
  15. public class HelloSender {
  16. @Autowired
  17. private AmqpTemplate rabbitTemplate;
  18. public void send() {
  19. String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());// 24小时制
  20. String context = "hello " + date;
  21. System.out.println("Sender : " + context);
  22. // 简单对列的情况下routingKey即为Q名
  23. this.rabbitTemplate.convertAndSend("q_hello", context);
  24. }
  25. }

生产者发送消息测试:

  1. package cn.cuit.rabbit.one2one.producer;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7. @RunWith(SpringRunner.class)
  8. @SpringBootTest
  9. public class RabbitMqHelloTest {
  10. @Autowired
  11. private HelloSender helloSender;
  12. @Test
  13. public void hello() throws Exception {
  14. helloSender.send();
  15. }
  16. }

运行测试,然后观察在RabbitMQ的web界面去看看:

可以看到我们刚刚代码中创建的q_hello队列 

查看消息:

下面来消费端是否可以在RabbitMQ中获取到这个消息

消费端:

  1. package cn.cuit.rabbit.one2one.consumer;
  2. import org.springframework.amqp.rabbit.annotation.RabbitHandler;
  3. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @RabbitListener(queues = "q_hello")
  7. public class HelloReceiver {
  8. @RabbitHandler
  9. public void process(String hello) {
  10. System.out.println("Receiver : " + hello);
  11. }
  12. }

启动类:

  1. package cn.cuit.rabbit;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class RabbitConsumerDemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(RabbitConsumerDemoApplication.class, args);
  8. }
  9. }

启动消费端SpringBoot程序,观察控制台输出:

OK的,消费者从RabbitMQ中获取到刚刚的消息了,RabbitMQ有好几种发送消息的模式和消费消息的模式,更多例子参考代码中的链接

********************************* 不积跬步无以至千里,不积小流无以成江海 *********************************

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/728219
推荐阅读
相关标签