Zookeeper installation and configuration

brew info zookeeper
Copy the code

brew install zookeeper
Copy the code

The installation starts. After a certain period of time, the installation is complete. The installation path is /usr/local/etc/zookeeper

cd /usr/local/etc/zookeeper
ls
Copy the code

You will find the following files.

Start the ZooKeeper Server.

zkServer start
Copy the code

Next, connect the client:

zkClient
Copy the code

Enter help to view the command help.

Exit the ZooKeeper Server.

zkServer stop
Copy the code

Kafka installation and configuration

brew info kafka
Copy the code

brew install kafka
Copy the code

The installation starts. After a certain period of time, the installation is complete. The installation path is /usr/local/etc/kafka

Start Kafka with a Brew Service plug-in

brew services start zookeeper
brew services start kafka
Copy the code

Create a topic

kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
Copy the code

View the created topics

kafka-topics --list --zookeeper localhost:2181
Copy the code

Production send message

kafka-console-producer --broker-list localhost:9092 --topic test 
Copy the code

News consumption

kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning
Copy the code

Let’s start with a simple example that applies to Kafka.

Idea to create a Spring project, remember to select two options. (POM file visible)

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.7. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < name > demo < / name > < description > demo projectforSpring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> < the groupId > org. Springframework. Kafka < / groupId > < artifactId > spring - kafka < / artifactId > < version > 2.3.8. RELEASE < / version > </dependency> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>  <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Copy the code

The following is my poM dependency. Please note that the client version is different according to your kafka version.

Then write the configuration in the application.yml file:

spring:
  kafka:
    # consumers
    consumer: 
      group-id: foo
      auto-offset-reset: earliest
      bootstrap-servers: localhost:9092 
    # producers
    producer: 
      bootstrap-servers: localhost:9092 
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer

Copy the code

The @kafKalistEnter listener annotation KafkaTemplate message is sent

@RestController
@AllArgsConstructor
public class SimpleController {
    
    private final KafkaTemplate<Object, Object> kafkaTemplate;

    @GetMapping("/send/{messge}")
    public String send(@PathVariable String messge) {
        kafkaTemplate.send("topic1"."topci1:" + messge);
        kafkaTemplate.send("topic2"."topci2:" + messge);
        returnmessge; }}Copy the code
@RestController
@AllArgsConstructor
public class SimpleController {
    
    private final KafkaTemplate<Object, Object> kafkaTemplate;

    @GetMapping("/send/{messge}")
    public String send(@PathVariable String messge) {
        kafkaTemplate.send("topic1"."topci1:" + messge);
        kafkaTemplate.send("topic2"."topci2:" + messge);
        returnmessge; }}Copy the code

Zookeeper and Kafka configured before are running on the terminal. (Must be opened)

An error has been reported. Port 8080 is occupied. An error is reported after the process occupying port 8080 is killed several times. After checking, it was found that the zooKeeper version of the application has an embedded management console that is started through Jetty and will occupy port 8080.

The solution to see links: blog.csdn.net/yang1356375…

Finally, I choose to add in zoo. CFG:

Rerun the project and it passed smoothly.

However, in the postman test, the connection timeout problem was found.

Add the following configuration to kafka’s server.properties:

Listeners =PLAINTEXT:// Your IP address :9092

Then pass the test.