美文网首页
spring-cloud-stream 使用篇

spring-cloud-stream 使用篇

作者: 迷狮 | 来源:发表于2022-05-20 00:17 被阅读0次

    如何使用

    以上文自定义sns binder为例,生产者和消费者使用方式如下

    生产者

    1. 依赖引入及配置

    依赖导入

            <!-- stream -->
            <dependency>
                <groupId>com.kadadesign.framework</groupId>
                <artifactId>spring-cloud-stream-binder-sns</artifactId>
            </dependency>
    

    Binding配置及默认Binder配置

    spring:
      cloud:
        stream:
          bindings:
            sendMessage1-out-0:
              destination: kadadesign
              binder: sns
            sendMessage2-out-0:
              destination: kadadesign
              binder: sns
    # aws 
    cloud:
      aws:
        credentials:
          access-key: <your-access-key>
          secret-key: <your-secret-key>
        region:
          static: <your-region>
        sns:
          enabled: true
    

    2. 发送消息

    StreamBridge#send(bindingName, data);
    

    消费者

    1. 依赖引入及配置

    依赖导入

            <!-- stream -->
            <dependency>
                <groupId>com.kadadesign.framework</groupId>
                <artifactId>spring-cloud-stream-binder-sns</artifactId>
            </dependency>
    

    Binding配置及默认Binder配置

    spring:
      cloud:
        stream:
          bindings:
            sendMessage1-in-0:
              destination: kadadesign
              binder: sns
            sendMessage2-in-0:
              destination: kadadesign
              binder: sns
        # 多组消费者需要进行function定义配置
        function:
          definition: sendMessage1;sendMessage2
    # aws 
    cloud:
      aws:
        credentials:
          access-key: <your-access-key>
          secret-key: <your-secret-key>
        region:
          static: <your-region>
        sns:
          enabled: true
          # 扩展配置,用于订阅sns的访问域名,含schema,例如:http://xx.xx.xx
          subscription:
            endpoint: <your-endpoint> 
    

    2. 接收消息

    @Service
    public class MessageReceiveService {
    
        @Bean
        public Consumer<UserDTO> sendMessage1() {
            return (message) -> {
                System.out.println("Received Message 1 userDTO: " + message);
            };
        }
    
        @Bean
        public Consumer<String> sendMessage2() {
            return (message) -> {
                System.out.println("Received Message 2 String: " + message);
            };
        }
    }
    

    相关文章

      网友评论

          本文标题:spring-cloud-stream 使用篇

          本文链接:https://www.haomeiwen.com/subject/xhixprtx.html