一:安装kafka
1.下载地址
https://kafka.apache.org/downloads
需先安装好 zookeeper
2.解压后修改配置文件
vim server.properties
# 修改内容
listeners=PLAINTEXT://内网ip:9092
advertised.host.name=外网ip
advertised.listeners=PLAINTEXT://外网ip:9092
log.dirs=/root/kafka_2.12-2.2.1/logs 需要在kafka安装目录新建logs目录
# 启动kafka
./kafka-server-start.sh ../config/server.properties
二:springBoot 集成 kafka
1.创建工程,引入依赖
kafka对springboot版本有要求
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<kafka.client.version>2.0.1</kafka.client.version>
<kafka.version>2.2.7.RELEASE</kafka.version>
<kafka.client.version>2.0.1</kafka.client.version>
<fastjson.version>1.2.58</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>${kafka.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>${kafka.client.version}</version>
<exclusions>
<exclusion>
<artifactId>connect-json</artifactId>
<groupId>org.apache.kafka</groupId>
</exclusion>
<exclusion>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>${kafka.client.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
2.配置文件
server:
port: 9991
spring:
application:
name: kafka-demo
kafka:
bootstrap-servers: ip:9092
producer:
retries: 10
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
consumer:
group-id: test-group
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
3.测试类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private KafkaTemplate<String,String> kafkaTemplate;
@GetMapping("/hello")
public String hello(){
//第一个参数:topics
//第二个参数:消息内容
kafkaTemplate.send("kafka-hello","kafka测试");
return "ok";
}
}
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
public class HelloListener {
@KafkaListener(topics = {"kafka-hello"})
public void receiverMessage(ConsumerRecord<?,?> record){
Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
if(optional.isPresent()){
Object value = record.value();
System.out.println("kafka收到消息。。。。。。"+value);
}
}
}