本Demo的spring boot 版本为 2.2.4.RELEASE ,Rabbitmq的版本3.8.2
生产者,消费者工程 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
生产者工程
1 更改application.yml文件
server:
port: 8001 # 端口
spring:
application:
name: stream-producer # 应用名称
rabbitmq:
host: 127.0.0.1 # 服务器 IP
port: 5672 # 服务器端口
username: guest # 用户名
password: guest # 密码
virtual-host: / # 虚拟主机地址
cloud:
stream:
bindings:
# 消息发送通道
# 与 org.springframework.cloud.stream.messaging.Source 中的 @Output("output") 注解的 value 相同
output:
destination: exchange_1 # 绑定的交换机名称
2 生产者发送消息的业务类 MessageProducer.java
package com.test.shenchanzhe.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
/**
* 消息生产者
*/
@Component
@EnableBinding(Source.class)
public class MessageProducer {
@Autowired
private Source source;
/**
* 发送消息
*
* @param message
*/
public void send(String message) {
final boolean send = source.output().send(MessageBuilder.withPayload(message).build());
System.out.println(send);
}
}
3 编写测试类
package com.test.shenchanzhe;
import com.test.shenchanzhe.service.MessageProducer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
@SpringBootTest(classes = {ShenchanzheApplication.class})
class ShenchanzheApplicationTests {
@Autowired
private MessageProducer messageProducer;
@Test
void fangfa_1() {
for (int i = 1; i <= 9999; i++) {
messageProducer.send("hello,小明 "+i+" 时间 "+new Date().toLocaleString());
}
}
}
消费者工程
1 更改application.yml文件
server:
port: 8002 # 端口
spring:
application:
name: stream-consumer # 应用名称
rabbitmq:
host: 127.0.0.1 # 服务器 IP
port: 5672 # 服务器端口
username: guest # 用户名
password: guest # 密码
virtual-host: / # 虚拟主机地址
cloud:
stream:
bindings:
# 消息接收通道
# 与 org.springframework.cloud.stream.messaging.Sink 中的 @Input("input") 注解的 value 相同
input:
destination: exchange_1 # 绑定的交换机名称
group: group-1 #没定义前 队列名为exchange_1.anonymous.AZ8z2HmdRaaqdit8fQbQ5w 定义后 队列名为exchange_1.group-1
2 编写接收消息的业务类
package com.test.xiaofeizhe.service;
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 MessageConsumer {
/**
* 接收消息
*
* @param message
*/
@StreamListener(Sink.INPUT)
public void receive(String message) {
System.out.println("message = " + message);
}
}
以上发送的消息是字符,如果想发送pojo,直接修改@EnableBinding所在类里面的方法参数类型就行了
开始测试,为方便理解上面的代码,部分截图如下
org.springframework.cloud.stream.messaging.Source.java文件
org.springframework.cloud.stream.messaging.Sink.java文件