前言
本篇文章主要介绍的是springboot整合kafka。
安装kafka
1.使用docker安装kafka,移步 https://www.jianshu.com/p/89b19f5b28ec
创建工程
1.创建一个名为springboot-kafka的pom项目作为父工程,将main和resource文件夹都删除,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lxg</groupId>
<artifactId>springboot-kafka</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springboot-kafka</name>
<modules>
<module>springboot-kafka-common</module>
<module>springboot-kafka-consumer</module>
<module>springboot-kafka-producer</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.67</version>
</dependency>
</dependencies>
</project>
3.创建公共服务模块
创建一个名为springboot-kafka-common的微服务,打包方式为jar,存放一些公共配置和公共类,如util等
1.配置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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.lxg</groupId>
<artifactId>springboot-kafka</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-kafka-common</artifactId>
</project>
pom文件中以父工程作为父依赖,就不需要额外引入依赖了
2.新建一个user实体类
@Data
public class User implements Serializable {
/**
* id
*/
private Integer id;
/**
* 用户名字
*/
private String username;
/**
* 密码
*/
private String password;
}
3.创建application-common.yml配置文件,主要添加kafka的公共配置
spring:
kafka:
#kafka配置
bootstrap-servers: 192.168.56.102:9092
producer:
retries: 0
# 每次批量发送消息的数量
batch-size: 16384
buffer-memory: 33554432
# 指定消息key和消息体的编解码方式
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
consumer:
# 指定默认消费者group id
group-id: test-consumer-group
auto-offset-reset: earliest
enable-auto-commit: true
auto-commit-interval: 5000
# 指定消息key和消息体的编解码方式
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
#自己定义的主题名称,在微服务中使用Value注解注入调用,如果kafka中没有该主题,则会自动创建
topic:
userTopic: userInfo
4.创建消息生产者,即创建一个名为springboot-kafka-producer的普通springboot项目
1.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.lxg</groupId>
<artifactId>springboot-kafka-producer</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>com.lxg</groupId>
<artifactId>springboot-kafka-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.application.yml配置文件,配置端口,设置微服务名称,引入公共服务模块中的application-common.yml
server:
port: 8081
spring:
application:
name: kafka-producer
profiles:
active: common
3.controller层
创建UserController
@Slf4j
@Controller
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@GetMapping("/getUser")
public void getUser() {
userService.sendUserMsg();
log.info("getUser");
}
}
4.service层
创建UserService
public interface UserService {
/**
* 发送用户信息
*
* @return
*/
Boolean sendUserMsg();
}
创建UserServiceImpl
@Slf4j
@Service
public class UserServiceImpl implements UserService {
@Value("${spring.kafka.topic.userTopic}")
private String userTopic;
@Autowired
KafkaTemplate kafkaTemplate;
@Override
public Boolean sendUserMsg() {
User user = new User();
user.setId(1);
user.setUsername("lxg");
user.setPassword("6767167");
kafkaTemplate.send(userTopic, JSONObject.toJSONString(user));
log.info("lxg");
return Boolean.TRUE;
}
}
5.创建启动类
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
5.创建消息消费者
1.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.lxg</groupId>
<artifactId>springboot-kafka-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>com.lxg</groupId>
<artifactId>springboot-kafka-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
2.创建yml配置文件
server:
port: 8082
spring:
application:
name: kafka-consumer
profiles:
active: common
3.创建consumer消费者类
@Slf4j
@Component
public class UserConsumer {
@KafkaListener(topics = {"${spring.kafka.topic.userTopic}"})
public void userConsumer(String message) {
log.info("receive msg " + message);
}
}
4.启动类
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
测试
启动producer和consumer两个服务模块
访问producer微服务中的接口 http://localhost:8081/api/user/getUser
会发现consumer微服务中的控制台打印了producer中创建并推送过来的的user实体
本文GitHub源码:https://github.com/lixianguo5097/springboot/tree/master/springboot-kafka
CSDN:https://blog.csdn.net/qq_27682773
简书:https://www.jianshu.com/u/e99381e6886e
博客园:https://www.cnblogs.com/lixianguo
个人博客:https://www.lxgblog.com`