Spring Cloud Stream of RocketMQ message-driven entry (a)

SpringCloudStream currently supported middleware has RabbitMQ, Kafka, and my recent RocketMQ study, the following is my study notes
learning Spring cloud Stream can first learn about understand Spring Messaging and Spring Integration,

Take a look at the model Spring Message message

file

Messaging corresponding model including a message body and a message header Payload Header

file

MessageChannel message channel for receiving a message, calling the send method may send a message to the message channel, the line and direct it demo

pom.xml dependence

<?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: This annotation is used to specify one or more defined interfaces @Input or @Output annotation, in order to achieve binding of the message channel (Channel) of
@StreamListener: This is defined primarily on annotation method, is to be registered as the method for modifying data stream middleware message event listener, the attribute value corresponding to the annotation message listening channel name

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

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


}

@Input annotation bound a channel named input

public interface StreamInput {

    String input = "input";

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

@Output binding annotation of a channel called Output

public interface StreamInput {

    String input = "input";

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

Test
start classes with the two interfaces just added
@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";
    }

Do not forget to comment binding @EnableBinding

Personal contact QQ: 944484545, welcome to join, to share learning is a happy thing

Guess you like

Origin www.cnblogs.com/hy-xiaobin/p/12173233.html