RocketMQ 整合 Spring Cloud Stream

mrathena發表於2020-12-11

博文目錄


# 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

為什麼引入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());
	}
}

相關文章