zookeeper安装与配置
brew info zookeeper
brew install zookeeper
开始安装,等待一定时间后,安装完毕。安装路径为:/usr/local/etc/zookeeper
cd /usr/local/etc/zookeeper
ls
会发现有如下文件。
不妨vi看一下zoo.cfg里的默认配置项,做到心里有数。启动zookeeper server。
zkServer start
接下来进行client的连接:
zkClient
输入help可以查看命令帮助。
退出zookeeper server。
zkServer stop
Kafka安装与配置
brew info kafka
brew install kafka
开始安装,等待一定时间后,安装完毕。安装路径为:/usr/local/etc/kafka
通过brew service插件启动kafka
brew services start zookeeper
brew services start kafka
创建topic
kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
查看创建的topic
kafka-topics --list --zookeeper localhost:2181
生产发送消息
kafka-console-producer --broker-list localhost:9092 --topic test
消费消息
kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning
先介绍一个简单的应用到了Kafka的案例。
idea创建spring项目,记得选上两个选项。(pom文件可见)
贴一下我的pom文件:<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-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-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<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>lombok</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>
上面的是我的pom的依赖,大家请注意,根据自己的kafka版本,对应不同的client版本。
然后在application.yml文件里写配置:
spring:
kafka:
# 消费者
consumer:
group-id: foo
auto-offset-reset: earliest
bootstrap-servers: localhost:9092
# 生产者
producer:
bootstrap-servers: localhost:9092
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
@KafkaListenter 监听器注解 KafkaTemplate 消息发送
@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);
return messge;
}
}
@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);
return messge;
}
}
终端打开之前配置好的zookeeper和kafka开始运行。(必须打开)
出现报错,8080端口被占用。多次杀死占用8080端口的进程,依旧报错。 经过检查发现,是应用的zookeeper的版本中有个内嵌的管理控制台是通过jetty启动,会占用8080 端口。
解决办法见链接: blog.csdn.net/yang1356375…
最后本人选择在zoo.cfg中添加:
重新运行项目,顺利通过。
但在postman中测试,发现出现连接超时的问题。
检查后在kafka的server.properties加上配置:
listeners=PLAINTEXT://你的ip地址:9092
之后通过测试。