Spring Boot Integrated Kafka, note to install Kafka and zookeeper first
The jar MAVEN configuration for importing Spring-kafka first is as follows:
<!--Kafka client needs and Kafka's jar version corresponds to the version 0.9 client--
<dependency>
<groupId> org.springframework.kafka</groupid>
<artifactId>spring-kafka</artifactId>
< Version>1.0.6.release</version>
</dependency>
application.properties configuration is as follows: spring.kafka.bootstrap-servers=127.0.0.1:9092
Spring.kafka.producer.acks=all Spring.kafka.consumer.enable-auto-commit=false
Spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.stringserializer
Spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.stringserializer
Spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.stringdeserializer
Spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.stringdeserializer Spring.kafka.consumer.group-id=test2
Package Com.zyc.zspringboot.kafka;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.kafka.annotation.KafkaListener;
Import org.springframework.kafka.core.KafkaTemplate;
Import org.springframework.stereotype.Component;
/**
* @author zyc-admin
* @data 2018-03-29 11:14
**/
@Component public
class Mykafkaconsumer {
@Autowired
private kafkatemplate<string,string> kafkatemplate;
@KafkaListener (topics = {"My-topic"}) public
void consumer (String message) {
System.out.printf (" Print message {%s} ", message);
System.out.println ();
}
public void Send (String message) {
kafkatemplate.send ("My-topic", string.valueof (1), message);
}
}
The test classes are as follows:
Package Com.zyc.zspringboot.kafka;
Import Com.zyc.zspringboot.ProfilesResolver;
Import com.zyc.zspringboot.ZspringbootApplication;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.boot.test.context.SpringBootTest;
Import Org.springframework.test.context.ActiveProfiles;
Import Org.springframework.test.context.junit4.SpringRunner;
/**
* @author zyc-admin
* @data 2018-03-29 11:26
**/
@RunWith (springrunner.class)
@ Springboottest (classes = {Zspringbootapplication.class}) public
class Kafkatest {
@Autowired
private Mykafkaconsumer Mykafkaconsumer;
@Test public
Void, send () {for
(int i = 0; i < i++) {
mykafkaconsumer.send ("data" + i);
} While
(true) {
}
}
}