SpringBoot集成kafka
pom文件
< parent>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-parent</ artifactId>
< version> 2.1.5.RELEASE</ version>
</ parent>
< properties>
< 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.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< 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>
配置文件:application.yml
server :
port : 9991
spring :
application :
name : kafka- demo
kafka :
bootstrap-servers : 192.168.200.130: 9092
producer :
retries : 10
key-serializer : org.apache.kafka.common.serialization.StringSerializer
value-serializer : org.apache.kafka.common.serialization.StringSerializer
consumer :
group-id : test- hello- group
key-deserializer : org.apache.kafka.common.serialization.StringDeserializer
value-deserializer : org.apache.kafka.common.serialization.StringDeserializer
消息生产者
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 ;
@Component
public class Producer {
@Autowired
private KafkaTemplate < String , String > kafkaTemplate;
public void sendMsg ( ) {
kafkaTemplate. send ( "kafka-topic" , "kafka-data" ) ;
}
}
消息消费者
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 Consumer {
@KafkaListener ( topics = { "kafka-topic" } )
public void receiverMessage ( ConsumerRecord < ? , ? > record ) {
Optional < ? extends ConsumerRecord < ? , ? > > optional = Optional . ofNullable ( record ) ;
if ( optional. isPresent ( ) ) {
Object value = record . value ( ) ;
System . out. println ( value) ;
}
}
}
数据为实体的消息发送
实体
@Data
public class User {
private String username;
private Integer age;
}
消息生产者
@Component
public class Producer {
@Autowired
private KafkaTemplate < String , String > kafkaTemplate;
public void sendMsg ( ) {
User user = new User ( ) ;
user. setUsername ( "zhangsan" ) ;
user. setAge ( 18 ) ;
kafkaTemplate. send ( "kafka-hello" , JSON. toJSONString ( user) ) ;
}
}
消息消费者
@Component
public class Consumer {
@KafkaListener ( topics = { "kafka-topic" } )
public void receiverMessage ( ConsumerRecord < ? , ? > record ) {
Optional < ? extends ConsumerRecord < ? , ? > > optional = Optional . ofNullable ( record ) ;
if ( optional. isPresent ( ) ) {
Object value = record . value ( ) ;
User user = JSON. parseObject ( ( String ) value, User . class ) ;
System . out. println ( user) ;
}
}
}