RocketMQ 消费者

学习完整课程请移步 互联网 Java 全栈工程师

本节视频

  • 【视频】Spring Cloud Alibaba-RocketMQ-消费者

POM

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



    4.0.0

    
        com.funtl
        hello-spring-cloud-alibaba-dependencies
        1.0.0-SNAPSHOT
        ../hello-spring-cloud-alibaba-dependencies/pom.xml
    

    hello-spring-cloud-alibaba-rocketmq-consumer
    jar

    hello-spring-cloud-alibaba-rocketmq-consumer
    http://www.funtl.com
    2018-Now

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        

        
        
            org.springframework.cloud
            spring-cloud-starter-stream-rocketmq
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    com.funtl.hello.spring.cloud.alibaba.rocketmq.consumer.RocketMQConsumerApplication
                
            
        
    

消息消费者服务

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

package com.funtl.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.funtl.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

你可能感兴趣的:(RocketMQ 消费者)