提交 3d472854 编写于 作者: 武汉红喜's avatar 武汉红喜

kafka & spring-boot 2.x

上级 8fd00564
......@@ -28,6 +28,7 @@
<module>whatsmars-boot-sample-elasticsearch</module>
<module>whatsmars-boot-sample-redis</module>
<module>whatsmars-boot-sample-mybatis</module>
<module>whatsmars-boot-sample-kafka</module>
</modules>
<properties>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>whatsmars-spring-boot-samples</artifactId>
<groupId>org.hongxi</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>whatsmars-boot-sample-kafka</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
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);
}
}
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<Object, String> kafkaTemplate;
public static void main(String[] args) {
SpringApplication.run(KafkaApplication.class, args);
}
@Override
public void run(String... strings) throws Exception {
kafkaTemplate.send("kafkaTest", "hello");
}
}
spring:
kafka:
bootstrap-servers: 127.0.0.1:9092
consumer:
group-id: testGroup
auto-offset-reset: earliest
\ No newline at end of file
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 {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册