Spring Boot Kafka Integration

Source: Internet
Author: User
Tags serialization
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) {

		}
	}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.