增加 spring cloud stream rabbitmq 示例(消息确认,开始乱炖`)

This commit is contained in:
YunaiV
2020-03-06 09:12:00 +08:00
parent 19d1078752
commit 8e804523e6
8 changed files with 224 additions and 1 deletions

View File

@@ -27,7 +27,7 @@ public class Demo01Consumer {
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
// 提交消费进度
// if (message.getId() % 2 == 1) {
if (index.incrementAndGet() % 2 == 1) {
if (index.incrementAndGet() == 1) {
// ack 确认消息
// 第二个参数 multiple ,用于批量确认消息,为了减少网络流量,手动确认可以被批处。
// 1. 当 multiple 为 true 时,则可以一次性确认 deliveryTag 小于等于传入值的所有消息

View File

@@ -0,0 +1,58 @@
<?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>labx-10</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>labx-10-sc-stream-rabbitmq-producer-confirm</artifactId>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
</properties>
<!--
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
在 https://dwz.cn/mcLIfNKt 文章中Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入 Spring Cloud Stream RabbitMQ 相关依赖,将 RabbitMQ 作为消息队列,并实现对其的自动配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,16 @@
package cn.iocoder.springcloud.labx10.rabbitmqdemo.producerdemo;
import cn.iocoder.springcloud.labx10.rabbitmqdemo.producerdemo.message.MySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
@SpringBootApplication
@EnableBinding(MySource.class)
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}

View File

@@ -0,0 +1,67 @@
package cn.iocoder.springcloud.labx10.rabbitmqdemo.producerdemo.controller;
import cn.iocoder.springcloud.labx10.rabbitmqdemo.producerdemo.message.Demo01Message;
import cn.iocoder.springcloud.labx10.rabbitmqdemo.producerdemo.message.MySource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Random;
@RestController
@RequestMapping("/demo01")
public class Demo01Controller {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private MySource mySource;
@GetMapping("/send")
public boolean send() {
// 创建 Message
Demo01Message message = new Demo01Message()
.setId(new Random().nextInt());
// 创建 Spring Message 对象
Message<Demo01Message> springMessage = MessageBuilder.withPayload(message)
.build();
// 发送消息
return mySource.demo01Output().send(springMessage);
}
// @StreamListener(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME) // errorChannel
// public void globalHandleError(ErrorMessage errorMessage) {
// logger.error("[globalHandleError][payload{}]", errorMessage.getPayload().getMessage());
// logger.error("[globalHandleError][originalMessage{}]", errorMessage.getOriginalMessage());
// logger.error("[globalHandleError][headers{}]", errorMessage.getHeaders());
// }
// @StreamListener("ooxx") // errorChannel
// public void ooxx(ErrorMessage errorMessage) {
// logger.error("[globalHandleError][payload{}]", errorMessage.getPayload().getMessage());
// logger.error("[globalHandleError][originalMessage{}]", errorMessage.getOriginalMessage());
// logger.error("[globalHandleError][headers{}]", errorMessage.getHeaders());
// }
//// @ServiceActivator(inputChannel = "demo-producer-application.ooxx")
// @ServiceActivator(inputChannel = "demo-producer-application.ooxx")
//// @StreamListener("demo-producer-application.ooxx") // errorChannel
// public void handleError(Message<?> errorMessage) {
//// logger.error("[handleError][payload{}]", errorMessage.getPayload().getMessage());
//// logger.error("[handleError][originalMessage{}]", errorMessage.getOriginalMessage());
//// logger.error("[handleError][headers{}]", errorMessage.getHeaders());
// System.out.println();
// }
@ServiceActivator(inputChannel = "publisher-confirm")
public void onPublisherConfirm(Message message) {
logger.debug("on publisher confirm");
}
}

View File

@@ -0,0 +1,29 @@
package cn.iocoder.springcloud.labx10.rabbitmqdemo.producerdemo.message;
/**
* 示例 01 的 Message 消息
*/
public class Demo01Message {
/**
* 编号
*/
private Integer id;
public Demo01Message setId(Integer id) {
this.id = id;
return this;
}
public Integer getId() {
return id;
}
@Override
public String toString() {
return "Demo01Message{" +
"id=" + id +
'}';
}
}

View File

@@ -0,0 +1,11 @@
package cn.iocoder.springcloud.labx10.rabbitmqdemo.producerdemo.message;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
public interface MySource {
@Output("demo01-output")
MessageChannel demo01Output();
}

View File

@@ -0,0 +1,39 @@
spring:
application:
name: demo-producer-application
cloud:
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
stream:
# Binder 配置项,对应 BinderProperties Map
binders:
rabbit001:
type: rabbit # 设置 Binder 的类型
environment: # 设置 Binder 的环境配置
# 如果是 RabbitMQ 类型的时候,则对应的是 RabbitProperties 类
spring:
rabbitmq:
host: 127.0.0.1 # RabbitMQ 服务的地址
port: 5672 # RabbitMQ 服务的端口
username: guest # RabbitMQ 服务的账号
password: guest # RabbitMQ 服务的密码
publisherConfirms: true
publisherReturns: true
publisher-confirm-type: simple # 设置 Confirm 类型为 SIMPLE 。
# Binding 配置项,对应 BindingProperties Map
bindings:
demo01-output:
destination: DEMO-TOPIC-01 # 目的地。这里使用 RabbitMQ Exchange
content-type: application/json # 内容格式。这里使用 JSON
binder: rabbit001 # 设置使用的 Binder 名字
producer:
errorChannelEnabled: true
# RabbitMQ 自定义 Binding 配置项,对应 RabbitBindingProperties Map
rabbit:
bindings:
demo01-output:
# RabbitMQ Producer 配置项,对应 RabbitProducerProperties 类
producer:
confirmAckChannel: publisher-confirm
server:
port: 18080

View File

@@ -41,6 +41,9 @@
<!--搭配 <module>labx-10-sc-stream-rabbitmq-producer-demo</module> 作为生产者-->
<module>labx-10-sc-stream-rabbitmq-consumer-ack</module>
<module>labx-10-sc-stream-rabbitmq-producer-confirm</module>
<!--搭配 <module>labx-10-sc-stream-rabbitmq-consumer-demo</module> 作为消费者-->
</modules>