SpringCloud集成Kafka

31人阅读 评论(0) 收藏 举报
分类:

1: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>

2:provider (.yml文件)

server:
  port: 8081
spring:
  application:
    name: lilili-provider
  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: mytopic
            content-type: text/plain
            producer:
              partitionCount: 1
@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);
    }
    
}

3:consumer (.yml文件)

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
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;

/**
 * lilili 2018/4/14.
 */
@EnableBinding(Sink.class)
public class KafkaConsumer {
    
    @StreamListener(Sink.INPUT)
    public void onReceive(byte[] msg) {
        System.out.println("get Kafka message:" + new String(msg));

    }
}

或者:

@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();
        }
    }
}




查看评论

.NET Framework環境下的ASP網頁製作

.NET Framework環境下的ASP網頁製作網路公司裁員、網站關閉、電子報停刊…,經歷電子商務的退潮之後,有人開始質疑電子商務是不是被高估了。也許網際網路不再編織賺大錢的美夢,但經過這幾年的洗禮...
  • rh
  • rh
  • 2002-03-26 11:07:00
  • 1514

Spring Cloud 集成 kafka

1、在pom.xml里面添加kafka的maven依赖 org.springframework.cloud spring-cloud-starter-stream-kafka 2、在prope...
  • LDY1016
  • LDY1016
  • 2017-06-03 15:59:34
  • 9706

SpringCloudStream 构建消息驱动的微服务框架 集成kafka_http://blog.spring-cloud.io/blog/sc-stream.html

SpringCloudStream 构建消息驱动的微服务框架 2016-09-02 /  Wang Sihan 前言 Spring Cloud Stream,用精简的语言概括...
  • a363722188
  • a363722188
  • 2017-07-17 09:41:45
  • 5852

spring cloud stream配置多个kafka binders

实际工作中可能在一个工程里面同时连接多个不同的kafka集群读写数据,spring cloud stream也提供了类似的配置方式,首先给出一个demo配置: spring: cloud: ...
  • xiao_jun_0820
  • xiao_jun_0820
  • 2017-10-31 10:31:17
  • 2021

Spring Cloud Bus整合Kafka

Kafka是由LinkedIn开发并开源的分布式消息系统,因其分布式及高吞吐率而被广泛使用,现已与Cloudera Hadoop,Apache Storm,Apache Spark集成,具备许多优秀的...
  • u012702547
  • u012702547
  • 2017-11-25 10:52:59
  • 1604

Spring Cloud (20) | Spring Cloud Bus 使用kafka消息总线、gitlab添加webhooks实现自动刷新配置

Spring Cloud Bus 使用kafka实现消息总线, 第一步,首先在项目中pom.xml添加如下: org.springframework.boot...
  • superdangbo
  • superdangbo
  • 2017-12-11 20:44:59
  • 870

SpringCloud集成Kafka

1:maven依赖&amp;lt;dependencyManagement&amp;gt; &amp;lt;dependencies&amp;gt; &amp;...
  • merrylilili
  • merrylilili
  • 2018-04-17 20:02:46
  • 31

windows环境搭建调用链监控 <em>spring cloud</em>+es+<em>kafka</em>+zipkin

<em>Spring Cloud 集成 kafka</em> SpringCloudStream 构建消息驱动的微服务框架 集成kafka_http://blog.spring-cloud.io/blog/sc-stream.html 9个完整android开源app项目...
  • 2018年04月16日 00:00

spring cloud初学者-spring-kafka

上一篇博客我们讲了如何安装kafka,这一章我们就讲一下如何用springcloud来连接kafka并发布消息和获取消息。创建程序pom文件 ...
  • lovemenghaibin
  • lovemenghaibin
  • 2017-07-05 23:35:52
  • 523

Spring Cloud构建微服务架构(七)消息总线(续:Kafka)

Spring Cloud Bus除了支持RabbitMQ的自动化配置之外,还支持现在被广泛应用的Kafka。在本文中,我们将搭建一个Kafka的本地环境,并通过它来尝试使用Spring Cloud B...
  • sosfnima
  • sosfnima
  • 2016-11-15 22:26:15
  • 4757
    个人资料
    持之以恒
    等级:
    访问量: 12万+
    积分: 2881
    排名: 1万+
    最新评论