My latest and most complete articles are in the pumpkin slow say www.pkslow.com, welcome to tea!

1 introduction

Spring Cloud Stream RabbitMQ and GCP Pubsub are very simple and the code is the same. This article explains the integration of Spring Cloud Stram with Kafka, which is also very simple.

Previous posts:

Spring Cloud Stream Binder with RabbitMQ for sending and receiving messages

“Integrating Spring Cloud Stream Binder with GCP Pubsub for Message sending and receiving”

2 install Kafka

To keep the demonstration simple, just install the Standalone version here, not the cluster. Download the installation package from the official website and decompress it to use.

Unzip the installation package$tar - XZF kafka_2. 13-2.8.0. TGZ# enter directory
$ cdKafka_2. 13-2.8.0 /# start the zookeeper
$ $ bin/zookeeper-server-start.sh config/zookeeper.properties
# start kafka(new command line terminal)
$ bin/kafka-server-start.sh config/server.properties
Copy the code

Both Zookeeper and Kafka use the default configuration, so you don’t need to change it. Logs are generated during startup. If no error message is displayed, the startup is successful.

3 integration

Introducing dependent dependencies:

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

Implement simple Publisher and Consumer:

package com.pkslow.cloud.stream.binder.kafka;

@SpringBootApplication
public class StreamBinderKafka {
    private static final Logger log = LoggerFactory.getLogger(StreamBinderKafka.class);
    public static void main(String[] args) {
        SpringApplication.run(StreamBinderKafka.class, args);
    }

    @Bean
    public Supplier<String> pkslowSource(a) {
        return () -> {
            String message = "www.pkslow.com";
            log.info("Sending value: " + message);
            return message;
        };
    }

    @Bean
    public Consumer<String> pkslowSink(a) {
        return message -> {
            log.info("Received message "+ message); }; }}Copy the code

Configure the required attributes as follows:

spring:
  cloud:
    stream:
      function:
        definition: pkslowSource; pkslowSink
      bindings:
        pkslowSource-out-0:
         destination: pkslow-topic
        pkslowSink-in-0:
          destination: pkslow-topic
      poller:
        fixed-delay: 500
      kafka:
        binder:
          brokers: localhost:9092
          auto-create-topics: true
          required-acks: 1
Copy the code

Run logs are as follows:

4 summarizes

The integration of the three MQS makes almost no difference and does not require major code changes, which is the benefit of Spring Cloud Stream.

Please check the code: github.com/LarryDpk/pk…


Welcome to pay attention to the wechat public number “Pumpkin slow Talk”, will continue to update for you…

Read more and share more; Write more. Organize more.