springcloud stream 整合 rocketmq

spring 版本:

 
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR3
                pom
                import
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.1.RELEASE
                pom
                import
            
            
                org.springframework.boot
                spring-boot-starter
                2.2.5.RELEASE
                pom
                import
            
        
    

引入依赖:

        
            org.springframework.boot
            spring-boot-starter-web
        
       
            com.alibaba.cloud
            spring-cloud-starter-stream-rocketmq
            
                
                    org.apache.rocketmq
                    rocketmq-client
                
                
                    org.apache.rocketmq
                    rocketmq-acl
                
            
        

        
            org.apache.rocketmq
            rocketmq-client
            4.9.4
        
        
        
            org.apache.rocketmq
            rocketmq-acl
            4.9.4
        

注意:这里要自选rocketmq客户端依赖的版本,是因为stream集成的版本不兼容,会报下面错误:

2022-11-10 09:50:26.995 ERROR 18076 --- [enderExecutor_1] c.a.c.s.b.r.i.RocketMQMessageHandler     : RocketMQ Message hasn't been sent. Caused by No route info of this topic, test-toptic
See http://rocketmq.apache.org/docs/faq/ for further details.

生产方

配置文件:

server:
  port: 8085
  servlet:
    context-path: /stream-rocketmq


spring:
  application:
    name: stream-rocketmq  #不能写在Application.yml里面
  cloud:
#    nacos:
#      password: nacos
#      username: nacos
#      server-addr: 192.168.37.128:8848
    stream:
      rocketmq:
        binder:
          name-server: http://192.168.37.128:9876
      bindings:
        #默认的channel名称
        output:
          #topic名称
          destination: test-toptic
          content-type: application/json
        #自定义channel名称
        order-output:
          destination: order-toptic
          content-type: application/json

默认的output和input:

service层:

 

public interface MessageProviderService {

    String send();
}

实现层:

import com.lindl.business.service.ChannelService;
import com.lindl.business.service.MessageProviderService;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@EnableBinding(Source.class)
public class MessageProviderServiceImpl implements MessageProviderService {

    @Resource
    private Source channelService;

    @Override
    public String send() {
        String uuid = UUID.randomUUID().toString();
        Message message = MessageBuilder.withPayload(uuid)
                .setHeader(MessageConst.PROPERTY_TAGS, "SINGLE_TAG") // 设置 Tag
                .build();
        channelService.output().send(message);
        return "send success";
    }
}

controller层:

import com.lindl.business.service.MessageProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ProductController {

    @Resource
    private MessageProviderService messageProviderService;


    @GetMapping(value = "/provider")
    public String sendClusterMsg(@RequestParam("message") String message) {
        return messageProviderService.send();
    }

}

消费方:

配置文件:

server:
  port: 8086
  servlet:
    context-path: /stream-rocketmq-constumer


spring:
  application:
    name: stream-rocketmq-constumer  #不能写在Application.yml里面
  cloud:
#    nacos:
#      password: nacos
#      username: nacos
#      server-addr: 192.168.37.128:8848
    stream:
      rocketmq:
        binder:
          name-server: http://192.168.37.128:9876
      bindings:
        #默认的channel名称
        input:
          #topic名称
          destination: test-toptic
          group: order
        #自定义channel
        order-input:
          #topic名称
          destination: order-toptic
          group: order-constum

消费监听:

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;


@EnableBinding(Sink.class)
public class ConstumerReceiver {

    @StreamListener(Sink.INPUT)
    public void receive(String payload) {
        System.out.println("Received: " + payload);
    }


}

自定义channel

生产方:需要对配置文件的自定义的channel进行配置,然后将它进行绑定:

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

public interface ChannelService {

    String OUTPUT = "order-output";
    @Output("order-output")
    MessageChannel output();
}
@EnableBinding(ChannelService.class)
public class ConstumMessageProviderServiceImpl implements ConstumProviderService {

    @Resource
    private ChannelService channelService;

    @Override
    public String send() {
        String uuid = UUID.randomUUID().toString();
        Message message = MessageBuilder.withPayload(uuid)
                .setHeader(MessageConst.PROPERTY_TAGS, "ORDER_TAG") // 设置 Tag
                .build();
        channelService.output().send(message);
        return "send success";
    }
}

消费方也是一样:


import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;

public interface ChannelService {

    String INPUT = "order-input";
    @Input("order-input")
    SubscribableChannel input();
}
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;

@EnableBinding(ChannelService.class)
public class ConstumChannelReceiver {

    @StreamListener(ChannelService.INPUT)
    public void receive(String payload) {
        System.out.println("自定义channel:Received: " + payload);
    }
}

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