Spring Cloud Stream

1.导入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

2.配置yml

spring:
  rabbitmq:
    host: 192.168.0.133
    port: 5672
    password: guest
    username: guest

3.新建接口

public interface StreamClient {
    String INPUT="input";
    String OUTPUT="output";
    @Input(StreamClient.INPUT)
    SubscribableChannel input();
    @Output(StreamClient.OUTPUT)
    MessageChannel output();
}

4.消费者

@Component
@EnableBinding(StreamClient.class)
@Slf4j
public class StreamReceiver {
    @StreamListener(StreamClient.INPUT)
    @SendTo(StreamClient.OUTPUT)
    public Object process(Object message){
      log.info("StreamReceiver1: {}",message);
      return message;
    }
    @StreamListener(StreamClient.OUTPUT)
    public void processOutput(Object message){
      log.info("StreamReceiver2: {}",message);
    }
}

5.生产者

@RestController
public class SendMessageController {
    @Autowired
    private StreamClient streamClient;
    @GetMapping("/sendMessage")
    public void process(){
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setOrderId("1111111111");
        streamClient.output().send(MessageBuilder.withPayload("now "+orderDTO).build());
    }
}

6.注意消息分组

spring:
  cloud:
    stream:
      bindings:
        input:
          group: order #消息分组
            content-type: application/json  #消息存储格式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值