首页 > 编程语言 > 详细

Spring Cloud Stream监听已存在的Queues/Exchanges

时间:2019-08-22 09:11:27      阅读:138      评论:0      收藏:0      [点我收藏+]
  • 环境准备

    • rabbitmq已运行,端口5672,控制台web端口15672,用户名密码guest/guest
  • 引入spring cloud stream依赖

    compile('org.springframework.cloud:spring-cloud-starter-stream-rabbit')
  • 创建StreamClient接口

    public interface StreamClient {
    
        String INPUT = "input";
    
        String OUTPUT = "output";
    
        @Input(StreamClient.INPUT)
        SubscribableChannel input();
    
        @Output(StreamClient.OUTPUT)
        MessageChannel output();
    }
  • 创建监听StreamReceiver

    @Slf4j
    @Component
    @EnableBinding(StreamClient.class)
    public class StreamReceiver {
    
        // 直接监听队列
    //    @RabbitListener(queues = "error_log")
    //    public void listenerObject(byte[] message) throws UnsupportedEncodingException {
    //        String errorLog = new String(message, "utf-8");
    //        log.info("Stream Receiver Object: {}", errorLog);
    //
    //    }
    
        @StreamListener(StreamClient.INPUT)
        public void processInput(Object message) {
            String errorLog = new String((byte[]) message, StandardCharsets.UTF_8);
            log.info("StreamInput Receiver Object: {}", errorLog);
        }
    
    }
  • 配置application.yaml

    spring:
      rabbitmq:
        host: 192.168.0.219
        port: 5672
        username: guest
        password: guest
      cloud:
        stream:
          bindings:
            input:
              destination: CoeusLogExchanger #Exchange名称
              group: error_log #队列名称
          #        output:
          #          destination: CoeusLogExchanger
          #          group: error_log
          rabbit:
            bindings:
              input:
                consumer:
                  bindQueue: false
                  declareExchange: false
                  queueNameGroupOnly: true

    参考:https://cloud.spring.io/spring-cloud-static/spring-cloud-stream-binder-rabbit/2.2.0.RELEASE/spring-cloud-stream-binder-rabbit.html#_using_existing_queuesexchanges

Spring Cloud Stream监听已存在的Queues/Exchanges

原文:https://www.cnblogs.com/startRuning/p/11392342.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!