微服务 异步调用,-使用微服务Spring Cloud集成Kafka实现异步通信(消费者) - autoconfigure
1、本文架构
本文目标是使用微服务Spring Cloud集成Kafka实现异步通信。其中Kafka Server部署在Ubuntu虚拟机上,微服务部署在Windows 11系统上,Kafka Producer微服务和Kafka Consumer微服务分别注册到Eureka注册中心。Kafka Producer和Kafka Consumer之间通过Kafka Server实现异步通信。
出于便于测试的目的,我通过浏览器触发Kafka Producer发送消息,观察Kafka Consumer的后台是否打印出接收到的消息内容。
Ubuntu 上部署Kafka Server,详见博文:Ubuntu下Kafka安装及使用
Eureka注册中心的搭建过程和完整代码,详见博文:微服务1:搭建微服务注册中心(命令行简易版,不使用IDE)
Kafka Producer微服务的完整代码,详见博文:使用微服务Spring Cloud集成Kafka实现异步通信 拼多多帮助力入口
本文的重点是实现下图中的深蓝色部分:Kafka Consumer微服务。
2、创建Spring boot项目(Kafka Consumer微服务项目):
mvn archetype:generate -DgroupId=com.test -DartifactId=microservice-kafka-consumer -DarchetypeArtifactId=maven-archetype-quickstart
项目代码的完整目录如下图所示:
编辑pom.xml,添加依赖包:
org.springframework.cloud spring-cloud-stream-binder-kafka
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.test
microservice-kafka-consumer
jar
1.0-SNAPSHOT
microservice-kafka-consumer
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
2.3.0.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
junit
junit
3.8.1
test
org.springframework.cloud
spring-cloud-stream-binder-kafka
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR4
pom
import
org.springframework.boot
spring-boot-maven-plugin
编辑application.yml,配置kafka消费者:
consumer: #消费的主题 topic: test-topic #消费者组id group-id: test-group #是否自动提交偏移量 enable-auto-commit: true #提交偏移量的间隔-毫秒 auto-commit-ms: 1000 #客户端消费的会话超时时间-毫秒 session-timeout-ms: 10000 #实现DeSerializer接口的反序列化类键 key-deserializer: org.apache.kafka.common.serialization.StringDeserializer #实现DeSerializer接口的反序列化类值 value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
server:
port: 8030
spring:
application:
name: microservice-kafka-consumer
kafka:
bootstrap-servers: 192.168.23.131:9092
consumer:
group-id: test-group
enable-auto-commit: true
auto-commit-ms: 1000
session-timeout-ms: 10000
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
instance:
prefer-ip-address: true
App.java的完整代码如下:
package com.test;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.kafka.annotation.KafkaListener;
@SpringBootApplication
@EnableDiscoveryClient
public class App
{
@KafkaListener(topics = "mydemo1")
public void listen(String msg) throws Exception {
System.out.println( "----->Recv a msg: " + msg );
}
public static void main( String[] args ){
System.out.println( "Hello World!" );
SpringApplication.run(App.class, args);
}
}
3、测试
在浏览器输入,触发Kafka Producer向Kafka Server发送消息:
http://localhost:8020/kafka/sendMsg?msg=测试消息testmsg
在Kafka Consumer的后台打印出收到的消息: