SpringCloudStream事件驱动2步骤-RabbitMQ

Spring Cloud Stream 事件驱动 2步骤

文章目录

  • Spring Cloud Stream 事件驱动 2步骤
        • 注解/接口了解
        • 一、java实现(任何一种即可)
          • 1、默认实现
          • 2、自定义实现
        • 二、配置输出流/输入流绑定

注解/接口了解

Spring Cloud Stream 内置了三个Source、Sink、Processor接口

  • Source 发送消息接口 内含@Output(“output”)
  • Sink 消息的消费者接口 内含@Input(“input”)
  • Processor 继承了Source,Sink

@Output(“output”) 标志为一个输出流,会加入到Spring IOC管理

@Input(“input”) 标志为一个输入流,会加入到Spring IOC管理

@EnableBinding({Source.class,Sink,class}) 开启输入流和输出流的代理绑定

@StreamListener(Sink.INPUT) 监听输入流

@ServiceActivator(inputChannel = Sink.INPUT) 监听输入流,优先级比@StreamListener低


<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-stream-rabbitartifactId>
dependency>

一、java实现(任何一种即可)

1、默认实现
  • 发布消息实现,发布到Source.OUTPUT的输出流(此是内置的输出接口)

    package com.ksaas.cloud.service.system.infrastructure.event.default2;
    
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.cloud.stream.messaging.Source;
    import org.springframework.messaging.MessageChannel;
    import org.springframework.messaging.support.MessageBuilder;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    /**
     * @author kylin
     * @desc 内置的发送代码demo
     */
    @Component
    public class SourcePublisher {
    
        @Resource
        @Qualifier(Source.OUTPUT)
        MessageChannel messageChannel;
    
        public void test(String message) {
            messageChannel.send(MessageBuilder.withPayload(message).build());
            System.out.println("test default source");
        }
    }
    
    
  • 接受消息实现,接受到Sink.INPUT的输入流(此是内置的输如入接口)

    package com.ksaas.cloud.service.system.infrastructure.event.default2;
    
    import org.springframework.cloud.stream.annotation.StreamListener;
    import org.springframework.cloud.stream.messaging.Sink;
    import org.springframework.messaging.Message;
    import org.springframework.stereotype.Component;
    
    /**
     * @author kylin
     * 内置的接受端代码demo
     */
    @Component
    public class SinkReceiver {
    
        // 第一种实现,优先级最高
        @StreamListener(Sink.INPUT)
        public void test(Message message) {
            System.out.println("test default sink: " + message);
        }
    
    //	 // 第二种实现,优先级低于@StreamListener
    //    @ServiceActivator(inputChannel = Sink.INPUT)
    //    public void test(Message message) {
    //        System.out.println("test default sink: " + message);
    //    }
    
    //	 // 第三种,优先级最低,采用的@PostConstruct+SubscribableChannel实现
    //    @Resource
    //    @Qualifier(Sink.INPUT)
    //    SubscribableChannel subscribableChannel;
    //    @PostConstruct
    //    public void test() {
    //        subscribableChannel.subscribe(message -> {
    //            System.out.println("test default sink: " + message);
    //        });
    //    }
    }
    
    
  • 启动代理绑定

    package com.ksaas.cloud.service.system.infrastructure.event.default2;
    
    import org.springframework.cloud.stream.annotation.EnableBinding;
    import org.springframework.cloud.stream.messaging.Sink;
    import org.springframework.cloud.stream.messaging.Source;
    
    /**
     * @author kylin
     */
    @EnableBinding({Source.class, Sink.class})
    public class DefaultSpringCloudStreamConfig {
    }
    
    
2、自定义实现
  • 创建CustomSource接口(名字自定义):输出流

    package com.ksaas.cloud.service.system.infrastructure.event.custom;
    
    import org.springframework.cloud.stream.annotation.Output;
    import org.springframework.messaging.MessageChannel;
    
    /**
     * 自定义发送端需申明接口,(同Source一样,只是名字区分)
     *
     * @author kylin
     */
    public interface CustomSource {
        String TEST_OUT_PUT = "customOutput";
    
        @Output(TEST_OUT_PUT)
        MessageChannel testOutput();
    }
    
    
  • 创建CustomSink接口(名字自定义):输入流

    package com.ksaas.cloud.service.system.infrastructure.event.custom;
    
    import org.springframework.cloud.stream.annotation.Input;
    import org.springframework.messaging.MessageChannel;
    
    /**
     * 自定义接受端需申明接口,(同Sink一样,只是名字区分)
     *
     * @author kylin
     */
    public interface CustomSink {
        String TEST_IN_PUT = "customInput";
    
        @Input(TEST_IN_PUT)
        MessageChannel testOutput();
    }
    
    
  • 发布消息实现,发布到CustomSource.TEST_OUT_PUT的输出流(其实和默认的操作含义一样)

    package com.ksaas.cloud.service.system.infrastructure.event.custom;
    
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.messaging.MessageChannel;
    import org.springframework.messaging.support.MessageBuilder;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.Resource;
    
    /**
     * 自定义的发送端代码demo
     *
     * @author kylin
     */
    
    @Component
    public class CustomSourcePublisher {
    
        @Resource
        @Qualifier(CustomSource.TEST_OUT_PUT)
        MessageChannel messageChannel;
    
        public void test(String message) {
            messageChannel.send(MessageBuilder.withPayload(message).build());
            System.out.println("test custom source");
        }
    }
    
    
  • 接受消息实现,接受到CustomSink.TEST_IN_PUT的输入流(其实和默认的操作含义一样)

    package com.ksaas.cloud.service.system.infrastructure.event.custom;
    
    import org.springframework.cloud.stream.annotation.StreamListener;
    import org.springframework.messaging.Message;
    import org.springframework.stereotype.Component;
    
    /**
     * 自定义的接受端代码demo
     *
     * @author kylin
     */
    @Component
    public class CustomSinkReceiver {
    
        @StreamListener(CustomSink.TEST_IN_PUT)
        public void test(Message message) {
            System.out.println();
            System.out.println("test custom sink: " + message);
        }
    }
    // .... 
    
  • 启动自定义代理绑定(其实和默认的操作含义一样)

    package com.ksaas.cloud.service.system.infrastructure.event.custom;
    
    import org.springframework.cloud.stream.annotation.EnableBinding;
    
    /**
     * @author kylin
     */
    @EnableBinding({CustomSource.class, CustomSink.class})
    public class CustomSpringCloudStreamConfig {
    }
    
    

二、配置输出流/输入流绑定

配置发送端和消费端的绑定

spring:
  cloud:
    stream:
      bindings:
      	# 默认的发送端 destination 代表绑定到的名称
      	#(不管是默认还是自定义都需要这样关联,除非发送/接口都是一个名称)
        output:
          destination: kylin_default
          content-type: application/json
        # 默认的接受 destination 代表绑定到的名称
        input:
          destination: kylin_default
          content-type: application/json
          group: group-cus1
        
        
        # 自定义:说明customOutput 和customInput 绑定到了 kylin_custom
        customOutput:
          destination: kylin_custom
          content-type: application/json
        customInput:
          destination: kylin_custom
          content-type: application/json
          group: group-cus1

你可能感兴趣的:(Spring,Cloud框架,事件驱动,Springcloud,最终一致性)