Sending and receiving data of Kafka

Posted by francoisp on Sun, 02 Feb 2020 18:44:41 +0100

First, configure the port number and the topic should be
Second, load the configured producers and consumers in spring bean.xml
As follows:
Consumer: consumer configuration

<?xml version="1.0" encoding="UTF-8"?>

<bean id="consumerProperties" class="java.util.HashMap">
			<!--Kafka Service address -->
			<entry key="bootstrap.servers" value="" />
			<!--Consumer Group ID,identical Of consumer Belongs to the same group. Different members of a group,Only one person can receive the same message -->
			<entry key="" value="test-consumer-group" />
			<!--If this value is set to true,consumer Will periodically offset Value is saved to zookeeper. When consumer This value will be used as the new consumption value after a failed restart. -->
			<entry key="" value="true" />
			<!--Network requested socket Timeout. The actual timeout is determined by max.fetch.wait + Determine -->
			<entry key="" value="15000 " />

			<entry key="key.deserializer"
				value="org.apache.kafka.common.serialization.StringDeserializer" />

			<entry key="value.deserializer"
				value="org.apache.kafka.common.serialization.StringDeserializer" />

<!-- Establish consumerFactory bean -->
<bean id="consumerFactory"
		<ref bean="consumerProperties" />

<bean id="messageListenerContainer"
	<constructor-arg ref="consumerFactory" />
	<constructor-arg ref="containerProperties" />

<!-- Remember to change the theme -->
<bean id="containerProperties" class="org.springframework.kafka.listener.ContainerProperties">
	<!-- The constructor is the parameter value of the topic -->
	<constructor-arg value="1708E" />
	<!-- The class where you want to declare your own defined listener -->
	<property name="messageListener" ref="msgListener" />

<!--Specify the bean This class needs to be written by hand-->
<bean id="msgListener" class="com.lixuecheng.kafka.MentListener" />
Producer: producer's configuration <?xml version="1.0" encoding="UTF-8"?>
    <!--Parameter configuration -->
<bean id="producerProperties" class="java.util.HashMap">
			<!-- kafka Service address, possibly cluster value="localhost:9092,localhost:9093,localhost:9094"-->
			<entry key="bootstrap.servers" value="" />
			<!-- May cause broker Duplicate message received-->
			<entry key="retries" value="0" />
			<!-- Number of messages sent in bulk each time -->
			<entry key="batch.size" value="1638" />
			<!-- Default 0 ms,Asynchronous IO After the thread is triggered (any one topic,partition Full can trigger) -->
			<entry key="" value="1" />
			<!--producer The amount of memory that can be used to cache data. If the data generation speed is greater than the direction broker Speed of transmission, producer Can block or throw an exception -->
			<entry key="buffer.memory" value="33554432 " />
			<entry key="key.serializer"
				value="org.apache.kafka.common.serialization.StringSerializer" />
			<entry key="value.serializer"
				value="org.apache.kafka.common.serialization.StringSerializer" />

<!-- Establish kafkatemplate Required producerfactory bean -->
<bean id="producerFactory"
		<ref bean="producerProperties" />

<!-- Establish kafkatemplate bean,When using, just inject this bean,Can use template Of send Message method -->
<bean id="kafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate">
	<constructor-arg ref="producerFactory" />
	<!--Set corresponding topic If the subject does not exist,Automatically created-->
	<property name="defaultTopic" value="1708E" />

Then the test class loads the spring-beans.xml file
Running tests in code
To create a listening kafka configuration
package com.lixuecheng.kafka;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.listener.MessageListener;

import com.lixuecheng.entity.Investment;
import com.lixuecheng.service.InMentService;

public class MentListener implements MessageListener<String, String> {

private InMentService inMentService;

public void onMessage(ConsumerRecord<String, String> data) {

	String value = data.value();
	System.err.println("11111" + value);
	try {
	} catch (InterruptedException e) { // TODO Auto-generated catch block
	System.out.println("I got it!!!!");

	Investment parseObject = JSON.parseObject(value, Investment.class);




Finally, start kafka and its related applications in virtual machine

Published 3 original articles, won 0 praise and visited 37
Private letter follow

Topics: kafka Apache xml Spring