Add Kafka sample

See gh-11597
This commit is contained in:
xinhc 2018-01-11 11:00:00 +08:00 committed by Stephane Nicoll
parent 7b32cf82df
commit 89e1d95363
9 changed files with 263 additions and 0 deletions

View File

@ -230,3 +230,6 @@ The following sample applications are provided:
| link:spring-boot-sample-xml[spring-boot-sample-xml]
| Example show how Spring Boot can be mixed with traditional XML configuration (we
generally recommend using Java `@Configuration` whenever possible
| link:spring-boot-sample-kafka[spring-boot-sample-kafka]
| consumer and producer using Apache Kafka

View File

@ -97,6 +97,7 @@
<module>spring-boot-sample-websocket-undertow</module>
<module>spring-boot-sample-webservices</module>
<module>spring-boot-sample-xml</module>
<module>spring-boot-sample-kafka</module>
</modules>
<!-- No dependencies - otherwise the samples won't work if you change the
parent -->

View File

@ -0,0 +1,53 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<artifactId>spring-boot-samples</artifactId>
<groupId>org.springframework.boot</groupId>
<version>${revision}</version>
</parent>
<artifactId>spring-boot-sample-kafka</artifactId>
<name>Spring Boot Kafka Sample</name>
<description>Spring Boot Kafka Sample</description>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<!-- Compile -->
<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.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,28 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.kafka;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
@KafkaListener(topics = "myTopic")
public void processMessage(SampleMessage message) {
System.out.println("consumer has received message : [" + message + "]");
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.kafka;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
@Component
public class Producer {
@Autowired
private KafkaTemplate kafkaTemplate;
public void send(SampleMessage message) {
kafkaTemplate.send("myTopic", message);
System.out.println("producer has sent message.");
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.kafka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleKafkaApplication {
public static void main(String[] args) {
SpringApplication.run(SampleKafkaApplication.class, args);
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.kafka;
public class SampleMessage {
private Integer id;
private String message;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public SampleMessage() {
}
public SampleMessage(Integer id, String message) {
this.id = id;
this.message = message;
}
@Override
public String toString() {
return "SampleMessage{" +
"id=" + id +
", message='" + message + '\'' +
'}';
}
}

View File

@ -0,0 +1,5 @@
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=myGroup
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.Producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.consumer.properties.spring.json.trusted.packages=sample.kafka

View File

@ -0,0 +1,61 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.kafka;
import org.junit.Rule;
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.boot.test.rule.OutputCapture;
import org.springframework.kafka.test.rule.KafkaEmbedded;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for demo application.
*
* @author hcxin
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleKafkaApplicationTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
@Autowired
private Producer producer;
@Test
public void sendSimpleMessage() throws Exception {
initKafkaEmbedded();
SampleMessage message = new SampleMessage(1, "Test message");
producer.send(message);
Thread.sleep(1000L);
assertThat(this.outputCapture.toString().contains("Test message")).isTrue();
}
public void initKafkaEmbedded() throws Exception {
KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, true);
embeddedKafka.setKafkaPorts(9092);
embeddedKafka.afterPropertiesSet();
//Need 10s, waiting for the Kafka server start.
Thread.sleep(10000L);
}
}