E-commerce platform source code Please add Penguin: 103874626. kafka message platform uses spring+kafka integration scheme, details are as follows:
-
Use the maximum version 2.1.0.RELEASE integration jar package: Spring integration Kafka
-
Zookeeper and Kafka distributed clusters use init.properties configuration scheme.
kafka.servers=127.0.0.1:9092 kafka.topic=xxxooo
- Use the message producer spring context producer configuration scheme.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Definition producer Parameters --> <bean id="producerProperties" class="java.util.HashMap"> <constructor-arg> <map> <entry key="bootstrap.servers" value="localhost:9092" /> <entry key="group.id" value="2" /> <entry key="retries" value="10" /> <entry key="batch.size" value="16384" /> <entry key="linger.ms" value="1" /> <entry key="buffer.memory" value="33554432" /> <entry key="key.serializer" value="org.apache.kafka.common.serialization.IntegerSerializer" /> <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" /> <constructor-arg name="autoFlush" value="true" /> <property name="defaultTopic" value="test" /> </bean> </beans>
- Use the message consumer spring context producer configuration scheme.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Definition consumer Parameters --> <bean id="consumerProperties" class="java.util.HashMap"> <constructor-arg> <map> <entry key="bootstrap.servers" value="localhost:9092" /> <entry key="group.id" value="0" /> <entry key="enable.auto.commit" value="true" /> <entry key="auto.commit.interval.ms" value="1000" /> <entry key="session.timeout.ms" value="15000" /> <entry key="key.deserializer" value="org.apache.kafka.common.serialization.IntegerDeserializer" /> <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> <!-- Classes that actually execute message consumption --> <bean id="messageListernerConsumerService" class="com.sml.sz.kafka.KafKaConsumer" /> <!-- Consumer container configuration information --> <bean id="containerProperties" class="org.springframework.kafka.listener.config.ContainerProperties"> <constructor-arg value="test" /> <property name="messageListener" ref="messageListernerConsumerService" /> </bean> <!-- Establish kafkatemplate bean,When using, just inject this bean,Can use template Of send Message method --> <bean id="messageListenerContainer" class="org.springframework.kafka.listener.KafkaMessageListenerContainer" init-method="doStart"> <constructor-arg ref="consumerFactory" /> <constructor-arg ref="containerProperties" /> </bean> </beans>
- Using annotation to inject message types
@Autowired
private KafkaTemplate<xxx, ooo> kafkaTemplate;
-
Rewrite the getMessage method of MessageListener to get the message (Business Implementation)
-
RestFul service mode test message service
@CrossOrigin(origins = "*", maxAge = 3600, methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.PUT }) @RestController @RequestMapping(value = "/rest/kafka") public class KafKaProducer { @RequestMapping(value = "/send", method = RequestMethod.GET) public JSONObject save() { System.out.println("+++++++++++++++++++++++++++++++"); kafkaTemplate.sendDefault("HongHu KAFKA Distributed message service test"); return null; } @Autowired private KafkaTemplate<Integer, String> kafkaTemplate; }
@RestController public class KafKaConsumer implements MessageListener<Integer, String> { @Autowired private LogService logService; public void onMessage(ConsumerRecord<Integer, String> records) { System.out.println("====================" + records); Object o = records.value(); Log log = new Log(); log.setIsNewRecord(true); log.setId(IdGen.uuid()); log.setTitle(String.valueOf(o)); logService.save(log); } }
Received the message: consumerrecord (topic = xxxoo, partition = 0, offset = 2489, createtime = 14796476448299, checksum = 3372898135, serialized key size = - 1, serialized value size = 40, key = null, value = Honghu Kafka distributed message service test)
This is the end!
Welcome to join me in learning about spring cloud to build a microservice Cloud Architecture. I will record the process and essence of the recently developed spring cloud microservice Cloud Architecture, and help more friends who are interested in developing the spring cloud framework. Let's discuss the process of building the spring cloud architecture and how to apply it to enterprise projects.