diff --git a/whatsmars-spring-boot-samples/pom.xml b/whatsmars-spring-boot-samples/pom.xml index af35687f99c86c9b6a8affb99f467b72a6330d21..e918d22777a0fc4bc48b2aa0b99bc787fd6cc362 100644 --- a/whatsmars-spring-boot-samples/pom.xml +++ b/whatsmars-spring-boot-samples/pom.xml @@ -28,6 +28,7 @@ whatsmars-boot-sample-elasticsearch whatsmars-boot-sample-redis whatsmars-boot-sample-mybatis + whatsmars-boot-sample-kafka diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/pom.xml b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..c87366fa2520f3c7649201c7e35891a76a85340e --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/pom.xml @@ -0,0 +1,32 @@ + + + + whatsmars-spring-boot-samples + org.hongxi + 1.0-SNAPSHOT + + 4.0.0 + + whatsmars-boot-sample-kafka + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.kafka + spring-kafka + + + + org.springframework.kafka + spring-kafka-test + test + + + + + \ No newline at end of file diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/main/java/org/hongxi/whatsmars/boot/sample/kafka/Consumer.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/main/java/org/hongxi/whatsmars/boot/sample/kafka/Consumer.java new file mode 100644 index 0000000000000000000000000000000000000000..fc606fec9a49bdc1d2ba16fdf515078b333c5b64 --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/main/java/org/hongxi/whatsmars/boot/sample/kafka/Consumer.java @@ -0,0 +1,18 @@ +package org.hongxi.whatsmars.boot.sample.kafka; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.stereotype.Component; + +/** + * Created by shenhongxi on 2018/12/12. + */ +@Slf4j +@Component +public class Consumer { + + @KafkaListener(topics = "kafkaTest") + public void onMessage(String message) { + log.info("receive message:{}", message); + } +} diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/main/java/org/hongxi/whatsmars/boot/sample/kafka/KafkaApplication.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/main/java/org/hongxi/whatsmars/boot/sample/kafka/KafkaApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..be0c7a18f370d7a48349c485b7389c767b986747 --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/main/java/org/hongxi/whatsmars/boot/sample/kafka/KafkaApplication.java @@ -0,0 +1,26 @@ +package org.hongxi.whatsmars.boot.sample.kafka; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.kafka.core.KafkaTemplate; + +/** + * Created by shenhongxi on 2018/12/12. + */ +@SpringBootApplication +public class KafkaApplication implements CommandLineRunner { + + @Autowired + private KafkaTemplate kafkaTemplate; + + public static void main(String[] args) { + SpringApplication.run(KafkaApplication.class, args); + } + + @Override + public void run(String... strings) throws Exception { + kafkaTemplate.send("kafkaTest", "hello"); + } +} diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/main/resources/application.yml b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..e690fb49fb166406a26030664a0525713a5fb636 --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + kafka: + bootstrap-servers: 127.0.0.1:9092 + consumer: + group-id: testGroup + auto-offset-reset: earliest \ No newline at end of file diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/test/java/org/hongxi/whatsmars/boot/sample/kafka/KafkaTests.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/test/java/org/hongxi/whatsmars/boot/sample/kafka/KafkaTests.java new file mode 100644 index 0000000000000000000000000000000000000000..ff0737f5e600ff2281007cc5efd70c65e3dc2557 --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-kafka/src/test/java/org/hongxi/whatsmars/boot/sample/kafka/KafkaTests.java @@ -0,0 +1,21 @@ +package org.hongxi.whatsmars.boot.sample.kafka; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.kafka.test.context.EmbeddedKafka; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Created by shenhongxi on 2018/12/12. + */ +@RunWith(SpringRunner.class) +@SpringBootTest(properties = "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}") +@EmbeddedKafka +public class KafkaTests { + + @Test + public void kafka() throws Exception { + + } +}