前言
本篇文章主要介绍的是springboot整合kafka。
安装kafka
1.使用docker安装kafka,移步 https://www.cnblogs.com/lixianguo/p/13254950.html
创建工程
1.创建一个名为springboot-kafka的pom项目作为父工程,将main和resource文件夹都删除,pom文件添加配置
4.0.0
com.lxg
springboot-kafka
1.0-SNAPSHOT
pom
springboot-kafka
springboot-kafka-common
springboot-kafka-consumer
springboot-kafka-producer
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.kafka
spring-kafka
org.projectlombok
lombok
1.18.12
com.alibaba
fastjson
1.2.67
3.创建公共服务模块
创建一个名为springboot-kafka-common的微服务,打包方式为jar,存放一些公共配置和公共类,如util等
1.配置pom文件
com.lxg
springboot-kafka
1.0-SNAPSHOT
4.0.0
springboot-kafka-common
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文件配置
com.lxg
springboot-kafka-producer
1.0-SNAPSHOT
4.0.0
com.lxg
springboot-kafka-common
1.0-SNAPSHOT
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文件
com.lxg
springboot-kafka-consumer
1.0-SNAPSHOT
4.0.0
com.lxg
springboot-kafka-common
1.0-SNAPSHOT
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