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">
	<constructor-arg>
		<map>
			<!--Kafka Service address -->
			<entry key="bootstrap.servers" value="192.168.27.128:9092" />
			<!--Consumer Group ID,identical group.id Of consumer Belongs to the same group. Different members of a group,Only one person can receive the same message -->
			<entry key="group.id" 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="enable.auto.commit" value="true" />
			<!--Network requested socket Timeout. The actual timeout is determined by max.fetch.wait + socket.timeout.ms Determine -->
			<entry key="session.timeout.ms" value="15000 " />

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

			<entry key="value.deserializer"
				value="org.apache.kafka.common.serialization.StringDeserializer" />
		</map>
	</constructor-arg>
</bean>


<!-- Establish consumerFactory bean -->
<bean id="consumerFactory"
	class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
	<constructor-arg>
		<ref bean="consumerProperties" />
	</constructor-arg>
</bean>


<bean id="messageListenerContainer"
	class="org.springframework.kafka.listener.KafkaMessageListenerContainer"
	init-method="doStart">
	<constructor-arg ref="consumerFactory" />
	<constructor-arg ref="containerProperties" />
</bean>



<!-- 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" />
</bean>



<!--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">
	<constructor-arg>
		<map>
			<!-- kafka Service address, possibly cluster value="localhost:9092,localhost:9093,localhost:9094"-->
			<entry key="bootstrap.servers" value="192.168.27.128:9092" />
			
			<!-- 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="linger.ms" 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" />
		</map>
	</constructor-arg>
</bean>

<!-- Establish kafkatemplate Required producerfactory bean -->
<bean id="producerFactory"
	class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
	<constructor-arg>
		<ref bean="producerProperties" />
	</constructor-arg>
</bean>

<!-- 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" />
</bean>

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.alibaba.fastjson.JSON;
import com.lixuecheng.entity.Investment;
import com.lixuecheng.service.InMentService;

public class MentListener implements MessageListener<String, String> {

@Autowired
private InMentService inMentService;

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

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

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

	System.out.println(parseObject);

	 inMentService.add(parseObject);

}

}
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