RocketMQ 整合 Spring Cloud Stream
# SpringCloud Stream Springcloud Stream 是一個框架, 用於構建與共享訊息傳遞系統連線的高度可擴充套件的事件驅動型微服務。
該框架提供了基於已經建立和熟悉的Spring習語和最佳實踐的靈活程式設計模型,包括對永續性Pub/sub(釋出/訂閱),使用者組和有狀態分割槽的支援。
Spring Cloud Stream 的核心構建塊是:
- 目標繫結器(Destination Binders):負責提供與外部訊息傳遞系統整合的元件。Binder是Spring Cloud對訊息容器的抽象,不同的訊息容器有不同的實現,通過它可以遮蔽各訊息容器的內部細節。
- 目標繫結(Destination Bindings):外部訊息傳遞系統和應用程式之間的橋接訊息的生產者和消費者(由目標繫結器建立)。Binder可以生成Binding,Binding用來繫結訊息容器的生產者和消費者,它有兩種型別,INPUT和OUTPUT,INPUT對應於消費者,OUTPUT對應於生產者。
- 訊息(Message):生產者和使用者使用的規範資料結構與目標繫結器(以及通過外部訊息傳遞系統的其他應用程式)進行通訊。
Spring Cloud Stream 支援的 Banders
- RabbitMQ
- Apache Kafka
- Kafka Streams
- Amazon Kinesis
- Google PubSub (partner maintained)
- Solace PubSub+ (partner maintained)
- Azure Event Hubs (partner maintained)
- Apache RocketMQ (partner maintained)
為什麼引入Stream
遮蔽底層訊息中介軟體的差異,降低切換成本,統一訊息的程式設計模型。類似於Hibernate的作用一樣, 我們更多的注重業務開發。Hibernate遮蔽了資料庫的差異,可以很好的實現資料庫的切換。Stream遮蔽了底層MQ的區別,可以很好的實現切換。目前主流的有RocketMQ、RabbitMQ、Kafka,Stream支援的有RabbitMQ和Kafka。
應用結構
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mrathena.middle.ware</groupId>
<artifactId>rocket.mq.spring.cloud.stream</artifactId>
<version>1.0.0</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
<version>2.2.3.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-acl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-acl</artifactId>
<version>4.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
#ScStream通用的配置以spring.cloud.stream開頭
spring.cloud.stream.bindings.input.destination=topic
spring.cloud.stream.bindings.input.group=consumer
spring.cloud.stream.bindings.output.destination=topic
#rocketMQ的個性化配置以spring.cloud.stream.rocketmq開頭
#spring.cloud.stream.rocketmq.binder.name-server=192.168.232.128:9876;192.168.232.129:9876;192.168.232.130:9876
spring.cloud.stream.rocketmq.binder.name-server=116.62.162.47:9876
Application
package com.mrathena.rocket.mq;
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;
// 這是SpringCloudStream引入的Binder配置
@EnableBinding({Source.class, Sink.class})
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
消費者 TopicConsumer
package com.mrathena.rocket.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class TopicConsumer {
@StreamListener(Sink.INPUT)
public void onMessage(String message){
log.info("{}, {}", message, Sink.INPUT);
}
}
生產者 ProducerController
package com.mrathena.rocket.mq;
import org.springframework.cloud.stream.messaging.Source;
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 javax.annotation.Resource;
@RestController
@RequestMapping("test")
public class ProducerController {
@Resource
private Source source;
@GetMapping("send")
public Object send() {
return source.output().send(MessageBuilder.withPayload("你好").build());
}
}
相關文章
- springcloud(一)——spring-cloud-alibaba整合rocketmqSpringGCCloudMQ
- (十七) 整合spring cloud雲架構 -訊息驅動 Spring Cloud StreamSpringCloud架構
- Spring Cloud Stream應用與自定義RocketMQ Binder:實現RocketMQ繫結器SpringCloudMQ
- Spring Cloud 快速入門(八)訊息系統整合框架 Spring Cloud StreamSpringCloud框架
- Spring Cloud Stream應用與自定義RocketMQ Binder:程式設計模型SpringCloudMQ程式設計模型
- Spring Cloud 整合SpringCloud
- Spring Cloud Stream事件路由 - spring.ioSpringCloud事件路由
- spring cloud 整合 nacosSpringCloud
- spring-boot-route(十五)整合RocketMQSpringbootMQ
- Spring Cloud Stream如何深度支援Apache Kafka?SpringCloudApacheKafka
- Spring Cloud Stream微服務訊息框架SpringCloud微服務框架
- spring cloud config 整合svnSpringCloud
- Spring Boot整合Spring Cloud Netflix元件Spring BootCloud元件
- Spring Cloud Stream 體系及原理介紹SpringCloud
- 最完整的 Spring Cloud 元件-訊息中介軟體 Spring Cloud Stream 使用教程SpringCloud元件
- Spring Cloud OpenFeign整合Protocol BufferSpringCloudProtocol
- 介紹一下Spring Cloud Stream主要概念SpringCloud
- Spring Cloud Alibaba(14)---SpringCloudAlibaba整合SleuthSpringCloudGC
- [Spring Cloud Tutorial翻譯系列二]Spring Cloud Config Server與git整合SpringCloudServerGit
- Spring-Cloud整合Spring-Session的注意點SpringCloudSession
- 訊息驅動式微服務:Spring Cloud Stream & RabbitMQ微服務SpringCloudMQ
- springCloud學習5(Spring-Cloud-Stream事件驅動)SpringGCCloud事件
- (十六) 整合spring cloud雲架構 -使用spring cloud Bus重新整理配置SpringCloud架構
- Spring Cloud Gateway 整合Eureka路由轉發SpringCloudGateway路由
- spring cloud架構整合-springcloud簡介SpringCloud架構GC
- Spring Cloud Stream與Spring Integration整合以及Spring Cloud Function的關係:開啟從基於註釋到函數語言程式設計的漫長轉換 - spring.ioSpringCloudFunction函數程式設計
- Spring Boot整合Spring Cloud Commons進行公共元件管理Spring BootCloud元件
- Spring Boot整合Spring Cloud Vault進行安全儲存Spring BootCloud
- spring cloud微服務分散式雲架構 - Spring Cloud整合專案簡介SpringCloud微服務分散式架構
- Spring cloud系列教程第十篇- Spring cloud整合Eureka總結篇SpringCloud
- Spring Cloud Stream如何處理訊息重複消費?SpringCloud
- Spring Cloud Stream如何消費自己生產的訊息?SpringCloud
- 阿里RocketMq試用記錄+簡單的Spring整合阿里MQSpring
- RocketMQ整合SpringBootMQSpring Boot
- 整合原始碼深度剖析:Fescar x Spring Cloud原始碼SpringCloud
- Spring Cloud整合Thrift RPC(二) - 應用案例SpringCloudRPC
- Spring Cloud整合Thrift RPC(一) - 使用指南SpringCloudRPC
- 使用Spring Cloud Stream和Spring State Machine建立事件驅動的微服務案例SpringCloudMac事件微服務