前言
本小结我们将学习一下如何使用Spring Cloud Stream 来集成Rabbit MQ 并实现常规业务场景中的发布/订阅模式及其确认机制。
在学习本小结之前我假设大家已对Spring Cloud Stream 和Rabbit MQ 有实际场景应用的能力,如果暂未掌握请自行学习,这里不赘述。
通常而言,对于发布/订阅模式模式而言,消息的发送者一般只注重将消息推送到相应的Exchange 对应的Channel中,并不在意订阅者是否成功接收并消费掉某条消息。消息发布者只负责把消息送到队列中,订阅者只负责把消息从队列中取出然后消费,两者在业务逻辑上理应是不存在任何耦合或关联的,这也是发布/订阅模式的职责和优点所在。
案例
案例说明:本小结将创建两个服务节点Publisher和Subscribe 其中两者互为消息的发布和订阅者。即Pub是Sub的消息发布者,同时为了实现消息确认机制Pub也是Sub的订阅者,Sub至于Pub也是如此。
-
新建 microservice-deal-cloud-stream-rabbitmq-publisher服务节点
- 项目结构

- Core Code
-
pom.xml
<?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%20http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>microservice-deal-cloud-stream-rabbitmq-publisher</artifactId> <packaging>jar</packaging> <name>microservice-deal-cloud-stream-rabbitmq-publisher</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>microservice-deal-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <!-- 包含 mvc,aop 等jar资源 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion><!-- 去除默认log配置 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 配置log4j2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- 配置log4j2 --> <!-- 支持识别yml配置 --> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> </dependency> <!-- 支持识别yml配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> </dependencies> </project> -
application.yml
server: port: 8081 spring: application: name: microservice-deal-cloud-stream-rabbitmq-publisher #与 microservice-deal-
-
- 项目结构

本文介绍了如何使用Spring Cloud Stream与RabbitMQ集成,实现发布/订阅模式及消息确认机制。文章提供了一个案例,创建了Publisher和Subscriber两个服务节点,详细展示了项目结构和核心代码。
2895

被折叠的 条评论
为什么被折叠?



