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
        
    


二、配置文件

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

三、启动类:

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);
    }
}

四、发送消息的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";

    }
}

五、接收消息的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);
    }
}

你可能感兴趣的:(SpringCloud,rocketmq,spring,cloud,rocketmq,spring)