電子商務平臺源碼請加企鵝求求:一零三八七七四六二六。kafka消息平臺使用spring+kafka的集成方案,詳情如下:
使用最高版本2.1.0.RELEASE集成jar包:spring-integration-kafka
Zookeeper、Kafka分布式集群使用init.properties配置化方案。
kafka.servers=127.0.0.1:9092
kafka.topic=xxxooo
復制代碼使用消息生產者spring-context-producer配置化方案。<?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"> <!-- 定義producer的參數 --> <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> <!-- 創建kafkatemplate需要使用的producerfactory bean --> <bean id=
"producerFactory" class=
"org.springframework.kafka.core.DefaultKafkaProducerFactory"> <constructor-arg> <ref bean=
"producerProperties" /> </constructor-arg> </bean> <!-- 創建kafkatemplate bean,使用的時候,只需要注入這個bean,即可使用template的send消息方法 --> <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>
復制代碼使用消息消費者spring-context-producer配置化方案。<?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"> <!-- 定義consumer的參數 --> <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> <!-- 創建consumerFactory bean --> <bean id=
"consumerFactory" class=
"org.springframework.kafka.core.DefaultKafkaConsumerFactory"> <constructor-arg> <ref bean=
"consumerProperties" /> </constructor-arg> </bean> <!-- 實際執行消息消費的類 --> <bean id=
"messageListernerConsumerService" class=
"com.sml.sz.kafka.KafKaConsumer" /> <!-- 消費者容器配置信息 --> <bean id=
"containerProperties" class=
"org.springframework.kafka.listener.config.ContainerProperties"> <constructor-arg value=
"test" /> <property name=
"messageListener" ref=
"messageListernerConsumerService" /> </bean> <!-- 創建kafkatemplate bean,使用的時候,只需要注入這個bean,即可使用template的send消息方法 --> <bean id=
"messageListenerContainer" class=
"org.springframework.kafka.listener.KafkaMessageListenerContainer" init-method=
"doStart"> <constructor-arg ref=
"consumerFactory" /> <constructor-arg ref=
"containerProperties" /> </bean>
</beans>
復制代碼使用注解方式注入消息類型@Autowired
private KafkaTemplate<xxx, ooo> kafkaTemplate;
重寫MessageListener 的getMessage方法獲取消息(業務實現)
RestFul服務方式測試消息服務
@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分布式消息服務測試");
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); } }
復制代碼接受消息了------------------:ConsumerRecord(topic = xxxooo, partition = 0, offset = 2489, CreateTime = 1479647648299, checksum = 3372898135, serialized key size = -1, serialized value size = 40, key = null, value = HongHu KAFKA分布式消息服務測試)
到此結束!
歡迎大家和我一起學習spring cloud構建微服務云架構,我這邊會將近期研發的spring cloud微服務云架構的搭建過程和精髓記錄下來,幫助更多有興趣研發spring cloud框架的朋友,大家來一起探討spring cloud架構的搭建過程及如何運用于企業項目。
轉載于:https://juejin.im/post/5cedeb05e51d4555e372a57c
總結
以上是生活随笔為你收集整理的(十七)java版spring cloud+spring boot 社交电子商务平台-spring+springmvc+kafka分布式消息中间件集成方案...的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。