使用springcloudstream操作rabbitmq
代码部分
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 | <?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-springcloudstream</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>rabbitmq-springcloudstream</name> <description>Demo project for Spring Boot</description> <properties> <java.version> 1.8 </java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR6</version> <type>pom</type> <scope> import </scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version> 2.3 . 4 .RELEASE</version> <type>pom</type> <scope> import </scope> </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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency> </dependencies> </project> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.java; /** * @Description: * @Author: qiuxie * @Create: 2023/7/21 12:22 */ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.stream.annotation.EnableBinding; import org.springframework.cloud.stream.messaging.Sink; import org.springframework.cloud.stream.messaging.Source; @SpringBootApplication @EnableBinding ({Source. class , Sink. class }) public class SCApplication { public static void main(String[] args) { SpringApplication.run(SCApplication. class , args); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.java.consumer; /** * @Description: * @Author: qiuxie * @Create: 2023/7/21 12:25 */ import org.springframework.cloud.stream.annotation.EnableBinding; import org.springframework.cloud.stream.annotation.StreamListener; import org.springframework.cloud.stream.messaging.Sink; import org.springframework.stereotype.Component; @Component @EnableBinding (Sink. class ) public class MessageReceiver { @StreamListener (Sink.INPUT) public void process(Object message) { System.out.println( "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 | package com.java.producer; /** * @Description: * @Author: qiuxie * @Create: 2023/7/21 12:24 */ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.stream.messaging.Source; import org.springframework.integration.support.MessageBuilder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SendMessageController { @Autowired private Source source; @GetMapping ( "/sendfour" ) public Object sendfour(String message) { MessageBuilder<String> messageBuilder = MessageBuilder.withPayload(message).setHeader( "routingkey" , "info" ); source.output().send(messageBuilder.build()); return "message sended : " +message; } @GetMapping ( "/send1" ) public Object send1(String message) { MessageBuilder<String> messageBuilder = MessageBuilder.withPayload(message); source.output().send(messageBuilder.build()); return "message sended : " +message; } @GetMapping ( "/sendBatch" ) public Object sendbatch() { for ( int i = 0 ; i < 10 ; i ++) { MessageBuilder<String> messageBuilder = MessageBuilder.withPayload( "这是第" +i+ "条消息" ); source.output().send(messageBuilder.build()); } return "message batch sended" ; } } |
配置文件
one
1 2 3 4 5 6 7 8 9 10 11 | #定义服务端rabbitmq地址 spring.rabbitmq.addresses= 192.168 . 0.106 : 5672 spring.rabbitmq.username=root spring.rabbitmq.password=qwejkl1992 #定义虚拟机 #spring.rabbitmq.virtualHost=/mirror spring.cloud.stream.bindings.output.destination=qiuxieStream20230722 spring.cloud.stream.bindings.input.destination=qiuxieStream20230722 spring.cloud.stream.bindings.input.group=stream spring.cloud.stream.bindings.input.content-type=text/plain |
two
1 2 3 4 5 6 7 8 9 10 11 12 13 | #使用springcloudstream配置rabbitmq spring.cloud.stream.binders.qiuxie.type=rabbit spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.addresses= 192.168 . 0.106 spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.port= 5672 spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.username=root spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.password=qwejkl1992 spring.cloud.stream.bindings.output.destination=two spring.cloud.stream.bindings.input.destination=two spring.cloud.stream.bindings.input.group=stream spring.cloud.stream.bindings.input.content-type=text/plain |
three
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | spring.cloud.stream.binders.qiuxie.type=rabbit spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.addresses= 192.168 . 0.106 spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.port= 5672 spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.username=root spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.password=qwejkl1992 #使用现有的交换机和队列 spring.cloud.stream.bindings.output.destination=fanoutExchange #指明交换机类型 spring.cloud.stream.rabbit.bindings.output.producer.exchange-type=fanout spring.cloud.stream.rabbit.bindings.output.producer.bind-queue= false spring.cloud.stream.bindings.input.destination=fanoutExchange #指明交换机类型 spring.cloud.stream.rabbit.bindings.input.producer.exchange-type=fanout spring.cloud.stream.bindings.input.group=fanout.q1 spring.cloud.stream.rabbit.bindings.input.consumer.bind-queue= false spring.cloud.stream.rabbit.bindings.input.consumer.queue-name-group-only= true spring.cloud.stream.bindings.input.content-type=text/plain |
four
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 | logging.level.com.java=debug logging.level.web=debug spring.devtools.add-properties= false spring.cloud.stream.binders.qiuxie.type=rabbit spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.addresses= 192.168 . 0.106 spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.port= 5672 spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.username=root spring.cloud.stream.binders.qiuxie.environment.spring.rabbitmq.password=qwejkl1992 #使用现有的交换机和队列 spring.cloud.stream.bindings.output.destination=directExchange #指明交换机类型 spring.cloud.stream.rabbit.bindings.output.producer.exchange-type=direct #为 false 表示使用已经有的交换机和队列,为 true 就会创建交换机和队列 spring.cloud.stream.rabbit.bindings.output.producer.bind-queue= false spring.cloud.stream.rabbit.bindings.output.producer.exchange-durable= false spring.cloud.stream.rabbit.bindings.output.producer.routing-key-expression=headers.routingkey spring.cloud.stream.bindings.input.destination=directExchange #指明交换机类型 spring.cloud.stream.rabbit.bindings.input.consumer.exchange-type=direct spring.cloud.stream.bindings.input.group=direct_queue #为 false 表示使用已经有的交换机和队列,为 true 就会创建交换机和队列 spring.cloud.stream.rabbit.bindings.input.consumer.bind-queue= false #为 true 表示交换机的名字只有direct_queue,没有destination spring.cloud.stream.rabbit.bindings.input.consumer.queue-name-group-only= true spring.cloud.stream.rabbit.bindings.input.consumer.exchange-durable= false spring.cloud.stream.rabbit.bindings.input.consumer.binding-routing-key=info spring.cloud.stream.bindings.input.content-type=text/plain |
1 | spring.profiles.active=one |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!