• RocketMQ 消费者


    POM

    主要增加了 org.springframework.cloud:spring-cloud-starter-stream-rocketmq 依赖

    <?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>
    
        <parent>
            <groupId>com.snake</groupId>
            <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
        </parent>
    
        <artifactId>hello-spring-cloud-alibaba-rocketmq-consumer</artifactId>
        <packaging>jar</packaging>
    
        <name>hello-spring-cloud-alibaba-rocketmq-consumer</name>
        <url>http://www.snake.com</url>
        <inceptionYear>2018-Now</inceptionYear>
    
        <dependencies>
            <!-- Spring Boot Begin -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- Spring Boot End -->
    
            <!-- Spring Cloud Begin -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
            </dependency>
            <!-- Spring Cloud End -->
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.snake.hello.spring.cloud.alibaba.rocketmq.consumer.RocketMQConsumerApplication</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    #消息消费者服务

    主要使用 @StreamListener("input") 注解来订阅从名为 input 的 Binding 中接收的消息

    package com.snake.hello.spring.cloud.alibaba.rocketmq.consumer.receive;
    
    import org.springframework.cloud.stream.annotation.StreamListener;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ConsumerReceive {
    
        @StreamListener("input")
        public void receiveInput(String message) {
            System.out.println("Receive input: " + message);
        }
    }
    

    #Application

    配置 Input(Sink.class) 的 Binding 信息并配合 @EnableBinding 注解使其生效

    package com.snake.hello.spring.cloud.alibaba.rocketmq.consumer;
    
    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;
    
    @SpringBootApplication
    @EnableBinding({Sink.class})
    public class RocketMQConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(RocketMQConsumerApplication.class, args);
        }
    }
    

    #application.yml

    spring:
      application:
        name: rocketmq-consumer
      cloud:
        stream:
          rocketmq:
            binder:
              namesrv-addr: 192.168.10.149:9876
            bindings:
              input: {consumer.orderly: true}
          bindings:
            input: {destination: test-topic, content-type: text/plain, group: test-group, consumer.maxAttempts: 1}
    
    server:
      port: 9094
    
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    

    运行成功后即可在控制台接收到消息:Receive input: Hello RocketMQ

    等你看到的时候,想变得有一点点不一样
  • 相关阅读:
    jsp报源码
    c#简单写售票系统
    linux常用命令大全[转]
    【转载】大型网站渗透思之信息收集
    Ajax初窥
    屏蔽win10中文输入法
    win10禁止更新的方法
    win10进入到安全模式的三种方法
    7代CPU安装win7的方法
    python的输出问题
  • 原文地址:https://www.cnblogs.com/snake107/p/11920900.html
Copyright © 2020-2023  润新知