spring cloud stream kafka实例

maven

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
    </dependency>

生产者配置

server:
  port: 8081
spring:
  application:
    name: output-demo
  cloud:
     instance-count: 1
     instance-index: 0
     stream:
        kafka:
          binder:
            brokers: localhost:9092
            zk-nodes: localhost:2182
            auto-add-partitions: true
            auto-create-topics: true
            min-partition-count: 1
        bindings:
          output:
            destination: event-demo
            content-type: text/plain
            producer:
              partitionCount: 1
  • java代码
  • @EnableBinding(Source.class)
    public class SendService {
    
        @Autowired
        private Source source;
    
        public void sendMessage(String msg) {
            try {
                source.output().send(MessageBuilder.withPayload(msg).build());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    @RestController
    public class ProducerController {
        
        @Autowired
        private SendService service;
        
        @RequestMapping(value = "/send/{msg}", method = RequestMethod.GET)
        public void send(@PathVariable("msg") String msg){
            service.sendMessage(msg);
        }
        
    }
    

    消费者

  • spring:
      application:
        name: input-demo
      cloud:
         instance-count: 1
         instance-index: 0
         stream:
            kafka:
              binder:
                brokers: localhost:9092
                zk-nodes: localhost:2182
                auto-add-partitions: true
                auto-create-topics: true
                min-partition-count: 1
            bindings:
              input:
                destination: event-demo
                group: s1
                consumer:
                  autoCommitOffset: false
                  concurrency: 1
                  partitioned: false
    
    

     

  • java代码
@EnableBinding(Sink.class)
public class MsgSink {

    @StreamListener(Sink.INPUT)
    public void process(Message<?> message) {
        System.out.println(message.getPayload());
        Acknowledgment acknowledgment = message.getHeaders().get(KafkaHeaders.ACKNOWLEDGMENT, Acknowledgment.class);
        if (acknowledgment != null) {
            System.out.println("Acknowledgment provided");
            acknowledgment.acknowledge();
        }
    }
}

运行

先运行生产者,再运行消费者

curl -i localhost:8081/send/hello1

doc

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值