• springcloud: stream整合rocketmq


    一、依赖

    
    
        4.0.0
    
        org.example
        springcloudstreamrocketmq
        1.0-SNAPSHOT
    
        
            8
            8
        
    
        
            spring-boot-starter-parent
            org.springframework.boot
            2.6.6
        
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    Hoxton.SR12
                    pom
                    import
                
            
        
    
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.apache.rocketmq
                rocketmq-spring-boot-starter
                2.0.3
            
    
            
                com.alibaba.cloud
                spring-cloud-starter-stream-rocketmq
                2.0.3.RELEASE
            
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    二、配置文件

    server:
      port: 9967
    spring:
      cloud:
        stream:
          bindings:
            input:
              destination: my-demo7
              group: tju
            output:
              destination: my-demo7
          rocketmq:
            binder:
              nameServer: xx.xx.xx.xx:9876
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    三、启动类:

    package cn.edu.tju;
    
    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;
    
    @SpringBootApplication
    @EnableBinding({Source.class , Sink.class })
    public class Start {
        public static void main(String[] args) {
            SpringApplication.run(Start.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    四、发送消息的controller

    package cn.edu.tju.controller;
    
    
    import org.apache.rocketmq.common.message.MessageConst;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.messaging.Message;
    import org.springframework.messaging.MessageChannel;
    import org.springframework.messaging.MessageHeaders;
    import org.springframework.messaging.support.MessageBuilder;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    
    @RestController
    public class SendController {
        @Resource
        private MessageChannel output;
        @GetMapping("/test")
        public String test(){
            String uuid = UUID.randomUUID().toString();
            Map headerMap = new HashMap<>();
            headerMap.put(MessageConst.PROPERTY_TAGS,"testTag");
            MessageHeaders messageHeaders = new MessageHeaders(headerMap);
            Message message = MessageBuilder.createMessage(uuid, messageHeaders);
            boolean b = output.send(message);
            //System.out.println(b);
    
            return "ok";
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    五、接收消息的service

    package cn.edu.tju.service;
    
    import org.springframework.cloud.stream.annotation.StreamListener;
    import org.springframework.cloud.stream.messaging.Sink;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    
    @Component
    public class ReceiveService {
        @StreamListener(Sink.INPUT)
        public void onMessage(String message){
            System.out.print(new Date() + " " + "received: ");
            System.out.println(message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    【cmake】cmake生成Visual Studio工程后的INSTALL项目使用
    h5播放m3u8格式的视频
    Mathorcup数学建模竞赛第三届-【妈妈杯】C题:语音识别技术的应用(附带赛题解析&获奖论文&MATLAB代码)(一)
    1312. 序列统计
    CPSC发布关于亚马逊含有纽扣电池或硬币电池产品的相关规则标准!UL4200A
    Java基础知识面试题
    qt实现打开pdf(阅读器)功能用什么库比较合适
    MSDC 4.3 接口规范(7)
    JAVA毕业论文答辩管理系统计算机毕业设计Mybatis+系统+数据库+调试部署
    OpenMMLab MMYOLO目标检测环境搭建(一)
  • 原文地址:https://blog.csdn.net/amadeus_liu2/article/details/134439253