Spring Cloud Stream消息驅動之RocketMQ入門(一)


SpringCloudStream目前支持的中間件有RabbitMQ、Kafka,還有我最近在學習的RocketMQ,以下是我學習的筆記
學習Spring cloud Stream 可以先學習一下了解 Spring Messaging 和 Spring Integration,

先看看Spring Message 消息的模型

file

Messaging 對應的模型就包括一個消息體 Payload 和消息頭 Header

file

消息通道 MessageChannel 用於接收消息,調用 send 方法可以將消息發送至該消息通道中,直接擼demo吧

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>cloud-stream-rocketmq-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-stream-rocketmq-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rocketmq</artifactId>
            <version>0.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

@EnableBinding:該注解用來指定一個或多個定義了@Input或@Output注解的接口,以此實現對消息通道(Channel)的綁定
@StreamListener:該注解主要定義在方法上,作用是將被修飾的方法注冊為消息中間件上數據流的事件監聽器,注解中的屬性值對應了監聽的消息通道名

@Component
@EnableBinding(StreamInput.class)
@Slf4j
public class ReceiveClient {

    @StreamListener(StreamInput.input)
    public void receive01(String message){
        log.info("接收消息:"+message);
    }


}

@Input注解綁定了一個名為input的通道

public interface StreamInput {

    String input = "input";

    @Input(StreamInput.input)
    SubscribableChannel input();
}

@Output注解綁定了一個名為Output的通道

public interface StreamInput {

    String input = "input";

    @Input(StreamInput.input)
    SubscribableChannel input();
}

測試一下
啟動類加上剛剛添加的兩個接口
@EnableBinding({StreamInput.class, StreamOutput.class})

@Autowired
    private StreamOutput streamOutput;

    @GetMapping("/send")
    public String send(){
        MessageBuilder builder = MessageBuilder.withPayload("測試消息".getBytes());
        streamOutput.output().send(builder.build());
        return "ok";
    }

不要忘記@EnableBinding注解綁定

個人聯系方式QQ:944484545,歡迎大家的加入,分享學習是一件開心事


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM