使用springboot操作rabbitmq
因为使用了swagger,可以换种方式测试
地址 http://localhost:8080/swagger-ui.html
下面这个只有一个队列
代码部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.java</groupId> <artifactId>rabbitmq-server</artifactId> <packaging>pom</packaging> <version> 1.0 -SNAPSHOT</version> <modules> <module>rabbitmq-provider</module> <module>rabbitmq-consumer</module> <module>rabbitmq-original</module> <module>rabbitmq-springboot</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.5 . 7 </version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version> 1.8 </java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> <version> 2.5 . 7 </version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version> 2.5 . 7 </version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency> <!--引入junit单元测试依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.12 </version> </dependency> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version> 5.12 . 0 </version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version> 2.9 . 2 </version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version> 2.9 . 2 </version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> <version> 3.2 . 5 </version> </dependency> <!-- <dependency>--> <!-- <groupId>org.springframework.cloud</groupId>--> <!-- <artifactId>spring-cloud-stream-binder-kafka</artifactId>--> <!-- <version> 2.2 . 1 .RELEASE</version>--> <!-- </dependency>--> <!-- <dependency>--> <!-- <groupId>org.springframework.cloud</groupId>--> <!-- <artifactId>spring-cloud-stream-binder-rocketmq</artifactId>--> <!-- <version> 0.2 . 2 .RELEASE</version>--> <!-- </dependency>--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <parent> <artifactId>rabbitmq-server</artifactId> <groupId>com.java</groupId> <version> 1.0 -SNAPSHOT</version> </parent> <modelVersion> 4.0 . 0 </modelVersion> <artifactId>rabbitmq-springboot</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> </dependencies> </project> |
项目架构
1 2 3 4 5 6 7 8 9 | server.port= 8080 spring.rabbitmq.host= 192.168 . 0.106 spring.rabbitmq.port= 5672 spring.rabbitmq.username=root spring.rabbitmq.password=qwejkl1992 logging.level.com.java=debug logging.level.web=debug spring.devtools.add-properties= false |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package com.java; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Description: * @Author: qiuxie * @Create: 2023/7/21 10:01 */ @SpringBootApplication public class RabbitmqSpringbootApplication { public static void main(String[] args) { SpringApplication.run(RabbitmqSpringbootApplication. class ,args); } } package com.java; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage( "com.java" )) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title( "Spring Boot中使用spring-boot-starter-amqp集成rabbitmq" ) .description( "包含web示例和java方法的示例" ) .termsOfServiceUrl( "http://www.myapp.com/" ) .contact( "roykingw" ) .version( "1.0" ) .build(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | package com.java.config; import com.java.constant.MyConstants; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.FanoutExchange; import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Description: * @Author: qiuxie * @Create: 2023/7/21 10:04 */ @Configuration public class FanoutConfig { /** * 声明队列 * @return */ @Bean public Queue fanoutQ1() { return new Queue(MyConstants.QUEUE_FANOUT_Q1); } @Bean public Queue fanoutQ2() { return new Queue(MyConstants.QUEUE_FANOUT_Q2); } @Bean public Queue fanoutQ3() { return new Queue(MyConstants.QUEUE_FANOUT_Q3); } @Bean public Queue fanoutQ4() { return new Queue(MyConstants.QUEUE_FANOUT_Q4); } /** * 声明exchange * @return */ @Bean public FanoutExchange setFanoutExchange() { return new FanoutExchange(MyConstants.EXCHANGE_FANOUT); } /** * 声明Binding,exchange与queue的绑定关系 * @return */ @Bean public Binding bindQ1() { return BindingBuilder.bind(fanoutQ1()).to(setFanoutExchange()); } @Bean public Binding bindQ2() { return BindingBuilder.bind(fanoutQ2()).to(setFanoutExchange()); } @Bean public Binding bindQ3() { return BindingBuilder.bind(fanoutQ3()).to(setFanoutExchange()); } @Bean public Binding bindQ4() { return BindingBuilder.bind(fanoutQ4()).to(setFanoutExchange()); } /** * 声明队列 * @return */ @Bean public Queue fanout() { return new Queue( "fanout queue" ); } /** * 声明exchange * @return */ @Bean public FanoutExchange setFanoutExchangeSpringboot() { return new FanoutExchange( "fanoutExchangeSpringboot" , false , false , null ); } /** * 声明Binding,exchange与queue的绑定关系 * @return */ @Bean public Binding bind() { return new Binding( "fanout queue" ,Binding.DestinationType.QUEUE, "fanoutExchangeSpringboot" , "fanout.biz.ex" , null ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.java.constant; /** * @author roy * @date 2022/4/26 * @desc */ public class MyConstants { public static final String QUEUE_QUORUM = "quorumQueue" ; public static final String QUEUE_STREAM = "streamQueue" ; //direct模式,直接发送到队列 public static final String QUEUE_DIRECT = "directqueue" ; //fanout模式 public static final String EXCHANGE_FANOUT = "fanoutExchange" ; public static final String QUEUE_FANOUT_Q1 = "fanout.q1" ; public static final String QUEUE_FANOUT_Q2 = "fanout.q2" ; public static final String QUEUE_FANOUT_Q3 = "fanout.q3" ; public static final String QUEUE_FANOUT_Q4 = "fanout.q4" ; //topic模式 public static final String EXCHANGE_TOPIC = "topicExchange" ; public static final String QUEUE_TOPIC1 = "hunan.eco" ; public static final String QUEUE_TOPIC2 = "hunan.IT" ; public static final String QUEUE_TOPIC3 = "hebei.eco" ; public static final String QUEUE_TOPIC4 = "hebei.IT" ; //header模式 public static final String EXCHANGE_HEADER = "headerExchange" ; public static final String QUEUE_TXTYP1 = "txTyp1" ; public static final String QUEUE_BUSTYP1 = "busTyp1" ; public static final String QUEUE_TXBUSTYP1 = "txbusTyp1" ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package com.java.consume; import com.java.constant.MyConstants; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * @Description: 消息的消费者 * @Author: qiuxie * @Create: 2023/7/21 11:20 */ @Component public class MessageReceiver { /** * fanout模式接收还是只指定队列 * @param message */ //fanout模式接收还是只指定队列 @RabbitListener (queues= MyConstants.QUEUE_FANOUT_Q1) public void fanoutReceiveq1(String message) { System.out.println( "fanoutReceive q1 received message : " +message); } @RabbitListener (queues=MyConstants.QUEUE_FANOUT_Q2) public void fanoutReceiveq2(String message) { System.out.println( "fanoutReceive q2 received message : " +message); } @RabbitListener (queues=MyConstants.QUEUE_FANOUT_Q3) public void fanoutReceiveq3(String message) { System.out.println( "fanoutReceive q3 received message : " +message); } @RabbitListener (queues=MyConstants.QUEUE_FANOUT_Q4) public void fanoutReceiveq4(String message) { System.out.println( "fanoutReceive q4 received message : " +message); } /** * fanout模式接收还是只指定队列 * @param message */ @RabbitListener (queues= "fanout queue" ) public void fanoutReceive(String message) { System.out.println( "fanoutReceive q received message : " +message); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package com.java.produce; import com.java.constant.MyConstants; import io.swagger.annotations.ApiOperation; import org.springframework.amqp.AmqpException; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageBuilder; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.UnsupportedEncodingException; import java.util.UUID; /** * @Description: 消息生产者 * @Author: qiuxie * @Create: 2023/7/21 11:17 */ @RestController public class ProducerController { @Autowired private RabbitTemplate rabbitTemplate; @ApiOperation (value= "fanout发送接口" ,notes= "发送到fanoutExchange。消息将往该exchange下的所有queue转发" ) @GetMapping (value= "/fanoutSend" ) public Object fanoutSend(String message) throws AmqpException, UnsupportedEncodingException { MessageProperties messageProperties = new MessageProperties(); messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN); //fanout模式只往exchange里发送消息。分发到exchange下的所有queue rabbitTemplate.send(MyConstants.EXCHANGE_FANOUT, "" , new Message(message.getBytes( "UTF-8" ),messageProperties)); Message message2 = MessageBuilder.withBody(message.getBytes()).setMessageId(UUID.randomUUID().toString()).build(); rabbitTemplate.send(message2); return "message sended : " +message; } @ApiOperation (value= "fanout1发送接口" ,notes= "发送到fanoutExchange。消息将往该exchange下的所有queue转发" ) @GetMapping (value= "/fanoutSend1" ) public Object fanoutSend1(String message) throws AmqpException, UnsupportedEncodingException { MessageProperties messageProperties = new MessageProperties(); messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN); //fanout模式只往exchange里发送消息。分发到exchange下的所有queue rabbitTemplate.send( "fanoutExchangeSpringboot" , "" , new Message(message.getBytes( "UTF-8" ),messageProperties)); Message message2 = MessageBuilder.withBody(message.getBytes()).setMessageId(UUID.randomUUID().toString()).build(); rabbitTemplate.send(message2); return "message sended : " +message; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异